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