1 /*
   2  * Copyright (c) 2013, 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 
  24 
  25 package org.graalvm.compiler.core.test.backend;
  26 
  27 import java.util.HashSet;
  28 
  29 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  30 import org.graalvm.compiler.debug.DebugContext;
  31 import org.graalvm.compiler.lir.LIR;
  32 import org.graalvm.compiler.lir.LIRInstruction;
  33 import org.graalvm.compiler.lir.LIRValueUtil;
  34 import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
  35 import org.graalvm.compiler.lir.ValueProcedure;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.junit.Assert;
  39 
  40 import jdk.vm.ci.code.Register;
  41 import jdk.vm.ci.code.ValueUtil;
  42 import jdk.vm.ci.meta.Value;
  43 
  44 public class AllocatorTest extends BackendTest {
  45 
  46     @SuppressWarnings("try")
  47     protected void testAllocation(String snippet, final int expectedRegisters, final int expectedRegRegMoves, final int expectedSpillMoves) {
  48         final StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  49         DebugContext debug = graph.getDebug();
  50         try (DebugContext.Scope s = debug.scope("AllocatorTest", graph, graph.method(), getCodeCache())) {
  51             final RegisterStats stats = new RegisterStats(getLIRGenerationResult(graph).getLIR());
  52             try (DebugContext.Scope s2 = debug.scope("Assertions", stats.lir)) {
  53                 Assert.assertEquals("register count", expectedRegisters, stats.registers.size());
  54                 Assert.assertEquals("reg-reg moves", expectedRegRegMoves, stats.regRegMoves);
  55                 Assert.assertEquals("spill moves", expectedSpillMoves, stats.spillMoves);
  56             } catch (Throwable e) {
  57                 throw debug.handle(e);
  58             }
  59         } catch (Throwable e) {
  60             throw debug.handle(e);
  61         }
  62     }
  63 
  64     private class RegisterStats {
  65 
  66         public final LIR lir;
  67         public HashSet<Register> registers = new HashSet<>();
  68         public int regRegMoves;
  69         public int spillMoves;
  70 
  71         RegisterStats(LIR lir) {
  72             this.lir = lir;
  73 
  74             for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
  75                 if (block == null) {
  76                     continue;
  77                 }
  78                 for (LIRInstruction instr : lir.getLIRforBlock(block)) {
  79                     collectStats(instr);
  80                 }
  81             }
  82         }
  83 
  84         private ValueProcedure collectStatsProc = (value, mode, flags) -> {
  85             if (ValueUtil.isRegister(value)) {
  86                 final Register reg = ValueUtil.asRegister(value);
  87                 registers.add(reg);
  88             }
  89             return value;
  90         };
  91 
  92         private void collectStats(final LIRInstruction instr) {
  93             instr.forEachOutput(collectStatsProc);
  94 
  95             if (ValueMoveOp.isValueMoveOp(instr)) {
  96                 ValueMoveOp move = ValueMoveOp.asValueMoveOp(instr);
  97                 Value def = move.getResult();
  98                 Value use = move.getInput();
  99                 if (ValueUtil.isRegister(def)) {
 100                     if (ValueUtil.isRegister(use)) {
 101                         regRegMoves++;
 102                     }
 103                 } else if (LIRValueUtil.isStackSlotValue(def)) {
 104                     spillMoves++;
 105                 }
 106             }
 107         }
 108     }
 109 }