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