Skip to content

Commit c4d06ba

Browse files
LiuYu3966v8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[loong64][compiler] Extend Word64Select instruction functionality
Change-Id: Iba762777642d2d2d3aa904f9afc1e9005139992e Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7801520 Reviewed-by: Zhao Jiazhong <[email protected]> Commit-Queue: Liu Yu <[email protected]> Reviewed-by: Darius Mercadier <[email protected]> Auto-Submit: Liu Yu <[email protected]> Cr-Commit-Position: refs/heads/main@{#107619}
1 parent be455d8 commit c4d06ba

6 files changed

Lines changed: 442 additions & 44 deletions

File tree

‎src/codegen/loong64/macro-assembler-loong64.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,6 +3201,21 @@ void MacroAssembler::Float64Mod(DoubleRegister out, DoubleRegister left,
32013201
bind(&done_mod);
32023202
}
32033203

3204+
void MacroAssembler::SelectWord(Register result, Register cond, Register v_true,
3205+
Register v_false) {
3206+
if (v_false == zero_reg) {
3207+
maskeqz(result, v_true, cond);
3208+
} else if (v_true == zero_reg) {
3209+
masknez(result, v_false, cond);
3210+
} else {
3211+
UseScratchRegisterScope temps(this);
3212+
Register scratch = temps.Acquire();
3213+
maskeqz(scratch, v_true, cond);
3214+
masknez(result, v_false, cond);
3215+
or_(result, scratch, result);
3216+
}
3217+
}
3218+
32043219
void MacroAssembler::CompareWord(Condition cond, Register dst, Register lhs,
32053220
const Operand& rhs) {
32063221
switch (cond) {

‎src/codegen/loong64/macro-assembler-loong64.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ class V8_EXPORT_PRIVATE MacroAssembler : public MacroAssemblerBase {
142142
// Print a message to stdout and abort execution.
143143
void Abort(AbortReason msg);
144144

145+
// Select v_true if cond is non-zero, otherwise select v_false.
146+
void SelectWord(Register result, Register cond, Register v_true,
147+
Register v_false);
148+
145149
void CompareWord(Condition cond, Register dst, Register lhs,
146150
const Operand& rhs);
147151
void Branch(Label* label, bool need_link = false);

‎src/compiler/backend/loong64/code-generator-loong64.cc‎

Lines changed: 140 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,10 +1401,14 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
14011401
case kLoong64Sub_d:
14021402
__ Sub_d(i.OutputRegister(), i.InputRegister(0), i.InputOperand(1));
14031403
break;
1404-
case kLoong64SubOvf_d:
1404+
case kLoong64SubOvf_d: {
1405+
UseScratchRegisterScope temps(masm());
1406+
DCHECK(temps.hasAvailable());
1407+
temps.Exclude(t8);
14051408
__ SubOverflow_d(i.OutputRegister(), i.InputRegister(0),
14061409
i.InputOperand(1), t8);
14071410
break;
1411+
}
14081412
case kLoong64Sub128: {
14091413
UseScratchRegisterScope temps(masm());
14101414
Register scratch = temps.Acquire();
@@ -4701,7 +4705,7 @@ void CodeGenerator::AssembleArchBoolean(Instruction* instr,
47014705
}
47024706
return;
47034707
} else {
4704-
PrintF("AssembleArchBranch Unimplemented arch_opcode is : %d\n",
4708+
PrintF("AssembleArchBoolean Unimplemented arch_opcode is : %d\n",
47054709
instr->arch_opcode());
47064710
TRACE("UNIMPLEMENTED code_generator_loong64: %s at line %d\n", __FUNCTION__,
47074711
__LINE__);
@@ -4756,56 +4760,49 @@ void CodeGenerator::AssembleArchSelect(Instruction* instr,
47564760
size_t output_index = instr->OutputCount() - 1;
47574761
// We don't know how many inputs were consumed by the condition, so we have to
47584762
// calculate the indices of the last two inputs.
4759-
DCHECK_GE(instr->InputCount(), 4);
47604763
size_t true_value_index = instr->InputCount() - 2;
47614764
size_t false_value_index = instr->InputCount() - 1;
4765+
Register result = i.OutputRegister(output_index);
4766+
Register v_true = i.InputOrZeroRegister(true_value_index);
4767+
Register v_false = i.InputOrZeroRegister(false_value_index);
4768+
4769+
DCHECK(
4770+
LocationOperand::cast(instr->OutputAt(output_index))->representation() ==
4771+
MachineRepresentation::kWord64);
47624772

47634773
if (instr->arch_opcode() == kLoong64Tst) {
4774+
DCHECK_GE(instr->InputCount(), 4);
47644775
Condition cc = FlagsConditionToConditionTst(condition);
4765-
Register result = i.OutputRegister(output_index);
4766-
Register v_true = i.InputOrZeroRegister(true_value_index);
4767-
Register v_false = i.InputOrZeroRegister(false_value_index);
4768-
if (v_true == zero_reg || v_false == zero_reg) {
4769-
if (v_true == zero_reg) {
4770-
v_true = v_false;
4771-
cc = NegateCondition(cc);
4772-
}
4773-
if (cc == eq)
4774-
__ masknez(result, v_true, t8);
4775-
else
4776-
__ maskeqz(result, v_true, t8);
4777-
} else if (result == v_true || result == v_false) {
4778-
if (result == v_false) {
4779-
v_false = v_true;
4780-
cc = NegateCondition(cc);
4781-
}
4782-
Label done;
4783-
__ Branch(&done, cc, t8, Operand(0));
4784-
__ Move(result, v_false);
4785-
__ bind(&done);
4786-
} else {
4787-
UseScratchRegisterScope temps(masm());
4788-
Register scratch = temps.Acquire();
4789-
if (cc == eq) {
4790-
Register temp = v_true;
4791-
v_true = v_false;
4792-
v_false = temp;
4793-
}
4794-
__ maskeqz(scratch, v_true, t8);
4795-
__ masknez(result, v_false, t8);
4796-
__ or_(result, scratch, result);
4776+
if (cc == eq) {
4777+
Register temp = v_true;
4778+
v_true = v_false;
4779+
v_false = temp;
47974780
}
4781+
__ SelectWord(result, t8, v_true, v_false);
47984782
UseScratchRegisterScope temps(masm());
47994783
temps.Include(t8);
48004784
return;
48014785
} else if (instr->arch_opcode() == kLoong64Cmp64 ||
4802-
instr->arch_opcode() == kLoong64Cmp32) {
4786+
instr->arch_opcode() == kLoong64Cmp32 ||
4787+
instr->arch_opcode() == kArchStackPointerGreaterThan) {
48034788
Condition cc = FlagsConditionToConditionCmp(condition);
4804-
Register left = i.InputRegister(0);
4805-
Operand right = i.InputOperand(1);
4806-
Register result = i.OutputRegister(output_index);
4807-
Register v_true = i.InputOrZeroRegister(true_value_index);
4808-
Register v_false = i.InputOrZeroRegister(false_value_index);
4789+
Register left = no_reg;
4790+
Operand right = Operand(0);
4791+
if (instr->arch_opcode() == kArchStackPointerGreaterThan) {
4792+
DCHECK_GE(instr->InputCount(), 3);
4793+
DCHECK((cc == ls) || (cc == hi));
4794+
left = sp;
4795+
right = i.InputOperand(0);
4796+
uint32_t offset;
4797+
if (ShouldApplyOffsetToStackCheck(instr, &offset)) {
4798+
left = i.TempRegister(1);
4799+
__ Sub_d(left, sp, offset);
4800+
}
4801+
} else {
4802+
DCHECK_GE(instr->InputCount(), 4);
4803+
left = i.InputRegister(0);
4804+
right = i.InputOperand(1);
4805+
}
48094806
if (v_true == zero_reg || v_false == zero_reg) {
48104807
if (v_true == zero_reg) {
48114808
v_true = v_false;
@@ -4835,6 +4832,108 @@ void CodeGenerator::AssembleArchSelect(Instruction* instr,
48354832
__ bind(&done);
48364833
}
48374834
return;
4835+
} else if (instr->arch_opcode() == kLoong64Add_d ||
4836+
instr->arch_opcode() == kLoong64Sub_d) {
4837+
DCHECK_GE(instr->InputCount(), 4);
4838+
Condition cc = FlagsConditionToConditionOvf(condition);
4839+
if (cc == eq) {
4840+
Register temp = v_true;
4841+
v_true = v_false;
4842+
v_false = temp;
4843+
}
4844+
UseScratchRegisterScope temps(masm());
4845+
Register scratch1 = temps.Acquire();
4846+
Register scratch2 = temps.Acquire();
4847+
__ srai_d(scratch1, i.OutputRegister(), 32);
4848+
__ srai_w(scratch2, i.OutputRegister(), 31);
4849+
if (v_false == zero_reg) {
4850+
__ xor_(scratch1, scratch1, scratch2);
4851+
__ maskeqz(result, v_true, scratch1);
4852+
} else if (v_true == zero_reg) {
4853+
__ xor_(scratch1, scratch1, scratch2);
4854+
__ masknez(result, v_false, scratch1);
4855+
} else if (result == v_true || result == v_false) {
4856+
if (result == v_false) {
4857+
v_false = v_true;
4858+
cc = NegateCondition(cc);
4859+
}
4860+
Label done;
4861+
__ Branch(&done, cc, scratch2, Operand(scratch1));
4862+
__ Move(result, v_false);
4863+
__ bind(&done);
4864+
} else {
4865+
Label true_label, done;
4866+
__ Branch(&true_label, cc, scratch2, Operand(scratch1));
4867+
__ Move(result, v_false);
4868+
__ Branch(&done);
4869+
__ bind(&true_label);
4870+
__ Move(result, v_true);
4871+
__ bind(&done);
4872+
}
4873+
return;
4874+
} else if (instr->arch_opcode() == kLoong64AddOvf_d ||
4875+
instr->arch_opcode() == kLoong64SubOvf_d) {
4876+
DCHECK_GE(instr->InputCount(), 4);
4877+
// Overflow occurs if overflow register is negative
4878+
Condition cc = lt;
4879+
if (condition == kNotOverflow) {
4880+
Register temp = v_true;
4881+
v_true = v_false;
4882+
v_false = temp;
4883+
}
4884+
if (v_false == zero_reg) {
4885+
__ slt(t8, t8, zero_reg);
4886+
__ maskeqz(result, v_true, t8);
4887+
} else if (v_true == zero_reg) {
4888+
__ slt(t8, t8, zero_reg);
4889+
__ masknez(result, v_false, t8);
4890+
} else if (result == v_true || result == v_false) {
4891+
if (result == v_false) {
4892+
v_false = v_true;
4893+
cc = NegateCondition(cc);
4894+
}
4895+
Label done;
4896+
__ Branch(&done, cc, t8, Operand(zero_reg));
4897+
__ Move(result, v_false);
4898+
__ bind(&done);
4899+
} else {
4900+
Label true_label, done;
4901+
__ Branch(&true_label, cc, t8, Operand(zero_reg));
4902+
__ Move(result, v_false);
4903+
__ Branch(&done);
4904+
__ bind(&true_label);
4905+
__ Move(result, v_true);
4906+
__ bind(&done);
4907+
}
4908+
UseScratchRegisterScope temps(masm());
4909+
temps.Include(t8);
4910+
} else if (instr->arch_opcode() == kLoong64MulOvf_w ||
4911+
instr->arch_opcode() == kLoong64MulOvf_d) {
4912+
DCHECK_GE(instr->InputCount(), 4);
4913+
Condition cc = FlagsConditionToConditionOvf(condition);
4914+
if (cc == eq) {
4915+
Register temp = v_true;
4916+
v_true = v_false;
4917+
v_false = temp;
4918+
}
4919+
__ SelectWord(result, t8, v_true, v_false);
4920+
UseScratchRegisterScope temps(masm());
4921+
temps.Include(t8);
4922+
return;
4923+
} else if (instr->arch_opcode() == kLoong64Float32Cmp ||
4924+
instr->arch_opcode() == kLoong64Float64Cmp) {
4925+
bool predicate;
4926+
FlagsConditionToConditionCmpFPU(&predicate, condition);
4927+
UseScratchRegisterScope temps(masm());
4928+
Register scratch = temps.Acquire();
4929+
if (!predicate) {
4930+
Register temp = v_true;
4931+
v_true = v_false;
4932+
v_false = temp;
4933+
}
4934+
__ movcf2gr(scratch, FCC0);
4935+
__ SelectWord(result, scratch, v_true, v_false);
4936+
return;
48384937
} else {
48394938
PrintF("AssembleArchSelect Unimplemented arch_opcode is : %d\n",
48404939
instr->arch_opcode());

‎src/compiler/backend/loong64/instruction-selector-loong64.cc‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ class Loong64OperandGenerator final : public OperandGenerator {
4848
return UseRegister(node);
4949
}
5050

51+
InstructionOperand UseRegisterAtEndOrImmediateZero(OpIndex node) {
52+
if (const ConstantOp* constant =
53+
selector()->Get(node).TryCast<ConstantOp>()) {
54+
if ((constant->IsIntegral() && constant->integral() == 0) ||
55+
(constant->kind == ConstantOp::Kind::kFloat32 &&
56+
constant->float32().get_bits() == 0) ||
57+
(constant->kind == ConstantOp::Kind::kFloat64 &&
58+
constant->float64().get_bits() == 0)) {
59+
return UseImmediate(node);
60+
}
61+
}
62+
return UseRegisterAtEnd(node);
63+
}
64+
5165
bool IsIntegerConstant(OpIndex node) {
5266
int64_t unused;
5367
return selector()->MatchSignedIntegralConstant(node, &unused);
@@ -307,7 +321,7 @@ static void VisitBinop(InstructionSelector* selector, turboshaft::OpIndex node,
307321
InstructionCode reverse_opcode,
308322
FlagsContinuation* cont) {
309323
Loong64OperandGenerator g(selector);
310-
InstructionOperand inputs[2];
324+
InstructionOperand inputs[4];
311325
size_t input_count = 0;
312326
InstructionOperand outputs[1];
313327
size_t output_count = 0;
@@ -331,6 +345,13 @@ static void VisitBinop(InstructionSelector* selector, turboshaft::OpIndex node,
331345
inputs[input_count++] = g.UseOperand(right_node, opcode);
332346
}
333347

348+
if (cont->IsSelect()) {
349+
inputs[input_count++] =
350+
g.UseRegisterAtEndOrImmediateZero(cont->true_value());
351+
inputs[input_count++] =
352+
g.UseRegisterAtEndOrImmediateZero(cont->false_value());
353+
}
354+
334355
outputs[output_count++] = g.DefineAsRegister(node);
335356

336357
DCHECK_NE(0u, input_count);
@@ -2228,9 +2249,14 @@ void InstructionSelector::VisitStackPointerGreaterThan(
22282249
? OperandGenerator::kUniqueRegister
22292250
: OperandGenerator::kRegister;
22302251

2231-
InstructionOperand inputs[] = {g.UseRegisterWithMode(value, register_mode)};
2232-
static constexpr int input_count = arraysize(inputs);
2252+
InstructionOperand inputs[3];
2253+
int input_count = 0;
2254+
inputs[input_count++] = g.UseRegisterWithMode(value, register_mode);
22332255

2256+
if (cont->IsSelect()) {
2257+
inputs[input_count++] = g.UseRegisterOrImmediateZero(cont->true_value());
2258+
inputs[input_count++] = g.UseRegisterOrImmediateZero(cont->false_value());
2259+
}
22342260
EmitWithContinuation(opcode, output_count, outputs, input_count, inputs,
22352261
temp_count, temps, cont);
22362262
}

‎test/cctest/BUILD.gn‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ v8_cluster_source_set("cctest_sources") {
245245
if (is_win) {
246246
sources += [ "test-stack-unwinding-win64.cc" ]
247247
}
248+
} else if (v8_current_cpu == "loong64") {
249+
if (v8_enable_turbofan) {
250+
sources += [ "compiler/test-run-machops-select-loong64.cc" ]
251+
}
248252
}
249253

250254
if (v8_use_perfetto) {

0 commit comments

Comments
 (0)