Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4061344

Compile-time expression simplification misses some cases

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.5.1

      Description

      In the following program, the subexpression "i < 0" is side-effect-free and
      appears in a context in which its value is not required.

          public class Bug {
              public static void main(String args[]) {
                  int i = 1;
                  boolean b = i < 0 || true;
              }
          }

      When compiled with javac (1.1.3), the following code results:

          public class Bug {
           // Compiled from Bug.java
      // Compiler version 3.45;


          public static Method main:"([Ljava/lang/String;)V"
         stack 1 locals 2
          {
         iconst_1;
      istore_1;
      iload_1;
      iflt L9;
      goto L9;
      L9: iconst_1;
      pop;
      return;
          }

          public Method <init>:"()V"
      stack 1 locals 1
          {
      aload_0;
      invokespecial Method java/lang/Object.<init>:"()V";
      return;
          }

          } // end Class Bug

      Examination of the compiler code, e.g, Expression.simplify, Expression.inline,
      and Expression.inlineValue, reveals that expressions are not always re-examined
      for further simplifications enabled by previous simplifications. In the
      case illustrated, the CommaExpression introduced during the simplification
      of 'i < 0 || true' to '(i < 0, true)' is not further simplified, which would
      allow the expression 'i < 0' to be discarded.

      While the only observed effects are on code quality, it is possible that
      the correctness of the compiler may be impacted, as the compile-time
      simplification machinery is also used to evaluate constant expressions as
      defined in the JLS. Such expressions must simplify to a literal constant.

        Attachments

          Activity

            People

            • Assignee:
              gafter Neal Gafter
              Reporter:
              wmaddoxsunw William Maddox (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: