I've been working on the reassociation prototype JDK-8351409 and while developing an IR test I've seen that in some cases commoning does not exactly happen. The attached template-framework generated test fails with the following when my reassociation logic has been applied:
```
1) Method "public java.lang.Object[] compiler.loopopts.templated.ReductionReassociation.test_MAX_L_2()" - [Failed IR rules: 1]:
* @IR rule 1: "@compiler.lib.ir_framework.IR(phase={AFTER_LOOP_OPTS}, applyIfPlatformAnd={}, applyIfCPUFeatureOr={}, counts={"_#MAX_L#_", "= 4"}, applyIfPlatform={}, applyIfPlatformOr={}, failOn={}, applyIfOr={}, applyIfCPUFeatureAnd={}, applyIf={}, applyIfCPUFeature={}, applyIfAnd={}, applyIfNot={})"
> Phase "After Loop Optimizations":
- counts: Graph contains wrong number of nodes:
* Constraint 1: "(\d+(\s){2}(MaxL.*)+(\s){2}===.*)"
- Failed comparison: [found] 5 = 4 [given]
- Matched nodes (5):
* 258 MaxL === _ 770 815 [[ 260 ]] !orig=[962] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:102 (line 34)
* 259 MaxL === _ 863 912 [[ 260 ]] !orig=[961] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:111 (line 34)
* 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
* 261 MaxL === _ 260 964 [[ 288 724 ]] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:128 (line 34)
* 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
```
I've been debugging this with the help of Roland and seems like there are situations where MaxL uses are not been re-added to the worklist after optimisations have been applied. This issue seems similar toJDK-8371558 but for MaxL.
Roland suggested applying this change:
```
diff --git a/src/hotspot/share/opto/phaseX.cpp b/src/hotspot/share/opto/phaseX.cpp
index 1fe911aa7ac..cc9de8b0cd2 100644
--- a/src/hotspot/share/opto/phaseX.cpp
+++ b/src/hotspot/share/opto/phaseX.cpp
@@ -1977,7 +1977,6 @@ bool PhaseIterGVN::verify_Identity_for(Node* n) {
// Found with:
// compiler/codegen/TestBooleanVect.java
// -XX:VerifyIterativeGVN=1110
- case Op_MaxL:
case Op_MinL:
case Op_MaxI:
case Op_MinI:
```
And when I did that, the test failed with:
```
Missed Identity optimization:
Old node:
dist dump
---------------------------------------------
1 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
1 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
0 261 MaxL === _ 260 964 [[ 288 724 ]] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:128 (line 34)
New node:
dist dump
---------------------------------------------
1 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
1 124 Phi === 723 22 964 [[ 964 ]] #long:minint..maxlong, 0u..maxulong, widen: 3 !jvms: ReductionReassociation::test_MAX_L_2 @ bci:20 (line 30)
0 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (/Users/g/src/jdk-avoid-cmov-long-min-max/src/hotspot/share/opto/phaseX.cpp:1109), pid=30200, tid=26371
# assert(!failure) failed: Missed optimization opportunity in PhaseIterGVN
```
I will be looking into this myself as I need a fix for it to get consistent results with my reassociation IR tests for JDK-8351409. I will need to do this for all nodes that I'm expecting to reassociate. I'm expecting to reassociate for all AddNode subclasses.
```
1) Method "public java.lang.Object[] compiler.loopopts.templated.ReductionReassociation.test_MAX_L_2()" - [Failed IR rules: 1]:
* @IR rule 1: "@compiler.lib.ir_framework.IR(phase={AFTER_LOOP_OPTS}, applyIfPlatformAnd={}, applyIfCPUFeatureOr={}, counts={"_#MAX_L#_", "= 4"}, applyIfPlatform={}, applyIfPlatformOr={}, failOn={}, applyIfOr={}, applyIfCPUFeatureAnd={}, applyIf={}, applyIfCPUFeature={}, applyIfAnd={}, applyIfNot={})"
> Phase "After Loop Optimizations":
- counts: Graph contains wrong number of nodes:
* Constraint 1: "(\d+(\s){2}(MaxL.*)+(\s){2}===.*)"
- Failed comparison: [found] 5 = 4 [given]
- Matched nodes (5):
* 258 MaxL === _ 770 815 [[ 260 ]] !orig=[962] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:102 (line 34)
* 259 MaxL === _ 863 912 [[ 260 ]] !orig=[961] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:111 (line 34)
* 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
* 261 MaxL === _ 260 964 [[ 288 724 ]] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:128 (line 34)
* 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
```
I've been debugging this with the help of Roland and seems like there are situations where MaxL uses are not been re-added to the worklist after optimisations have been applied. This issue seems similar to
Roland suggested applying this change:
```
diff --git a/src/hotspot/share/opto/phaseX.cpp b/src/hotspot/share/opto/phaseX.cpp
index 1fe911aa7ac..cc9de8b0cd2 100644
--- a/src/hotspot/share/opto/phaseX.cpp
+++ b/src/hotspot/share/opto/phaseX.cpp
@@ -1977,7 +1977,6 @@ bool PhaseIterGVN::verify_Identity_for(Node* n) {
// Found with:
// compiler/codegen/TestBooleanVect.java
// -XX:VerifyIterativeGVN=1110
- case Op_MaxL:
case Op_MinL:
case Op_MaxI:
case Op_MinI:
```
And when I did that, the test failed with:
```
Missed Identity optimization:
Old node:
dist dump
---------------------------------------------
1 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
1 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
0 261 MaxL === _ 260 964 [[ 288 724 ]] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:128 (line 34)
New node:
dist dump
---------------------------------------------
1 260 MaxL === _ 258 259 [[ 261 964 ]] !orig=[963] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:120 (line 34)
1 124 Phi === 723 22 964 [[ 964 ]] #long:minint..maxlong, 0u..maxulong, widen: 3 !jvms: ReductionReassociation::test_MAX_L_2 @ bci:20 (line 30)
0 964 MaxL === _ 124 260 [[ 124 724 261 287 ]] !orig=[257] !jvms: ReductionReassociation::test_MAX_L_2 @ bci:149 (line 35)
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (/Users/g/src/jdk-avoid-cmov-long-min-max/src/hotspot/share/opto/phaseX.cpp:1109), pid=30200, tid=26371
# assert(!failure) failed: Missed optimization opportunity in PhaseIterGVN
```
I will be looking into this myself as I need a fix for it to get consistent results with my reassociation IR tests for JDK-8351409. I will need to do this for all nodes that I'm expecting to reassociate. I'm expecting to reassociate for all AddNode subclasses.