1 /* 2 * Copyright (c) 2016, 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.lir.alloc.trace; 24 25 import static org.graalvm.compiler.lir.alloc.trace.TraceUtil.isTrivialTrace; 26 27 import java.util.List; 28 29 import org.graalvm.compiler.core.common.alloc.BiDirectionalTraceBuilder; 30 import org.graalvm.compiler.core.common.alloc.SingleBlockTraceBuilder; 31 import org.graalvm.compiler.core.common.alloc.Trace; 32 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult; 33 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult.TrivialTracePredicate; 34 import org.graalvm.compiler.core.common.alloc.TraceStatisticsPrinter; 35 import org.graalvm.compiler.core.common.alloc.UniDirectionalTraceBuilder; 36 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 37 import org.graalvm.compiler.debug.Debug; 38 import org.graalvm.compiler.debug.GraalError; 39 import org.graalvm.compiler.lir.LIR; 40 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 41 import org.graalvm.compiler.lir.phases.AllocationPhase; 42 import org.graalvm.compiler.options.EnumOptionValue; 43 import org.graalvm.compiler.options.Option; 44 import org.graalvm.compiler.options.OptionType; 45 import org.graalvm.compiler.options.OptionValue; 46 47 import jdk.vm.ci.code.TargetDescription; 48 49 public class TraceBuilderPhase extends AllocationPhase { 50 51 public enum TraceBuilder { 52 UniDirectional, 53 BiDirectional, 54 SingleBlock 55 } 56 57 public static class Options { 58 // @formatter:off 59 @Option(help = "Trace building algorithm.", type = OptionType.Debug) 60 public static final EnumOptionValue<TraceBuilder> TraceBuilding = new EnumOptionValue<>(TraceBuilder.UniDirectional); 61 @Option(help = "Schedule trivial traces as early as possible.", type = OptionType.Debug) 62 public static final OptionValue<Boolean> TraceRAScheduleTrivialTracesEarly = new OptionValue<>(true); 63 // @formatter:on 64 } 65 66 private static final int TRACE_LOG_LEVEL = 1; 67 public static final int TRACE_DUMP_LEVEL = 3; 68 69 @Override 70 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) { 71 AbstractBlockBase<?>[] linearScanOrder = lirGenRes.getLIR().linearScanOrder(); 72 AbstractBlockBase<?> startBlock = linearScanOrder[0]; 73 LIR lir = lirGenRes.getLIR(); 74 assert startBlock.equals(lir.getControlFlowGraph().getStartBlock()); 75 76 final TraceBuilderResult traceBuilderResult = getTraceBuilderResult(lir, startBlock, linearScanOrder); 77 78 if (Debug.isLogEnabled(TRACE_LOG_LEVEL)) { 79 List<Trace> traces = traceBuilderResult.getTraces(); 80 for (int i = 0; i < traces.size(); i++) { 81 Trace trace = traces.get(i); 82 Debug.log(TRACE_LOG_LEVEL, "Trace %5d: %s%s", i, trace, isTrivialTrace(lirGenRes.getLIR(), trace) ? " (trivial)" : ""); 83 } 84 } 85 TraceStatisticsPrinter.printTraceStatistics(traceBuilderResult, lirGenRes.getCompilationUnitName()); 86 Debug.dump(TRACE_DUMP_LEVEL, traceBuilderResult, "After TraceBuilding"); 87 context.contextAdd(traceBuilderResult); 88 } 89 90 private static TraceBuilderResult getTraceBuilderResult(LIR lir, AbstractBlockBase<?> startBlock, AbstractBlockBase<?>[] linearScanOrder) { 91 TraceBuilderResult.TrivialTracePredicate pred = getTrivialTracePredicate(lir); 92 93 TraceBuilder selectedTraceBuilder = Options.TraceBuilding.getValue(); 94 Debug.log(TRACE_LOG_LEVEL, "Building Traces using %s", selectedTraceBuilder); 95 switch (Options.TraceBuilding.getValue()) { 96 case SingleBlock: 97 return SingleBlockTraceBuilder.computeTraces(startBlock, linearScanOrder, pred); 98 case BiDirectional: 99 return BiDirectionalTraceBuilder.computeTraces(startBlock, linearScanOrder, pred); 100 case UniDirectional: 101 return UniDirectionalTraceBuilder.computeTraces(startBlock, linearScanOrder, pred); 102 } 103 throw GraalError.shouldNotReachHere("Unknown trace building algorithm: " + Options.TraceBuilding.getValue()); 104 } 105 106 public static TraceBuilderResult.TrivialTracePredicate getTrivialTracePredicate(LIR lir) { 107 if (!Options.TraceRAScheduleTrivialTracesEarly.getValue()) { 108 return null; 109 } 110 return new TrivialTracePredicate() { 111 @Override 112 public boolean isTrivialTrace(Trace trace) { 113 return TraceUtil.isTrivialTrace(lir, trace); 114 } 115 }; 116 } 117 }