Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:40

0001 //====- Internalize.h - Internalization API ---------------------*- 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 loops over all of the functions and variables in the input module.
0010 // If the function or variable does not need to be preserved according to the
0011 // client supplied callback, it is marked as internal.
0012 //
0013 // This transformation would not be legal in a regular compilation, but it gets
0014 // extra information from the linker about what is safe.
0015 //
0016 // For example: Internalizing a function with external linkage. Only if we are
0017 // told it is only used from within this module, it is safe to do it.
0018 //
0019 //===----------------------------------------------------------------------===//
0020 
0021 #ifndef LLVM_TRANSFORMS_IPO_INTERNALIZE_H
0022 #define LLVM_TRANSFORMS_IPO_INTERNALIZE_H
0023 
0024 #include "llvm/ADT/DenseMap.h"
0025 #include "llvm/ADT/StringSet.h"
0026 #include "llvm/IR/PassManager.h"
0027 #include <functional>
0028 
0029 namespace llvm {
0030 class Comdat;
0031 class GlobalValue;
0032 class Module;
0033 
0034 /// A pass that internalizes all functions and variables other than those that
0035 /// must be preserved according to \c MustPreserveGV.
0036 class InternalizePass : public PassInfoMixin<InternalizePass> {
0037   struct ComdatInfo {
0038     // The number of members. A comdat with one member which is not externally
0039     // visible can be freely dropped.
0040     size_t Size = 0;
0041     // Whether the comdat has an externally visible member.
0042     bool External = false;
0043   };
0044 
0045   bool IsWasm = false;
0046 
0047   /// Client supplied callback to control wheter a symbol must be preserved.
0048   const std::function<bool(const GlobalValue &)> MustPreserveGV;
0049   /// Set of symbols private to the compiler that this pass should not touch.
0050   StringSet<> AlwaysPreserved;
0051 
0052   /// Return false if we're allowed to internalize this GV.
0053   bool shouldPreserveGV(const GlobalValue &GV);
0054   /// Internalize GV if it is possible to do so, i.e. it is not externally
0055   /// visible and is not a member of an externally visible comdat.
0056   bool maybeInternalize(GlobalValue &GV,
0057                         DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
0058   /// If GV is part of a comdat and is externally visible, keep track of its
0059   /// comdat so that we don't internalize any of its members.
0060   void checkComdat(GlobalValue &GV,
0061                    DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
0062 
0063 public:
0064   InternalizePass();
0065   InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
0066       : MustPreserveGV(std::move(MustPreserveGV)) {}
0067 
0068   /// Run the internalizer on \p TheModule, returns true if any changes was
0069   /// made.
0070   bool internalizeModule(Module &TheModule);
0071 
0072   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0073 };
0074 
0075 /// Helper function to internalize functions and variables in a Module.
0076 inline bool
0077 internalizeModule(Module &TheModule,
0078                   std::function<bool(const GlobalValue &)> MustPreserveGV) {
0079   return InternalizePass(std::move(MustPreserveGV))
0080       .internalizeModule(TheModule);
0081 }
0082 } // end namespace llvm
0083 
0084 #endif // LLVM_TRANSFORMS_IPO_INTERNALIZE_H