86using namespace SymbolRewriter;
88#define DEBUG_TYPE "symbol-rewriter"
96 const std::string &Source,
97 const std::string &
Target) {
99 auto &Comdats = M.getComdatSymbolTable();
102 C->setSelectionKind(CD->getSelectionKind());
105 Comdats.erase(Comdats.find(Source));
134bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(
Module &M) {
135 bool Changed =
false;
141 S->setValueName(
T->getValueName());
159 const std::string Transform;
178bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
179performOnModule(
Module &M) {
180 bool Changed =
false;
181 for (
auto &
C : (
M.*Iterator)()) {
187 M.getModuleIdentifier() +
": " +
Error);
189 if (
C.getName() ==
Name)
196 C.setValueName(
V->getValueName());
210using ExplicitRewriteFunctionDescriptor =
211 ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
Function,
217using ExplicitRewriteGlobalVariableDescriptor =
218 ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
223using ExplicitRewriteNamedAliasDescriptor =
224 ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
GlobalAlias,
230using PatternRewriteFunctionDescriptor =
231 PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
Function,
238using PatternRewriteGlobalVariableDescriptor =
239 PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
246using PatternRewriteNamedAliasDescriptor =
247 PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
GlobalAlias,
259 "': " + Mapping.
getError().message());
272 for (
auto &Document : YS) {
276 if (isa<yaml::NullNode>(Document.getRoot()))
279 DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
280 if (!DescriptorList) {
281 YS.printError(Document.getRoot(),
"DescriptorList node must be a map");
285 for (
auto &Descriptor : *DescriptorList)
286 if (!parseEntry(YS, Descriptor,
DL))
300 Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
302 YS.
printError(Entry.getKey(),
"rewrite type must be a scalar");
306 Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
308 YS.
printError(Entry.getValue(),
"rewrite descriptor must be a map");
312 RewriteType =
Key->getValue(KeyStorage);
313 if (RewriteType.
equals(
"function"))
314 return parseRewriteFunctionDescriptor(YS, Key,
Value,
DL);
315 else if (RewriteType.
equals(
"global variable"))
316 return parseRewriteGlobalVariableDescriptor(YS, Key,
Value,
DL);
317 else if (RewriteType.
equals(
"global alias"))
318 return parseRewriteGlobalAliasDescriptor(YS, Key,
Value,
DL);
320 YS.
printError(Entry.getKey(),
"unknown rewrite type");
324bool RewriteMapParser::
331 std::string Transform;
333 for (
auto &
Field : *Descriptor) {
340 Key = dyn_cast<yaml::ScalarNode>(
Field.getKey());
346 Value = dyn_cast<yaml::ScalarNode>(
Field.getValue());
352 KeyValue =
Key->getValue(KeyStorage);
353 if (KeyValue.
equals(
"source")) {
356 Source = std::string(
Value->getValue(ValueStorage));
361 }
else if (KeyValue.
equals(
"target")) {
362 Target = std::string(
Value->getValue(ValueStorage));
363 }
else if (KeyValue.
equals(
"transform")) {
364 Transform = std::string(
Value->getValue(ValueStorage));
365 }
else if (KeyValue.
equals(
"naked")) {
366 std::string Undecorated;
368 Undecorated = std::string(
Value->getValue(ValueStorage));
376 if (Transform.empty() ==
Target.empty()) {
378 "exactly one of transform or target must be specified");
385 DL->
push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
389 std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
394bool RewriteMapParser::
400 std::string Transform;
402 for (
auto &
Field : *Descriptor) {
409 Key = dyn_cast<yaml::ScalarNode>(
Field.getKey());
415 Value = dyn_cast<yaml::ScalarNode>(
Field.getValue());
421 KeyValue =
Key->getValue(KeyStorage);
422 if (KeyValue.
equals(
"source")) {
425 Source = std::string(
Value->getValue(ValueStorage));
430 }
else if (KeyValue.
equals(
"target")) {
431 Target = std::string(
Value->getValue(ValueStorage));
432 }
else if (KeyValue.
equals(
"transform")) {
433 Transform = std::string(
Value->getValue(ValueStorage));
440 if (Transform.empty() ==
Target.empty()) {
442 "exactly one of transform or target must be specified");
447 DL->
push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
451 DL->
push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
457bool RewriteMapParser::
463 std::string Transform;
465 for (
auto &
Field : *Descriptor) {
472 Key = dyn_cast<yaml::ScalarNode>(
Field.getKey());
478 Value = dyn_cast<yaml::ScalarNode>(
Field.getValue());
484 KeyValue =
Key->getValue(KeyStorage);
485 if (KeyValue.
equals(
"source")) {
488 Source = std::string(
Value->getValue(ValueStorage));
493 }
else if (KeyValue.
equals(
"target")) {
494 Target = std::string(
Value->getValue(ValueStorage));
495 }
else if (KeyValue.
equals(
"transform")) {
496 Transform = std::string(
Value->getValue(ValueStorage));
503 if (Transform.empty() ==
Target.empty()) {
505 "exactly one of transform or target must be specified");
510 DL->
push_back(std::make_unique<ExplicitRewriteNamedAliasDescriptor>(
514 DL->
push_back(std::make_unique<PatternRewriteNamedAliasDescriptor>(
522class RewriteSymbolsLegacyPass :
public ModulePass {
526 RewriteSymbolsLegacyPass();
529 bool runOnModule(
Module &M)
override;
537char RewriteSymbolsLegacyPass::ID = 0;
539RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() :
ModulePass(
ID) {
543RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
547bool RewriteSymbolsLegacyPass::runOnModule(
Module &M) {
548 return Impl.runImpl(M);
562 for (
auto &Descriptor : Descriptors)
563 Changed |= Descriptor->performOnModule(M);
568void RewriteSymbolPass::loadAndParseMapFiles() {
572 for (
const auto &MapFile : MapFiles)
573 Parser.
parse(MapFile, &Descriptors);
580 return new RewriteSymbolsLegacyPass();
585 return new RewriteSymbolsLegacyPass(
DL);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Provides ErrorOr<T> smart pointer.
static bool runImpl(Function &F, const TargetLowering &TLI)
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file defines the SmallString class.
static cl::list< std::string > RewriteMapFiles("rewrite-map-file", cl::desc("Symbol Rewrite Map"), cl::value_desc("filename"), cl::Hidden)
static void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, const std::string &Target)
A container for analyses that lazily runs them and caches their results.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
void setComdat(Comdat *C)
const Comdat * getComdat() const
void push_back(MachineInstr *MI)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
iterator_range< iterator > functions()
iterator_range< alias_iterator > aliases()
iterator_range< global_iterator > globals()
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
GlobalAlias * getNamedAlias(StringRef Name) const
Return the global alias in the module with the specified name, of arbitrary type.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
std::string sub(StringRef Repl, StringRef String, std::string *Error=nullptr) const
sub - Return the result of replacing the first match of the regex in String with the Repl string.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
StringRef - Represent a constant reference to a string, i.e.
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
std::string lower() const
The basic entity representing a rewrite operation.
virtual bool performOnModule(Module &M)=0
bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors)
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
A range adaptor for a pair of iterators.
Represents a YAML map created from either a block map for a flow map.
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
This class represents a YAML stream potentially containing multiple documents.
void printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind=SourceMgr::DK_Error)
This file defines classes to implement an intrusive doubly linked list class (i.e.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
std::list< std::unique_ptr< RewriteDescriptor > > RewriteDescriptorList
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createRewriteSymbolsPass()
void initializeRewriteSymbolsLegacyPassPass(PassRegistry &)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType