1 //===- SymbolRewriter.cpp - Symbol Rewriter ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // SymbolRewriter is a LLVM pass which can rewrite symbols transparently within
11 // existing code. It is implemented as a compiler pass and is configured via a
12 // YAML configuration file.
14 // The YAML configuration file format is as follows:
16 // RewriteMapFile := RewriteDescriptors
17 // RewriteDescriptors := RewriteDescriptor | RewriteDescriptors
18 // RewriteDescriptor := RewriteDescriptorType ':' '{' RewriteDescriptorFields '}'
19 // RewriteDescriptorFields := RewriteDescriptorField | RewriteDescriptorFields
20 // RewriteDescriptorField := FieldIdentifier ':' FieldValue ','
21 // RewriteDescriptorType := Identifier
22 // FieldIdentifier := Identifier
23 // FieldValue := Identifier
24 // Identifier := [0-9a-zA-Z]+
26 // Currently, the following descriptor types are supported:
28 // - function: (function rewriting)
29 // + Source (original name of the function)
30 // + Target (explicit transformation)
31 // + Transform (pattern transformation)
32 // + Naked (boolean, whether the function is undecorated)
33 // - global variable: (external linkage global variable rewriting)
34 // + Source (original name of externally visible variable)
35 // + Target (explicit transformation)
36 // + Transform (pattern transformation)
37 // - global alias: (global alias rewriting)
38 // + Source (original name of the aliased name)
39 // + Target (explicit transformation)
40 // + Transform (pattern transformation)
42 // Note that source and exactly one of [Target, Transform] must be provided
44 // New rewrite descriptors can be created. Addding a new rewrite descriptor
47 // a) extended the rewrite descriptor kind enumeration
48 // (<anonymous>::RewriteDescriptor::RewriteDescriptorType)
49 // b) implementing the new descriptor
50 // (c.f. <anonymous>::ExplicitRewriteFunctionDescriptor)
51 // c) extending the rewrite map parser
52 // (<anonymous>::RewriteMapParser::parseEntry)
54 // Specify to rewrite the symbols using the `-rewrite-symbols` option, and
55 // specify the map file to use for the rewriting via the `-rewrite-map-file`
58 //===----------------------------------------------------------------------===//
60 #define DEBUG_TYPE "symbol-rewriter"
61 #include "llvm/CodeGen/Passes.h"
62 #include "llvm/Pass.h"
63 #include "llvm/PassManager.h"
64 #include "llvm/Support/CommandLine.h"
65 #include "llvm/Support/Debug.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/Regex.h"
68 #include "llvm/Support/SourceMgr.h"
69 #include "llvm/Support/YAMLParser.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
72 #include "llvm/Transforms/Utils/SymbolRewriter.h"
76 static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
77 cl::desc("Symbol Rewrite Map"),
78 cl::value_desc("filename"));
81 namespace SymbolRewriter {
82 template <RewriteDescriptor::Type DT, typename ValueType,
83 ValueType *(llvm::Module::*Get)(StringRef) const>
84 class ExplicitRewriteDescriptor : public RewriteDescriptor {
86 const std::string Source;
87 const std::string Target;
89 ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
90 : RewriteDescriptor(DT), Source(Naked ? StringRef("\01" + S.str()) : S),
93 bool performOnModule(Module &M) override;
95 static bool classof(const RewriteDescriptor *RD) {
96 return RD->getType() == DT;
100 template <RewriteDescriptor::Type DT, typename ValueType,
101 ValueType *(llvm::Module::*Get)(StringRef) const>
102 bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
103 bool Changed = false;
104 if (ValueType *S = (M.*Get)(Source)) {
105 if (Value *T = (M.*Get)(Target))
106 S->setValueName(T->getValueName());
114 template <RewriteDescriptor::Type DT, typename ValueType,
115 ValueType *(llvm::Module::*Get)(StringRef) const,
116 iterator_range<typename iplist<ValueType>::iterator>
117 (llvm::Module::*Iterator)()>
118 class PatternRewriteDescriptor : public RewriteDescriptor {
120 const std::string Pattern;
121 const std::string Transform;
123 PatternRewriteDescriptor(StringRef P, StringRef T)
124 : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
126 bool performOnModule(Module &M) override;
128 static bool classof(const RewriteDescriptor *RD) {
129 return RD->getType() == DT;
133 template <RewriteDescriptor::Type DT, typename ValueType,
134 ValueType *(llvm::Module::*Get)(StringRef) const,
135 iterator_range<typename iplist<ValueType>::iterator>
136 (llvm::Module::*Iterator)()>
137 bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
138 performOnModule(Module &M) {
139 bool Changed = false;
140 for (auto &C : (M.*Iterator)()) {
143 std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
145 report_fatal_error("unable to transforn " + C.getName() + " in " +
146 M.getModuleIdentifier() + ": " + Error);
148 if (C.getName() == Name)
151 if (Value *V = (M.*Get)(Name))
152 C.setValueName(V->getValueName());
161 /// Represents a rewrite for an explicitly named (function) symbol. Both the
162 /// source function name and target function name of the transformation are
163 /// explicitly spelt out.
164 typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
165 llvm::Function, &llvm::Module::getFunction>
166 ExplicitRewriteFunctionDescriptor;
168 /// Represents a rewrite for an explicitly named (global variable) symbol. Both
169 /// the source variable name and target variable name are spelt out. This
170 /// applies only to module level variables.
171 typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
172 llvm::GlobalVariable,
173 &llvm::Module::getGlobalVariable>
174 ExplicitRewriteGlobalVariableDescriptor;
176 /// Represents a rewrite for an explicitly named global alias. Both the source
177 /// and target name are explicitly spelt out.
178 typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
180 &llvm::Module::getNamedAlias>
181 ExplicitRewriteNamedAliasDescriptor;
183 /// Represents a rewrite for a regular expression based pattern for functions.
184 /// A pattern for the function name is provided and a transformation for that
185 /// pattern to determine the target function name create the rewrite rule.
186 typedef PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
187 llvm::Function, &llvm::Module::getFunction,
188 &llvm::Module::functions>
189 PatternRewriteFunctionDescriptor;
191 /// Represents a rewrite for a global variable based upon a matching pattern.
192 /// Each global variable matching the provided pattern will be transformed as
193 /// described in the transformation pattern for the target. Applies only to
194 /// module level variables.
195 typedef PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
196 llvm::GlobalVariable,
197 &llvm::Module::getGlobalVariable,
198 &llvm::Module::globals>
199 PatternRewriteGlobalVariableDescriptor;
201 /// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
202 /// aliases which match a given pattern. The provided transformation will be
203 /// applied to each of the matching names.
204 typedef PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
206 &llvm::Module::getNamedAlias,
207 &llvm::Module::aliases>
208 PatternRewriteNamedAliasDescriptor;
210 bool RewriteMapParser::parse(const std::string &MapFile,
211 RewriteDescriptorList *DL) {
212 ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
213 MemoryBuffer::getFile(MapFile);
216 report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
217 Mapping.getError().message());
219 if (!parse(*Mapping, DL))
220 report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
225 bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
226 RewriteDescriptorList *DL) {
228 yaml::Stream YS(MapFile->getBuffer(), SM);
230 for (auto &Document : YS) {
231 yaml::MappingNode *DescriptorList;
233 // ignore empty documents
234 if (isa<yaml::NullNode>(Document.getRoot()))
237 DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
238 if (!DescriptorList) {
239 YS.printError(Document.getRoot(), "DescriptorList node must be a map");
243 for (auto &Descriptor : *DescriptorList)
244 if (!parseEntry(YS, Descriptor, DL))
251 bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
252 RewriteDescriptorList *DL) {
253 yaml::ScalarNode *Key;
254 yaml::MappingNode *Value;
255 SmallString<32> KeyStorage;
256 StringRef RewriteType;
258 Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
260 YS.printError(Entry.getKey(), "rewrite type must be a scalar");
264 Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
266 YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
270 RewriteType = Key->getValue(KeyStorage);
271 if (RewriteType.equals("function"))
272 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
273 else if (RewriteType.equals("global variable"))
274 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
275 else if (RewriteType.equals("global alias"))
276 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
278 YS.printError(Entry.getKey(), "unknown rewrite type");
282 bool RewriteMapParser::
283 parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
284 yaml::MappingNode *Descriptor,
285 RewriteDescriptorList *DL) {
289 std::string Transform;
291 for (auto &Field : *Descriptor) {
292 yaml::ScalarNode *Key;
293 yaml::ScalarNode *Value;
294 SmallString<32> KeyStorage;
295 SmallString<32> ValueStorage;
298 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
300 YS.printError(Field.getKey(), "descriptor key must be a scalar");
304 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
306 YS.printError(Field.getValue(), "descriptor value must be a scalar");
310 KeyValue = Key->getValue(KeyStorage);
311 if (KeyValue.equals("source")) {
314 Source = Value->getValue(ValueStorage);
315 if (!Regex(Source).isValid(Error)) {
316 YS.printError(Field.getKey(), "invalid regex: " + Error);
319 } else if (KeyValue.equals("target")) {
320 Target = Value->getValue(ValueStorage);
321 } else if (KeyValue.equals("transform")) {
322 Transform = Value->getValue(ValueStorage);
323 } else if (KeyValue.equals("naked")) {
324 std::string Undecorated;
326 Undecorated = Value->getValue(ValueStorage);
327 Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
329 YS.printError(Field.getKey(), "unknown key for function");
334 if (Transform.empty() == Target.empty()) {
335 YS.printError(Descriptor,
336 "exactly one of transform or target must be specified");
340 // TODO see if there is a more elegant solution to selecting the rewrite
343 DL->push_back(new ExplicitRewriteFunctionDescriptor(Source, Target, Naked));
345 DL->push_back(new PatternRewriteFunctionDescriptor(Source, Transform));
350 bool RewriteMapParser::
351 parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
352 yaml::MappingNode *Descriptor,
353 RewriteDescriptorList *DL) {
356 std::string Transform;
358 for (auto &Field : *Descriptor) {
359 yaml::ScalarNode *Key;
360 yaml::ScalarNode *Value;
361 SmallString<32> KeyStorage;
362 SmallString<32> ValueStorage;
365 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
367 YS.printError(Field.getKey(), "descriptor Key must be a scalar");
371 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
373 YS.printError(Field.getValue(), "descriptor value must be a scalar");
377 KeyValue = Key->getValue(KeyStorage);
378 if (KeyValue.equals("source")) {
381 Source = Value->getValue(ValueStorage);
382 if (!Regex(Source).isValid(Error)) {
383 YS.printError(Field.getKey(), "invalid regex: " + Error);
386 } else if (KeyValue.equals("target")) {
387 Target = Value->getValue(ValueStorage);
388 } else if (KeyValue.equals("transform")) {
389 Transform = Value->getValue(ValueStorage);
391 YS.printError(Field.getKey(), "unknown Key for Global Variable");
396 if (Transform.empty() == Target.empty()) {
397 YS.printError(Descriptor,
398 "exactly one of transform or target must be specified");
403 DL->push_back(new ExplicitRewriteGlobalVariableDescriptor(Source, Target,
406 DL->push_back(new PatternRewriteGlobalVariableDescriptor(Source,
412 bool RewriteMapParser::
413 parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
414 yaml::MappingNode *Descriptor,
415 RewriteDescriptorList *DL) {
418 std::string Transform;
420 for (auto &Field : *Descriptor) {
421 yaml::ScalarNode *Key;
422 yaml::ScalarNode *Value;
423 SmallString<32> KeyStorage;
424 SmallString<32> ValueStorage;
427 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
429 YS.printError(Field.getKey(), "descriptor key must be a scalar");
433 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
435 YS.printError(Field.getValue(), "descriptor value must be a scalar");
439 KeyValue = Key->getValue(KeyStorage);
440 if (KeyValue.equals("source")) {
443 Source = Value->getValue(ValueStorage);
444 if (!Regex(Source).isValid(Error)) {
445 YS.printError(Field.getKey(), "invalid regex: " + Error);
448 } else if (KeyValue.equals("target")) {
449 Target = Value->getValue(ValueStorage);
450 } else if (KeyValue.equals("transform")) {
451 Transform = Value->getValue(ValueStorage);
453 YS.printError(Field.getKey(), "unknown key for Global Alias");
458 if (Transform.empty() == Target.empty()) {
459 YS.printError(Descriptor,
460 "exactly one of transform or target must be specified");
465 DL->push_back(new ExplicitRewriteNamedAliasDescriptor(Source, Target,
468 DL->push_back(new PatternRewriteNamedAliasDescriptor(Source, Transform));
476 class RewriteSymbols : public ModulePass {
478 static char ID; // Pass identification, replacement for typeid
481 RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL);
483 bool runOnModule(Module &M) override;
486 void loadAndParseMapFiles();
488 SymbolRewriter::RewriteDescriptorList Descriptors;
491 char RewriteSymbols::ID = 0;
493 RewriteSymbols::RewriteSymbols() : ModulePass(ID) {
494 initializeRewriteSymbolsPass(*PassRegistry::getPassRegistry());
495 loadAndParseMapFiles();
498 RewriteSymbols::RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL)
500 Descriptors.splice(Descriptors.begin(), DL);
503 bool RewriteSymbols::runOnModule(Module &M) {
507 for (auto &Descriptor : Descriptors)
508 Changed |= Descriptor.performOnModule(M);
513 void RewriteSymbols::loadAndParseMapFiles() {
514 const std::vector<std::string> MapFiles(RewriteMapFiles);
515 SymbolRewriter::RewriteMapParser parser;
517 for (const auto &MapFile : MapFiles)
518 parser.parse(MapFile, &Descriptors);
522 INITIALIZE_PASS(RewriteSymbols, "rewrite-symbols", "Rewrite Symbols", false,
525 ModulePass *llvm::createRewriteSymbolsPass() { return new RewriteSymbols(); }
528 llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
529 return new RewriteSymbols(DL);