LLVM 19.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/DenseMap.h"
25#include "llvm/ADT/StringSet.h"
26#include "llvm/IR/PassManager.h"
27#include <functional>
28
29namespace llvm {
30class Module;
31
32/// A pass that internalizes all functions and variables other than those that
33/// must be preserved according to \c MustPreserveGV.
34class InternalizePass : public PassInfoMixin<InternalizePass> {
35 struct ComdatInfo {
36 // The number of members. A comdat with one member which is not externally
37 // visible can be freely dropped.
38 size_t Size = 0;
39 // Whether the comdat has an externally visible member.
40 bool External = false;
41 };
42
43 bool IsWasm = false;
44
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
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,
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,
60
61public:
63 InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
64 : MustPreserveGV(std::move(MustPreserveGV)) {}
65
66 /// Run the internalizer on \p TheModule, returns true if any changes was
67 /// made.
68 bool internalizeModule(Module &TheModule);
69
71};
72
73/// Helper function to internalize functions and variables in a Module.
74inline bool
76 std::function<bool(const GlobalValue &)> MustPreserveGV) {
77 return InternalizePass(std::move(MustPreserveGV))
78 .internalizeModule(TheModule);
79}
80} // end namespace llvm
81
82#endif // LLVM_TRANSFORMS_IPO_INTERNALIZE_H
This file defines the DenseMap class.
Machine Check Debug Module
This header defines various interfaces for pass management in LLVM.
StringSet - A set-like wrapper for the StringMap.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
A pass that internalizes all functions and variables other than those that must be preserved accordin...
Definition: Internalize.h:34
InternalizePass(std::function< bool(const GlobalValue &)> MustPreserveGV)
Definition: Internalize.h:63
bool internalizeModule(Module &TheModule)
Run the internalizer on TheModule, returns true if any changes was made.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:75
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:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74