public static int neg(int i) {
return -(-i);
}
For this simple test case above, c2 could reduce the redundant 'sub' through SubNode::Ideal. But every time when 'sub' be removed, there would generate two useless nodes.
The Ideal first transform '0-(0-i)' into 'i - 0', which generated a new SubNode[1]. Then the generated 'i-0' continues to be replaced with 'i+(-0)', a new generated AddNode[2].
Those generated and useless nodes would be removed by later phases, but perhaps it's better to optimize 0-(0-i) to i itself, without new node was generated.
There already have code to remove double negation[3], but never been used since they have been transformed to others in Ideal(), which happens before Identity.
[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
return -(-i);
}
For this simple test case above, c2 could reduce the redundant 'sub' through SubNode::Ideal. But every time when 'sub' be removed, there would generate two useless nodes.
The Ideal first transform '0-(0-i)' into 'i - 0', which generated a new SubNode[1]. Then the generated 'i-0' continues to be replaced with 'i+(-0)', a new generated AddNode[2].
Those generated and useless nodes would be removed by later phases, but perhaps it's better to optimize 0-(0-i) to i itself, without new node was generated.
There already have code to remove double negation[3], but never been used since they have been transformed to others in Ideal(), which happens before Identity.
[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