Skip to content

Commit c7e9ebb

Browse files
Suchismith RoyTheRealMDoerr
authored andcommitted
8331732: [PPC64] Unify and optimize code which converts != 0 to 1
Reviewed-by: mdoerr, amitkumar
1 parent 53242cd commit c7e9ebb

File tree

7 files changed

+43
-21
lines changed

7 files changed

+43
-21
lines changed

src/hotspot/cpu/ppc/assembler_ppc.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2012, 2023 SAP SE. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -350,6 +350,7 @@ class Assembler : public AbstractAssembler {
350350

351351
SETBC_OPCODE = (31u << OPCODE_SHIFT | 384u << 1),
352352
SETNBC_OPCODE = (31u << OPCODE_SHIFT | 448u << 1),
353+
SETBCR_OPCODE = (31u << OPCODE_SHIFT | 416u << 1),
353354

354355
// condition register logic instructions
355356
CRAND_OPCODE = (19u << OPCODE_SHIFT | 257u << 1),
@@ -1780,6 +1781,8 @@ class Assembler : public AbstractAssembler {
17801781
inline void setbc( Register d, ConditionRegister cr, Condition cc);
17811782
inline void setnbc(Register d, int biint);
17821783
inline void setnbc(Register d, ConditionRegister cr, Condition cc);
1784+
inline void setbcr(Register d, int biint);
1785+
inline void setbcr(Register d, ConditionRegister cr, Condition cc);
17831786

17841787
// Special purpose registers
17851788
// Exception Register

src/hotspot/cpu/ppc/assembler_ppc.inline.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2012, 2020 SAP SE. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -419,6 +419,11 @@ inline void Assembler::setnbc(Register d, int biint)
419419
inline void Assembler::setnbc(Register d, ConditionRegister cr, Condition cc) {
420420
setnbc(d, bi0(cr, cc));
421421
}
422+
inline void Assembler::setbcr(Register d, int biint)
423+
{ emit_int32(SETBCR_OPCODE | rt(d) | bi(biint)); }
424+
inline void Assembler::setbcr(Register d, ConditionRegister cr, Condition cc) {
425+
setbcr(d, bi0(cr, cc));
426+
}
422427

423428
// Special purpose registers
424429
// Exception Register

src/hotspot/cpu/ppc/macroAssembler_ppc.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,10 +2383,7 @@ void MacroAssembler::verify_secondary_supers_table(Register r_sub_klass,
23832383
addi(r_array_base, r_array_base, Array<Klass*>::base_offset_in_bytes());
23842384

23852385
// convert !=0 to 1
2386-
neg(R0, result);
2387-
orr(result, result, R0);
2388-
srdi(result, result, 63);
2389-
2386+
normalize_bool(result, R0, true);
23902387
const Register linear_result = r_array_index; // reuse
23912388
li(linear_result, 1);
23922389
cmpdi(CCR0, r_array_length, 0);
@@ -2395,9 +2392,7 @@ void MacroAssembler::verify_secondary_supers_table(Register r_sub_klass,
23952392
bind(failure);
23962393

23972394
// convert !=0 to 1
2398-
neg(R0, linear_result);
2399-
orr(linear_result, linear_result, R0);
2400-
srdi(linear_result, linear_result, 63);
2395+
normalize_bool(linear_result, R0, true);
24012396

24022397
cmpd(CCR0, result, linear_result);
24032398
beq(CCR0, passed);

src/hotspot/cpu/ppc/macroAssembler_ppc.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class MacroAssembler: public Assembler {
178178
void inline set_cmp3(Register dst);
179179
// set dst to (treat_unordered_like_less ? -1 : +1)
180180
void inline set_cmpu3(Register dst, bool treat_unordered_like_less);
181+
// Branch-free implementation to convert !=0 to 1.
182+
void inline normalize_bool(Register dst, Register temp = R0, bool is_64bit = false);
181183

182184
inline void pd_patch_instruction(address branch, address target, const char* file, int line);
183185
NOT_PRODUCT(static void pd_print_patched_instruction(address branch);)

src/hotspot/cpu/ppc/macroAssembler_ppc.inline.hpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2012, 2021 SAP SE. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -264,6 +264,29 @@ inline void MacroAssembler::set_cmpu3(Register dst, bool treat_unordered_like_le
264264
set_cmp3(dst);
265265
}
266266

267+
// Branch-free implementation to convert !=0 to 1
268+
// Set register dst to 1 if dst is non-zero. Uses setbcr instruction on Power10.
269+
inline void MacroAssembler::normalize_bool(Register dst, Register temp, bool is_64bit) {
270+
271+
if (VM_Version::has_brw()) {
272+
if (is_64bit) {
273+
cmpdi(CCR0, dst, 0);
274+
} else {
275+
cmpwi(CCR0, dst, 0);
276+
}
277+
setbcr(dst, CCR0, Assembler::equal);
278+
} else {
279+
assert_different_registers(temp, dst);
280+
neg(temp, dst);
281+
orr(temp, dst, temp);
282+
if (is_64bit) {
283+
srdi(dst, temp, 63);
284+
} else {
285+
srwi(dst, temp, 31);
286+
}
287+
}
288+
}
289+
267290
// Convenience bc_far versions
268291
inline void MacroAssembler::blt_far(ConditionRegister crx, Label& L, int optimize) { MacroAssembler::bc_far(bcondCRbiIs1, bi0(crx, less), L, optimize); }
269292
inline void MacroAssembler::bgt_far(ConditionRegister crx, Label& L, int optimize) { MacroAssembler::bc_far(bcondCRbiIs1, bi0(crx, greater), L, optimize); }

src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,11 +2472,7 @@ nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm,
24722472
case T_ARRAY: break;
24732473

24742474
case T_BOOLEAN: { // 0 -> false(0); !0 -> true(1)
2475-
Label skip_modify;
2476-
__ cmpwi(CCR0, R3_RET, 0);
2477-
__ beq(CCR0, skip_modify);
2478-
__ li(R3_RET, 1);
2479-
__ bind(skip_modify);
2475+
__ normalize_bool(R3_RET);
24802476
break;
24812477
}
24822478
case T_BYTE: { // sign extension

src/hotspot/cpu/ppc/templateInterpreterGenerator_ppc.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,7 @@ address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type
372372
switch (type) {
373373
case T_BOOLEAN:
374374
// convert !=0 to 1
375-
__ neg(R0, R3_RET);
376-
__ orr(R0, R3_RET, R0);
377-
__ srwi(R3_RET, R0, 31);
375+
__ normalize_bool(R3_RET);
378376
break;
379377
case T_BYTE:
380378
// sign extend 8 bits

0 commit comments

Comments
 (0)