TraceUtil.java revision 12657:6ef01bd40ce2
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 */
23package org.graalvm.compiler.lir.alloc.trace;
24
25import java.util.List;
26
27import org.graalvm.compiler.core.common.alloc.Trace;
28import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
29import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
30import org.graalvm.compiler.lir.LIR;
31import org.graalvm.compiler.lir.LIRInstruction;
32import org.graalvm.compiler.lir.StandardOp.JumpOp;
33import org.graalvm.compiler.lir.StandardOp.LabelOp;
34
35import jdk.vm.ci.meta.Value;
36
37public class TraceUtil {
38
39    public static AbstractBlockBase<?> getBestTraceInterPredecessor(TraceBuilderResult traceResult, AbstractBlockBase<?> block) {
40        AbstractBlockBase<?> bestPred = null;
41        int bestTraceId = traceResult.getTraceForBlock(block).getId();
42        for (AbstractBlockBase<?> pred : block.getPredecessors()) {
43            int predTraceId = traceResult.getTraceForBlock(pred).getId();
44            if (predTraceId < bestTraceId) {
45                bestPred = pred;
46                bestTraceId = predTraceId;
47            }
48        }
49        return bestPred;
50    }
51
52    public static boolean isShadowedRegisterValue(Value value) {
53        assert value != null;
54        return value instanceof ShadowedRegisterValue;
55    }
56
57    public static ShadowedRegisterValue asShadowedRegisterValue(Value value) {
58        assert isShadowedRegisterValue(value);
59        return (ShadowedRegisterValue) value;
60    }
61
62    public static boolean isTrivialTrace(LIR lir, Trace trace) {
63        if (trace.size() != 1) {
64            return false;
65        }
66        List<LIRInstruction> instructions = lir.getLIRforBlock(trace.getBlocks()[0]);
67        if (instructions.size() != 2) {
68            return false;
69        }
70        assert instructions.get(0) instanceof LabelOp : "First instruction not a LabelOp: " + instructions.get(0);
71        if (((LabelOp) instructions.get(0)).isPhiIn()) {
72            /*
73             * Merge blocks are in general not trivial block because variables defined by a PHI
74             * always need a location. If the outgoing value of the predecessor is a constant we
75             * need to find an appropriate location (register or stack).
76             *
77             * Note that this case should not happen in practice since the trace containing the
78             * merge block should also contain one of the predecessors. For non-standard trace
79             * builders (e.g. the single block trace builder) this is not the true, though.
80             */
81            return false;
82        }
83        /*
84         * Now we need to check if the BlockEndOp has no special operand requirements (i.e.
85         * stack-slot, register). For now we just check for JumpOp because we know that it doesn't.
86         */
87        return instructions.get(1) instanceof JumpOp;
88    }
89}
90