Internalize.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/PassManager.h"
  30. #include <functional>
  31. namespace llvm {
  32. class Module;
  33. class CallGraph;
  34. /// A pass that internalizes all functions and variables other than those that
  35. /// must be preserved according to \c MustPreserveGV.
  36. class InternalizePass : public PassInfoMixin<InternalizePass> {
  37. struct ComdatInfo {
  38. // The number of members. A comdat with one member which is not externally
  39. // visible can be freely dropped.
  40. size_t Size = 0;
  41. // Whether the comdat has an externally visible member.
  42. bool External = false;
  43. };
  44. bool IsWasm = false;
  45. /// Client supplied callback to control wheter a symbol must be preserved.
  46. const std::function<bool(const GlobalValue &)> MustPreserveGV;
  47. /// Set of symbols private to the compiler that this pass should not touch.
  48. StringSet<> AlwaysPreserved;
  49. /// Return false if we're allowed to internalize this GV.
  50. bool shouldPreserveGV(const GlobalValue &GV);
  51. /// Internalize GV if it is possible to do so, i.e. it is not externally
  52. /// visible and is not a member of an externally visible comdat.
  53. bool maybeInternalize(GlobalValue &GV,
  54. DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
  55. /// If GV is part of a comdat and is externally visible, keep track of its
  56. /// comdat so that we don't internalize any of its members.
  57. void checkComdat(GlobalValue &GV,
  58. DenseMap<const Comdat *, ComdatInfo> &ComdatMap);
  59. public:
  60. InternalizePass();
  61. InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
  62. : MustPreserveGV(std::move(MustPreserveGV)) {}
  63. /// Run the internalizer on \p TheModule, returns true if any changes was
  64. /// made.
  65. ///
  66. /// If the CallGraph \p CG is supplied, it will be updated when
  67. /// internalizing a function (by removing any edge from the "external node")
  68. bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
  69. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  70. };
  71. /// Helper function to internalize functions and variables in a Module.
  72. inline bool
  73. internalizeModule(Module &TheModule,
  74. std::function<bool(const GlobalValue &)> MustPreserveGV,
  75. CallGraph *CG = nullptr) {
  76. return InternalizePass(std::move(MustPreserveGV))
  77. .internalizeModule(TheModule, CG);
  78. }
  79. } // end namespace llvm
  80. #endif // LLVM_TRANSFORMS_IPO_INTERNALIZE_H
  81. #ifdef __GNUC__
  82. #pragma GCC diagnostic pop
  83. #endif