mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05:00
LibRegex: Merge alternations based on blocks and not instructions
The instructions can have dependencies (e.g. Repeat), so only unify equal blocks instead of consecutive instructions. Fixes #11247. Also adds the minimal test case(s) from that issue.
This commit is contained in:
parent
92233660b8
commit
d2e51fafa9
3 changed files with 23 additions and 17 deletions
|
@ -899,7 +899,9 @@ TEST_CASE(optimizer_atomic_groups)
|
||||||
// Alternative fuse
|
// Alternative fuse
|
||||||
Tuple { "(abcfoo|abcbar|abcbaz).*x"sv, "abcbarx"sv, true },
|
Tuple { "(abcfoo|abcbar|abcbaz).*x"sv, "abcbarx"sv, true },
|
||||||
Tuple { "(a|a)"sv, "a"sv, true },
|
Tuple { "(a|a)"sv, "a"sv, true },
|
||||||
Tuple { "(a|)"sv, ""sv, true }, // Ensure that empty alternatives are not outright removed
|
Tuple { "(a|)"sv, ""sv, true }, // Ensure that empty alternatives are not outright removed
|
||||||
|
Tuple { "a{2,3}|a{5,8}"sv, "abc"sv, false }, // Optimiser should not mess up the instruction stream by ignoring inter-insn dependencies, see #11247.
|
||||||
|
Tuple { "^(a{2,3}|a{5,8})$"sv, "aaaa"sv, false }, // Optimiser should not mess up the instruction stream by ignoring inter-insn dependencies, see #11247.
|
||||||
// ForkReplace shouldn't be applied where it would change the semantics
|
// ForkReplace shouldn't be applied where it would change the semantics
|
||||||
Tuple { "(1+)\\1"sv, "11"sv, true },
|
Tuple { "(1+)\\1"sv, "11"sv, true },
|
||||||
Tuple { "(1+)1"sv, "11"sv, true },
|
Tuple { "(1+)1"sv, "11"sv, true },
|
||||||
|
|
|
@ -227,10 +227,11 @@ public:
|
||||||
return result.success;
|
return result.success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using BasicBlockList = Vector<Detail::Block>;
|
||||||
|
static BasicBlockList split_basic_blocks(ByteCode const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void run_optimization_passes();
|
void run_optimization_passes();
|
||||||
using BasicBlockList = Vector<Detail::Block>;
|
|
||||||
BasicBlockList split_basic_blocks();
|
|
||||||
void attempt_rewrite_loops_as_atomic_groups(BasicBlockList const&);
|
void attempt_rewrite_loops_as_atomic_groups(BasicBlockList const&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -21,16 +21,15 @@ void Regex<Parser>::run_optimization_passes()
|
||||||
|
|
||||||
// Rewrite fork loops as atomic groups
|
// Rewrite fork loops as atomic groups
|
||||||
// e.g. a*b -> (ATOMIC a*)b
|
// e.g. a*b -> (ATOMIC a*)b
|
||||||
attempt_rewrite_loops_as_atomic_groups(split_basic_blocks());
|
attempt_rewrite_loops_as_atomic_groups(split_basic_blocks(parser_result.bytecode));
|
||||||
|
|
||||||
parser_result.bytecode.flatten();
|
parser_result.bytecode.flatten();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Parser>
|
template<typename Parser>
|
||||||
typename Regex<Parser>::BasicBlockList Regex<Parser>::split_basic_blocks()
|
typename Regex<Parser>::BasicBlockList Regex<Parser>::split_basic_blocks(ByteCode const& bytecode)
|
||||||
{
|
{
|
||||||
BasicBlockList block_boundaries;
|
BasicBlockList block_boundaries;
|
||||||
auto& bytecode = parser_result.bytecode;
|
|
||||||
size_t end_of_last_block = 0;
|
size_t end_of_last_block = 0;
|
||||||
|
|
||||||
MatchState state;
|
MatchState state;
|
||||||
|
@ -472,33 +471,37 @@ void Optimizer::append_alternation(ByteCode& target, ByteCode&& left, ByteCode&&
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto left_blocks = Regex<PosixBasicParser>::split_basic_blocks(left);
|
||||||
|
auto right_blocks = Regex<PosixBasicParser>::split_basic_blocks(right);
|
||||||
|
|
||||||
size_t left_skip = 0;
|
size_t left_skip = 0;
|
||||||
MatchState state;
|
MatchState state;
|
||||||
for (state.instruction_position = 0; state.instruction_position < left.size() && state.instruction_position < right.size();) {
|
for (size_t block_index = 0; block_index < left_blocks.size() && block_index < right_blocks.size(); block_index++) {
|
||||||
auto left_size = left.get_opcode(state).size();
|
auto& left_block = left_blocks[block_index];
|
||||||
auto right_size = right.get_opcode(state).size();
|
auto& right_block = right_blocks[block_index];
|
||||||
if (left_size != right_size)
|
auto left_end = block_index + 1 == left_blocks.size() ? left_block.end : left_blocks[block_index + 1].start;
|
||||||
|
auto right_end = block_index + 1 == right_blocks.size() ? right_block.end : right_blocks[block_index + 1].start;
|
||||||
|
|
||||||
|
if (left_end - left_block.start != right_end - right_block.start)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (left.spans().slice(state.instruction_position, left_size) == right.spans().slice(state.instruction_position, right_size))
|
if (left.spans().slice(left_block.start, left_end - left_block.start) != right.spans().slice(right_block.start, right_end - right_block.start))
|
||||||
left_skip = state.instruction_position + left_size;
|
|
||||||
else
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
state.instruction_position += left_size;
|
left_skip = left_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
dbgln_if(REGEX_DEBUG, "Skipping {}/{} bytecode entries from {}/{}", left_skip, 0, left.size(), right.size());
|
dbgln_if(REGEX_DEBUG, "Skipping {}/{} bytecode entries from {}/{}", left_skip, 0, left.size(), right.size());
|
||||||
|
|
||||||
if (left_skip) {
|
if (left_skip > 0) {
|
||||||
target.extend(left.release_slice(0, left_skip));
|
target.extend(left.release_slice(left_blocks.first().start, left_skip));
|
||||||
right = right.release_slice(left_skip);
|
right = right.release_slice(left_skip);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto left_size = left.size();
|
auto left_size = left.size();
|
||||||
|
|
||||||
target.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
|
target.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
|
||||||
target.empend(right.size() + (left_size ? 2 : 0)); // Jump to the _ALT label
|
target.empend(right.size() + (left_size > 0 ? 2 : 0)); // Jump to the _ALT label
|
||||||
|
|
||||||
target.extend(move(right));
|
target.extend(move(right));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue