blob: aacc945054637845160800229874197054e7c976 [file] [log] [blame]
Saleem Abdulrasool5898e092014-11-07 21:32:08 +00001//===- SymbolRewriter.cpp - Symbol Rewriter ---------------------*- 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// 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.
13//
14// The YAML configuration file format is as follows:
15//
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]+
25//
26// Currently, the following descriptor types are supported:
27//
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)
41//
42// Note that source and exactly one of [Target, Transform] must be provided
43//
44// New rewrite descriptors can be created. Addding a new rewrite descriptor
45// involves:
46//
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)
53//
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`
56// option.
57//
58//===----------------------------------------------------------------------===//
59
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"
73
74using namespace llvm;
75
76static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
77 cl::desc("Symbol Rewrite Map"),
78 cl::value_desc("filename"));
79
80namespace llvm {
81namespace SymbolRewriter {
82template <RewriteDescriptor::Type DT, typename ValueType,
83 ValueType *(llvm::Module::*Get)(StringRef) const>
84class ExplicitRewriteDescriptor : public RewriteDescriptor {
85public:
86 const std::string Source;
87 const std::string Target;
88
89 ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
90 : RewriteDescriptor(DT), Source(Naked ? StringRef("\01" + S.str()) : S),
91 Target(T) {}
92
93 bool performOnModule(Module &M) override;
94
95 static bool classof(const RewriteDescriptor *RD) {
96 return RD->getType() == DT;
97 }
98};
99
100template <RewriteDescriptor::Type DT, typename ValueType,
101 ValueType *(llvm::Module::*Get)(StringRef) const>
102bool 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());
107 else
108 S->setName(Target);
109 Changed = true;
110 }
111 return Changed;
112}
113
114template <RewriteDescriptor::Type DT, typename ValueType,
115 ValueType *(llvm::Module::*Get)(StringRef) const,
116 iterator_range<typename iplist<ValueType>::iterator> (llvm::Module::*Iterator)()>
117class PatternRewriteDescriptor : public RewriteDescriptor {
118public:
119 const std::string Pattern;
120 const std::string Transform;
121
122 PatternRewriteDescriptor(StringRef P, StringRef T)
123 : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
124
125 bool performOnModule(Module &M) override;
126
127 static bool classof(const RewriteDescriptor *RD) {
128 return RD->getType() == DT;
129 }
130};
131
132template <RewriteDescriptor::Type DT, typename ValueType,
133 ValueType *(llvm::Module::*Get)(StringRef) const,
134 iterator_range<typename iplist<ValueType>::iterator> (llvm::Module::*Iterator)()>
135bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
136performOnModule(Module &M) {
137 bool Changed = false;
138 for (auto &C : (M.*Iterator)()) {
139 std::string Error;
140
141 std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
142 if (!Error.empty())
143 report_fatal_error("unable to transforn " + C.getName() + " in " +
144 M.getModuleIdentifier() + ": " + Error);
145
146 if (Value *V = (M.*Get)(Name))
147 C.setValueName(V->getValueName());
148 else
149 C.setName(Name);
150
151 Changed = true;
152 }
153 return Changed;
154}
155
156/// Represents a rewrite for an explicitly named (function) symbol. Both the
157/// source function name and target function name of the transformation are
158/// explicitly spelt out.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000159typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
160 llvm::Function, &llvm::Module::getFunction>
161 ExplicitRewriteFunctionDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000162
163/// Represents a rewrite for an explicitly named (global variable) symbol. Both
164/// the source variable name and target variable name are spelt out. This
165/// applies only to module level variables.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000166typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
167 llvm::GlobalVariable,
168 &llvm::Module::getGlobalVariable>
169 ExplicitRewriteGlobalVariableDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000170
171/// Represents a rewrite for an explicitly named global alias. Both the source
172/// and target name are explicitly spelt out.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000173typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
174 llvm::GlobalAlias,
175 &llvm::Module::getNamedAlias>
176 ExplicitRewriteNamedAliasDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000177
178/// Represents a rewrite for a regular expression based pattern for functions.
179/// A pattern for the function name is provided and a transformation for that
180/// pattern to determine the target function name create the rewrite rule.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000181typedef PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
182 llvm::Function, &llvm::Module::getFunction,
183 &llvm::Module::functions>
184 PatternRewriteFunctionDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000185
186/// Represents a rewrite for a global variable based upon a matching pattern.
187/// Each global variable matching the provided pattern will be transformed as
188/// described in the transformation pattern for the target. Applies only to
189/// module level variables.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000190typedef PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
191 llvm::GlobalVariable,
192 &llvm::Module::getGlobalVariable,
193 &llvm::Module::globals>
194 PatternRewriteGlobalVariableDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000195
196/// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
197/// aliases which match a given pattern. The provided transformation will be
198/// applied to each of the matching names.
Saleem Abdulrasool89c5ad42014-11-07 22:09:52 +0000199typedef PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
200 llvm::GlobalAlias,
201 &llvm::Module::getNamedAlias,
202 &llvm::Module::aliases>
203 PatternRewriteNamedAliasDescriptor;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000204
205bool RewriteMapParser::parse(const std::string &MapFile,
206 RewriteDescriptorList *DL) {
207 ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
208 MemoryBuffer::getFile(MapFile);
209
210 if (!Mapping)
211 report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
212 Mapping.getError().message());
213
214 if (!parse(*Mapping, DL))
215 report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
216
217 return true;
218}
219
220bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
221 RewriteDescriptorList *DL) {
222 SourceMgr SM;
223 yaml::Stream YS(MapFile->getBuffer(), SM);
224
225 for (auto &Document : YS) {
226 yaml::MappingNode *DescriptorList;
227
228 // ignore empty documents
229 if (isa<yaml::NullNode>(Document.getRoot()))
230 continue;
231
232 DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
233 if (!DescriptorList) {
234 YS.printError(Document.getRoot(), "DescriptorList node must be a map");
235 return false;
236 }
237
238 for (auto &Descriptor : *DescriptorList)
239 if (!parseEntry(YS, Descriptor, DL))
240 return false;
241 }
242
243 return true;
244}
245
246bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
247 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000248 yaml::ScalarNode *Key;
249 yaml::MappingNode *Value;
250 SmallString<32> KeyStorage;
251 StringRef RewriteType;
252
253 Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
254 if (!Key) {
255 YS.printError(Entry.getKey(), "rewrite type must be a scalar");
256 return false;
257 }
258
259 Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
260 if (!Value) {
261 YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
262 return false;
263 }
264
265 RewriteType = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000266 if (RewriteType.equals("function"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000267 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000268 else if (RewriteType.equals("global variable"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000269 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000270 else if (RewriteType.equals("global alias"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000271 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
272
273 YS.printError(Entry.getKey(), "unknown rewrite type");
274 return false;
275}
276
277bool RewriteMapParser::
278parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
279 yaml::MappingNode *Descriptor,
280 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000281 bool Naked = false;
282 std::string Source;
283 std::string Target;
284 std::string Transform;
285
286 for (auto &Field : *Descriptor) {
287 yaml::ScalarNode *Key;
288 yaml::ScalarNode *Value;
289 SmallString<32> KeyStorage;
290 SmallString<32> ValueStorage;
291 StringRef KeyValue;
292
293 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
294 if (!Key) {
295 YS.printError(Field.getKey(), "descriptor key must be a scalar");
296 return false;
297 }
298
299 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
300 if (!Value) {
301 YS.printError(Field.getValue(), "descriptor value must be a scalar");
302 return false;
303 }
304
305 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000306 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000307 std::string Error;
308
309 Source = Value->getValue(ValueStorage);
310 if (!Regex(Source).isValid(Error)) {
311 YS.printError(Field.getKey(), "invalid regex: " + Error);
312 return false;
313 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000314 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000315 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000316 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000317 Transform = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000318 } else if (KeyValue.equals("naked")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000319 std::string Undecorated;
320
321 Undecorated = Value->getValue(ValueStorage);
322 Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
323 } else {
324 YS.printError(Field.getKey(), "unknown key for function");
325 return false;
326 }
327 }
328
329 if (Transform.empty() == Target.empty()) {
330 YS.printError(Descriptor,
331 "exactly one of transform or target must be specified");
332 return false;
333 }
334
335 // TODO see if there is a more elegant solution to selecting the rewrite
336 // descriptor type
337 if (!Target.empty())
338 DL->push_back(new ExplicitRewriteFunctionDescriptor(Source, Target, Naked));
339 else
340 DL->push_back(new PatternRewriteFunctionDescriptor(Source, Transform));
341
342 return true;
343}
344
345bool RewriteMapParser::
346parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
347 yaml::MappingNode *Descriptor,
348 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000349 std::string Source;
350 std::string Target;
351 std::string Transform;
352
353 for (auto &Field : *Descriptor) {
354 yaml::ScalarNode *Key;
355 yaml::ScalarNode *Value;
356 SmallString<32> KeyStorage;
357 SmallString<32> ValueStorage;
358 StringRef KeyValue;
359
360 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
361 if (!Key) {
362 YS.printError(Field.getKey(), "descriptor Key must be a scalar");
363 return false;
364 }
365
366 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
367 if (!Value) {
368 YS.printError(Field.getValue(), "descriptor value must be a scalar");
369 return false;
370 }
371
372 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000373 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000374 std::string Error;
375
376 Source = Value->getValue(ValueStorage);
377 if (!Regex(Source).isValid(Error)) {
378 YS.printError(Field.getKey(), "invalid regex: " + Error);
379 return false;
380 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000381 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000382 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000383 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000384 Transform = Value->getValue(ValueStorage);
385 } else {
386 YS.printError(Field.getKey(), "unknown Key for Global Variable");
387 return false;
388 }
389 }
390
391 if (Transform.empty() == Target.empty()) {
392 YS.printError(Descriptor,
393 "exactly one of transform or target must be specified");
394 return false;
395 }
396
397 if (!Target.empty())
398 DL->push_back(new ExplicitRewriteGlobalVariableDescriptor(Source, Target,
399 /*Naked*/false));
400 else
401 DL->push_back(new PatternRewriteGlobalVariableDescriptor(Source,
402 Transform));
403
404 return true;
405}
406
407bool RewriteMapParser::
408parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
409 yaml::MappingNode *Descriptor,
410 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000411 std::string Source;
412 std::string Target;
413 std::string Transform;
414
415 for (auto &Field : *Descriptor) {
416 yaml::ScalarNode *Key;
417 yaml::ScalarNode *Value;
418 SmallString<32> KeyStorage;
419 SmallString<32> ValueStorage;
420 StringRef KeyValue;
421
422 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
423 if (!Key) {
424 YS.printError(Field.getKey(), "descriptor key must be a scalar");
425 return false;
426 }
427
428 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
429 if (!Value) {
430 YS.printError(Field.getValue(), "descriptor value must be a scalar");
431 return false;
432 }
433
434 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000435 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000436 std::string Error;
437
438 Source = Value->getValue(ValueStorage);
439 if (!Regex(Source).isValid(Error)) {
440 YS.printError(Field.getKey(), "invalid regex: " + Error);
441 return false;
442 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000443 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000444 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000445 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000446 Transform = Value->getValue(ValueStorage);
447 } else {
448 YS.printError(Field.getKey(), "unknown key for Global Alias");
449 return false;
450 }
451 }
452
453 if (Transform.empty() == Target.empty()) {
454 YS.printError(Descriptor,
455 "exactly one of transform or target must be specified");
456 return false;
457 }
458
459 if (!Target.empty())
460 DL->push_back(new ExplicitRewriteNamedAliasDescriptor(Source, Target,
461 /*Naked*/false));
462 else
463 DL->push_back(new PatternRewriteNamedAliasDescriptor(Source, Transform));
464
465 return true;
466}
467}
468}
469
470namespace {
471class RewriteSymbols : public ModulePass {
472public:
473 static char ID; // Pass identification, replacement for typeid
474
475 RewriteSymbols();
476 RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL);
477
David Blaikie711cd9c2014-11-14 19:06:36 +0000478 bool runOnModule(Module &M) override;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000479
480private:
481 void loadAndParseMapFiles();
482
483 SymbolRewriter::RewriteDescriptorList Descriptors;
484};
485
486char RewriteSymbols::ID = 0;
487
488RewriteSymbols::RewriteSymbols() : ModulePass(ID) {
489 initializeRewriteSymbolsPass(*PassRegistry::getPassRegistry());
490 loadAndParseMapFiles();
491}
492
493RewriteSymbols::RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL)
494 : ModulePass(ID) {
495 std::swap(Descriptors, DL);
496}
497
498bool RewriteSymbols::runOnModule(Module &M) {
499 bool Changed;
500
501 Changed = false;
502 for (auto &Descriptor : Descriptors)
503 Changed |= Descriptor.performOnModule(M);
504
505 return Changed;
506}
507
508void RewriteSymbols::loadAndParseMapFiles() {
509 const std::vector<std::string> MapFiles(RewriteMapFiles);
510 SymbolRewriter::RewriteMapParser parser;
511
512 for (const auto &MapFile : MapFiles)
513 parser.parse(MapFile, &Descriptors);
514}
515}
516
517INITIALIZE_PASS(RewriteSymbols, "rewrite-symbols", "Rewrite Symbols", false,
Vasileios Kalintirisccde2a92014-11-10 17:37:53 +0000518 false)
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000519
520ModulePass *llvm::createRewriteSymbolsPass() { return new RewriteSymbols(); }
521
522ModulePass *
523llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
524 return new RewriteSymbols(DL);
525}