Skip to content

Improve dead code elimination for new arrays #10223

@niloc132

Description

@niloc132

Next perhaps to lambdas, array creation is one of the more verbose bits of output that the compiler creates, so that it can correctly emit the class literal, castable type map, the length of the array, and the type of the elements. The DeadCodeElimination pass doesn't consider array creation at all, except that creating an array has side effects if either the size expressions have side effects, or if the initializers have side effects (as you can't specify both size and initializers at once). This seems like an easy pass to improve: given array expressions where the value is unused

new int[hasSideEffects()]

or

new int[]{has(), side(), effects()]

we can instead emit

hasSideEffects()

or

(has(), side(), effects())

respectively. A quick sketch might look like this:

    /**
     * Simplify new array expressions whose output is ignored.
     */
    @Override
    public void endVisit(JNewArray x, Context ctx) {
      if (ignoringExpressionOutput.contains(x)) {
        if (x.getDimensionExpressions() != null) {
          ctx.replaceMe(new JMultiExpression(x.getSourceInfo(), x.getDimensionExpressions()));
        } else {
          assert x.getInitializers() != null;
          ctx.replaceMe(new JMultiExpression(x.getSourceInfo(), x.getInitializers()));
        }
        ignoringExpressionOutput.remove(x);
      }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions