llvm.org GIT mirror llvm / 10d5949
[SchedModel] Simplify InstRegexOp::apply. NFCI. As discussed on D44687, there was no need for 2 separate for loops for collecting the Regex and then matching against instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328052 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Pilgrim 2 years ago
1 changed file(s) with 28 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
7676
7777 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
7878 ArrayRef Loc) override {
79 SmallVector>, 4> RegexList;
8079 for (Init *Arg : make_range(Expr->arg_begin(), Expr->arg_end())) {
8180 StringInit *SI = dyn_cast(Arg);
8281 if (!SI)
8382 PrintFatalError(Loc, "instregex requires pattern string: " +
8483 Expr->getAsString());
84 StringRef Original = SI->getValue();
85
8586 // Extract a prefix that we can binary search on.
8687 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
87 auto FirstMeta = SI->getValue().find_first_of(RegexMetachars);
88 auto FirstMeta = Original.find_first_of(RegexMetachars);
89
8890 // Look for top-level | or ?. We cannot optimize them to binary search.
89 if (removeParens(SI->getValue()).find_first_of("|?") != std::string::npos)
91 if (removeParens(Original).find_first_of("|?") != std::string::npos)
9092 FirstMeta = 0;
91 StringRef Prefix = SI->getValue().substr(0, FirstMeta);
92 std::string pat = SI->getValue().substr(FirstMeta);
93 if (pat.empty()) {
94 RegexList.push_back(std::make_pair(Prefix, None));
95 continue;
96 }
97 // For the rest use a python-style prefix match.
98 if (pat[0] != '^') {
99 pat.insert(0, "^(");
100 pat.insert(pat.end(), ')');
101 }
102 RegexList.push_back(std::make_pair(Prefix, Regex(pat)));
103 }
104 for (auto &R : RegexList) {
93
94 Optional Regexpr = None;
95 StringRef Prefix = Original.substr(0, FirstMeta);
96 std::string pat = Original.substr(FirstMeta);
97 if (!pat.empty()) {
98 // For the rest use a python-style prefix match.
99 if (pat[0] != '^') {
100 pat.insert(0, "^(");
101 pat.insert(pat.end(), ')');
102 }
103 Regexpr = Regex(pat);
104 }
105
105106 unsigned NumGeneric = Target.getNumFixedInstructions();
107 ArrayRef Generics =
108 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1);
109
106110 // The generic opcodes are unsorted, handle them manually.
107 for (auto *Inst :
108 Target.getInstructionsByEnumValue().slice(0, NumGeneric + 1)) {
109 if (Inst->TheDef->getName().startswith(R.first) &&
110 (!R.second ||
111 R.second->match(Inst->TheDef->getName().substr(R.first.size()))))
111 for (auto *Inst : Generics) {
112 StringRef InstName = Inst->TheDef->getName();
113 if (InstName.startswith(Prefix) &&
114 (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))))
112115 Elts.insert(Inst->TheDef);
113116 }
114117
127130 }
128131 };
129132 auto Range = std::equal_range(Instructions.begin(), Instructions.end(),
130 R.first, Comp());
133 Prefix, Comp());
131134
132135 // For this range we know that it starts with the prefix. Check if there's
133136 // a regex that needs to be checked.
134137 for (auto *Inst : make_range(Range)) {
135 if (!R.second ||
136 R.second->match(Inst->TheDef->getName().substr(R.first.size())))
138 StringRef InstName = Inst->TheDef->getName();
139 if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))
137140 Elts.insert(Inst->TheDef);
138141 }
139142 }