Skip to content

Commit

Permalink
8292867: RISC-V: Simplify weak CAS return value handling
Browse files Browse the repository at this point in the history
Reviewed-by: fyang
Backport-of: f57d34242c9b47936d137589fc75ab794d39a9d1
  • Loading branch information
DingliZhang authored and RealFYang committed Apr 17, 2023
1 parent 2009e6e commit b934d81
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
27 changes: 14 additions & 13 deletions src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
Expand Up @@ -2623,7 +2623,7 @@ void MacroAssembler::cmpxchg_narrow_value(Register addr, Register expected,
bnez(tmp, retry);

if (result_as_bool) {
addi(result, zr, 1);
li(result, 1);
j(done);

bind(fail);
Expand Down Expand Up @@ -2658,7 +2658,7 @@ void MacroAssembler::weak_cmpxchg_narrow_value(Register addr, Register expected,
assert_different_registers(addr, old, mask, not_mask, new_val, expected, shift, tmp);
cmpxchg_narrow_value_helper(addr, expected, new_val, size, tmp1, tmp2, tmp3);

Label succ, fail, done;
Label fail, done;

lr_w(old, aligned_addr, acquire);
andr(tmp, old, mask);
Expand All @@ -2667,13 +2667,14 @@ void MacroAssembler::weak_cmpxchg_narrow_value(Register addr, Register expected,
andr(tmp, old, not_mask);
orr(tmp, tmp, new_val);
sc_w(tmp, tmp, aligned_addr, release);
beqz(tmp, succ);
bnez(tmp, fail);

bind(fail);
addi(result, zr, 1);
// Success
li(result, 1);
j(done);

bind(succ);
// Fail
bind(fail);
mv(result, zr);

bind(done);
Expand Down Expand Up @@ -2717,20 +2718,20 @@ void MacroAssembler::cmpxchg_weak(Register addr, Register expected,
enum operand_size size,
Assembler::Aqrl acquire, Assembler::Aqrl release,
Register result) {
Label fail, done, sc_done;
Label fail, done;
load_reserved(addr, size, acquire);
bne(t0, expected, fail);
store_conditional(addr, new_val, size, release);
beqz(t0, sc_done);
bnez(t0, fail);

// fail
bind(fail);
// Success
li(result, 1);
j(done);

// sc_done
bind(sc_done);
mv(result, 0);
// Fail
bind(fail);
mv(result, zr);

bind(done);
}

Expand Down
36 changes: 12 additions & 24 deletions src/hotspot/cpu/riscv/riscv.ad
Expand Up @@ -5723,14 +5723,13 @@ instruct weakCompareAndSwapB(iRegINoSp res, indirect mem, iRegI_R12 oldval, iReg

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (byte, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapB"
"# $res == 1 when success, #@weakCompareAndSwapB"
%}

ins_encode %{
__ weak_cmpxchg_narrow_value(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int8,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register,
$tmp1$$Register, $tmp2$$Register, $tmp3$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5747,14 +5746,13 @@ instruct weakCompareAndSwapS(iRegINoSp res, indirect mem, iRegI_R12 oldval, iReg

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (short, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapS"
"# $res == 1 when success, #@weakCompareAndSwapS"
%}

ins_encode %{
__ weak_cmpxchg_narrow_value(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int16,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register,
$tmp1$$Register, $tmp2$$Register, $tmp3$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5768,13 +5766,12 @@ instruct weakCompareAndSwapI(iRegINoSp res, indirect mem, iRegI oldval, iRegI ne

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (int, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapI"
"# $res == 1 when success, #@weakCompareAndSwapI"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int32,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5788,13 +5785,12 @@ instruct weakCompareAndSwapL(iRegINoSp res, indirect mem, iRegL oldval, iRegL ne

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (long, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapL"
"# $res == 1 when success, #@weakCompareAndSwapL"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int64,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5808,13 +5804,12 @@ instruct weakCompareAndSwapN(iRegINoSp res, indirect mem, iRegN oldval, iRegN ne

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (narrow oop, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapN"
"# $res == 1 when success, #@weakCompareAndSwapN"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::uint32,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5829,13 +5824,12 @@ instruct weakCompareAndSwapP(iRegINoSp res, indirect mem, iRegP oldval, iRegP ne

format %{
"cmpxchg_weak $mem, $oldval, $newval\t# (ptr, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapP"
"# $res == 1 when success, #@weakCompareAndSwapP"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int64,
/*acquire*/ Assembler::relaxed, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5854,14 +5848,13 @@ instruct weakCompareAndSwapBAcq(iRegINoSp res, indirect mem, iRegI_R12 oldval, i

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (byte, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapBAcq"
"# $res == 1 when success, #@weakCompareAndSwapBAcq"
%}

ins_encode %{
__ weak_cmpxchg_narrow_value(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int8,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register,
$tmp1$$Register, $tmp2$$Register, $tmp3$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5880,14 +5873,13 @@ instruct weakCompareAndSwapSAcq(iRegINoSp res, indirect mem, iRegI_R12 oldval, i

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (short, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapSAcq"
"# $res == 1 when success, #@weakCompareAndSwapSAcq"
%}

ins_encode %{
__ weak_cmpxchg_narrow_value(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int16,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register,
$tmp1$$Register, $tmp2$$Register, $tmp3$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5903,13 +5895,12 @@ instruct weakCompareAndSwapIAcq(iRegINoSp res, indirect mem, iRegI oldval, iRegI

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (int, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapIAcq"
"# $res == 1 when success, #@weakCompareAndSwapIAcq"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int32,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5925,13 +5916,12 @@ instruct weakCompareAndSwapLAcq(iRegINoSp res, indirect mem, iRegL oldval, iRegL

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (long, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapLAcq"
"# $res == 1 when success, #@weakCompareAndSwapLAcq"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int64,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5947,13 +5937,12 @@ instruct weakCompareAndSwapNAcq(iRegINoSp res, indirect mem, iRegN oldval, iRegN

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (narrow oop, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapNAcq"
"# $res == 1 when success, #@weakCompareAndSwapNAcq"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::uint32,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand All @@ -5969,13 +5958,12 @@ instruct weakCompareAndSwapPAcq(iRegINoSp res, indirect mem, iRegP oldval, iRegP

format %{
"cmpxchg_weak_acq $mem, $oldval, $newval\t# (ptr, weak) if $mem == $oldval then $mem <-- $newval\n\t"
"xori $res, $res, 1\t# $res == 1 when success, #@weakCompareAndSwapPAcq"
"\t# $res == 1 when success, #@weakCompareAndSwapPAcq"
%}

ins_encode %{
__ cmpxchg_weak(as_Register($mem$$base), $oldval$$Register, $newval$$Register, Assembler::int64,
/*acquire*/ Assembler::aq, /*release*/ Assembler::rl, $res$$Register);
__ xori($res$$Register, $res$$Register, 1);
%}

ins_pipe(pipe_slow);
Expand Down

0 comments on commit b934d81

Please sign in to comment.