
jdk.graal.compiler.lir.dfa.MarkBasePointersPhase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compiler Show documentation
Show all versions of compiler Show documentation
The GraalVM compiler and the Graal-truffle optimizer.
The newest version!
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.lir.dfa;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jdk.graal.compiler.core.common.LIRKind;
import jdk.graal.compiler.lir.LIR;
import jdk.graal.compiler.lir.LIRFrameState;
import jdk.graal.compiler.lir.LIRInstruction;
import jdk.graal.compiler.lir.LIRValueUtil;
import jdk.graal.compiler.lir.Variable;
import jdk.graal.compiler.lir.framemap.FrameMap;
import jdk.graal.compiler.lir.gen.LIRGenerationResult;
import jdk.graal.compiler.lir.phases.AllocationPhase;
import jdk.graal.compiler.lir.util.ValueSet;
import jdk.graal.compiler.lir.util.IndexedValueMap;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.Value;
import jdk.vm.ci.meta.ValueKind;
/**
* Record all derived reference base pointers in a frame state.
*/
public final class MarkBasePointersPhase extends AllocationPhase {
@Override
protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
new Marker(lirGenRes.getLIR(), null).build();
}
private static final class Marker extends LocationMarker {
private final class BasePointersSet extends ValueSet {
private final Map> baseDerivedRefs;
BasePointersSet() {
baseDerivedRefs = new HashMap<>();
}
private BasePointersSet(BasePointersSet other) {
// Deep copy.
baseDerivedRefs = new HashMap<>(other.baseDerivedRefs.size());
for (Map.Entry> entry : other.baseDerivedRefs.entrySet()) {
Set s = new HashSet<>(entry.getValue());
baseDerivedRefs.put(entry.getKey(), s);
}
}
@Override
public Marker.BasePointersSet copy() {
return new BasePointersSet(this);
}
// Verify that there is no base includes derivedRef already.
// The single derivedRef maps to multiple bases case can not happen.
private boolean verifyDerivedRefs(Value derivedRef, int base) {
for (Map.Entry> entry : baseDerivedRefs.entrySet()) {
Set s = entry.getValue();
if (s.contains(derivedRef) && base != entry.getKey()) {
return false;
}
}
return true;
}
@Override
public void put(Value v) {
Variable base = LIRValueUtil.asVariable(v.getValueKind(LIRKind.class).getDerivedReferenceBase());
assert !base.getValueKind(LIRKind.class).isValue();
Set derivedRefs = baseDerivedRefs.get(base.index);
assert verifyDerivedRefs(v, base.index);
if (derivedRefs == null) {
HashSet s = new HashSet<>();
s.add(v);
baseDerivedRefs.put(base.index, s);
} else {
derivedRefs.add(v);
}
}
@Override
public void putAll(BasePointersSet v) {
for (Map.Entry> entry : v.baseDerivedRefs.entrySet()) {
Integer k = entry.getKey();
Set derivedRefsOther = entry.getValue();
Set derivedRefs = baseDerivedRefs.get(k);
if (derivedRefs == null) {
// Deep copy.
Set s = new HashSet<>(derivedRefsOther);
baseDerivedRefs.put(k, s);
} else {
derivedRefs.addAll(derivedRefsOther);
}
}
}
@Override
public void remove(Value v) {
Variable base = LIRValueUtil.asVariable(v.getValueKind(LIRKind.class).getDerivedReferenceBase());
assert !base.getValueKind(LIRKind.class).isValue();
Set derivedRefs = baseDerivedRefs.get(base.index);
// Just mark the base pointer as null if no derived references exist.
if (derivedRefs == null) {
return;
}
// Remove the value from the derived reference set if the set exists.
if (derivedRefs.contains(v)) {
derivedRefs.remove(v);
if (derivedRefs.isEmpty()) {
baseDerivedRefs.remove(base.index);
}
}
}
private IndexedValueMap getMap() {
IndexedValueMap result = new IndexedValueMap();
for (Set entry : baseDerivedRefs.values()) {
if (entry.isEmpty()) {
continue;
}
Value v = entry.iterator().next();
Variable base = LIRValueUtil.asVariable(v.getValueKind(LIRKind.class).getDerivedReferenceBase());
result.put(base.index, base);
}
return result;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Marker.BasePointersSet) {
BasePointersSet other = (BasePointersSet) obj;
return baseDerivedRefs.equals(other.baseDerivedRefs);
} else {
return false;
}
}
@Override
public int hashCode() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[BasePointersSet] baseDerivedRefs map: {");
boolean mapHaveElement = false;
for (Map.Entry> entry : baseDerivedRefs.entrySet()) {
sb.append(entry.getKey());
sb.append(": (");
boolean setHaveElement = false;
for (Value v : entry.getValue()) {
sb.append(v + ",");
setHaveElement = true;
}
if (setHaveElement) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append("),");
mapHaveElement = true;
}
if (mapHaveElement) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append("}");
return sb.toString();
}
}
private Marker(LIR lir, FrameMap frameMap) {
super(lir, frameMap);
}
@Override
protected Marker.BasePointersSet newLiveValueSet() {
return new BasePointersSet();
}
@Override
protected boolean shouldProcessValue(Value operand) {
ValueKind> kind = operand.getValueKind();
if (kind instanceof LIRKind) {
return ((LIRKind) kind).isDerivedReference();
} else {
return false;
}
}
@Override
protected void processState(LIRInstruction op, LIRFrameState info, BasePointersSet values) {
info.setLiveBasePointers(values.getMap());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy