llvm.org GIT mirror llvm / 7ca333b
[ThinLTO] Move summary computation from BitcodeWriter to new pass Summary: This is the first step in also serializing the index out to LLVM assembly. The per-module summary written to bitcode is moved out of the bitcode writer and to a new analysis pass (ModuleSummaryIndexWrapperPass). The pass itself uses a new builder class to compute index, and the builder class is used directly in places where we don't have a pass manager (e.g. llvm-as). Because we are computing summaries outside of the bitcode writer, we no longer can use value ids created by the bitcode writer's ValueEnumerator. This required changing the reference graph edge type to use a new ValueInfo class holding a union between a GUID (combined index) and Value* (permodule index). The Value* are converted to the appropriate value ID during bitcode writing. Also, this enables removal of the BitWriter library's dependence on the Analysis library that was previously required for the summary computation. Reviewers: joker.eph Subscribers: joker.eph, llvm-commits Differential Revision: http://reviews.llvm.org/D18763 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265941 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 3 years ago
17 changed file(s) with 434 addition(s) and 183 deletion(s). Raw diff Collapse all Expand all
0 //===- ModuleSummaryAnalysis.h - Module summary index builder ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This is the interface to build a ModuleSummaryIndex for a module.
10 ///
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H
14 #define LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H
15
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/ModuleSummaryIndex.h"
18 #include "llvm/Pass.h"
19
20 namespace llvm {
21
22 class BlockFrequencyInfo;
23
24 /// Class to build a module summary index for the given Module, possibly from
25 /// a Pass.
26 class ModuleSummaryIndexBuilder {
27 /// The index being built
28 std::unique_ptr Index;
29 /// The module for which we are building an index
30 const Module *M;
31
32 public:
33 /// Default constructor
34 ModuleSummaryIndexBuilder() = default;
35
36 /// Constructor that builds an index for the given Module. An optional
37 /// callback can be supplied to obtain the frequency info for a function.
38 ModuleSummaryIndexBuilder(
39 const Module *M,
40 std::function Ftor = nullptr);
41
42 /// Get a reference to the index owned by builder
43 ModuleSummaryIndex &getIndex() const { return *Index; }
44
45 /// Take ownership of the built index
46 std::unique_ptr takeIndex() { return std::move(Index); }
47
48 private:
49 /// Compute info for given function with optional frequency information
50 void computeFunctionInfo(const Function &F,
51 BlockFrequencyInfo *BFI = nullptr);
52
53 /// Compute info for given variable with optional frequency information
54 void computeVariableInfo(const GlobalVariable &V);
55 };
56
57 /// Legacy wrapper pass to provide the ModuleSummaryIndex object.
58 class ModuleSummaryIndexWrapperPass : public ModulePass {
59 std::unique_ptr IndexBuilder;
60
61 public:
62 static char ID;
63
64 ModuleSummaryIndexWrapperPass();
65
66 /// Get the index built by pass
67 ModuleSummaryIndex &getIndex() { return IndexBuilder->getIndex(); }
68 const ModuleSummaryIndex &getIndex() const {
69 return IndexBuilder->getIndex();
70 }
71
72 bool runOnModule(Module &M) override;
73 bool doFinalization(Module &M) override;
74 void getAnalysisUsage(AnalysisUsage &AU) const override;
75 };
76
77 //===--------------------------------------------------------------------===//
78 //
79 // createModuleSummaryIndexWrapperPass - This pass builds a ModuleSummaryIndex
80 // object for the module, to be written to bitcode or LLVM assembly.
81 //
82 ModulePass *createModuleSummaryIndexWrapperPass();
83 }
84
85 #endif
106106 /// for use in ThinLTO optimization).
107107 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
108108 bool ShouldPreserveUseListOrder = false,
109 bool EmitSummaryIndex = false,
109 const ModuleSummaryIndex *Index = nullptr,
110110 bool GenerateHash = false);
111111
112112 /// Write the specified module summary index to the given raw output stream,
1717
1818 #include "llvm/ADT/DenseMap.h"
1919 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.h"
2021 #include "llvm/ADT/SmallString.h"
2122 #include "llvm/ADT/StringMap.h"
2223 #include "llvm/IR/Function.h"
4546 }
4647 };
4748
49 /// Struct to hold value either by GUID or Value*, depending on whether this
50 /// is a combined or per-module index, respectively.
51 struct ValueInfo {
52 /// The value representation used in this instance.
53 enum ValueInfoKind {
54 VI_GUID,
55 VI_Value,
56 };
57
58 /// Union of the two possible value types.
59 union ValueUnion {
60 GlobalValue::GUID Id;
61 const Value *V;
62 ValueUnion(GlobalValue::GUID Id) : Id(Id) {}
63 ValueUnion(const Value *V) : V(V) {}
64 };
65
66 /// The value being represented.
67 ValueUnion TheValue;
68 /// The value representation.
69 ValueInfoKind Kind;
70 /// Constructor for a GUID value
71 ValueInfo(GlobalValue::GUID Id = 0) : TheValue(Id), Kind(VI_GUID) {}
72 /// Constructor for a Value* value
73 ValueInfo(const Value *V) : TheValue(V), Kind(VI_Value) {}
74 /// Accessor for GUID value
75 GlobalValue::GUID getGUID() const {
76 assert(Kind == VI_GUID && "Not a GUID type");
77 return TheValue.Id;
78 }
79 /// Accessor for Value* value
80 const Value *getValue() const {
81 assert(Kind == VI_Value && "Not a Value type");
82 return TheValue.V;
83 }
84 };
85
4886 /// \brief Function and variable summary information to aid decisions and
4987 /// implementation of importing.
5088 ///
77115 /// types based on global summary-based analysis.
78116 GlobalValue::LinkageTypes Linkage;
79117
80 /// List of GUIDs of values referenced by this global value's definition
118 /// List of values referenced by this global value's definition
81119 /// (either by the initializer of a global variable, or referenced
82120 /// from within a function). This does not include functions called, which
83121 /// are listed in the derived FunctionSummary object.
84 std::vector<GlobalValue::GUID> RefEdgeList;
122 std::vector<ValueInfo> RefEdgeList;
85123
86124 protected:
87125 /// GlobalValueSummary constructor.
108146 /// by \p RefGUID.
109147 void addRefEdge(GlobalValue::GUID RefGUID) { RefEdgeList.push_back(RefGUID); }
110148
149 /// Record a reference from this global value to the global value identified
150 /// by \p RefV.
151 void addRefEdge(const Value *RefV) { RefEdgeList.push_back(RefV); }
152
111153 /// Record a reference from this global value to each global value identified
112154 /// in \p RefEdges.
113 void addRefEdges(DenseSet<unsigned> &RefEdges) {
155 void addRefEdges(DenseSet<const Value *> &RefEdges) {
114156 for (auto &RI : RefEdges)
115157 addRefEdge(RI);
116158 }
117159
118 /// Return the list of GUIDs referenced by this global value definition.
119 std::vector &refs() { return RefEdgeList; }
120 const std::vector &refs() const { return RefEdgeList; }
160 /// Return the list of values referenced by this global value definition.
161 std::vector &refs() { return RefEdgeList; }
162 const std::vector &refs() const { return RefEdgeList; }
121163 };
122164
123165 /// \brief Function summary information to aid decisions and implementation of
124166 /// importing.
125167 class FunctionSummary : public GlobalValueSummary {
126168 public:
127 /// call edge pair.
128 typedef std::pair EdgeTy;
169 /// call edge pair.
170 typedef std::pair EdgeTy;
129171
130172 private:
131173 /// Number of instructions (ignoring debug instructions, e.g.) computed
132174 /// during the initial compile step when the summary index is first built.
133175 unsigned InstCount;
134176
135 /// List of GUID, CalleeInfo> call edge pairs from this function.
177 /// List of ValueInfo, CalleeInfo> call edge pairs from this function.
136178 std::vector CallGraphEdgeList;
137179
138180 public:
155197 CallGraphEdgeList.push_back(std::make_pair(CalleeGUID, Info));
156198 }
157199
200 /// Record a call graph edge from this function to the function identified
201 /// by \p CalleeV, with \p CalleeInfo including the cumulative profile
202 /// count (across all calls from this function) or 0 if no PGO.
203 void addCallGraphEdge(const Value *CalleeV, CalleeInfo Info) {
204 CallGraphEdgeList.push_back(std::make_pair(CalleeV, Info));
205 }
206
158207 /// Record a call graph edge from this function to each function recorded
159208 /// in \p CallGraphEdges.
160 void addCallGraphEdges(DenseMap<unsigned, CalleeInfo> &CallGraphEdges) {
209 void addCallGraphEdges(DenseMap<const Value *, CalleeInfo> &CallGraphEdges) {
161210 for (auto &EI : CallGraphEdges)
162211 addCallGraphEdge(EI.first, EI.second);
163212 }
164213
165 /// Return the list of GUID, ProfileCount> pairs.
214 /// Return the list of ValueInfo, CalleeInfo> pairs.
166215 std::vector &calls() { return CallGraphEdgeList; }
167216 const std::vector &calls() const { return CallGraphEdgeList; }
168217 };
197197 void initializeMachineBlockPlacementStatsPass(PassRegistry&);
198198 void initializeMachineBranchProbabilityInfoPass(PassRegistry&);
199199 void initializeMachineCSEPass(PassRegistry&);
200 void initializeModuleSummaryIndexWrapperPassPass(PassRegistry &);
200201 void initializeImplicitNullChecksPass(PassRegistry&);
201202 void initializeMachineDominatorTreePass(PassRegistry&);
202203 void initializeMachineDominanceFrontierPass(PassRegistry&);
313314 void initializeLoadCombinePass(PassRegistry&);
314315 void initializeRewriteSymbolsPass(PassRegistry&);
315316 void initializeWinEHPreparePass(PassRegistry&);
317 void initializeWriteBitcodePassPass(PassRegistry &);
316318 void initializePlaceBackedgeSafepointsImplPass(PassRegistry&);
317319 void initializePlaceSafepointsPass(PassRegistry&);
318320 void initializeDwarfEHPreparePass(PassRegistry&);
5959 initializeMemDerefPrinterPass(Registry);
6060 initializeMemoryDependenceWrapperPassPass(Registry);
6161 initializeModuleDebugInfoPrinterPass(Registry);
62 initializeModuleSummaryIndexWrapperPassPass(Registry);
6263 initializeObjCARCAAWrapperPassPass(Registry);
6364 initializePostDominatorTreeWrapperPassPass(Registry);
6465 initializeRegionInfoPassPass(Registry);
4848 MemoryDependenceAnalysis.cpp
4949 MemoryLocation.cpp
5050 ModuleDebugInfoPrinter.cpp
51 ModuleSummaryAnalysis.cpp
5152 ObjCARCAliasAnalysis.cpp
5253 ObjCARCAnalysisUtils.cpp
5354 ObjCARCInstKind.cpp
0 //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass builds a ModuleSummaryIndex object for the module, to be written
10 // to bitcode or LLVM assembly.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
15 #include "llvm/Analysis/BlockFrequencyInfo.h"
16 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
17 #include "llvm/Analysis/BranchProbabilityInfo.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/ValueSymbolTable.h"
23 #include "llvm/Pass.h"
24 using namespace llvm;
25
26 #define DEBUG_TYPE "module-summary-analysis"
27
28 // Walk through the operands of a given User via worklist iteration and populate
29 // the set of GlobalValue references encountered. Invoked either on an
30 // Instruction or a GlobalVariable (which walks its initializer).
31 static void findRefEdges(const User *CurUser, DenseSet &RefEdges,
32 SmallPtrSet &Visited) {
33 SmallVector Worklist;
34 Worklist.push_back(CurUser);
35
36 while (!Worklist.empty()) {
37 const User *U = Worklist.pop_back_val();
38
39 if (!Visited.insert(U).second)
40 continue;
41
42 ImmutableCallSite CS(U);
43
44 for (const auto &OI : U->operands()) {
45 const User *Operand = dyn_cast(OI);
46 if (!Operand)
47 continue;
48 if (isa(Operand))
49 continue;
50 if (isa(Operand)) {
51 // We have a reference to a global value. This should be added to
52 // the reference set unless it is a callee. Callees are handled
53 // specially by WriteFunction and are added to a separate list.
54 if (!(CS && CS.isCallee(&OI)))
55 RefEdges.insert(Operand);
56 continue;
57 }
58 Worklist.push_back(Operand);
59 }
60 }
61 }
62
63 void ModuleSummaryIndexBuilder::computeFunctionInfo(const Function &F,
64 BlockFrequencyInfo *BFI) {
65 // Summary not currently supported for anonymous functions, they must
66 // be renamed.
67 if (!F.hasName())
68 return;
69
70 unsigned NumInsts = 0;
71 // Map from callee ValueId to profile count. Used to accumulate profile
72 // counts for all static calls to a given callee.
73 DenseMap CallGraphEdges;
74 DenseSet RefEdges;
75
76 SmallPtrSet Visited;
77 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
79 ++I) {
80 if (!isa(I))
81 ++NumInsts;
82
83 if (auto CS = ImmutableCallSite(&*I)) {
84 auto *CalledFunction = CS.getCalledFunction();
85 if (CalledFunction && CalledFunction->hasName() &&
86 !CalledFunction->isIntrinsic()) {
87 auto ScaledCount = BFI ? BFI->getBlockProfileCount(&*BB) : None;
88 auto *CalleeId =
89 M->getValueSymbolTable().lookup(CalledFunction->getName());
90 CallGraphEdges[CalleeId] +=
91 (ScaledCount ? ScaledCount.getValue() : 0);
92 }
93 }
94 findRefEdges(&*I, RefEdges, Visited);
95 }
96
97 std::unique_ptr FuncSummary =
98 llvm::make_unique(F.getLinkage(), NumInsts);
99 FuncSummary->addCallGraphEdges(CallGraphEdges);
100 FuncSummary->addRefEdges(RefEdges);
101 std::unique_ptr GVInfo =
102 llvm::make_unique(0, std::move(FuncSummary));
103 Index->addGlobalValueInfo(F.getName(), std::move(GVInfo));
104 }
105
106 void ModuleSummaryIndexBuilder::computeVariableInfo(const GlobalVariable &V) {
107 DenseSet RefEdges;
108 SmallPtrSet Visited;
109 findRefEdges(&V, RefEdges, Visited);
110 std::unique_ptr GVarSummary =
111 llvm::make_unique(V.getLinkage());
112 GVarSummary->addRefEdges(RefEdges);
113 std::unique_ptr GVInfo =
114 llvm::make_unique(0, std::move(GVarSummary));
115 Index->addGlobalValueInfo(V.getName(), std::move(GVInfo));
116 }
117
118 ModuleSummaryIndexBuilder::ModuleSummaryIndexBuilder(
119 const Module *M,
120 std::function Ftor)
121 : Index(llvm::make_unique()), M(M) {
122 // Compute summaries for all functions defined in module, and save in the
123 // index.
124 for (auto &F : *M) {
125 if (F.isDeclaration())
126 continue;
127
128 BlockFrequencyInfo *BFI = nullptr;
129 std::unique_ptr BFIPtr;
130 if (Ftor)
131 BFI = Ftor(F);
132 else if (F.getEntryCount().hasValue()) {
133 LoopInfo LI{DominatorTree(const_cast(F))};
134 BranchProbabilityInfo BPI{F, LI};
135 BFIPtr = llvm::make_unique(F, BPI, LI);
136 BFI = BFIPtr.get();
137 }
138
139 computeFunctionInfo(F, BFI);
140 }
141
142 // Compute summaries for all variables defined in module, and save in the
143 // index.
144 for (const GlobalVariable &G : M->globals()) {
145 if (G.isDeclaration())
146 continue;
147 computeVariableInfo(G);
148 }
149 }
150
151 char ModuleSummaryIndexWrapperPass::ID = 0;
152 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
153 "Module Summary Analysis", false, true)
154 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
155 INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
156 "Module Summary Analysis", false, true)
157
158 ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
159 return new ModuleSummaryIndexWrapperPass();
160 }
161
162 ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
163 : ModulePass(ID) {
164 initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
165 }
166
167 bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
168 IndexBuilder = llvm::make_unique(
169 &M, [this](const Function &F) {
170 return &(this->getAnalysis(
171 *const_cast(&F))
172 .getBFI());
173 });
174 return false;
175 }
176
177 bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
178 IndexBuilder.reset();
179 return false;
180 }
181
182 void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
183 AU.setPreservesAll();
184 AU.addRequired();
185 }
1212
1313 #include "ValueEnumerator.h"
1414 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/STLExtras.h"
1615 #include "llvm/ADT/Triple.h"
17 #include "llvm/Analysis/BlockFrequencyInfo.h"
18 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
19 #include "llvm/Analysis/BranchProbabilityInfo.h"
20 #include "llvm/Analysis/LoopInfo.h"
2116 #include "llvm/Bitcode/BitstreamWriter.h"
2217 #include "llvm/Bitcode/LLVMBitCodes.h"
2318 #include "llvm/Bitcode/ReaderWriter.h"
2520 #include "llvm/IR/Constants.h"
2621 #include "llvm/IR/DebugInfoMetadata.h"
2722 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Dominators.h"
2923 #include "llvm/IR/InlineAsm.h"
3024 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
3225 #include "llvm/IR/LLVMContext.h"
3326 #include "llvm/IR/Module.h"
3427 #include "llvm/IR/Operator.h"
22812274 const ValueSymbolTable &VST, const ValueEnumerator &VE,
22822275 BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0,
22832276 uint64_t BitcodeStartBit = 0,
2284 DenseMap> *
2285 GlobalValueIndex = nullptr) {
2277 DenseMap *FunctionToBitcodeIndex = nullptr) {
22862278 if (VST.empty()) {
22872279 // WriteValueSymbolTableForwardDecl should have returned early as
22882280 // well. Ensure this handling remains in sync by asserting that
23712363 // Must be the module-level VST, where we pass in the Index and
23722364 // have a VSTOffsetPlaceholder. The function-level VST should not
23732365 // contain any Function symbols.
2374 assert(GlobalValueIndex);
2366 assert(FunctionToBitcodeIndex);
23752367 assert(VSTOffsetPlaceholder > 0);
23762368
23772369 // Save the word offset of the function (from the start of the
23782370 // actual bitcode written to the stream).
2379 uint64_t BitcodeIndex =
2380 (*GlobalValueIndex)[F]->bitcodeIndex() - BitcodeStartBit;
2371 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - BitcodeStartBit;
23812372 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
23822373 NameVals.push_back(BitcodeIndex / 32);
23832374
24992490 Stream.ExitBlock();
25002491 }
25012492
2502 // Walk through the operands of a given User via worklist iteration and populate
2503 // the set of GlobalValue references encountered. Invoked either on an
2504 // Instruction or a GlobalVariable (which walks its initializer).
2505 static void findRefEdges(const User *CurUser, const ValueEnumerator &VE,
2506 DenseSet &RefEdges,
2507 SmallPtrSet &Visited) {
2508 SmallVector Worklist;
2509 Worklist.push_back(CurUser);
2510
2511 while (!Worklist.empty()) {
2512 const User *U = Worklist.pop_back_val();
2513
2514 if (!Visited.insert(U).second)
2515 continue;
2516
2517 ImmutableCallSite CS(U);
2518
2519 for (const auto &OI : U->operands()) {
2520 const User *Operand = dyn_cast(OI);
2521 if (!Operand)
2522 continue;
2523 if (isa(Operand))
2524 continue;
2525 if (isa(Operand)) {
2526 // We have a reference to a global value. This should be added to
2527 // the reference set unless it is a callee. Callees are handled
2528 // specially by WriteFunction and are added to a separate list.
2529 if (!(CS && CS.isCallee(&OI)))
2530 RefEdges.insert(VE.getValueID(Operand));
2531 continue;
2532 }
2533 Worklist.push_back(Operand);
2534 }
2535 }
2536 }
2537
25382493 /// Emit a function body to the module stream.
25392494 static void
25402495 WriteFunction(const Function &F, const Module *M, ValueEnumerator &VE,
25412496 BitstreamWriter &Stream,
2542 DenseMap> &
2543 GlobalValueIndex,
2544 bool EmitSummaryIndex) {
2497 DenseMap &FunctionToBitcodeIndex) {
25452498 // Save the bitcode index of the start of this function block for recording
25462499 // in the VST.
2547 uint64_t BitcodeIndex = Stream.GetCurrentBitNo();
2548
2549 bool HasProfileData = F.getEntryCount().hasValue();
2550 std::unique_ptr BFI;
2551 if (EmitSummaryIndex && HasProfileData) {
2552 Function &Func = const_cast(F);
2553 LoopInfo LI{DominatorTree(Func)};
2554 BranchProbabilityInfo BPI{Func, LI};
2555 BFI = llvm::make_unique(Func, BPI, LI);
2556 }
2500 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
25572501
25582502 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
25592503 VE.incorporateFunction(F);
25802524 bool NeedsMetadataAttachment = F.hasMetadata();
25812525
25822526 DILocation *LastDL = nullptr;
2583 unsigned NumInsts = 0;
2584 // Map from callee ValueId to profile count. Used to accumulate profile
2585 // counts for all static calls to a given callee.
2586 DenseMap CallGraphEdges;
2587 DenseSet RefEdges;
2588
2589 SmallPtrSet Visited;
25902527 // Finally, emit all the instructions, in order.
25912528 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
25922529 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
25932530 I != E; ++I) {
25942531 WriteInstruction(*I, InstID, VE, Stream, Vals);
25952532
2596 if (!isa(I))
2597 ++NumInsts;
2598
25992533 if (!I->getType()->isVoidTy())
26002534 ++InstID;
2601
2602 if (EmitSummaryIndex) {
2603 if (auto CS = ImmutableCallSite(&*I)) {
2604 auto *CalledFunction = CS.getCalledFunction();
2605 if (CalledFunction && CalledFunction->hasName() &&
2606 !CalledFunction->isIntrinsic()) {
2607 auto ScaledCount = BFI ? BFI->getBlockProfileCount(&*BB) : None;
2608 unsigned CalleeId = VE.getValueID(
2609 M->getValueSymbolTable().lookup(CalledFunction->getName()));
2610 CallGraphEdges[CalleeId] +=
2611 (ScaledCount ? ScaledCount.getValue() : 0);
2612 }
2613 }
2614 findRefEdges(&*I, VE, RefEdges, Visited);
2615 }
26162535
26172536 // If the instruction has metadata, write a metadata attachment later.
26182537 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
26372556
26382557 LastDL = DL;
26392558 }
2640
2641 std::unique_ptr FuncSummary;
2642 if (EmitSummaryIndex) {
2643 FuncSummary = llvm::make_unique(F.getLinkage(), NumInsts);
2644 FuncSummary->addCallGraphEdges(CallGraphEdges);
2645 FuncSummary->addRefEdges(RefEdges);
2646 }
2647 GlobalValueIndex[&F] =
2648 llvm::make_unique(BitcodeIndex, std::move(FuncSummary));
26492559
26502560 // Emit names for all the instructions etc.
26512561 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
29142824
29152825 // Helper to emit a single function summary record.
29162826 static void WritePerModuleFunctionSummaryRecord(
2917 SmallVector &NameVals, FunctionSummary *FS, unsigned ValueID,
2918 unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
2919 BitstreamWriter &Stream, const Function &F) {
2920 assert(FS);
2827 SmallVector &NameVals, GlobalValueInfo *Info,
2828 unsigned ValueID, const ValueEnumerator &VE, unsigned FSCallsAbbrev,
2829 unsigned FSCallsProfileAbbrev, BitstreamWriter &Stream, const Function &F) {
29212830 NameVals.push_back(ValueID);
2831
2832 FunctionSummary *FS = cast(Info->summary());
29222833 NameVals.push_back(getEncodedLinkage(FS->linkage()));
29232834 NameVals.push_back(FS->instCount());
29242835 NameVals.push_back(FS->refs().size());
29252836
29262837 for (auto &RI : FS->refs())
2927 NameVals.push_back(RI);
2838 NameVals.push_back(VE.getValueID(RI.getValue()));
29282839
29292840 bool HasProfileData = F.getEntryCount().hasValue();
29302841 for (auto &ECI : FS->calls()) {
2931 NameVals.push_back(ECI.first);
2842 NameVals.push_back(VE.getValueID(ECI.first.getValue()));
29322843 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
29332844 NameVals.push_back(ECI.second.CallsiteCount);
29342845 if (HasProfileData)
29472858 // Collect the global value references in the given variable's initializer,
29482859 // and emit them in a summary record.
29492860 static void WriteModuleLevelReferences(const GlobalVariable &V,
2861 const ModuleSummaryIndex &Index,
29502862 const ValueEnumerator &VE,
29512863 SmallVector &NameVals,
29522864 unsigned FSModRefsAbbrev,
29542866 // Only interested in recording variable defs in the summary.
29552867 if (V.isDeclaration())
29562868 return;
2957 DenseSet RefEdges;
2958 SmallPtrSet Visited;
2959 findRefEdges(&V, VE, RefEdges, Visited);
29602869 NameVals.push_back(VE.getValueID(&V));
29612870 NameVals.push_back(getEncodedLinkage(V.getLinkage()));
2962 for (auto RefId : RefEdges) {
2963 NameVals.push_back(RefId);
2964 }
2871 auto *Info = Index.getGlobalValueInfo(V);
2872 GlobalVarSummary *VS = cast(Info->summary());
2873 for (auto Ref : VS->refs())
2874 NameVals.push_back(VE.getValueID(Ref.getValue()));
29652875 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
29662876 FSModRefsAbbrev);
29672877 NameVals.clear();
29692879
29702880 /// Emit the per-module summary section alongside the rest of
29712881 /// the module's bitcode.
2972 static void WritePerModuleGlobalValueSummary(
2973 DenseMap> &
2974 GlobalValueIndex,
2975 const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
2882 static void WritePerModuleGlobalValueSummary(const Module *M,
2883 const ModuleSummaryIndex &Index,
2884 const ValueEnumerator &VE,
2885 BitstreamWriter &Stream) {
29762886 if (M->empty())
29772887 return;
29782888
30122922 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
30132923
30142924 SmallVector NameVals;
3015 // Iterate over the list of functions instead of the GlobalValueIndex map to
2925 // Iterate over the list of functions instead of the Index to
30162926 // ensure the ordering is stable.
30172927 for (const Function &F : *M) {
30182928 if (F.isDeclaration())
30222932 if (!F.hasName())
30232933 continue;
30242934
3025 assert(GlobalValueIndex.count(&F) == 1);
3026
2935 auto *Info = Index.getGlobalValueInfo(F);
30272936 WritePerModuleFunctionSummaryRecord(
3028 NameVals, cast(GlobalValueIndex[&F]->summary()),
3029 VE.getValueID(M->getValueSymbolTable().lookup(F.getName())),
2937 NameVals, Info,
2938 VE.getValueID(M->getValueSymbolTable().lookup(F.getName())), VE,
30302939 FSCallsAbbrev, FSCallsProfileAbbrev, Stream, F);
3031 }
3032
3033 for (const GlobalAlias &A : M->aliases()) {
3034 if (!A.getBaseObject())
3035 continue;
3036 const Function *F = dyn_cast(A.getBaseObject());
3037 if (!F || F->isDeclaration())
3038 continue;
3039
3040 assert(GlobalValueIndex.count(F) == 1);
3041 FunctionSummary *FS = cast(GlobalValueIndex[F]->summary());
3042 // Add the alias to the reference list of aliasee function.
3043 FS->addRefEdge(
3044 VE.getValueID(M->getValueSymbolTable().lookup(A.getName())));
3045 WritePerModuleFunctionSummaryRecord(
3046 NameVals, FS,
3047 VE.getValueID(M->getValueSymbolTable().lookup(A.getName())),
3048 FSCallsAbbrev, FSCallsProfileAbbrev, Stream, *F);
30492940 }
30502941
30512942 // Capture references from GlobalVariable initializers, which are outside
30522943 // of a function scope.
30532944 for (const GlobalVariable &G : M->globals())
3054 WriteModuleLevelReferences(G, VE, NameVals, FSModRefsAbbrev, Stream);
3055 for (const GlobalAlias &A : M->aliases())
3056 if (auto *GV = dyn_cast(A.getBaseObject()))
3057 WriteModuleLevelReferences(*GV, VE, NameVals, FSModRefsAbbrev, Stream);
2945 WriteModuleLevelReferences(G, Index, VE, NameVals, FSModRefsAbbrev, Stream);
30582946
30592947 Stream.ExitBlock();
30602948 }
31092997 NameVals.push_back(Index.getModuleId(VS->modulePath()));
31102998 NameVals.push_back(getEncodedLinkage(VS->linkage()));
31112999 for (auto &RI : VS->refs()) {
3112 const auto &VMI = GUIDToValueIdMap.find(RI);
3000 const auto &VMI = GUIDToValueIdMap.find(RI.getGUID());
31133001 unsigned RefId;
31143002 // If this GUID doesn't have an entry, assign one.
31153003 if (VMI == GUIDToValueIdMap.end()) {
3116 GUIDToValueIdMap[RI] = ++GlobalValueId;
3004 GUIDToValueIdMap[RI.getGUID()] = ++GlobalValueId;
31173005 RefId = GlobalValueId;
31183006 } else {
31193007 RefId = VMI->second;
31413029 NameVals.push_back(FS->refs().size());
31423030
31433031 for (auto &RI : FS->refs()) {
3144 const auto &VMI = GUIDToValueIdMap.find(RI);
3032 const auto &VMI = GUIDToValueIdMap.find(RI.getGUID());
31453033 unsigned RefId;
31463034 // If this GUID doesn't have an entry, assign one.
31473035 if (VMI == GUIDToValueIdMap.end()) {
3148 GUIDToValueIdMap[RI] = ++GlobalValueId;
3036 GUIDToValueIdMap[RI.getGUID()] = ++GlobalValueId;
31493037 RefId = GlobalValueId;
31503038 } else {
31513039 RefId = VMI->second;
31613049 }
31623050
31633051 for (auto &EI : FS->calls()) {
3164 const auto &VMI = GUIDToValueIdMap.find(EI.first);
3052 const auto &VMI = GUIDToValueIdMap.find(EI.first.getGUID());
31653053 // If this GUID doesn't have an entry, it doesn't have a function
31663054 // summary and we don't need to record any calls to it.
31673055 if (VMI == GUIDToValueIdMap.end())
32423130 /// WriteModule - Emit the specified module to the bitstream.
32433131 static void WriteModule(const Module *M, BitstreamWriter &Stream,
32443132 bool ShouldPreserveUseListOrder,
3245 uint64_t BitcodeStartBit, bool EmitSummaryIndex,
3246 bool GenerateHash, SmallVectorImpl &Buffer) {
3133 uint64_t BitcodeStartBit,
3134 const ModuleSummaryIndex *Index, bool GenerateHash,
3135 SmallVectorImpl &Buffer) {
32473136 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
32483137 size_t BlockStartPos = Buffer.size();
32493138
32893178 WriteOperandBundleTags(M, Stream);
32903179
32913180 // Emit function bodies.
3292 DenseMapstd::unique_ptr> GlobalValueIndex;
3181 DenseMapuint64_t> FunctionToBitcodeIndex;
32933182 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
32943183 if (!F->isDeclaration())
3295 WriteFunction(*F, M, VE, Stream, GlobalValueIndex, EmitSummaryIndex);
3184 WriteFunction(*F, M, VE, Stream, FunctionToBitcodeIndex);
32963185
32973186 // Need to write after the above call to WriteFunction which populates
32983187 // the summary information in the index.
3299 if (EmitSummaryIndex)
3300 WritePerModuleGlobalValueSummary(GlobalValueIndex, M, VE, Stream);
3188 if (Index)
3189 WritePerModuleGlobalValueSummary(M, *Index, VE, Stream);
33013190
33023191 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
33033192 VSTOffsetPlaceholder, BitcodeStartBit,
3304 &GlobalValueIndex);
3193 &FunctionToBitcodeIndex);
33053194
33063195 if (GenerateHash) {
33073196 writeModuleHash(Stream, Buffer, BlockStartPos);
33913280 /// stream.
33923281 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
33933282 bool ShouldPreserveUseListOrder,
3394 bool EmitSummaryIndex, bool GenerateHash) {
3283 const ModuleSummaryIndex *Index,
3284 bool GenerateHash) {
33953285 SmallVector Buffer;
33963286 Buffer.reserve(256*1024);
33973287
34163306 WriteIdentificationBlock(M, Stream);
34173307
34183308 // Emit the module.
3419 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
3420 EmitSummaryIndex, GenerateHash, Buffer);
3309 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit, Index,
3310 GenerateHash, Buffer);
34213311 }
34223312
34233313 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Bitcode/BitcodeWriterPass.h"
14 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
1415 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/IR/Module.h"
1617 #include "llvm/IR/PassManager.h"
1819 using namespace llvm;
1920
2021 PreservedAnalyses BitcodeWriterPass::run(Module &M) {
21 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, EmitSummaryIndex, EmitModuleHash);
22 std::unique_ptr Index;
23 if (EmitSummaryIndex)
24 Index = ModuleSummaryIndexBuilder(&M).takeIndex();
25 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index.get(),
26 EmitModuleHash);
2227 return PreservedAnalyses::all();
2328 }
2429
3136
3237 public:
3338 static char ID; // Pass identification, replacement for typeid
39 WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
40 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
41 }
42
3443 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
3544 bool EmitSummaryIndex, bool EmitModuleHash)
3645 : ModulePass(ID), OS(o),
3746 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
38 EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {}
47 EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
48 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
49 }
3950
4051 const char *getPassName() const override { return "Bitcode Writer"; }
4152
4253 bool runOnModule(Module &M) override {
43 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, EmitSummaryIndex, EmitModuleHash);
54 const ModuleSummaryIndex *Index =
55 EmitSummaryIndex
56 ? &(getAnalysis().getIndex())
57 : nullptr;
58 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index,
59 EmitModuleHash);
4460 return false;
61 }
62 void getAnalysisUsage(AnalysisUsage &AU) const override {
63 AU.setPreservesAll();
64 if (EmitSummaryIndex)
65 AU.addRequired();
4566 }
4667 };
4768 }
4869
4970 char WriteBitcodePass::ID = 0;
71 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
72 true)
73 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
74 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
75 true)
5076
5177 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
5278 bool ShouldPreserveUseListOrder,
1818 type = Library
1919 name = BitWriter
2020 parent = Bitcode
21 required_libraries = Analysis Core Support
21 required_libraries = Core Support
1515
1616 #include "llvm/ADT/Statistic.h"
1717 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
1819 #include "llvm/Analysis/TargetLibraryInfo.h"
1920 #include "llvm/Analysis/TargetTransformInfo.h"
2021 #include "llvm/Bitcode/BitcodeWriterPass.h"
325326 SmallVector OutputBuffer;
326327 {
327328 raw_svector_ostream OS(OutputBuffer);
328 WriteBitcodeToFile(&TheModule, OS, true, true);
329 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
330 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
329331 }
330332 return make_unique(std::move(OutputBuffer));
331333 }
144144 FunctionImporter::ImportMapTy &ImportsForModule,
145145 StringMap &ExportLists) {
146146 for (auto &Edge : Summary.calls()) {
147 auto GUID = Edge.first;
147 auto GUID = Edge.first.getGUID();
148148 DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
149149
150150 if (DefinedFunctions.count(GUID)) {
180180 // Mark all functions and globals referenced by this function as exported to
181181 // the outside if they are defined in the same source module.
182182 for (auto &Edge : CalleeSummary->calls()) {
183 auto CalleeGUID = Edge.first;
183 auto CalleeGUID = Edge.first.getGUID();
184184 if (isGlobalExported(Index, ExportModulePath, CalleeGUID))
185185 ExportList.insert(CalleeGUID);
186186 }
187 for (auto &GUID : CalleeSummary->refs()) {
187 for (auto &Ref : CalleeSummary->refs()) {
188 auto GUID = Ref.getGUID();
188189 if (isGlobalExported(Index, ExportModulePath, GUID))
189190 ExportList.insert(GUID);
190191 }
66 ; BC:
77 ; BC-NEXT:
88 ; BC-NEXT:
9 ; BC-NEXT:
109 ; BC-NEXT:
1110 ; BC-NEXT:
1211 ; BC-NEXT: record string = 'foo'
3635 ret i32 %x
3736 }
3837
38 ; FIXME: Anonymous function and alias not currently in summary until
39 ; follow on fixes to rename anonymous functions and emit alias summary
40 ; entries are committed.
3941 ; Check an anonymous function as well, since in that case only the alias
4042 ; ends up in the value symbol table and having a summary.
4143 @f = alias void (), void ()* @0 ; [#uses=0]
3333 ; CHECK-DAG: declare void @analias
3434 declare void @analias(...) #1
3535
36 ; FIXME: Add this checking back when follow on fix to add alias summary
37 ; records is committed.
3638 ; Aliases import the aliasee function
3739 declare void @linkoncealias(...) #1
38 ; INSTLIMDEF-DAG: Import linkoncealias
39 ; INSTLIMDEF-DAG: Import linkoncefunc
40 ; CHECK-DAG: define linkonce_odr void @linkoncefunc()
41 ; CHECK-DAG: @linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*
4240
4341 ; INSTLIMDEF-DAG: Import referencestatics
4442 ; INSTLIMDEF-DAG: define available_externally i32 @referencestatics(i32 %i)
8886 ; INSTLIM5-DAG: declare hidden void @funcwithpersonality.llvm.2()
8987
9088 ; INSTLIMDEF-DAG: Import globalfunc2
91 ; INSTLIMDEF-DAG: 11 function-import - Number of functions imported
89 ; INSTLIMDEF-DAG: 9 function-import - Number of functions imported
9290
9391 ; The actual GUID values will depend on path to test.
9492 ; GUID-DAG: GUID {{.*}} is weakalias
0 set(LLVM_LINK_COMPONENTS
1 Analysis
12 AsmParser
23 BitWriter
34 Core
1818 type = Tool
1919 name = llvm-as
2020 parent = Tools
21 required_libraries = AsmParser BitWriter
21 required_libraries = Analysis AsmParser BitWriter
1414 //
1515 //===----------------------------------------------------------------------===//
1616
17 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
1718 #include "llvm/AsmParser/Parser.h"
1819 #include "llvm/Bitcode/ReaderWriter.h"
1920 #include "llvm/IR/LLVMContext.h"
8283 exit(1);
8384 }
8485
85 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
86 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
87 EmitSummaryIndex, EmitModuleHash);
86 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
87 std::unique_ptr Index;
88 if (EmitSummaryIndex)
89 Index = ModuleSummaryIndexBuilder(M).takeIndex();
90
91 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, Index.get(),
92 EmitModuleHash);
93 }
8894
8995 // Declare success.
9096 Out->keep();