001/* 002 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.compiler.phases; 024 025import java.util.*; 026import java.util.stream.*; 027 028import com.oracle.graal.debug.*; 029import com.oracle.graal.debug.Debug.*; 030 031import com.oracle.graal.graph.Graph.NodeEvent; 032import com.oracle.graal.graph.Graph.NodeEventScope; 033import com.oracle.graal.graph.Node; 034import com.oracle.graal.nodes.*; 035import com.oracle.graal.phases.*; 036import com.oracle.graal.phases.common.util.*; 037import com.oracle.graal.phases.tiers.*; 038 039/** 040 * A utility phase for detecting when a phase would change the graph and reporting extra information 041 * about the effects. The phase is first run on a copy of the graph and if a change in that graph is 042 * detected then it's rerun on the original graph inside a new debug scope under 043 * GraphChangeMonitoringPhase. The message argument can be used to distinguish between the same 044 * phase run at different points. 045 * 046 * @param <C> 047 */ 048public class GraphChangeMonitoringPhase<C extends PhaseContext> extends PhaseSuite<C> { 049 050 private final String message; 051 052 public GraphChangeMonitoringPhase(String message, BasePhase<C> phase) { 053 super(); 054 this.message = message; 055 appendPhase(phase); 056 } 057 058 public GraphChangeMonitoringPhase(String message) { 059 super(); 060 this.message = message; 061 } 062 063 @Override 064 protected void run(StructuredGraph graph, C context) { 065 /* 066 * Phase may add nodes but not end up using them so ignore additions. Nodes going dead and 067 * having their inputs change are the main interesting differences. 068 */ 069 HashSetNodeEventListener listener = new HashSetNodeEventListener().exclude(NodeEvent.NODE_ADDED); 070 StructuredGraph graphCopy = (StructuredGraph) graph.copy(); 071 try (NodeEventScope s = graphCopy.trackNodeEvents(listener)) { 072 try (Scope s2 = Debug.sandbox("WithoutMonitoring", null)) { 073 super.run(graphCopy, context); 074 } catch (Throwable t) { 075 Debug.handle(t); 076 } 077 } 078 /* 079 * Ignore LogicConstantNode since those are sometimes created and deleted as part of running 080 * a phase. 081 */ 082 if (listener.getNodes().stream().filter(e -> !(e instanceof LogicConstantNode)).findFirst().get() != null) { 083 /* rerun it on the real graph in a new Debug scope so Dump and Log can find it. */ 084 listener = new HashSetNodeEventListener(); 085 try (NodeEventScope s = graph.trackNodeEvents(listener)) { 086 try (Scope s2 = Debug.scope("WithGraphChangeMonitoring." + getName() + "-" + message)) { 087 if (Debug.isDumpEnabled(BasePhase.PHASE_DUMP_LEVEL)) { 088 Debug.dump(BasePhase.PHASE_DUMP_LEVEL, graph, "*** Before phase %s", getName()); 089 } 090 super.run(graph, context); 091 Set<Node> collect = listener.getNodes().stream().filter(e -> !e.isAlive()).filter(e -> !(e instanceof LogicConstantNode)).collect(Collectors.toSet()); 092 if (Debug.isDumpEnabled(BasePhase.PHASE_DUMP_LEVEL)) { 093 Debug.dump(BasePhase.PHASE_DUMP_LEVEL, graph, "*** After phase %s %s", getName(), collect); 094 } 095 Debug.log("*** %s %s %s\n", message, graph, collect); 096 } 097 } 098 } else { 099 // Go ahead and run it normally even though it should have no effect 100 super.run(graph, context); 101 } 102 } 103}