60 #define DEBUG_TYPE "symbol-rewriter"
74 using namespace SymbolRewriter;
82 const std::string &
Target) {
90 Comdats.erase(Comdats.find(Source));
103 : RewriteDescriptor(DT), Source(Naked ?
StringRef(
"\01" + S.str()) : S),
106 bool performOnModule(
Module &M)
override;
108 static bool classof(
const RewriteDescriptor *RD) {
115 bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(
Module &M) {
116 bool Changed =
false;
117 if (ValueType *S = (M.*Get)(
Source)) {
122 S->setValueName(
T->getValueName());
134 (llvm::
Module::*Iterator)()>
135 class PatternRewriteDescriptor : public RewriteDescriptor {
137 const std::string Pattern;
138 const std::string Transform;
141 : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
143 bool performOnModule(
Module &M)
override;
145 static bool classof(
const RewriteDescriptor *RD) {
153 (llvm::
Module::*Iterator)()>
154 bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
155 performOnModule(
Module &M) {
156 bool Changed =
false;
157 for (
auto &
C : (M.*Iterator)()) {
163 M.getModuleIdentifier() +
": " +
Error);
165 if (
C.getName() ==
Name)
172 C.setValueName(V->getValueName());
186 ExplicitRewriteFunctionDescriptor;
194 ExplicitRewriteGlobalVariableDescriptor;
201 ExplicitRewriteNamedAliasDescriptor;
209 PatternRewriteFunctionDescriptor;
216 llvm::GlobalVariable,
219 PatternRewriteGlobalVariableDescriptor;
228 PatternRewriteNamedAliasDescriptor;
240 if (!
parse(*Mapping, DL))
251 for (
auto &Document : YS) {
255 if (isa<yaml::NullNode>(Document.getRoot()))
259 if (!DescriptorList) {
260 YS.printError(Document.getRoot(),
"DescriptorList node must be a map");
264 for (
auto &Descriptor : *DescriptorList)
265 if (!parseEntry(YS, Descriptor, DL))
291 RewriteType = Key->
getValue(KeyStorage);
292 if (RewriteType.
equals(
"function"))
293 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
294 else if (RewriteType.
equals(
"global variable"))
295 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
296 else if (RewriteType.
equals(
"global alias"))
297 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
303 bool RewriteMapParser::
310 std::string Transform;
312 for (
auto &
Field : *Descriptor) {
331 KeyValue = Key->
getValue(KeyStorage);
332 if (KeyValue.
equals(
"source")) {
335 Source = Value->
getValue(ValueStorage);
340 }
else if (KeyValue.
equals(
"target")) {
341 Target = Value->
getValue(ValueStorage);
342 }
else if (KeyValue.
equals(
"transform")) {
343 Transform = Value->
getValue(ValueStorage);
344 }
else if (KeyValue.
equals(
"naked")) {
345 std::string Undecorated;
347 Undecorated = Value->
getValue(ValueStorage);
348 Naked =
StringRef(Undecorated).
lower() ==
"true" || Undecorated ==
"1";
355 if (Transform.empty() == Target.empty()) {
357 "exactly one of transform or target must be specified");
364 DL->push_back(llvm::make_unique<ExplicitRewriteFunctionDescriptor>(
365 Source, Target, Naked));
368 llvm::make_unique<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);
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(llvm::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
430 DL->push_back(llvm::make_unique<PatternRewriteGlobalVariableDescriptor>(
436 bool RewriteMapParser::
442 std::string Transform;
444 for (
auto &
Field : *Descriptor) {
463 KeyValue = Key->
getValue(KeyStorage);
464 if (KeyValue.
equals(
"source")) {
467 Source = Value->
getValue(ValueStorage);
472 }
else if (KeyValue.
equals(
"target")) {
473 Target = Value->
getValue(ValueStorage);
474 }
else if (KeyValue.
equals(
"transform")) {
475 Transform = Value->
getValue(ValueStorage);
482 if (Transform.empty() == Target.empty()) {
484 "exactly one of transform or target must be specified");
489 DL->push_back(llvm::make_unique<ExplicitRewriteNamedAliasDescriptor>(
493 DL->push_back(llvm::make_unique<PatternRewriteNamedAliasDescriptor>(
500 class RewriteSymbolsLegacyPass :
public ModulePass {
504 RewriteSymbolsLegacyPass();
507 bool runOnModule(
Module &M)
override;
515 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() :
ModulePass(
ID), Impl() {
519 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
523 bool RewriteSymbolsLegacyPass::runOnModule(
Module &M) {
524 return Impl.runImpl(M);
540 for (
auto &Descriptor : Descriptors)
541 Changed |= Descriptor->performOnModule(M);
546 void RewriteSymbolPass::loadAndParseMapFiles() {
550 for (
const auto &MapFile : MapFiles)
551 Parser.
parse(MapFile, &Descriptors);
559 return new RewriteSymbolsLegacyPass();
564 return new RewriteSymbolsLegacyPass(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...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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"))
The basic entity representing a rewrite operation.
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 ...
bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors)
void setComdat(Comdat *C)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
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)
const Comdat * getComdat() const
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
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...
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for 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.
static const char * Target
INITIALIZE_PASS(HexagonGenMux,"hexagon-mux","Hexagon generate mux instructions", false, false) void HexagonGenMux I isValid()
ModulePass * createRewriteSymbolsPass()
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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. ...
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...
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 &)
LLVM Value Representation.
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.
static void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, const std::string &Target)
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVM_NODISCARD std::string lower() const
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
iterator_range< alias_iterator > aliases()