[aarch64-port-dev ] RFR(S): 8239914: AArch64: Combine add/sub with comparison against zero

Andrew Haley aph at redhat.com
Fri Feb 28 14:12:07 UTC 2020


On 2/27/20 3:41 AM, Pengfei Li wrote:
> For Java code like "if (a + b > 0) {...}", C2 AArch64 backend generates
> instructions like below.
>   add     w10, w2, w1
>   cmp     w10, #0x0
>   b.gt    0x0000ffff790d5274
> 
> Indeed, the ADD with the following CMP against 0 can be combined into a
> CMN (an alias of ADDS). Also, a SUB with a following CMP against 0 can
> be combined into a single CMP (an alias of SUBS).
> 
> JBS: https://bugs.openjdk.java.net/browse/JDK-8239914
> Webrev: http://cr.openjdk.java.net/~pli/rfr/8239914/webrev.00/

I am not sure you're thought about this clearly enough. Try this
program:

public class DifferenceTest {

    static int theTest(int x, int y) {
        int sum = 0;
        while (x - y > 0) {
            sum++;
            y--;
        }
        return sum;
    }

    static int n = -0x7000_0000;
    static int m = +0x7000_0000;

    void trial() {
        int prev = theTest(n, m);
        for (int i = 0; i < 10_000_000; i++) {
            int result = theTest(n, m);
            if (prev != result) {
                throw new RuntimeException("last sum was " + prev + ", is now " + result);
            }
            prev = result;
        }
    }

    public static void main(String[] args) {
        new DifferenceTest().trial();
    }
}

-- 
Andrew Haley  (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671



More information about the aarch64-port-dev mailing list