001/*
002 * Copyright (c) 2015, 2015, 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.lir.alloc.lsra.ssi;
024
025import static com.oracle.graal.lir.alloc.lsra.ssa.SSALinearScanLifetimeAnalysisPhase.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030import jdk.internal.jvmci.meta.*;
031
032import com.oracle.graal.lir.*;
033import com.oracle.graal.lir.LIRInstruction.OperandFlag;
034import com.oracle.graal.lir.LIRInstruction.OperandMode;
035import com.oracle.graal.lir.StandardOp.LabelOp;
036import com.oracle.graal.lir.alloc.lsra.*;
037import com.oracle.graal.lir.alloc.lsra.Interval.RegisterPriority;
038import com.oracle.graal.lir.alloc.lsra.Interval.SpillState;
039import com.oracle.graal.lir.ssi.*;
040
041public class SSILinearScanLifetimeAnalysisPhase extends LinearScanLifetimeAnalysisPhase {
042
043    public SSILinearScanLifetimeAnalysisPhase(LinearScan linearScan) {
044        super(linearScan);
045    }
046
047    @Override
048    protected void addRegisterHint(final LIRInstruction op, final Value targetValue, OperandMode mode, EnumSet<OperandFlag> flags, final boolean hintAtDef) {
049        super.addRegisterHint(op, targetValue, mode, flags, hintAtDef);
050
051        if (hintAtDef && op instanceof LabelOp) {
052            LabelOp label = (LabelOp) op;
053
054            Interval to = allocator.getOrCreateInterval((AllocatableValue) targetValue);
055
056            SSIUtil.forEachRegisterHint(allocator.getLIR(), allocator.blockForId(label.id()), label, targetValue, mode, (ValueConsumer) (registerHint, valueMode, valueFlags) -> {
057                if (LinearScan.isVariableOrRegister(registerHint)) {
058                    Interval from = allocator.getOrCreateInterval((AllocatableValue) registerHint);
059
060                    setHint(op, to, from);
061                    setHint(op, from, to);
062                }
063            });
064        }
065    }
066
067    @Override
068    protected void changeSpillDefinitionPos(LIRInstruction op, AllocatableValue operand, Interval interval, int defPos) {
069        assert interval.isSplitParent() : "can only be called for split parents";
070
071        switch (interval.spillState()) {
072            case NoDefinitionFound:
073                // assert interval.spillDefinitionPos() == -1 : "must no be set before";
074                interval.setSpillDefinitionPos(defPos);
075                if (!(op instanceof LabelOp)) {
076                    // Do not update state for labels. This will be done afterwards.
077                    interval.setSpillState(SpillState.NoSpillStore);
078                }
079                break;
080
081            case NoSpillStore:
082                assert defPos <= interval.spillDefinitionPos() : "positions are processed in reverse order when intervals are created";
083                if (defPos < interval.spillDefinitionPos() - 2) {
084                    // second definition found, so no spill optimization possible for this interval
085                    interval.setSpillState(SpillState.NoOptimization);
086                } else {
087                    // two consecutive definitions (because of two-operand LIR form)
088                    assert allocator.blockForId(defPos) == allocator.blockForId(interval.spillDefinitionPos()) : "block must be equal";
089                }
090                break;
091
092            case NoOptimization:
093                // nothing to do
094                break;
095
096            default:
097                throw new BailoutException("other states not allowed at this time");
098        }
099    }
100
101    @Override
102    protected void buildIntervals() {
103        super.buildIntervals();
104        for (Interval interval : allocator.intervals()) {
105            if (interval != null && interval.spillState().equals(SpillState.NoDefinitionFound) && interval.spillDefinitionPos() != -1) {
106                // there was a definition in a phi/sigma
107                interval.setSpillState(SpillState.NoSpillStore);
108            }
109        }
110    }
111
112    @Override
113    protected RegisterPriority registerPriorityOfOutputOperand(LIRInstruction op) {
114        if (op instanceof LabelOp) {
115            LabelOp label = (LabelOp) op;
116            if (label.id() != 0) {
117                // skip method header
118                return RegisterPriority.None;
119            }
120        }
121        return super.registerPriorityOfOutputOperand(op);
122    }
123}