001/*
002 * Copyright (c) 2014, 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.dfa;
024
025import static jdk.internal.jvmci.code.ValueUtil.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030import jdk.internal.jvmci.meta.*;
031
032import com.oracle.graal.compiler.common.alloc.*;
033import com.oracle.graal.compiler.common.cfg.*;
034import com.oracle.graal.lir.*;
035import com.oracle.graal.lir.framemap.*;
036import com.oracle.graal.lir.gen.*;
037import com.oracle.graal.lir.gen.LIRGeneratorTool.SpillMoveFactory;
038import com.oracle.graal.lir.phases.*;
039
040/**
041 * Mark all live references for a frame state. The frame state use this information to build the OOP
042 * maps.
043 */
044public final class LocationMarkerPhase extends AllocationPhase {
045
046    @Override
047    protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder, SpillMoveFactory spillMoveFactory,
048                    RegisterAllocationConfig registerAllocationConfig) {
049        new Marker<B>(lirGenRes.getLIR(), lirGenRes.getFrameMap()).build();
050    }
051
052    static final class Marker<T extends AbstractBlockBase<T>> extends LocationMarker<T, RegStackValueSet> {
053
054        private final RegisterAttributes[] registerAttributes;
055
056        private Marker(LIR lir, FrameMap frameMap) {
057            super(lir, frameMap);
058            this.registerAttributes = frameMap.getRegisterConfig().getAttributesMap();
059        }
060
061        @Override
062        protected RegStackValueSet newLiveValueSet() {
063            return new RegStackValueSet(frameMap);
064        }
065
066        @Override
067        protected boolean shouldProcessValue(Value operand) {
068            return (isRegister(operand) && attributes(asRegister(operand)).isAllocatable() || isStackSlot(operand)) && operand.getPlatformKind() != Kind.Illegal;
069        }
070
071        /**
072         * This method does the actual marking.
073         */
074        @Override
075        protected void processState(LIRInstruction op, LIRFrameState info, RegStackValueSet values) {
076            if (!info.hasDebugInfo()) {
077                info.initDebugInfo(frameMap, !op.destroysCallerSavedRegisters() || !frameMap.getRegisterConfig().areAllAllocatableRegistersCallerSaved());
078            }
079
080            ReferenceMapBuilder refMap = frameMap.newReferenceMapBuilder();
081            frameMap.addLiveValues(refMap);
082            values.addLiveValues(refMap);
083
084            info.debugInfo().setReferenceMap(refMap.finish(info));
085        }
086
087        /**
088         * Gets an object describing the attributes of a given register according to this register
089         * configuration.
090         */
091        private RegisterAttributes attributes(Register reg) {
092            return registerAttributes[reg.number];
093        }
094
095    }
096}