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.dfa;
024
025import java.util.*;
026
027import jdk.internal.jvmci.code.*;
028import jdk.internal.jvmci.meta.*;
029
030import com.oracle.graal.compiler.common.alloc.*;
031import com.oracle.graal.compiler.common.cfg.*;
032import com.oracle.graal.lir.*;
033import com.oracle.graal.lir.framemap.*;
034import com.oracle.graal.lir.gen.*;
035import com.oracle.graal.lir.gen.LIRGeneratorTool.SpillMoveFactory;
036import com.oracle.graal.lir.phases.*;
037import com.oracle.graal.lir.util.*;
038
039/**
040 * Record all derived reference base pointers in a frame state.
041 */
042public final class MarkBasePointersPhase extends AllocationPhase {
043
044    @Override
045    protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder, SpillMoveFactory spillMoveFactory,
046                    RegisterAllocationConfig registerAllocationConfig) {
047        new Marker<B>(lirGenRes.getLIR(), null).build();
048    }
049
050    private static final class Marker<T extends AbstractBlockBase<T>> extends LocationMarker<T, Marker<T>.BasePointersSet> {
051
052        private final class BasePointersSet extends ValueSet<Marker<T>.BasePointersSet> {
053
054            private final IndexedValueMap variables;
055
056            public BasePointersSet() {
057                variables = new IndexedValueMap();
058            }
059
060            private BasePointersSet(BasePointersSet s) {
061                variables = new IndexedValueMap(s.variables);
062            }
063
064            @Override
065            public Marker<T>.BasePointersSet copy() {
066                return new BasePointersSet(this);
067            }
068
069            @Override
070            public void put(Value v) {
071                Variable base = (Variable) v.getLIRKind().getDerivedReferenceBase();
072                assert !base.getLIRKind().isValue();
073                variables.put(base.index, base);
074            }
075
076            @Override
077            public void putAll(BasePointersSet v) {
078                variables.putAll(v.variables);
079            }
080
081            @Override
082            public void remove(Value v) {
083                Variable base = (Variable) v.getLIRKind().getDerivedReferenceBase();
084                assert !base.getLIRKind().isValue();
085                variables.put(base.index, null);
086            }
087
088            @SuppressWarnings("unchecked")
089            @Override
090            public boolean equals(Object obj) {
091                if (obj instanceof Marker.BasePointersSet) {
092                    BasePointersSet other = (BasePointersSet) obj;
093                    return variables.equals(other.variables);
094                } else {
095                    return false;
096                }
097            }
098
099            @Override
100            public int hashCode() {
101                throw new UnsupportedOperationException();
102            }
103        }
104
105        private Marker(LIR lir, FrameMap frameMap) {
106            super(lir, frameMap);
107        }
108
109        @Override
110        protected Marker<T>.BasePointersSet newLiveValueSet() {
111            return new BasePointersSet();
112        }
113
114        @Override
115        protected boolean shouldProcessValue(Value operand) {
116            return operand.getLIRKind().isDerivedReference();
117        }
118
119        @Override
120        protected void processState(LIRInstruction op, LIRFrameState info, BasePointersSet values) {
121            info.setLiveBasePointers(new IndexedValueMap(values.variables));
122        }
123    }
124}