Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:28

0001 //===------ GlobalMergeFunctions.h - Global merge functions -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This pass defines the implementation of a function merging mechanism
0010 // that utilizes a stable function hash to track differences in constants and
0011 // identify potential merge candidates. The process involves two rounds:
0012 // 1. The first round collects stable function hashes and identifies merge
0013 //    candidates with matching hashes. It also computes the set of parameters
0014 //    that point to different constants during the stable function merge.
0015 // 2. The second round leverages this collected global function information to
0016 //    optimistically create a merged function in each module context, ensuring
0017 //    correct transformation.
0018 // Similar to the global outliner, this approach uses the linker's deduplication
0019 // (ICF) to fold identical merged functions, thereby reducing the final binary
0020 // size. The work is inspired by the concepts discussed in the following paper:
0021 // https://dl.acm.org/doi/pdf/10.1145/3652032.3657575.
0022 //
0023 //===----------------------------------------------------------------------===//
0024 
0025 #ifndef LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
0026 #define LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
0027 
0028 #include "llvm/CGData/StableFunctionMap.h"
0029 #include "llvm/IR/Module.h"
0030 #include "llvm/IR/PassManager.h"
0031 #include "llvm/Pass.h"
0032 
0033 enum class HashFunctionMode {
0034   Local,
0035   BuildingHashFuncion,
0036   UsingHashFunction,
0037 };
0038 
0039 namespace llvm {
0040 
0041 // A vector of locations (the pair of (instruction, operand) indices) reachable
0042 // from a parameter.
0043 using ParamLocs = SmallVector<IndexPair, 4>;
0044 // A vector of parameters
0045 using ParamLocsVecTy = SmallVector<ParamLocs, 8>;
0046 
0047 /// GlobalMergeFunc is a ModulePass that implements a function merging mechanism
0048 /// using stable function hashes. It identifies and merges functions with
0049 /// matching hashes across modules to optimize binary size.
0050 class GlobalMergeFunc {
0051   HashFunctionMode MergerMode = HashFunctionMode::Local;
0052 
0053   std::unique_ptr<StableFunctionMap> LocalFunctionMap;
0054 
0055   const ModuleSummaryIndex *Index;
0056 
0057 public:
0058   /// The suffix used to identify the merged function that parameterizes
0059   /// the constant values. Note that the original function, without this suffix,
0060   /// becomes a thunk supplying contexts to the merged function via parameters.
0061   static constexpr const char MergingInstanceSuffix[] = ".Tgm";
0062 
0063   GlobalMergeFunc(const ModuleSummaryIndex *Index) : Index(Index) {};
0064 
0065   void initializeMergerMode(const Module &M);
0066 
0067   bool run(Module &M);
0068 
0069   /// Analyze module to create stable function into LocalFunctionMap.
0070   void analyze(Module &M);
0071 
0072   /// Emit LocalFunctionMap into __llvm_merge section.
0073   void emitFunctionMap(Module &M);
0074 
0075   /// Merge functions in the module using the given function map.
0076   bool merge(Module &M, const StableFunctionMap *FunctionMap);
0077 };
0078 
0079 /// Global function merging pass for new pass manager.
0080 struct GlobalMergeFuncPass : public PassInfoMixin<GlobalMergeFuncPass> {
0081   const ModuleSummaryIndex *ImportSummary = nullptr;
0082   GlobalMergeFuncPass() = default;
0083   GlobalMergeFuncPass(const ModuleSummaryIndex *ImportSummary)
0084       : ImportSummary(ImportSummary) {}
0085   PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
0086 };
0087 
0088 } // end namespace llvm
0089 #endif // LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H