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 
  61     @Override
  62     protected final void run(TargetDescription target, LIRGenerationResult lirGenRes, LIRGenerationPhase.LIRGenerationContext context) {
  63         NodeLIRBuilderTool nodeLirBuilder = context.nodeLirBuilder;
  64         StructuredGraph graph = context.graph;
  65         ScheduleResult schedule = context.schedule;
  66         for (AbstractBlockBase<?> b : lirGenRes.getLIR().getControlFlowGraph().getBlocks()) {
  67             emitBlock(nodeLirBuilder, lirGenRes, (Block) b, graph, schedule.getBlockToNodesMap());
  68         }
  69         context.lirGen.beforeRegisterAllocation();
  70         assert SSAUtil.verifySSAForm(lirGenRes.getLIR());
  71     }
  72 
  73     private static void emitBlock(NodeLIRBuilderTool nodeLirGen, LIRGenerationResult lirGenRes, Block b, StructuredGraph graph, BlockMap<List<Node>> blockMap) {
  74         assert !isProcessed(lirGenRes, b) : "Block already processed " + b;
  75         assert verifyPredecessors(lirGenRes, b);
  76         nodeLirGen.doBlock(b, graph, blockMap);
  77         if (instructionCounter.isEnabled()) {
  78             instructionCounter.add(lirGenRes.getLIR().getLIRforBlock(b).size());
  79         }
  80     }
  81 
  82     private static boolean verifyPredecessors(LIRGenerationResult lirGenRes, Block block) {
  83         for (Block pred : block.getPredecessors()) {
  84             if (!block.isLoopHeader() || !pred.isLoopEnd()) {
  85                 assert isProcessed(lirGenRes, pred) : "Predecessor not yet processed " + pred;
  86             }
  87         }
  88         return true;
  89     }
  90 
  91     private static boolean isProcessed(LIRGenerationResult lirGenRes, Block b) {
  92         return lirGenRes.getLIR().getLIRforBlock(b) != null;
  93     }
  94 
  95 }