Internalize.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //====- Internalize.h - Internalization API ---------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This pass loops over all of the functions and variables in the input module.
  15. // If the function or variable does not need to be preserved according to the
  16. // client supplied callback, it is marked as internal.
  17. //
  18. // This transformation would not be legal in a regular compilation, but it gets
  19. // extra information from the linker about what is safe.
  20. //
  21. // For example: Internalizing a function with external linkage. Only if we are
  22. // told it is only used from within this module, it is safe to do it.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_TRANSFORMS_IPO_INTERNALIZE_H
  26. #define LLVM_TRANSFORMS_IPO_INTERNALIZE_H
  27. #include "llvm/ADT/DenseMap.h"
  28. #include "llvm/ADT/StringSet.h"
  29. #include "llvm/IR/GlobalValue.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include <functional>
  32. namespace llvm {
  33. class Module;
  34. class CallGraph;
  35. /// A pass that internalizes all functions and variables other than those that
  36. /// must be preserved according to \c MustPreserveGV.
  37. class InternalizePass : public PassInfoMixin<InternalizePass> {
  38. struct ComdatInfo {
  39. // The number of members. A comdat with one member which is not externally
  40. // visible can be freely dropped.
  41. size_t Size = 0;
  42. // Whether the comdat has an externally visible member.
  43. bool External = false;
  44. };
  45. bool IsWasm = false;
  46. /// Client supplied callback to control wheter a symbol must be preserved.
  47. const std::function<bool(const GlobalValue &)> MustPreserveGV;
  48. /// Set of symbols private to the compiler that this pass should not touch.
  49. StringSet<> AlwaysPreserved;
  50. /// Return false if we're allowed to internalize this GV.
  51. bool shouldPreserveGV(const GlobalValue &GV);
  52. /// Internalize GV if it is possible to do so, i.e. it is not externally
  53. /// visible and is not a member of an externally visible comdat.
  54. bool maybeInternalize(GlobalValue &GV,
  55. DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
  56. /// If GV is part of a comdat and is externally visible, keep track of its
  57. /// comdat so that we don't internalize any of its members.
  58. void checkComdat(GlobalValue &GV,
  59. DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
  60. public:
  61. InternalizePass();
  62. InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
  63. : MustPreserveGV(std::move(MustPreserveGV)) {}
  64. /// Run the internalizer on \p TheModule, returns true if any changes was
  65. /// made.
  66. ///
  67. /// If the CallGraph \p CG is supplied, it will be updated when
  68. /// internalizing a function (by removing any edge from the "external node")
  69. bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
  70. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  71. };
  72. /// Helper function to internalize functions and variables in a Module.
  73. inline bool
  74. internalizeModule(Module &TheModule,
  75. std::function<bool(const GlobalValue &)> MustPreserveGV,
  76. CallGraph *CG = nullptr) {
  77. return InternalizePass(std::move(MustPreserveGV))
  78. .internalizeModule(TheModule, CG);
  79. }
  80. } // end namespace llvm
  81. #endif // LLVM_TRANSFORMS_IPO_INTERNALIZE_H
  82. #ifdef __GNUC__
  83. #pragma GCC diagnostic pop
  84. #endif