1 /*
2  * Copyright (c) 2015, 2018, 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  */
23 
24 
25 package org.graalvm.compiler.lir.dfa;
26 
27 import org.graalvm.compiler.core.common.LIRKind;
28 import org.graalvm.compiler.lir.LIR;
29 import org.graalvm.compiler.lir.LIRFrameState;
30 import org.graalvm.compiler.lir.LIRInstruction;
31 import org.graalvm.compiler.lir.Variable;
32 import org.graalvm.compiler.lir.framemap.FrameMap;
33 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
34 import org.graalvm.compiler.lir.phases.AllocationPhase;
35 import org.graalvm.compiler.lir.util.IndexedValueMap;
36 import org.graalvm.compiler.lir.util.ValueSet;
37 
38 import jdk.vm.ci.code.TargetDescription;
39 import jdk.vm.ci.meta.Value;
40 import jdk.vm.ci.meta.ValueKind;
41 
42 /**
43  * Record all derived reference base pointers in a frame state.
44  */
45 public final class MarkBasePointersPhase extends AllocationPhase {
46 
47     @Override
run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context)48     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
49         new Marker(lirGenRes.getLIR(), null).build();
50     }
51 
52     private static final class Marker extends LocationMarker<Marker.BasePointersSet> {
53 
54         private final class BasePointersSet extends ValueSet<Marker.BasePointersSet> {
55 
56             private final IndexedValueMap variables;
57 
BasePointersSet()58             BasePointersSet() {
59                 variables = new IndexedValueMap();
60             }
61 
BasePointersSet(BasePointersSet s)62             private BasePointersSet(BasePointersSet s) {
63                 variables = new IndexedValueMap(s.variables);
64             }
65 
66             @Override
copy()67             public Marker.BasePointersSet copy() {
68                 return new BasePointersSet(this);
69             }
70 
71             @Override
put(Value v)72             public void put(Value v) {
73                 Variable base = (Variable) v.getValueKind(LIRKind.class).getDerivedReferenceBase();
74                 assert !base.getValueKind(LIRKind.class).isValue();
75                 variables.put(base.index, base);
76             }
77 
78             @Override
putAll(BasePointersSet v)79             public void putAll(BasePointersSet v) {
80                 variables.putAll(v.variables);
81             }
82 
83             @Override
remove(Value v)84             public void remove(Value v) {
85                 Variable base = (Variable) v.getValueKind(LIRKind.class).getDerivedReferenceBase();
86                 assert !base.getValueKind(LIRKind.class).isValue();
87                 variables.put(base.index, null);
88             }
89 
90             @Override
equals(Object obj)91             public boolean equals(Object obj) {
92                 if (obj instanceof Marker.BasePointersSet) {
93                     BasePointersSet other = (BasePointersSet) obj;
94                     return variables.equals(other.variables);
95                 } else {
96                     return false;
97                 }
98             }
99 
100             @Override
hashCode()101             public int hashCode() {
102                 throw new UnsupportedOperationException();
103             }
104         }
105 
Marker(LIR lir, FrameMap frameMap)106         private Marker(LIR lir, FrameMap frameMap) {
107             super(lir, frameMap);
108         }
109 
110         @Override
newLiveValueSet()111         protected Marker.BasePointersSet newLiveValueSet() {
112             return new BasePointersSet();
113         }
114 
115         @Override
shouldProcessValue(Value operand)116         protected boolean shouldProcessValue(Value operand) {
117             ValueKind<?> kind = operand.getValueKind();
118             if (kind instanceof LIRKind) {
119                 return ((LIRKind) kind).isDerivedReference();
120             } else {
121                 return false;
122             }
123         }
124 
125         @Override
processState(LIRInstruction op, LIRFrameState info, BasePointersSet values)126         protected void processState(LIRInstruction op, LIRFrameState info, BasePointersSet values) {
127             info.setLiveBasePointers(new IndexedValueMap(values.variables));
128         }
129     }
130 }
131