60 #define DEBUG_TYPE "symbol-rewriter"
76 using namespace SymbolRewriter;
84 const std::string &
Target) {
92 Comdats.erase(Comdats.find(Source));
105 : RewriteDescriptor(DT), Source(Naked ?
StringRef(
"\01" + S.str()) : S),
108 bool performOnModule(
Module &M)
override;
110 static bool classof(
const RewriteDescriptor *RD) {
117 bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(
Module &M) {
118 bool Changed =
false;
119 if (ValueType *S = (M.*Get)(
Source)) {
123 if (
Value *
T = (M.*Get)(Target))
124 S->setValueName(
T->getValueName());
136 (llvm::
Module::*Iterator)()>
137 class PatternRewriteDescriptor : public RewriteDescriptor {
139 const std::string Pattern;
140 const std::string Transform;
143 : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
145 bool performOnModule(
Module &M)
override;
147 static bool classof(
const RewriteDescriptor *RD) {
155 (llvm::
Module::*Iterator)()>
156 bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
157 performOnModule(
Module &M) {
158 bool Changed =
false;
159 for (
auto &C : (M.*Iterator)()) {
165 M.getModuleIdentifier() +
": " +
Error);
167 if (
C.getName() ==
Name)
174 C.setValueName(V->getValueName());
188 ExplicitRewriteFunctionDescriptor;
196 ExplicitRewriteGlobalVariableDescriptor;
203 ExplicitRewriteNamedAliasDescriptor;
211 PatternRewriteFunctionDescriptor;
218 llvm::GlobalVariable,
221 PatternRewriteGlobalVariableDescriptor;
230 PatternRewriteNamedAliasDescriptor;
242 if (!
parse(*Mapping, DL))
253 for (
auto &Document : YS) {
257 if (isa<yaml::NullNode>(Document.getRoot()))
261 if (!DescriptorList) {
262 YS.printError(Document.getRoot(),
"DescriptorList node must be a map");
266 for (
auto &Descriptor : *DescriptorList)
267 if (!parseEntry(YS, Descriptor, DL))
293 RewriteType = Key->
getValue(KeyStorage);
294 if (RewriteType.
equals(
"function"))
295 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
296 else if (RewriteType.
equals(
"global variable"))
297 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
298 else if (RewriteType.
equals(
"global alias"))
299 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
305 bool RewriteMapParser::
312 std::string Transform;
314 for (
auto &
Field : *Descriptor) {
333 KeyValue = Key->
getValue(KeyStorage);
334 if (KeyValue.
equals(
"source")) {
337 Source = Value->
getValue(ValueStorage);
338 if (!
Regex(Source).isValid(Error)) {
342 }
else if (KeyValue.
equals(
"target")) {
343 Target = Value->
getValue(ValueStorage);
344 }
else if (KeyValue.
equals(
"transform")) {
345 Transform = Value->
getValue(ValueStorage);
346 }
else if (KeyValue.
equals(
"naked")) {
347 std::string Undecorated;
349 Undecorated = Value->
getValue(ValueStorage);
350 Naked =
StringRef(Undecorated).
lower() ==
"true" || Undecorated ==
"1";
357 if (Transform.empty() == Target.empty()) {
359 "exactly one of transform or target must be specified");
366 DL->
push_back(
new ExplicitRewriteFunctionDescriptor(Source, Target, Naked));
368 DL->
push_back(
new PatternRewriteFunctionDescriptor(Source, Transform));
373 bool RewriteMapParser::
379 std::string Transform;
381 for (
auto &
Field : *Descriptor) {
400 KeyValue = Key->
getValue(KeyStorage);
401 if (KeyValue.
equals(
"source")) {
404 Source = Value->
getValue(ValueStorage);
405 if (!
Regex(Source).isValid(Error)) {
409 }
else if (KeyValue.
equals(
"target")) {
410 Target = Value->
getValue(ValueStorage);
411 }
else if (KeyValue.
equals(
"transform")) {
412 Transform = Value->
getValue(ValueStorage);
419 if (Transform.empty() == Target.empty()) {
421 "exactly one of transform or target must be specified");
426 DL->
push_back(
new ExplicitRewriteGlobalVariableDescriptor(Source, Target,
429 DL->
push_back(
new PatternRewriteGlobalVariableDescriptor(Source,
435 bool RewriteMapParser::
441 std::string Transform;
443 for (
auto &
Field : *Descriptor) {
462 KeyValue = Key->
getValue(KeyStorage);
463 if (KeyValue.
equals(
"source")) {
466 Source = Value->
getValue(ValueStorage);
467 if (!
Regex(Source).isValid(Error)) {
471 }
else if (KeyValue.
equals(
"target")) {
472 Target = Value->
getValue(ValueStorage);
473 }
else if (KeyValue.
equals(
"transform")) {
474 Transform = Value->
getValue(ValueStorage);
481 if (Transform.empty() == Target.empty()) {
483 "exactly one of transform or target must be specified");
488 DL->
push_back(
new ExplicitRewriteNamedAliasDescriptor(Source, Target,
491 DL->
push_back(
new PatternRewriteNamedAliasDescriptor(Source, Transform));
504 bool runOnModule(
Module &M)
override;
507 void loadAndParseMapFiles();
516 loadAndParseMapFiles();
521 Descriptors.splice(Descriptors.begin(), DL);
524 bool RewriteSymbols::runOnModule(
Module &M) {
528 for (
auto &Descriptor : Descriptors)
529 Changed |= Descriptor.performOnModule(M);
534 void RewriteSymbols::loadAndParseMapFiles() {
538 for (
const auto &MapFile : MapFiles)
539 parser.
parse(MapFile, &Descriptors);
550 return new RewriteSymbols(DL);
std::error_code getError() const
Represents either an error or a value T.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
A Module instance is used to store all the information related to an LLVM module. ...
StringRef getValue(SmallVectorImpl< char > &Storage) const
Gets the value of this node as a StringRef.
function - descriptor rewrites a function
Node * getKey()
Parse and return the key.
static cl::list< std::string > RewriteMapFiles("rewrite-map-file", cl::desc("Symbol Rewrite Map"), cl::value_desc("filename"))
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
void push_back(NodeTy *val)
The basic entity representing a rewrite operation.
bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors)
void setComdat(Comdat *C)
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
iterator_range< iterator > functions()
void printError(Node *N, const Twine &Msg)
const Comdat * getComdat() const
INITIALIZE_PASS(RewriteSymbols,"rewrite-symbols","Rewrite Symbols", false, false) ModulePass *llvm
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
std::string sub(StringRef Repl, StringRef String, std::string *Error=nullptr)
sub - Return the result of replacing the first match of the regex in String with the Repl string...
iplist - The subset of list functionality that can safely be used on nodes of polymorphic types...
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
Node * getValue()
Parse and return the value.
void setSelectionKind(SelectionKind Val)
global variable - descriptor rewrites a global variable
This class represents a YAML stream potentially containing multiple documents.
ModulePass * createRewriteSymbolsPass()
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
GlobalAlias * getNamedAlias(StringRef Name) const
Return the global alias in the module with the specified name, of arbitrary type. ...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
const ComdatSymTabType & getComdatSymbolTable() const
Get the Module's symbol table for COMDATs (constant).
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Represents a YAML map created from either a block map for a flow map.
LLVM Value Representation.
iterator_range< global_iterator > globals()
C - The default llvm calling convention, compatible with C.
StringRef - Represent a constant reference to a string, i.e.
static void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, const std::string &Target)
std::string lower() const
void initializeRewriteSymbolsPass(PassRegistry &)
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
iterator_range< alias_iterator > aliases()