RFR: 8291336: Add ideal rule to convert floating point multiply by 2 into addition [v4]

Quan Anh Mai qamai at openjdk.org
Tue Sep 27 18:50:31 UTC 2022


On Thu, 4 Aug 2022 15:05:53 GMT, SuperCoder79 <duke at openjdk.org> wrote:

>> Hello,
>> I would like to propose an ideal transform that converts floating point multiply by 2 (`x * 2`) into an addition operation instead. This would allow for the elimination of the memory reference for the constant two, and keep the whole operation inside registers. My justifications for this optimization include:
>> * As per [Agner Fog's instruction tables](https://www.agner.org/optimize/instruction_tables.pdf) many older systems, such as the sandy bridge and ivy bridge architectures, have different latencies for addition and multiplication meaning this change could have beneficial effects when in hot code.
>> * The removal of the memory load would have a beneficial effect in cache bound situations.
>> * Multiplication by 2 is relatively common construct so this change can apply to a wide range of Java code.
>> 
>> As this is my first time looking into the c2 codebase, I have a few lingering questions about my implementation and how certain parts of the compiler work. Mainly, is this patch getting the type of the operands correctly? I saw some cases where code used `bottom_type()` and other cases where it used `phase->type(value)`. Similarly, are nodes able to be reused as is being done in the AddNode constructors? I saw some places where the clone method was being used, but other places where it wasn't.
>> 
>> I have attached an IR test and a jmh benchmark. Tier 1 testing passes on my machine.
>> 
>> Thanks for your time,
>> Jasmine
>
> SuperCoder79 has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Apply style changes from code review

The change looks good to me otherwise. Thanks.

src/hotspot/share/opto/mulnode.cpp line 438:

> 436: //------------------------------Ideal---------------------------------------
> 437: // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal
> 438: Node *MulFNode::Ideal(PhaseGVN *phase, bool can_reshape) {

Please use the format `Type* identifier` for new code (in this case it is `PhaseGVN* phase`). The same applies to other places.

test/hotspot/jtreg/compiler/c2/irTests/TestMulNodeIdealization.java line 61:

> 59:     @Run(test = "testFloat")
> 60:     public void runTestFloat() {
> 61:         testFloat(RANDOM.nextFloat());

Verification against the execution in the interpreter would be better here.

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

Marked as reviewed by qamai (Author).

PR: https://git.openjdk.org/jdk/pull/9642


More information about the hotspot-compiler-dev mailing list