1 /* 2 * Copyright (c) 2015, 2018, 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.core; 26 27 import java.util.List; 28 29 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 30 import org.graalvm.compiler.core.common.cfg.BlockMap; 31 import org.graalvm.compiler.debug.CounterKey; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.graph.Node; 34 import org.graalvm.compiler.lir.LIR; 35 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 36 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 37 import org.graalvm.compiler.lir.phases.LIRPhase; 38 import org.graalvm.compiler.lir.ssa.SSAUtil; 39 import org.graalvm.compiler.nodes.StructuredGraph; 40 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; 41 import org.graalvm.compiler.nodes.cfg.Block; 42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 43 44 import jdk.vm.ci.code.TargetDescription; 45 46 public class LIRGenerationPhase extends LIRPhase<LIRGenerationPhase.LIRGenerationContext> { 47 48 public static final class LIRGenerationContext { 49 private final NodeLIRBuilderTool nodeLirBuilder; 50 private final LIRGeneratorTool lirGen; 51 private final StructuredGraph graph; 52 private final ScheduleResult schedule; 53 54 public LIRGenerationContext(LIRGeneratorTool lirGen, NodeLIRBuilderTool nodeLirBuilder, StructuredGraph graph, ScheduleResult schedule) { 55 this.nodeLirBuilder = nodeLirBuilder; 56 this.lirGen = lirGen; 57 this.graph = graph; 58 this.schedule = schedule; 59 } 60 } 61 62 private static final CounterKey instructionCounter = DebugContext.counter("GeneratedLIRInstructions"); 63 private static final CounterKey nodeCount = DebugContext.counter("FinalNodeCount"); 64 65 @Override 66 protected final void run(TargetDescription target, LIRGenerationResult lirGenRes, LIRGenerationPhase.LIRGenerationContext context) { 67 NodeLIRBuilderTool nodeLirBuilder = context.nodeLirBuilder; 68 StructuredGraph graph = context.graph; 69 ScheduleResult schedule = context.schedule; 70 for (AbstractBlockBase<?> b : lirGenRes.getLIR().getControlFlowGraph().getBlocks()) { 71 emitBlock(nodeLirBuilder, lirGenRes, (Block) b, graph, schedule.getBlockToNodesMap()); 72 } 73 context.lirGen.beforeRegisterAllocation(); 74 assert SSAUtil.verifySSAForm(lirGenRes.getLIR()); 75 nodeCount.add(graph.getDebug(), graph.getNodeCount()); 76 } 77 78 private static void emitBlock(NodeLIRBuilderTool nodeLirGen, LIRGenerationResult lirGenRes, Block b, StructuredGraph graph, BlockMap<List<Node>> blockMap) { 79 assert !isProcessed(lirGenRes, b) : "Block already processed " + b; 80 assert verifyPredecessors(lirGenRes, b); 81 nodeLirGen.doBlock(b, graph, blockMap); 82 LIR lir = lirGenRes.getLIR(); 83 DebugContext debug = lir.getDebug(); 84 instructionCounter.add(debug, lir.getLIRforBlock(b).size()); 85 } 86 87 private static boolean verifyPredecessors(LIRGenerationResult lirGenRes, Block block) { 88 for (Block pred : block.getPredecessors()) { 89 if (!block.isLoopHeader() || !pred.isLoopEnd()) { 90 assert isProcessed(lirGenRes, pred) : "Predecessor not yet processed " + pred; 91 } 92 } 93 return true; 94 } 95 96 private static boolean isProcessed(LIRGenerationResult lirGenRes, Block b) { 97 return lirGenRes.getLIR().getLIRforBlock(b) != null; 98 } 99 100 }