86#define DEBUG_TYPE "symbol-rewriter"
94 const std::string &Source,
95 const std::string &
Target) {
97 auto &Comdats = M.getComdatSymbolTable();
100 C->setSelectionKind(CD->getSelectionKind());
103 Comdats.erase(Comdats.find(Source));
113 const std::string Source;
114 const std::string Target;
116 ExplicitRewriteDescriptor(StringRef S, StringRef
T,
const bool Naked)
117 : RewriteDescriptor(DT),
118 Source(std::
string(
Naked ? StringRef(
"\01" + S.str()) : S)),
121 bool performOnModule(
Module &M)
override;
123 static bool classof(
const RewriteDescriptor *RD) {
132bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(
Module &M) {
139 S->setValueName(
T->getValueName());
156 const std::string Pattern;
157 const std::string Transform;
159 PatternRewriteDescriptor(StringRef
P, StringRef
T)
160 : RewriteDescriptor(DT), Pattern(std::
string(
P)),
163 bool performOnModule(
Module &M)
override;
165 static bool classof(
const RewriteDescriptor *RD) {
176bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
177performOnModule(
Module &M) {
179 for (
auto &
C : (M.*Iterator)()) {
185 M.getModuleIdentifier() +
": " +
Error);
187 if (
C.getName() == Name)
193 if (
Value *V = (M.*Get)(Name))
194 C.setValueName(V->getValueName());
208using ExplicitRewriteFunctionDescriptor =
209 ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
Function,
215using ExplicitRewriteGlobalVariableDescriptor =
216 ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
221using ExplicitRewriteNamedAliasDescriptor =
222 ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
GlobalAlias,
228using PatternRewriteFunctionDescriptor =
229 PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
Function,
236using PatternRewriteGlobalVariableDescriptor =
237 PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
244using PatternRewriteNamedAliasDescriptor =
245 PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
GlobalAlias,
257 "': " + Mapping.
getError().message());
270 for (
auto &Document : YS) {
278 if (!DescriptorList) {
279 YS.printError(Document.getRoot(),
"DescriptorList node must be a map");
283 for (
auto &Descriptor : *DescriptorList)
284 if (!parseEntry(YS, Descriptor,
DL))
291bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
293 yaml::ScalarNode *
Key;
294 yaml::MappingNode *
Value;
295 SmallString<32> KeyStorage;
296 StringRef RewriteType;
310 RewriteType =
Key->getValue(KeyStorage);
311 if (RewriteType ==
"function")
312 return parseRewriteFunctionDescriptor(YS,
Key,
Value,
DL);
313 else if (RewriteType ==
"global variable")
314 return parseRewriteGlobalVariableDescriptor(YS,
Key,
Value,
DL);
315 else if (RewriteType ==
"global alias")
316 return parseRewriteGlobalAliasDescriptor(YS,
Key,
Value,
DL);
322bool RewriteMapParser::
323parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
324 yaml::MappingNode *Descriptor,
329 std::string Transform;
331 for (
auto &
Field : *Descriptor) {
332 yaml::ScalarNode *
Key;
333 yaml::ScalarNode *
Value;
334 SmallString<32> KeyStorage;
335 SmallString<32> ValueStorage;
350 KeyValue =
Key->getValue(KeyStorage);
351 if (KeyValue ==
"source") {
352 Source = std::string(
Value->getValue(ValueStorage));
353 }
else if (KeyValue ==
"target") {
354 Target = std::string(
Value->getValue(ValueStorage));
355 }
else if (KeyValue ==
"transform") {
356 Transform = std::string(
Value->getValue(ValueStorage));
357 }
else if (KeyValue ==
"naked") {
358 std::string Undecorated;
360 Undecorated = std::string(
Value->getValue(ValueStorage));
361 Naked = StringRef(Undecorated).lower() ==
"true" || Undecorated ==
"1";
368 if (Transform.empty() ==
Target.empty()) {
370 "exactly one of transform or target must be specified");
377 DL->
push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
378 Source, Target, Naked));
391 std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
396bool RewriteMapParser::
397parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
398 yaml::MappingNode *Descriptor,
402 std::string Transform;
404 for (
auto &
Field : *Descriptor) {
405 yaml::ScalarNode *
Key;
406 yaml::ScalarNode *
Value;
407 SmallString<32> KeyStorage;
408 SmallString<32> ValueStorage;
423 KeyValue =
Key->getValue(KeyStorage);
424 if (KeyValue ==
"source") {
425 Source = std::string(
Value->getValue(ValueStorage));
426 }
else if (KeyValue ==
"target") {
427 Target = std::string(
Value->getValue(ValueStorage));
428 }
else if (KeyValue ==
"transform") {
429 Transform = std::string(
Value->getValue(ValueStorage));
436 if (Transform.empty() ==
Target.empty()) {
438 "exactly one of transform or target must be specified");
443 DL->
push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
457 DL->
push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
463bool RewriteMapParser::
464parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
465 yaml::MappingNode *Descriptor,
469 std::string Transform;
471 for (
auto &
Field : *Descriptor) {
472 yaml::ScalarNode *
Key;
473 yaml::ScalarNode *
Value;
474 SmallString<32> KeyStorage;
475 SmallString<32> ValueStorage;
490 KeyValue =
Key->getValue(KeyStorage);
491 if (KeyValue ==
"source") {
492 Source = std::string(
Value->getValue(ValueStorage));
493 }
else if (KeyValue ==
"target") {
494 Target = std::string(
Value->getValue(ValueStorage));
495 }
else if (KeyValue ==
"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>(
525 std::make_unique<PatternRewriteNamedAliasDescriptor>(Source, Transform));
541 for (
auto &Descriptor : Descriptors)
542 Changed |= Descriptor->performOnModule(M);
547void RewriteSymbolPass::loadAndParseMapFiles() {
551 for (
const auto &MapFile : MapFiles)
552 Parser.
parse(MapFile, &Descriptors);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool runImpl(MachineFunction &MF)
Provides ErrorOr<T> smart pointer.
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
static constexpr unsigned SM(unsigned Version)
OptimizedStructLayoutField Field
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)
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
LLVM_ABI 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,...
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.
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.
LLVM_ABI 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.
LLVM_ABI bool runImpl(Module &M)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Represent a constant reference to a string, i.e.
The basic entity representing a rewrite operation.
LLVM_ABI 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.
This class represents a YAML stream potentially containing multiple documents.
LLVM_ABI 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.
@ 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.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.