Skip to content

Commit

Permalink
8264020: Optimize double negation elimination
Browse files Browse the repository at this point in the history
For the double negation '-(-x)', c2 could reduce the redundant 'sub'
through SubNode::Ideal. But every time when 'sub' is removed, there
would generate two useless nodes SubNode(x, 0)[1] and AddNode(x, -0)[2],
which would be removed by later phases but should have optimized better
to 'x' itself without generating those new stuffs.

This patch is a small fix for SubNode's Ideal, by leaving this special
case to 'Identity' which handled double negation already[3].

[1] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L238
[2] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L181
[3] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L55

Change-Id: I5597e43001a4593b32b1bb25d4a4bef0ba1bcc09
  • Loading branch information
Eric Liu committed Mar 26, 2021
1 parent a5e7a89 commit 934fe11
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/hotspot/share/opto/subnode.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -235,8 +235,8 @@ Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
return new SubINode(phase->intcon(0), in2->in(1));
}

// Convert "0 - (x-y)" into "y-x"
if( t1 == TypeInt::ZERO && op2 == Op_SubI )
// Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity().
if(t1 == TypeInt::ZERO && op2 == Op_SubI && phase->type(in2->in(1)) != TypeInt::ZERO)
return new SubINode( in2->in(2), in2->in(1) );

// Convert "0 - (x+con)" into "-con-x"
Expand Down Expand Up @@ -373,8 +373,8 @@ Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1));
}

// Convert "0 - (x-y)" into "y-x"
if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
// Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity.
if(t1 == TypeLong::ZERO && op2 == Op_SubL && phase->type(in2->in(1)) != TypeLong::ZERO)
return new SubLNode( in2->in(2), in2->in(1) );

// Convert "(X+A) - (X+B)" into "A - B"
Expand Down

0 comments on commit 934fe11

Please sign in to comment.