LLVM  13.0.0git
Internalize.h
Go to the documentation of this file.
1 //====- Internalize.h - Internalization API ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass loops over all of the functions and variables in the input module.
10 // If the function or variable does not need to be preserved according to the
11 // client supplied callback, it is marked as internal.
12 //
13 // This transformation would not be legal in a regular compilation, but it gets
14 // extra information from the linker about what is safe.
15 //
16 // For example: Internalizing a function with external linkage. Only if we are
17 // told it is only used from within this module, it is safe to do it.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_TRANSFORMS_IPO_INTERNALIZE_H
22 #define LLVM_TRANSFORMS_IPO_INTERNALIZE_H
23 
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/IR/PassManager.h"
28 #include <functional>
29 
30 namespace llvm {
31 class Module;
32 class CallGraph;
33 
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  /// Client supplied callback to control wheter a symbol must be preserved.
38  const std::function<bool(const GlobalValue &)> MustPreserveGV;
39  /// Set of symbols private to the compiler that this pass should not touch.
40  StringSet<> AlwaysPreserved;
41 
42  /// Return false if we're allowed to internalize this GV.
43  bool shouldPreserveGV(const GlobalValue &GV);
44  /// Internalize GV if it is possible to do so, i.e. it is not externally
45  /// visible and is not a member of an externally visible comdat.
46  bool maybeInternalize(GlobalValue &GV,
47  const DenseSet<const Comdat *> &ExternalComdats);
48  /// If GV is part of a comdat and is externally visible, keep track of its
49  /// comdat so that we don't internalize any of its members.
50  void checkComdatVisibility(GlobalValue &GV,
51  DenseSet<const Comdat *> &ExternalComdats);
52 
53 public:
55  InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
56  : MustPreserveGV(std::move(MustPreserveGV)) {}
57 
58  /// Run the internalizer on \p TheModule, returns true if any changes was
59  /// made.
60  ///
61  /// If the CallGraph \p CG is supplied, it will be updated when
62  /// internalizing a function (by removing any edge from the "external node")
63  bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
64 
66 };
67 
68 /// Helper function to internalize functions and variables in a Module.
69 inline bool
71  std::function<bool(const GlobalValue &)> MustPreserveGV,
72  CallGraph *CG = nullptr) {
73  return InternalizePass(std::move(MustPreserveGV))
74  .internalizeModule(TheModule, CG);
75 }
76 } // end namespace llvm
77 
78 #endif // LLVM_TRANSFORMS_IPO_INTERNALIZE_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
Definition: AllocatorList.h:23
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::InternalizePass::internalizeModule
bool internalizeModule(Module &TheModule, CallGraph *CG=nullptr)
Run the internalizer on TheModule, returns true if any changes was made.
Definition: Internalize.cpp:150
llvm::InternalizePass
A pass that internalizes all functions and variables other than those that must be preserved accordin...
Definition: Internalize.h:36
llvm::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
GlobalValue.h
llvm::InternalizePass::InternalizePass
InternalizePass()
Definition: Internalize.cpp:237
DenseSet.h
llvm::InternalizePass::InternalizePass
InternalizePass(std::function< bool(const GlobalValue &)> MustPreserveGV)
Definition: Internalize.h:55
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::GlobalValue
Definition: GlobalValue.h:44
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1540
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::internalizeModule
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV, CallGraph *CG=nullptr)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:70
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
llvm::InternalizePass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: Internalize.cpp:239
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
StringSet.h
std
Definition: BitVector.h:838
PassManager.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44