1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.phases.common;
  26 
  27 import static org.graalvm.compiler.graph.Graph.NodeEvent.NODE_ADDED;
  28 
  29 import org.graalvm.compiler.core.common.RetryableBailoutException;
  30 import org.graalvm.compiler.graph.Graph.NodeEventScope;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.spi.Simplifiable;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.phases.BasePhase;
  35 import org.graalvm.compiler.phases.common.util.EconomicSetNodeEventListener;
  36 import org.graalvm.compiler.phases.tiers.PhaseContext;
  37 
  38 public class IterativeConditionalEliminationPhase extends BasePhase<PhaseContext> {
  39 
  40     private static final int MAX_ITERATIONS = 256;
  41 
  42     private final CanonicalizerPhase canonicalizer;
  43     private final boolean fullSchedule;
  44 
  45     public IterativeConditionalEliminationPhase(CanonicalizerPhase canonicalizer, boolean fullSchedule) {
  46         this.canonicalizer = canonicalizer;
  47         this.fullSchedule = fullSchedule;
  48     }
  49 
  50     @Override
  51     @SuppressWarnings("try")
  52     protected void run(StructuredGraph graph, PhaseContext context) {
  53         EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener().exclude(NODE_ADDED);
  54         int count = 0;
  55         while (true) {
  56             try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
  57                 new ConditionalEliminationPhase(fullSchedule).apply(graph, context);
  58             }
  59             if (listener.getNodes().isEmpty()) {
  60                 break;
  61             }
  62             for (Node node : graph.getNodes()) {
  63                 if (node instanceof Simplifiable) {
  64                     listener.getNodes().add(node);
  65                 }
  66             }
  67             canonicalizer.applyIncremental(graph, context, listener.getNodes());
  68             listener.getNodes().clear();
  69             if (++count > MAX_ITERATIONS) {
  70                 throw new RetryableBailoutException("Number of iterations in ConditionalEliminationPhase phase exceeds %d", MAX_ITERATIONS);
  71             }
  72         }
  73     }
  74 
  75     @Override
  76     public float codeSizeIncrease() {
  77         return 2.0f;
  78     }
  79 }