JDK-8087309 : Constant folding "static final boolean" in boolean expressions is incomplete
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 9,10
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2015-06-12
  • Updated: 2019-01-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Related Reports
Relates :  
Below is a very frequent code pattern that is used to conditionalize the execution.

static final boolean FLAG = Boolean.getBoolean("flag");
if (somePredicate || FLAG) { ... }

In many cases, users expect the expression with FLAG to be completely folded away, or in other words, the original statement to be translated to something like:

FLAG=false:  if (somePredicate) { ... }
FLAG=true:   if (true) { ... } // and then the branch eliminated

Even though this plays games with logical/binary "or" semantics, it seems to be doable when "somePredicate" does not have any side effects. However, simple experiments show that current compiler only does this kind of transformation for some specific code shapes only. We need to figure out whether all basic forms are covered, and if not, if the optimizations can be extended to more code shapes.
Vladimir: I wonder if you can reuse the side-effect detection logic from JDK-8009303: http://hg.openjdk.java.net/jdk9/jdk9/hotspot/rev/90abdd727e64

Added the symmetric && and & tests, the same links above.

See the experimental data here: http://cr.openjdk.java.net/~shade/8087309/ The benchmark and performance data: http://cr.openjdk.java.net/~shade/8087309/StaticFinalConstantFold.java Runnable JAR: http://cr.openjdk.java.net/~shade/8087309/benchmarks.jar (accepts test regexp; use -prof perfnorm or -prof perfasm to get the low-level profile info or disassembly)