RFR: 8276673: Optimize abs operations in C2 compiler [v4]
Jie Fu
jiefu at openjdk.java.net
Thu Dec 16 11:21:57 UTC 2021
On Thu, 16 Dec 2021 10:31:00 GMT, Fei Gao <fgao at openjdk.org> wrote:
> > Hi @fg1417 ,
> > Thanks for your update.
> > Now I see that you are trying to optimize the following three abs() patterns:
> >
> > 1. Math.abs(-38)
> > 2. (char) Math.abs((char) c)
> > 3. Math.abs(0 - x)
> >
> > But did you see these code patterns in real programs? I'm a bit worried that we just improve the complexity of C2 with (almost) no performance gain in the real world. Thanks.
>
> Thanks for your review, @DamonFool . I really understand your concern.
>
> In terms of complexity, the change only involves AbsNode and doesn’t modify any other code part. I don’t think it will make C2 more complex.
>
> As for performance gain in the real world, as we all know, the ability of GVN to optimize a node often depends on the optimized result of its input nodes. For example, if the input node of one AbsNode is recognized as a constant after last round of GVN optimization, now, we can optimize abs(constant) to a simple constant value. Like C2 did in
>
> https://github.com/openjdk/jdk/blob/0bddd8af61b6c731f16b857c09de57ceefd72d06/src/hotspot/share/opto/subnode.cpp#L55
>
> , we may not see -(-x) or (x+y)-y in any java program directly, but it’s possible after C2 optimization. Whether the optimization to sub or to abs is trivial, low-cost but useful. Why not apply it :)
> Math.abs(-38) , (char) Math.abs((char) c) and Math.abs(0 - x) are just conformance testcases. As you said, maybe nobody writes these cases in the real world. These testcases are just simulating all possible scenarios that AbsNode may meet, to guarantee the correctness of the optimization.
>
> What do you think :)
>
> Thanks.
Then, shall we also opt cases like `Math.abs(-1 * x)`, `Math.abs(x / (-1))`, and so on?
Thanks.
-------------
PR: https://git.openjdk.java.net/jdk/pull/6755
More information about the hotspot-compiler-dev
mailing list