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 AbstractBlockBase<?>[] blocks = lirGenRes.getLIR().getControlFlowGraph().getBlocks(); 71 for (AbstractBlockBase<?> b : blocks) { 72 matchBlock(nodeLirBuilder, (Block) b, graph, schedule); 73 } 74 for (AbstractBlockBase<?> b : blocks) { 75 emitBlock(nodeLirBuilder, lirGenRes, (Block) b, graph, schedule.getBlockToNodesMap()); 76 } 77 context.lirGen.beforeRegisterAllocation(); 78 assert SSAUtil.verifySSAForm(lirGenRes.getLIR()); 79 nodeCount.add(graph.getDebug(), graph.getNodeCount()); 80 } 81 82 private static void emitBlock(NodeLIRBuilderTool nodeLirGen, LIRGenerationResult lirGenRes, Block b, StructuredGraph graph, BlockMap<List<Node>> blockMap) { 83 assert !isProcessed(lirGenRes, b) : "Block already processed " + b; 84 assert verifyPredecessors(lirGenRes, b); 85 nodeLirGen.doBlock(b, graph, blockMap); 86 LIR lir = lirGenRes.getLIR(); 87 DebugContext debug = lir.getDebug(); 88 instructionCounter.add(debug, lir.getLIRforBlock(b).size()); 89 } 90 91 private static void matchBlock(NodeLIRBuilderTool nodeLirGen, Block b, StructuredGraph graph, ScheduleResult schedule) { 92 nodeLirGen.matchBlock(b, graph, schedule); 93 } 94 95 private static boolean verifyPredecessors(LIRGenerationResult lirGenRes, Block block) { 96 for (Block pred : block.getPredecessors()) { 97 if (!block.isLoopHeader() || !pred.isLoopEnd()) { 98 assert isProcessed(lirGenRes, pred) : "Predecessor not yet processed " + pred; 99 } 100 } 101 return true; 102 } 103 104 private static boolean isProcessed(LIRGenerationResult lirGenRes, Block b) { 105 return lirGenRes.getLIR().getLIRforBlock(b) != null; 106 } 107 108 }