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