1 /* 2 * Copyright (c) 2014, 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 package org.graalvm.compiler.printer; 24 25 import java.util.List; 26 import java.util.Map; 27 import java.util.concurrent.ConcurrentHashMap; 28 29 import org.graalvm.compiler.debug.DebugContext; 30 import org.graalvm.compiler.debug.DebugVerifyHandler; 31 import org.graalvm.compiler.debug.GraalError; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.nodes.StructuredGraph; 34 import org.graalvm.compiler.options.Option; 35 import org.graalvm.compiler.options.OptionKey; 36 import org.graalvm.compiler.options.OptionType; 37 import org.graalvm.compiler.options.OptionValues; 38 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 39 40 /** 41 * Verifies that graphs have no dead code. 42 */ 43 public class NoDeadCodeVerifyHandler implements DebugVerifyHandler { 44 45 // The options below will be removed once all phases clean up their own dead code. 46 47 private static final int OFF = 0; 48 private static final int INFO = 1; 49 private static final int VERBOSE = 2; 50 private static final int FATAL = 3; 51 52 static class Options { 53 // @formatter:off 54 @Option(help = "Run level for NoDeadCodeVerifyHandler (0 = off, 1 = info, 2 = verbose, 3 = fatal)", type = OptionType.Debug) 55 public static final OptionKey<Integer> NDCV = new OptionKey<>(0); 56 // @formatter:on 57 } 58 59 /** 60 * Only the first instance of failure at any point is shown. This will also be removed once all 61 * phases clean up their own dead code. 62 */ 63 private static final Map<String, Boolean> discovered = new ConcurrentHashMap<>(); 64 65 @Override 66 public void verify(DebugContext debug, Object object, String format, Object... args) { 67 OptionValues options = debug.getOptions(); 68 if (Options.NDCV.getValue(options) != OFF && object instanceof StructuredGraph) { 69 StructuredGraph graph = (StructuredGraph) object; 70 List<Node> before = graph.getNodes().snapshot(); 71 new DeadCodeEliminationPhase().run(graph); 72 List<Node> after = graph.getNodes().snapshot(); 73 assert after.size() <= before.size(); 74 if (before.size() != after.size()) { 75 if (discovered.put(format, Boolean.TRUE) == null) { 76 before.removeAll(after); 77 String prefix = format == null ? "" : format + ": "; 78 GraalError error = new GraalError("%sfound dead nodes in %s: %s", prefix, graph, before); 79 if (Options.NDCV.getValue(options) == INFO) { 80 System.out.println(error.getMessage()); 81 } else if (Options.NDCV.getValue(options) == VERBOSE) { 82 error.printStackTrace(System.out); 83 } else { 84 assert Options.NDCV.getValue(options) == FATAL; 85 throw error; 86 } 87 } 88 } 89 } 90 } 91 }