comparison graal/com.oracle.max.graal.compiler/src/com/sun/c1x/graph/MemoryMap.java @ 2872:0341b6424579

Project renaming.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 08 Jun 2011 08:42:25 +0200
parents graal/GraalCompiler/src/com/sun/c1x/graph/MemoryMap.java@16b9a8b5ad39
children
comparison
equal deleted inserted replaced
2871:d704eb526603 2872:0341b6424579
1 /*
2 * Copyright (c) 2009, 2010, 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 package com.sun.c1x.graph;
24
25 import static java.lang.reflect.Modifier.*;
26
27 import java.util.*;
28
29 import com.sun.c1x.ir.*;
30 import com.sun.cri.ri.*;
31
32 /**
33 * The {@code MemoryMap} class is an approximation of memory that is used redundant load and
34 * store elimination. In C1, tracking of fields of new objects' fields was precise,
35 * while tracking of other fields is managed at the offset granularity (i.e. a write of a field with offset
36 * {@code off} will "overwrite" all fields with the offset {@code off}. However, C1X distinguishes all
37 * loaded fields as separate locations. Static fields have just one location, while instance fields are
38 * tracked for at most one instance object. Loads or stores of unloaded fields kill all memory locations.
39 * An object is no longer "new" if it is stored into a field or array.
40 *
41 * @author Ben L. Titzer
42 */
43 public class MemoryMap {
44
45 private final HashMap<RiField, Value> objectMap = new HashMap<RiField, Value>();
46 private final HashMap<RiField, Value> valueMap = new HashMap<RiField, Value>();
47 private final IdentityHashMap<Value, Value> newObjects = new IdentityHashMap<Value, Value>();
48
49 /**
50 * Kills all memory locations.
51 */
52 public void kill() {
53 objectMap.clear();
54 valueMap.clear();
55 newObjects.clear();
56 }
57
58 /**
59 * The specified instruction has just escaped, it can no longer be considered a "new object".
60 * @param x the instruction that just escaped
61 */
62 public void storeValue(Value x) {
63 newObjects.remove(x);
64 }
65
66 /**
67 * Record a newly allocated object.
68 * @param n the instruction generating the new object
69 */
70 public void newInstance(NewInstance n) {
71 newObjects.put(n, n);
72 }
73
74 /**
75 * Look up a load for load elimination, and put this load into the load elimination map.
76 * @param load the instruction representing the load
77 * @return a reference to the previous instruction that already loaded the value, if it is available; the
78 * {@code load} parameter otherwise
79 */
80 public Value load(LoadField load) {
81 if (!load.isLoaded()) {
82 // the field is not loaded, kill everything, because it will need to be resolved
83 kill();
84 return load;
85 }
86 RiField field = load.field();
87 if (load.isStatic()) {
88 // the field is static, look in the static map
89 Value r = valueMap.get(field);
90 if (r != null) {
91 return r;
92 }
93 valueMap.put(field, load);
94 } else {
95 // see if the value for this object for this field is in the map
96 if (objectMap.get(field) == load.object()) {
97 return valueMap.get(field);
98 }
99 objectMap.put(field, load.object());
100 valueMap.put(field, load);
101 }
102
103 return load; // load cannot be eliminated
104 }
105
106 /**
107 * Insert a new result for a load into the memory map.
108 * @param load the load instruction
109 * @param result the result that the load instruction should produce
110 */
111 public void setResult(LoadField load, Value result) {
112 if (load.isLoaded()) {
113 RiField field = load.field();
114 if (load.isStatic()) {
115 // the field is static, put it in the static map
116 valueMap.put(field, result);
117 } else {
118 // put the result for the loaded object into the map
119 objectMap.put(field, load.object());
120 valueMap.put(field, result);
121 }
122 }
123 }
124
125 /**
126 * Look up a store for store elimination, and put this store into the load elimination map.
127 * @param store the store instruction to put into the map
128 * @return {@code null} if the store operation is redundant; the {@code store} parameter
129 * otherwise
130 */
131 public StoreField store(StoreField store) {
132 if (!store.isLoaded()) {
133 // the field is not loaded, kill everything, because it will need to be resolved
134 kill();
135 return store;
136 }
137 RiField field = store.field();
138 Value value = store.value();
139 if (store.isStatic()) {
140 // the field is static, overwrite it into the static map
141 valueMap.put(field, value);
142 } else {
143 if (newObjects.containsKey(store.object())) {
144 // this is a store to a new object's field
145 if (fieldHasNoStores(field) && value.isConstant() && value.asConstant().isDefaultValue()) {
146 // this is a redundant initialization of a new object's field that has not been assigned to
147 return null;
148 }
149 }
150 Value obj = objectMap.get(field);
151 if (obj == store.object()) {
152 // is this a redundant store?
153 if (value == valueMap.get(field) && !isVolatile(field.accessFlags())) {
154 return null;
155 }
156 }
157 objectMap.put(field, store.object());
158 valueMap.put(field, value);
159 }
160 storeValue(value); // the value stored just escaped
161 return store; // the store cannot be eliminated
162 }
163
164 private boolean fieldHasNoStores(RiField field) {
165 return objectMap.get(field) == null;
166 }
167 }