LLVM  14.0.0git
SymbolRewriter.h
Go to the documentation of this file.
1 //===- SymbolRewriter.h - Symbol Rewriting Pass -----------------*- 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 file provides the prototypes and definitions related to the Symbol
10 // Rewriter pass.
11 //
12 // The Symbol Rewriter pass takes a set of rewrite descriptors which define
13 // transformations for symbol names. These can be either single name to name
14 // trnsformation or more broad regular expression based transformations.
15 //
16 // All the functions are re-written at the IR level. The Symbol Rewriter itself
17 // is exposed as a module level pass. All symbols at the module level are
18 // iterated. For any matching symbol, the requested transformation is applied,
19 // updating references to it as well (a la RAUW). The resulting binary will
20 // only contain the rewritten symbols.
21 //
22 // By performing this operation in the compiler, we are able to catch symbols
23 // that would otherwise not be possible to catch (e.g. inlined symbols).
24 //
25 // This makes it possible to cleanly transform symbols without resorting to
26 // overly-complex macro tricks and the pre-processor. An example of where this
27 // is useful is the sanitizers where we would like to intercept a well-defined
28 // set of functions across the module.
29 //
30 //===----------------------------------------------------------------------===//
31 
32 #ifndef LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H
33 #define LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H
34 
35 #include "llvm/IR/PassManager.h"
36 #include <list>
37 #include <memory>
38 #include <string>
39 
40 namespace llvm {
41 
42 class MemoryBuffer;
43 class Module;
44 class ModulePass;
45 
46 namespace yaml {
47 
48 class KeyValueNode;
49 class MappingNode;
50 class ScalarNode;
51 class Stream;
52 
53 } // end namespace yaml
54 
55 namespace SymbolRewriter {
56 
57 /// The basic entity representing a rewrite operation. It serves as the base
58 /// class for any rewrite descriptor. It has a certain set of specializations
59 /// which describe a particular rewrite.
60 ///
61 /// The RewriteMapParser can be used to parse a mapping file that provides the
62 /// mapping for rewriting the symbols. The descriptors individually describe
63 /// whether to rewrite a function, global variable, or global alias. Each of
64 /// these can be selected either by explicitly providing a name for the ones to
65 /// be rewritten or providing a (posix compatible) regular expression that will
66 /// select the symbols to rewrite. This descriptor list is passed to the
67 /// SymbolRewriter pass.
69 public:
70  enum class Type {
71  Invalid, /// invalid
72  Function, /// function - descriptor rewrites a function
73  GlobalVariable, /// global variable - descriptor rewrites a global variable
74  NamedAlias, /// named alias - descriptor rewrites a global alias
75  };
76 
77  RewriteDescriptor(const RewriteDescriptor &) = delete;
79  virtual ~RewriteDescriptor() = default;
80 
81  Type getType() const { return Kind; }
82 
83  virtual bool performOnModule(Module &M) = 0;
84 
85 protected:
86  explicit RewriteDescriptor(Type T) : Kind(T) {}
87 
88 private:
89  const Type Kind;
90 };
91 
92 using RewriteDescriptorList = std::list<std::unique_ptr<RewriteDescriptor>>;
93 
95 public:
96  bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors);
97 
98 private:
99  bool parse(std::unique_ptr<MemoryBuffer> &MapFile, RewriteDescriptorList *DL);
100  bool parseEntry(yaml::Stream &Stream, yaml::KeyValueNode &Entry,
102  bool parseRewriteFunctionDescriptor(yaml::Stream &Stream,
106  bool parseRewriteGlobalVariableDescriptor(yaml::Stream &Stream,
110  bool parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
113 };
114 
115 } // end namespace SymbolRewriter
116 
119 
120 class RewriteSymbolPass : public PassInfoMixin<RewriteSymbolPass> {
121 public:
122  RewriteSymbolPass() { loadAndParseMapFiles(); }
123 
125  Descriptors.splice(Descriptors.begin(), DL);
126  }
127 
129 
130  // Glue for old PM
131  bool runImpl(Module &M);
132 
133 private:
134  void loadAndParseMapFiles();
135 
137 };
138 
139 } // end namespace llvm
140 
141 #endif // LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::SymbolRewriter::RewriteDescriptor::RewriteDescriptor
RewriteDescriptor(const RewriteDescriptor &)=delete
llvm::RewriteSymbolPass::RewriteSymbolPass
RewriteSymbolPass()
Definition: SymbolRewriter.h:122
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::RewriteSymbolPass
Definition: SymbolRewriter.h:120
llvm::SymbolRewriter::RewriteDescriptorList
std::list< std::unique_ptr< RewriteDescriptor > > RewriteDescriptorList
Definition: SymbolRewriter.h:92
llvm::SymbolRewriter::RewriteDescriptor::~RewriteDescriptor
virtual ~RewriteDescriptor()=default
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::RewriteSymbolPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: SymbolRewriter.cpp:552
llvm::yaml::ScalarNode
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
Definition: YAMLParser.h:212
llvm::RewriteSymbolPass::runImpl
bool runImpl(Module &M)
Definition: SymbolRewriter.cpp:559
llvm::yaml::MappingNode
Represents a YAML map created from either a block map for a flow map.
Definition: YAMLParser.h:414
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::SymbolRewriter::RewriteDescriptor::RewriteDescriptor
RewriteDescriptor(Type T)
Definition: SymbolRewriter.h:86
llvm::SymbolRewriter::RewriteDescriptor::Type::Function
@ Function
invalid
llvm::createRewriteSymbolsPass
ModulePass * createRewriteSymbolsPass()
llvm::SymbolRewriter::RewriteDescriptor::getType
Type getType() const
Definition: SymbolRewriter.h:81
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::SymbolRewriter::RewriteDescriptor::Type
Type
Definition: SymbolRewriter.h:70
llvm::SymbolRewriter::RewriteMapParser
Definition: SymbolRewriter.h:94
llvm::SymbolRewriter::RewriteDescriptor::Type::GlobalVariable
@ GlobalVariable
function - descriptor rewrites a function
llvm::SymbolRewriter::RewriteDescriptor::operator=
RewriteDescriptor & operator=(const RewriteDescriptor &)=delete
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::SymbolRewriter::RewriteDescriptor::performOnModule
virtual bool performOnModule(Module &M)=0
llvm::yaml::KeyValueNode
A key and value pair.
Definition: YAMLParser.h:285
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::yaml::Stream
This class represents a YAML stream potentially containing multiple documents.
Definition: YAMLParser.h:86
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::SymbolRewriter::RewriteDescriptor
The basic entity representing a rewrite operation.
Definition: SymbolRewriter.h:68
llvm::SymbolRewriter::RewriteDescriptor::Type::NamedAlias
@ NamedAlias
global variable - descriptor rewrites a global variable
llvm::RewriteSymbolPass::RewriteSymbolPass
RewriteSymbolPass(SymbolRewriter::RewriteDescriptorList &DL)
Definition: SymbolRewriter.h:124
PassManager.h
llvm::SymbolRewriter::RewriteDescriptor::Type::Invalid
@ Invalid
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::SymbolRewriter::RewriteMapParser::parse
bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors)
Definition: SymbolRewriter.cpp:253