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