Commit 017c12b3 authored by Zhao Jiazhong's avatar Zhao Jiazhong Committed by V8 LUCI CQ

[loong64] Optimize GenerateSwitchTable

We should use pc relative branch instruction rather than loading
target address from memory for better performance.

Besides, just like arm64, currently we assume that none of our
relocation types are pc relative pointing outside the code buffer
nor pc absolute pointing inside the code buffer.

Change-Id: I9cce2e79c0afb00af967638405469f65df1deda2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3147314Reviewed-by: 's avatarLiu yu <liuyu@loongson.cn>
Commit-Queue: Liu yu <liuyu@loongson.cn>
Cr-Commit-Position: refs/heads/main@{#76721}
parent f7b9b1f0
......@@ -1042,20 +1042,15 @@ class V8_EXPORT_PRIVATE MacroAssembler : public TurboAssembler {
template <typename Func>
void TurboAssembler::GenerateSwitchTable(Register index, size_t case_count,
Func GetLabelFunction) {
// Ensure that dd-ed labels following this instruction use 8 bytes aligned
// addresses.
BlockTrampolinePoolFor(static_cast<int>(case_count) * 2 +
kSwitchTablePrologueSize);
UseScratchRegisterScope temps(this);
Register scratch = temps.Acquire();
Align(8); // next is 4 instrs.
pcaddi(scratch, 4);
// alsl_d will do sa
alsl_d(scratch, index, scratch, kPointerSizeLog2);
Ld_d(scratch, MemOperand(scratch, 0));
UseScratchRegisterScope scope(this);
Register scratch = scope.Acquire();
BlockTrampolinePoolFor((3 + case_count) * kInstrSize);
pcaddi(scratch, 3);
alsl_d(scratch, index, scratch, kInstrSizeLog2);
jirl(zero_reg, scratch, 0);
for (size_t index = 0; index < case_count; ++index) {
dd(GetLabelFunction(index));
b(GetLabelFunction(index));
}
}
......
......@@ -241,7 +241,7 @@ TEST(jump_tables6) {
MacroAssembler assembler(isolate, v8::internal::CodeObjectRequired::kYes);
MacroAssembler* masm = &assembler;
const int kSwitchTableCases = 40;
const int kSwitchTableCases = 80;
const int kMaxBranchOffset = (1 << (18 - 1)) - 1;
const int kTrampolineSlotsSize = Assembler::kTrampolineSlotsSize;
......@@ -250,8 +250,7 @@ TEST(jump_tables6) {
const int kMaxOffsetForTrampolineStart =
kMaxBranchOffset - 16 * kTrampolineSlotsSize;
const int kFillInstr = (kMaxOffsetForTrampolineStart / kInstrSize) -
(kSwitchTablePrologueSize + 2 * kSwitchTableCases) -
20;
(kSwitchTablePrologueSize + kSwitchTableCases) - 20;
int values[kSwitchTableCases];
isolate->random_number_generator()->NextBytes(values, sizeof(values));
......@@ -275,7 +274,7 @@ TEST(jump_tables6) {
__ GenerateSwitchTable(a0, kSwitchTableCases,
[&labels](size_t i) { return labels + i; });
gen_insn += (kSwitchTablePrologueSize + 2 * kSwitchTableCases);
gen_insn += (kSwitchTablePrologueSize + kSwitchTableCases);
for (int i = 0; i < kSwitchTableCases; ++i) {
__ bind(&labels[i]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment