31 for (
auto [Idx, Letter] :
enumerate(Matches[0]->first)) {
35 if (Match->first[Idx] != Letter)
39 return Matches[0]->first.
size();
47bool StringMatcher::EmitStringMatcherForChar(
49 bool IgnoreDuplicates)
const {
50 assert(!Matches.empty() &&
"Must have at least one string to match!");
51 std::string Indent(IndentCount * 2 + 4,
' ');
55 if (CharNo == Matches[0]->first.size()) {
56 if (Matches.size() > 1 && !IgnoreDuplicates)
60 StringRef
Code = Matches[0]->second;
62 std::pair<StringRef, StringRef>
Split =
Code.split(
'\n');
63 OS << Indent <<
Split.first <<
"\t // \"" << Matches[0]->first <<
"\"\n";
66 while (!
Code.empty()) {
68 OS << Indent <<
Split.first <<
"\n";
75 std::map<char, std::vector<const StringPair*>> MatchesByLetter;
78 MatchesByLetter[Match->first[CharNo]].push_back(Match);
82 if (MatchesByLetter.size() == 1) {
84 unsigned NumChars = FirstNonCommonLetter-CharNo;
90 OS << Indent <<
"if (" << StrVariableName <<
"[" << CharNo <<
"] != '"
91 << Matches[0]->first[CharNo] <<
"')\n";
92 OS << Indent <<
" break;\n";
96 OS << Indent <<
"if (memcmp(" << StrVariableName <<
".data()+" << CharNo
97 <<
", \"" << Matches[0]->first.substr(CharNo, NumChars) <<
"\", "
98 << NumChars <<
") != 0)\n";
99 OS << Indent <<
" break;\n";
102 return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,
108 OS << Indent <<
"switch (" << StrVariableName <<
"[" << CharNo <<
"]) {\n";
109 OS << Indent <<
"default: break;\n";
111 for (
const auto &[Letter, Matches] : MatchesByLetter) {
113 OS << Indent <<
"case '" << Letter <<
"':\t // " << Matches.size()
115 if (Matches.size() != 1)
117 OS <<
" to match.\n";
118 if (EmitStringMatcherForChar(Matches, CharNo + 1, IndentCount + 1,
120 OS << Indent <<
" break;\n";
123 OS << Indent <<
"}\n";
131 if (Matches.empty())
return;
134 std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;
137 MatchesByLength[Match.first.size()].push_back(&Match);
141 OS.indent(Indent*2+2) <<
"switch (" << StrVariableName <<
".size()) {\n";
142 OS.indent(Indent*2+2) <<
"default: break;\n";
144 for (
const auto &[
Length, Matches] : MatchesByLength) {
145 OS.indent(Indent * 2 + 2)
146 <<
"case " <<
Length <<
":\t // " << Matches.size() <<
" string"
147 << (Matches.size() == 1 ?
"" :
"s") <<
" to match.\n";
148 if (EmitStringMatcherForChar(Matches, 0, Indent, IgnoreDuplicates))
149 OS.indent(Indent*2+4) <<
"break;\n";
152 OS.indent(Indent*2+2) <<
"}\n";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static unsigned FindFirstNonCommonLetter(ArrayRef< const StringMatcher::StringPair * > Matches)
FindFirstNonCommonLetter - Find the first character in the keys of the string pairs that is not share...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
void Emit(unsigned Indent=0, bool IgnoreDuplicates=false) const
Emit - Top level entry point.
std::pair< std::string, std::string > StringPair
NodeAddr< CodeNode * > Code
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)