001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test.backend;
024
025import java.util.*;
026
027import jdk.internal.jvmci.code.*;
028import com.oracle.graal.debug.*;
029import com.oracle.graal.debug.Debug.*;
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034import com.oracle.graal.compiler.common.cfg.*;
035import com.oracle.graal.lir.*;
036import com.oracle.graal.lir.StandardOp.MoveOp;
037import com.oracle.graal.nodes.*;
038import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
039
040public class AllocatorTest extends BackendTest {
041
042    protected void testAllocation(String snippet, final int expectedRegisters, final int expectedRegRegMoves, final int expectedSpillMoves) {
043        final StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
044        try (Scope s = Debug.scope("AllocatorTest", graph, graph.method(), getCodeCache())) {
045            final RegisterStats stats = new RegisterStats(getLIRGenerationResult(graph).getLIR());
046            try (Scope s2 = Debug.scope("Assertions", stats.lir)) {
047                Assert.assertEquals("register count", expectedRegisters, stats.registers.size());
048                Assert.assertEquals("reg-reg moves", expectedRegRegMoves, stats.regRegMoves);
049                Assert.assertEquals("spill moves", expectedSpillMoves, stats.spillMoves);
050            } catch (Throwable e) {
051                throw Debug.handle(e);
052            }
053        } catch (Throwable e) {
054            throw Debug.handle(e);
055        }
056    }
057
058    private class RegisterStats {
059
060        public final LIR lir;
061        public HashSet<Register> registers = new HashSet<>();
062        public int regRegMoves;
063        public int spillMoves;
064
065        public RegisterStats(LIR lir) {
066            this.lir = lir;
067
068            for (AbstractBlockBase<?> block : lir.codeEmittingOrder()) {
069                for (LIRInstruction instr : lir.getLIRforBlock(block)) {
070                    collectStats(instr);
071                }
072            }
073        }
074
075        private ValueProcedure collectStatsProc = (value, mode, flags) -> {
076            if (ValueUtil.isRegister(value)) {
077                final Register reg = ValueUtil.asRegister(value);
078                registers.add(reg);
079            }
080            return value;
081        };
082
083        private void collectStats(final LIRInstruction instr) {
084            instr.forEachOutput(collectStatsProc);
085
086            if (instr instanceof MoveOp) {
087                MoveOp move = (MoveOp) instr;
088                Value def = move.getResult();
089                Value use = move.getInput();
090                if (ValueUtil.isRegister(def)) {
091                    if (ValueUtil.isRegister(use)) {
092                        regRegMoves++;
093                    }
094                } else if (ValueUtil.isStackSlotValue(def)) {
095                    spillMoves++;
096                }
097            }
098        }
099    }
100}