diff --git a/llvm/include/llvm/IR/Mangler.h b/llvm/include/llvm/IR/Mangler.h index 747a4085235c9..f28ffc961b6db 100644 --- a/llvm/include/llvm/IR/Mangler.h +++ b/llvm/include/llvm/IR/Mangler.h @@ -14,6 +14,7 @@ #define LLVM_IR_MANGLER_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/StringRef.h" namespace llvm { @@ -52,6 +53,9 @@ void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &T, Mangler &M); +std::optional getArm64ECMangledFunctionName(StringRef Name); +std::optional getArm64ECDemangledFunctionName(StringRef Name); + } // End llvm namespace #endif diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h index 2a5c3d8913b15..a548b2c15c5fd 100644 --- a/llvm/include/llvm/Object/COFF.h +++ b/llvm/include/llvm/Object/COFF.h @@ -1362,47 +1362,6 @@ class SectionStrippedError SectionStrippedError() { setErrorCode(object_error::section_stripped); } }; -inline std::optional -getArm64ECMangledFunctionName(StringRef Name) { - bool IsCppFn = Name[0] == '?'; - if (IsCppFn && Name.find("$$h") != std::string::npos) - return std::nullopt; - if (!IsCppFn && Name[0] == '#') - return std::nullopt; - - StringRef Prefix = "$$h"; - size_t InsertIdx = 0; - if (IsCppFn) { - InsertIdx = Name.find("@@"); - size_t ThreeAtSignsIdx = Name.find("@@@"); - if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) { - InsertIdx += 2; - } else { - InsertIdx = Name.find("@"); - if (InsertIdx != std::string::npos) - InsertIdx++; - } - } else { - Prefix = "#"; - } - - return std::optional( - (Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str()); -} - -inline std::optional -getArm64ECDemangledFunctionName(StringRef Name) { - if (Name[0] == '#') - return std::string(Name.substr(1)); - if (Name[0] != '?') - return std::nullopt; - - std::pair Pair = Name.split("$$h"); - if (Pair.second.empty()) - return std::nullopt; - return (Pair.first + Pair.second).str(); -} - } // end namespace object } // end namespace llvm diff --git a/llvm/include/llvm/Object/COFFImportFile.h b/llvm/include/llvm/Object/COFFImportFile.h index 7268faa87eb70..035bc17126da3 100644 --- a/llvm/include/llvm/Object/COFFImportFile.h +++ b/llvm/include/llvm/Object/COFFImportFile.h @@ -17,6 +17,7 @@ #define LLVM_OBJECT_COFFIMPORTFILE_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/IR/Mangler.h" #include "llvm/Object/COFF.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Object/SymbolicFile.h" diff --git a/llvm/lib/IR/Mangler.cpp b/llvm/lib/IR/Mangler.cpp index c4e8f005d6c8a..72e2bc1f24ac9 100644 --- a/llvm/lib/IR/Mangler.cpp +++ b/llvm/lib/IR/Mangler.cpp @@ -18,7 +18,6 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/Module.h" -#include "llvm/Object/COFF.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Triple.h" @@ -242,7 +241,7 @@ void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, // typically functions correctly; the linker can resolve the export // with the demangled alias. if (std::optional demangledName = - object::getArm64ECDemangledFunctionName(GV->getName())) + getArm64ECDemangledFunctionName(GV->getName())) OS << ",EXPORTAS," << *demangledName; } if (NeedQuotes) @@ -291,3 +290,42 @@ void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, OS << "\""; } +std::optional llvm::getArm64ECMangledFunctionName(StringRef Name) { + bool IsCppFn = Name[0] == '?'; + if (IsCppFn && Name.find("$$h") != std::string::npos) + return std::nullopt; + if (!IsCppFn && Name[0] == '#') + return std::nullopt; + + StringRef Prefix = "$$h"; + size_t InsertIdx = 0; + if (IsCppFn) { + InsertIdx = Name.find("@@"); + size_t ThreeAtSignsIdx = Name.find("@@@"); + if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) { + InsertIdx += 2; + } else { + InsertIdx = Name.find("@"); + if (InsertIdx != std::string::npos) + InsertIdx++; + } + } else { + Prefix = "#"; + } + + return std::optional( + (Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str()); +} + +std::optional +llvm::getArm64ECDemangledFunctionName(StringRef Name) { + if (Name[0] == '#') + return std::optional(Name.substr(1)); + if (Name[0] != '?') + return std::nullopt; + + std::pair Pair = Name.split("$$h"); + if (Pair.second.empty()) + return std::nullopt; + return std::optional((Pair.first + Pair.second).str()); +} diff --git a/llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp b/llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp index 9b5cbe3de137b..3bf6283b79e9b 100644 --- a/llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp @@ -23,6 +23,7 @@ #include "llvm/IR/CallingConv.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instruction.h" +#include "llvm/IR/Mangler.h" #include "llvm/InitializePasses.h" #include "llvm/Object/COFF.h" #include "llvm/Pass.h" @@ -31,7 +32,6 @@ using namespace llvm; using namespace llvm::COFF; -using namespace llvm::object; using OperandBundleDef = OperandBundleDefT;