87 using namespace SymbolRewriter;
89 #define DEBUG_TYPE "symbol-rewriter" 98 const std::string &
Target) {
106 Comdats.erase(Comdats.find(Source));
123 bool performOnModule(
Module &M)
override;
134 bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(
Module &M) {
135 bool Changed =
false;
136 if (ValueType *S = (M.*Get)(
Source)) {
141 S->setValueName(
T->getValueName());
158 const std::string Pattern;
159 const std::string Transform;
164 bool performOnModule(
Module &M)
override;
177 bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
178 performOnModule(
Module &M) {
179 bool Changed =
false;
180 for (
auto &
C : (M.*Iterator)()) {
209 using ExplicitRewriteFunctionDescriptor =
216 using ExplicitRewriteGlobalVariableDescriptor =
222 using ExplicitRewriteNamedAliasDescriptor =
229 using PatternRewriteFunctionDescriptor =
237 using PatternRewriteGlobalVariableDescriptor =
245 using PatternRewriteNamedAliasDescriptor =
260 if (!
parse(*Mapping, DL))
271 for (
auto &Document : YS) {
275 if (isa<yaml::NullNode>(Document.getRoot()))
279 if (!DescriptorList) {
280 YS.printError(Document.getRoot(),
"DescriptorList node must be a map");
284 for (
auto &Descriptor : *DescriptorList)
285 if (!parseEntry(YS, Descriptor, DL))
311 RewriteType = Key->
getValue(KeyStorage);
312 if (RewriteType.
equals(
"function"))
313 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
314 else if (RewriteType.
equals(
"global variable"))
315 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
316 else if (RewriteType.
equals(
"global alias"))
317 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
323 bool RewriteMapParser::
330 std::string Transform;
332 for (
auto &
Field : *Descriptor) {
351 KeyValue = Key->
getValue(KeyStorage);
352 if (KeyValue.
equals(
"source")) {
355 Source = Value->
getValue(ValueStorage);
356 if (!
Regex(Source).isValid(Error)) {
360 }
else if (KeyValue.
equals(
"target")) {
361 Target = Value->
getValue(ValueStorage);
362 }
else if (KeyValue.
equals(
"transform")) {
363 Transform = Value->
getValue(ValueStorage);
364 }
else if (KeyValue.
equals(
"naked")) {
365 std::string Undecorated;
367 Undecorated = Value->
getValue(ValueStorage);
368 Naked =
StringRef(Undecorated).
lower() ==
"true" || Undecorated ==
"1";
375 if (Transform.empty() == Target.empty()) {
377 "exactly one of transform or target must be specified");
384 DL->push_back(llvm::make_unique<ExplicitRewriteFunctionDescriptor>(
385 Source, Target, Naked));
388 llvm::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
393 bool RewriteMapParser::
399 std::string Transform;
401 for (
auto &
Field : *Descriptor) {
420 KeyValue = Key->
getValue(KeyStorage);
421 if (KeyValue.
equals(
"source")) {
424 Source = Value->
getValue(ValueStorage);
425 if (!
Regex(Source).isValid(Error)) {
429 }
else if (KeyValue.
equals(
"target")) {
430 Target = Value->
getValue(ValueStorage);
431 }
else if (KeyValue.
equals(
"transform")) {
432 Transform = Value->
getValue(ValueStorage);
439 if (Transform.empty() == Target.empty()) {
441 "exactly one of transform or target must be specified");
446 DL->push_back(llvm::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
450 DL->push_back(llvm::make_unique<PatternRewriteGlobalVariableDescriptor>(
456 bool RewriteMapParser::
462 std::string Transform;
464 for (
auto &
Field : *Descriptor) {
483 KeyValue = Key->
getValue(KeyStorage);
484 if (KeyValue.
equals(
"source")) {
487 Source = Value->
getValue(ValueStorage);
488 if (!
Regex(Source).isValid(Error)) {
492 }
else if (KeyValue.
equals(
"target")) {
493 Target = Value->
getValue(ValueStorage);
494 }
else if (KeyValue.
equals(
"transform")) {
495 Transform = Value->
getValue(ValueStorage);
502 if (Transform.empty() == Target.empty()) {
504 "exactly one of transform or target must be specified");
509 DL->push_back(llvm::make_unique<ExplicitRewriteNamedAliasDescriptor>(
513 DL->push_back(llvm::make_unique<PatternRewriteNamedAliasDescriptor>(
521 class RewriteSymbolsLegacyPass :
public ModulePass {
525 RewriteSymbolsLegacyPass();
528 bool runOnModule(
Module &M)
override;
538 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() :
ModulePass(
ID) {
542 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
546 bool RewriteSymbolsLegacyPass::runOnModule(
Module &M) {
547 return Impl.runImpl(M);
561 for (
auto &Descriptor : Descriptors)
562 Changed |= Descriptor->performOnModule(M);
567 void RewriteSymbolPass::loadAndParseMapFiles() {
571 for (
const auto &MapFile : MapFiles)
572 Parser.
parse(MapFile, &Descriptors);
579 return new RewriteSymbolsLegacyPass();
584 return new RewriteSymbolsLegacyPass(DL);
Represents either an error or a value T.
static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT)
This is the entry point for all transforms.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
function - descriptor rewrites a function
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
Node * getKey()
Parse and return the key.
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
INITIALIZE_PASS(RewriteSymbolsLegacyPass, "rewrite-symbols", "Rewrite Symbols", false, false) ModulePass *llvm
GlobalAlias * getNamedAlias(StringRef Name) const
Return the global alias in the module with the specified name, of arbitrary type. ...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const ComdatSymTabType & getComdatSymbolTable() const
Get the Module's symbol table for COMDATs (constant).
The basic entity representing a rewrite operation.
void setName(const Twine &Name)
Change the name of the value.
bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors)
void setComdat(Comdat *C)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
A set of analyses that are preserved following a run of a transformation pass.
iterator_range< iterator > functions()
void printError(Node *N, const Twine &Msg)
std::error_code getError() const
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
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...
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Node * getValue()
Parse and return the value.
void setSelectionKind(SelectionKind Val)
global variable - descriptor rewrites a global variable
Module.h This file contains the declarations for the Module class.
This class represents a YAML stream potentially containing multiple documents.
ModulePass * createRewriteSymbolsPass()
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
std::list< std::unique_ptr< RewriteDescriptor > > RewriteDescriptorList
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
const Comdat * getComdat() const
StringRef getName() const
Return a constant reference to the value's name.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Represents a YAML map created from either a block map for a flow map.
void initializeRewriteSymbolsLegacyPassPass(PassRegistry &)
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
LLVM_NODISCARD std::string lower() const
LLVM Value Representation.
Lightweight error class with error context and mandatory checking.
static cl::list< std::string > RewriteMapFiles("rewrite-map-file", cl::desc("Symbol Rewrite Map"), cl::value_desc("filename"), cl::Hidden)
void setValueName(ValueName *VN)
iterator_range< global_iterator > globals()
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
StringRef getValue(SmallVectorImpl< char > &Storage) const
Gets the value of this node as a StringRef.
static void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, const std::string &Target)
iterator_range< alias_iterator > aliases()