RFR: 8256436: AArch64: Fix undefined behavior for signed right shift in assembler

Andrew Haley aph at openjdk.java.net
Fri Nov 20 10:47:06 UTC 2020


On Fri, 20 Nov 2020 10:30:50 GMT, Xiaohong Gong <xgong at openjdk.org> wrote:

>> src/hotspot/cpu/aarch64/assembler_aarch64.hpp line 3212:
>> 
>>> 3210:     } else if (T != B && imm16 <= 32512 && imm16 >= -32768 && (imm16 & 0xff) == 0) {
>>> 3211:       sh = 1;
>>> 3212:       imm = (imm >> 8);
>> 
>> I'm very skeptical about the need for this. Every AArch64 compiler of which I'm aware treats signed right shift as well defined, and any incoming AArch64 compiler would need to do so in order to be compatible.
>
> Hi @theRealAph , thanks for looking at this PR, and thanks for your comment here. Yes, I agree that the compilers we know like GCC/LLVM can make sure the behavior is defined on AArch64. And actually we didn't met any issues here. However, I'm not quite sure whether other compilers can guarantee it. This is just used to avoid the undefined behavior in future.  So do you think we need to fix it here? I can abandon this patch if this is not valuable. Thanks!

The compilers do guarantee it. Here's GCC, for example:

4.5 Integers

GCC supports only two’s complement integer types, and all bit patterns are ordinary values. 

Bitwise operators act on the representation of the value including both the sign and value bits, where the sign bit is considered immediately above the highest-value value bit. Signed ‘>>’ acts on negative numbers by sign extension.

>> src/hotspot/cpu/aarch64/assembler_aarch64.hpp line 3219:
>> 
>>> 3217:     imm &= mask;
>>> 3218:     f(0b00100101, 31, 24), f(T, 23, 22), f(0b11100011, 21, 14);
>>> 3219:     f(sh, 13), f(imm, 12, 5), rf(Zd, 0);
>> 
>> imm here is a "signed immediate in the range -128 to 127," and it would be perverse to treat it as an unsigned value in the assembler.
>
> Hi @theRealAph , thanks for the comment. `imm` here is an unsigned immediate and it has been masked with `0xff` before, which is the same with the implementation of `sf`.

Yes, I'm saying don't do that.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1307


More information about the hotspot-compiler-dev mailing list