blob: 5d380dcf231c0b25b7915f4fd8dce286889223cc [file] [log] [blame]
Eugene Zelenko286d5892017-10-11 21:41:43 +00001//===- SymbolRewriter.cpp - Symbol Rewriter -------------------------------===//
Saleem Abdulrasool5898e092014-11-07 21:32:08 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Saleem Abdulrasool5898e092014-11-07 21:32:08 +00006//
7//===----------------------------------------------------------------------===//
8//
9// SymbolRewriter is a LLVM pass which can rewrite symbols transparently within
10// existing code. It is implemented as a compiler pass and is configured via a
11// YAML configuration file.
12//
13// The YAML configuration file format is as follows:
14//
15// RewriteMapFile := RewriteDescriptors
16// RewriteDescriptors := RewriteDescriptor | RewriteDescriptors
17// RewriteDescriptor := RewriteDescriptorType ':' '{' RewriteDescriptorFields '}'
18// RewriteDescriptorFields := RewriteDescriptorField | RewriteDescriptorFields
19// RewriteDescriptorField := FieldIdentifier ':' FieldValue ','
20// RewriteDescriptorType := Identifier
21// FieldIdentifier := Identifier
22// FieldValue := Identifier
23// Identifier := [0-9a-zA-Z]+
24//
25// Currently, the following descriptor types are supported:
26//
27// - function: (function rewriting)
28// + Source (original name of the function)
29// + Target (explicit transformation)
30// + Transform (pattern transformation)
31// + Naked (boolean, whether the function is undecorated)
32// - global variable: (external linkage global variable rewriting)
33// + Source (original name of externally visible variable)
34// + Target (explicit transformation)
35// + Transform (pattern transformation)
36// - global alias: (global alias rewriting)
37// + Source (original name of the aliased name)
38// + Target (explicit transformation)
39// + Transform (pattern transformation)
40//
41// Note that source and exactly one of [Target, Transform] must be provided
42//
43// New rewrite descriptors can be created. Addding a new rewrite descriptor
44// involves:
45//
46// a) extended the rewrite descriptor kind enumeration
47// (<anonymous>::RewriteDescriptor::RewriteDescriptorType)
48// b) implementing the new descriptor
49// (c.f. <anonymous>::ExplicitRewriteFunctionDescriptor)
50// c) extending the rewrite map parser
51// (<anonymous>::RewriteMapParser::parseEntry)
52//
53// Specify to rewrite the symbols using the `-rewrite-symbols` option, and
54// specify the map file to use for the rewriting via the `-rewrite-map-file`
55// option.
56//
57//===----------------------------------------------------------------------===//
58
Michael Kuperstein39feb622016-07-25 20:52:00 +000059#include "llvm/Transforms/Utils/SymbolRewriter.h"
Eugene Zelenko286d5892017-10-11 21:41:43 +000060#include "llvm/ADT/STLExtras.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000061#include "llvm/ADT/SmallString.h"
Eugene Zelenko286d5892017-10-11 21:41:43 +000062#include "llvm/ADT/StringRef.h"
63#include "llvm/ADT/ilist.h"
64#include "llvm/ADT/iterator_range.h"
65#include "llvm/IR/Comdat.h"
66#include "llvm/IR/Function.h"
67#include "llvm/IR/GlobalAlias.h"
68#include "llvm/IR/GlobalObject.h"
69#include "llvm/IR/GlobalVariable.h"
70#include "llvm/IR/Module.h"
71#include "llvm/IR/Value.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000072#include "llvm/Pass.h"
Eugene Zelenko286d5892017-10-11 21:41:43 +000073#include "llvm/Support/Casting.h"
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000074#include "llvm/Support/CommandLine.h"
Eugene Zelenko286d5892017-10-11 21:41:43 +000075#include "llvm/Support/ErrorHandling.h"
76#include "llvm/Support/ErrorOr.h"
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000077#include "llvm/Support/MemoryBuffer.h"
78#include "llvm/Support/Regex.h"
79#include "llvm/Support/SourceMgr.h"
80#include "llvm/Support/YAMLParser.h"
Eugene Zelenko286d5892017-10-11 21:41:43 +000081#include <memory>
82#include <string>
83#include <vector>
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000084
85using namespace llvm;
Benjamin Kramerfd3bc742015-03-09 15:50:47 +000086using namespace SymbolRewriter;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000087
Eugene Zelenko286d5892017-10-11 21:41:43 +000088#define DEBUG_TYPE "symbol-rewriter"
89
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000090static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
91 cl::desc("Symbol Rewrite Map"),
Zachary Turner8065f0b2017-12-01 00:53:10 +000092 cl::value_desc("filename"),
93 cl::Hidden);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +000094
Benjamin Kramerfd3bc742015-03-09 15:50:47 +000095static void rewriteComdat(Module &M, GlobalObject *GO,
96 const std::string &Source,
97 const std::string &Target) {
Saleem Abdulrasoolc44d71b2015-01-27 22:57:39 +000098 if (Comdat *CD = GO->getComdat()) {
99 auto &Comdats = M.getComdatSymbolTable();
100
101 Comdat *C = M.getOrInsertComdat(Target);
102 C->setSelectionKind(CD->getSelectionKind());
103 GO->setComdat(C);
104
105 Comdats.erase(Comdats.find(Source));
106 }
107}
108
Benjamin Kramerfd3bc742015-03-09 15:50:47 +0000109namespace {
Eugene Zelenko286d5892017-10-11 21:41:43 +0000110
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000111template <RewriteDescriptor::Type DT, typename ValueType,
Eugene Zelenko286d5892017-10-11 21:41:43 +0000112 ValueType *(Module::*Get)(StringRef) const>
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000113class ExplicitRewriteDescriptor : public RewriteDescriptor {
114public:
115 const std::string Source;
116 const std::string Target;
117
118 ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
119 : RewriteDescriptor(DT), Source(Naked ? StringRef("\01" + S.str()) : S),
120 Target(T) {}
121
122 bool performOnModule(Module &M) override;
123
124 static bool classof(const RewriteDescriptor *RD) {
125 return RD->getType() == DT;
126 }
127};
128
Eugene Zelenko286d5892017-10-11 21:41:43 +0000129} // end anonymous namespace
130
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000131template <RewriteDescriptor::Type DT, typename ValueType,
Eugene Zelenko286d5892017-10-11 21:41:43 +0000132 ValueType *(Module::*Get)(StringRef) const>
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000133bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
134 bool Changed = false;
135 if (ValueType *S = (M.*Get)(Source)) {
Saleem Abdulrasoolc44d71b2015-01-27 22:57:39 +0000136 if (GlobalObject *GO = dyn_cast<GlobalObject>(S))
137 rewriteComdat(M, GO, Source, Target);
138
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000139 if (Value *T = (M.*Get)(Target))
140 S->setValueName(T->getValueName());
141 else
142 S->setName(Target);
Saleem Abdulrasoolc44d71b2015-01-27 22:57:39 +0000143
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000144 Changed = true;
145 }
146 return Changed;
147}
148
Eugene Zelenko286d5892017-10-11 21:41:43 +0000149namespace {
150
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000151template <RewriteDescriptor::Type DT, typename ValueType,
Eugene Zelenko286d5892017-10-11 21:41:43 +0000152 ValueType *(Module::*Get)(StringRef) const,
Saleem Abdulrasoold37ce302015-01-05 17:56:29 +0000153 iterator_range<typename iplist<ValueType>::iterator>
Eugene Zelenko286d5892017-10-11 21:41:43 +0000154 (Module::*Iterator)()>
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000155class PatternRewriteDescriptor : public RewriteDescriptor {
156public:
157 const std::string Pattern;
158 const std::string Transform;
159
160 PatternRewriteDescriptor(StringRef P, StringRef T)
161 : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
162
163 bool performOnModule(Module &M) override;
164
165 static bool classof(const RewriteDescriptor *RD) {
166 return RD->getType() == DT;
167 }
168};
169
Eugene Zelenko286d5892017-10-11 21:41:43 +0000170} // end anonymous namespace
171
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000172template <RewriteDescriptor::Type DT, typename ValueType,
Eugene Zelenko286d5892017-10-11 21:41:43 +0000173 ValueType *(Module::*Get)(StringRef) const,
Saleem Abdulrasoold37ce302015-01-05 17:56:29 +0000174 iterator_range<typename iplist<ValueType>::iterator>
Eugene Zelenko286d5892017-10-11 21:41:43 +0000175 (Module::*Iterator)()>
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000176bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
177performOnModule(Module &M) {
178 bool Changed = false;
179 for (auto &C : (M.*Iterator)()) {
180 std::string Error;
181
182 std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
183 if (!Error.empty())
184 report_fatal_error("unable to transforn " + C.getName() + " in " +
185 M.getModuleIdentifier() + ": " + Error);
186
Saleem Abdulrasool9769b182015-01-27 22:57:35 +0000187 if (C.getName() == Name)
188 continue;
189
Saleem Abdulrasoolc44d71b2015-01-27 22:57:39 +0000190 if (GlobalObject *GO = dyn_cast<GlobalObject>(&C))
191 rewriteComdat(M, GO, C.getName(), Name);
192
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000193 if (Value *V = (M.*Get)(Name))
194 C.setValueName(V->getValueName());
195 else
196 C.setName(Name);
197
198 Changed = true;
199 }
200 return Changed;
201}
202
Eugene Zelenko286d5892017-10-11 21:41:43 +0000203namespace {
204
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000205/// Represents a rewrite for an explicitly named (function) symbol. Both the
206/// source function name and target function name of the transformation are
207/// explicitly spelt out.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000208using ExplicitRewriteFunctionDescriptor =
209 ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
210 &Module::getFunction>;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000211
212/// Represents a rewrite for an explicitly named (global variable) symbol. Both
213/// the source variable name and target variable name are spelt out. This
214/// applies only to module level variables.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000215using ExplicitRewriteGlobalVariableDescriptor =
216 ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
217 GlobalVariable, &Module::getGlobalVariable>;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000218
219/// Represents a rewrite for an explicitly named global alias. Both the source
220/// and target name are explicitly spelt out.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000221using ExplicitRewriteNamedAliasDescriptor =
222 ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
223 &Module::getNamedAlias>;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000224
225/// Represents a rewrite for a regular expression based pattern for functions.
226/// A pattern for the function name is provided and a transformation for that
227/// pattern to determine the target function name create the rewrite rule.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000228using PatternRewriteFunctionDescriptor =
229 PatternRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
230 &Module::getFunction, &Module::functions>;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000231
232/// Represents a rewrite for a global variable based upon a matching pattern.
233/// Each global variable matching the provided pattern will be transformed as
234/// described in the transformation pattern for the target. Applies only to
235/// module level variables.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000236using PatternRewriteGlobalVariableDescriptor =
237 PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
238 GlobalVariable, &Module::getGlobalVariable,
239 &Module::globals>;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000240
241/// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
242/// aliases which match a given pattern. The provided transformation will be
243/// applied to each of the matching names.
Eugene Zelenko286d5892017-10-11 21:41:43 +0000244using PatternRewriteNamedAliasDescriptor =
245 PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
246 &Module::getNamedAlias, &Module::aliases>;
247
248} // end anonymous namespace
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000249
250bool RewriteMapParser::parse(const std::string &MapFile,
251 RewriteDescriptorList *DL) {
252 ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
253 MemoryBuffer::getFile(MapFile);
254
255 if (!Mapping)
256 report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
257 Mapping.getError().message());
258
259 if (!parse(*Mapping, DL))
260 report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
261
262 return true;
263}
264
265bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
266 RewriteDescriptorList *DL) {
267 SourceMgr SM;
268 yaml::Stream YS(MapFile->getBuffer(), SM);
269
270 for (auto &Document : YS) {
271 yaml::MappingNode *DescriptorList;
272
273 // ignore empty documents
274 if (isa<yaml::NullNode>(Document.getRoot()))
275 continue;
276
277 DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
278 if (!DescriptorList) {
279 YS.printError(Document.getRoot(), "DescriptorList node must be a map");
280 return false;
281 }
282
283 for (auto &Descriptor : *DescriptorList)
284 if (!parseEntry(YS, Descriptor, DL))
285 return false;
286 }
287
288 return true;
289}
290
291bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
292 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000293 yaml::ScalarNode *Key;
294 yaml::MappingNode *Value;
295 SmallString<32> KeyStorage;
296 StringRef RewriteType;
297
298 Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
299 if (!Key) {
300 YS.printError(Entry.getKey(), "rewrite type must be a scalar");
301 return false;
302 }
303
304 Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
305 if (!Value) {
306 YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
307 return false;
308 }
309
310 RewriteType = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000311 if (RewriteType.equals("function"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000312 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000313 else if (RewriteType.equals("global variable"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000314 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000315 else if (RewriteType.equals("global alias"))
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000316 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
317
318 YS.printError(Entry.getKey(), "unknown rewrite type");
319 return false;
320}
321
322bool RewriteMapParser::
323parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
324 yaml::MappingNode *Descriptor,
325 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000326 bool Naked = false;
327 std::string Source;
328 std::string Target;
329 std::string Transform;
330
331 for (auto &Field : *Descriptor) {
332 yaml::ScalarNode *Key;
333 yaml::ScalarNode *Value;
334 SmallString<32> KeyStorage;
335 SmallString<32> ValueStorage;
336 StringRef KeyValue;
337
338 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
339 if (!Key) {
340 YS.printError(Field.getKey(), "descriptor key must be a scalar");
341 return false;
342 }
343
344 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
345 if (!Value) {
346 YS.printError(Field.getValue(), "descriptor value must be a scalar");
347 return false;
348 }
349
350 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000351 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000352 std::string Error;
353
354 Source = Value->getValue(ValueStorage);
355 if (!Regex(Source).isValid(Error)) {
356 YS.printError(Field.getKey(), "invalid regex: " + Error);
357 return false;
358 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000359 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000360 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000361 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000362 Transform = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000363 } else if (KeyValue.equals("naked")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000364 std::string Undecorated;
365
366 Undecorated = Value->getValue(ValueStorage);
367 Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
368 } else {
369 YS.printError(Field.getKey(), "unknown key for function");
370 return false;
371 }
372 }
373
374 if (Transform.empty() == Target.empty()) {
375 YS.printError(Descriptor,
376 "exactly one of transform or target must be specified");
377 return false;
378 }
379
380 // TODO see if there is a more elegant solution to selecting the rewrite
381 // descriptor type
382 if (!Target.empty())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000383 DL->push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
Michael Kuperstein9a89b152016-07-25 18:39:08 +0000384 Source, Target, Naked));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000385 else
Michael Kuperstein8f8e1d12016-07-25 18:10:54 +0000386 DL->push_back(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000387 std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000388
389 return true;
390}
391
392bool RewriteMapParser::
393parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
394 yaml::MappingNode *Descriptor,
395 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000396 std::string Source;
397 std::string Target;
398 std::string Transform;
399
400 for (auto &Field : *Descriptor) {
401 yaml::ScalarNode *Key;
402 yaml::ScalarNode *Value;
403 SmallString<32> KeyStorage;
404 SmallString<32> ValueStorage;
405 StringRef KeyValue;
406
407 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
408 if (!Key) {
409 YS.printError(Field.getKey(), "descriptor Key must be a scalar");
410 return false;
411 }
412
413 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
414 if (!Value) {
415 YS.printError(Field.getValue(), "descriptor value must be a scalar");
416 return false;
417 }
418
419 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000420 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000421 std::string Error;
422
423 Source = Value->getValue(ValueStorage);
424 if (!Regex(Source).isValid(Error)) {
425 YS.printError(Field.getKey(), "invalid regex: " + Error);
426 return false;
427 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000428 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000429 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000430 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000431 Transform = Value->getValue(ValueStorage);
432 } else {
433 YS.printError(Field.getKey(), "unknown Key for Global Variable");
434 return false;
435 }
436 }
437
438 if (Transform.empty() == Target.empty()) {
439 YS.printError(Descriptor,
440 "exactly one of transform or target must be specified");
441 return false;
442 }
443
444 if (!Target.empty())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000445 DL->push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
Michael Kuperstein9a89b152016-07-25 18:39:08 +0000446 Source, Target,
447 /*Naked*/ false));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000448 else
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000449 DL->push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
Michael Kuperstein9a89b152016-07-25 18:39:08 +0000450 Source, Transform));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000451
452 return true;
453}
454
455bool RewriteMapParser::
456parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
457 yaml::MappingNode *Descriptor,
458 RewriteDescriptorList *DL) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000459 std::string Source;
460 std::string Target;
461 std::string Transform;
462
463 for (auto &Field : *Descriptor) {
464 yaml::ScalarNode *Key;
465 yaml::ScalarNode *Value;
466 SmallString<32> KeyStorage;
467 SmallString<32> ValueStorage;
468 StringRef KeyValue;
469
470 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
471 if (!Key) {
472 YS.printError(Field.getKey(), "descriptor key must be a scalar");
473 return false;
474 }
475
476 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
477 if (!Value) {
478 YS.printError(Field.getValue(), "descriptor value must be a scalar");
479 return false;
480 }
481
482 KeyValue = Key->getValue(KeyStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000483 if (KeyValue.equals("source")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000484 std::string Error;
485
486 Source = Value->getValue(ValueStorage);
487 if (!Regex(Source).isValid(Error)) {
488 YS.printError(Field.getKey(), "invalid regex: " + Error);
489 return false;
490 }
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000491 } else if (KeyValue.equals("target")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000492 Target = Value->getValue(ValueStorage);
Saleem Abdulrasoold2c5d7f2014-11-08 00:00:50 +0000493 } else if (KeyValue.equals("transform")) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000494 Transform = Value->getValue(ValueStorage);
495 } else {
496 YS.printError(Field.getKey(), "unknown key for Global Alias");
497 return false;
498 }
499 }
500
501 if (Transform.empty() == Target.empty()) {
502 YS.printError(Descriptor,
503 "exactly one of transform or target must be specified");
504 return false;
505 }
506
507 if (!Target.empty())
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000508 DL->push_back(std::make_unique<ExplicitRewriteNamedAliasDescriptor>(
Michael Kuperstein9a89b152016-07-25 18:39:08 +0000509 Source, Target,
510 /*Naked*/ false));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000511 else
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000512 DL->push_back(std::make_unique<PatternRewriteNamedAliasDescriptor>(
Michael Kuperstein9a89b152016-07-25 18:39:08 +0000513 Source, Transform));
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000514
515 return true;
516}
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000517
518namespace {
Eugene Zelenko286d5892017-10-11 21:41:43 +0000519
Michael Kuperstein39feb622016-07-25 20:52:00 +0000520class RewriteSymbolsLegacyPass : public ModulePass {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000521public:
522 static char ID; // Pass identification, replacement for typeid
523
Michael Kuperstein39feb622016-07-25 20:52:00 +0000524 RewriteSymbolsLegacyPass();
525 RewriteSymbolsLegacyPass(SymbolRewriter::RewriteDescriptorList &DL);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000526
David Blaikie711cd9c2014-11-14 19:06:36 +0000527 bool runOnModule(Module &M) override;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000528
529private:
Michael Kuperstein39feb622016-07-25 20:52:00 +0000530 RewriteSymbolPass Impl;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000531};
532
Eugene Zelenko286d5892017-10-11 21:41:43 +0000533} // end anonymous namespace
534
Michael Kuperstein39feb622016-07-25 20:52:00 +0000535char RewriteSymbolsLegacyPass::ID = 0;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000536
Eugene Zelenko286d5892017-10-11 21:41:43 +0000537RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() : ModulePass(ID) {
Fangrui Songf78650a2018-07-30 19:41:25 +0000538 initializeRewriteSymbolsLegacyPassPass(*PassRegistry::getPassRegistry());
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000539}
540
Michael Kuperstein39feb622016-07-25 20:52:00 +0000541RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
542 SymbolRewriter::RewriteDescriptorList &DL)
543 : ModulePass(ID), Impl(DL) {}
544
545bool RewriteSymbolsLegacyPass::runOnModule(Module &M) {
546 return Impl.runImpl(M);
547}
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000548
Michael Kuperstein39feb622016-07-25 20:52:00 +0000549PreservedAnalyses RewriteSymbolPass::run(Module &M, ModuleAnalysisManager &AM) {
550 if (!runImpl(M))
551 return PreservedAnalyses::all();
552
553 return PreservedAnalyses::none();
554}
555
556bool RewriteSymbolPass::runImpl(Module &M) {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000557 bool Changed;
558
559 Changed = false;
560 for (auto &Descriptor : Descriptors)
Michael Kuperstein8f8e1d12016-07-25 18:10:54 +0000561 Changed |= Descriptor->performOnModule(M);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000562
563 return Changed;
564}
565
Michael Kuperstein39feb622016-07-25 20:52:00 +0000566void RewriteSymbolPass::loadAndParseMapFiles() {
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000567 const std::vector<std::string> MapFiles(RewriteMapFiles);
Michael Kuperstein39feb622016-07-25 20:52:00 +0000568 SymbolRewriter::RewriteMapParser Parser;
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000569
570 for (const auto &MapFile : MapFiles)
Michael Kuperstein39feb622016-07-25 20:52:00 +0000571 Parser.parse(MapFile, &Descriptors);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000572}
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000573
Michael Kuperstein39feb622016-07-25 20:52:00 +0000574INITIALIZE_PASS(RewriteSymbolsLegacyPass, "rewrite-symbols", "Rewrite Symbols",
575 false, false)
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000576
Michael Kuperstein39feb622016-07-25 20:52:00 +0000577ModulePass *llvm::createRewriteSymbolsPass() {
578 return new RewriteSymbolsLegacyPass();
579}
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000580
581ModulePass *
582llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
Michael Kuperstein39feb622016-07-25 20:52:00 +0000583 return new RewriteSymbolsLegacyPass(DL);
Saleem Abdulrasool5898e092014-11-07 21:32:08 +0000584}