AllocatorTest.java revision 12651:6ef01bd40ce2
10SN/A/* 2157SN/A * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 30SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 40SN/A * 50SN/A * This code is free software; you can redistribute it and/or modify it 60SN/A * under the terms of the GNU General Public License version 2 only, as 7157SN/A * published by the Free Software Foundation. 80SN/A * 9157SN/A * This code is distributed in the hope that it will be useful, but WITHOUT 100SN/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 110SN/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 120SN/A * version 2 for more details (a copy is included in the LICENSE file that 130SN/A * accompanied this code). 140SN/A * 150SN/A * You should have received a copy of the GNU General Public License version 160SN/A * 2 along with this work; if not, write to the Free Software Foundation, 170SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 180SN/A * 190SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 200SN/A * or visit www.oracle.com if you need additional information or have any 21157SN/A * questions. 22157SN/A */ 23157SN/Apackage org.graalvm.compiler.core.test.backend; 240SN/A 250SN/Aimport java.util.HashSet; 260SN/A 270SN/Aimport jdk.vm.ci.code.Register; 280SN/Aimport jdk.vm.ci.code.ValueUtil; 290SN/Aimport jdk.vm.ci.meta.Value; 300SN/A 310SN/Aimport org.junit.Assert; 320SN/A 330SN/Aimport org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 340SN/Aimport org.graalvm.compiler.debug.Debug; 350SN/Aimport org.graalvm.compiler.debug.Debug.Scope; 360SN/Aimport org.graalvm.compiler.lir.LIR; 370SN/Aimport org.graalvm.compiler.lir.LIRInstruction; 380SN/Aimport org.graalvm.compiler.lir.LIRValueUtil; 390SN/Aimport org.graalvm.compiler.lir.StandardOp.ValueMoveOp; 400SN/Aimport org.graalvm.compiler.lir.ValueProcedure; 410SN/Aimport org.graalvm.compiler.nodes.StructuredGraph; 420SN/Aimport org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 430SN/A 440SN/Apublic class AllocatorTest extends BackendTest { 450SN/A 460SN/A @SuppressWarnings("try") 470SN/A protected void testAllocation(String snippet, final int expectedRegisters, final int expectedRegRegMoves, final int expectedSpillMoves) { 480SN/A final StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 490SN/A try (Scope s = Debug.scope("AllocatorTest", graph, graph.method(), getCodeCache())) { 500SN/A final RegisterStats stats = new RegisterStats(getLIRGenerationResult(graph).getLIR()); 510SN/A try (Scope s2 = Debug.scope("Assertions", stats.lir)) { 520SN/A Assert.assertEquals("register count", expectedRegisters, stats.registers.size()); 530SN/A Assert.assertEquals("reg-reg moves", expectedRegRegMoves, stats.regRegMoves); 540SN/A Assert.assertEquals("spill moves", expectedSpillMoves, stats.spillMoves); 550SN/A } catch (Throwable e) { 560SN/A throw Debug.handle(e); 570SN/A } 580SN/A } catch (Throwable e) { 590SN/A throw Debug.handle(e); 600SN/A } 610SN/A } 620SN/A 630SN/A private class RegisterStats { 640SN/A 650SN/A public final LIR lir; 660SN/A public HashSet<Register> registers = new HashSet<>(); 670SN/A public int regRegMoves; 680SN/A public int spillMoves; 690SN/A 700SN/A RegisterStats(LIR lir) { 710SN/A this.lir = lir; 720SN/A 730SN/A for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) { 740SN/A if (block == null) { 750SN/A continue; 760SN/A } 770SN/A for (LIRInstruction instr : lir.getLIRforBlock(block)) { 780SN/A collectStats(instr); 790SN/A } 800SN/A } 810SN/A } 820SN/A 830SN/A private ValueProcedure collectStatsProc = (value, mode, flags) -> { 840SN/A 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} 109