comparison graal/GraalCompiler/src/com/sun/c1x/ir/Instruction.java @ 2509:16b9a8b5ad39

Renamings Runtime=>GraalRuntime and Compiler=>GraalCompiler
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 11:50:44 +0200
parents graal/Compiler/src/com/sun/c1x/ir/Instruction.java@9ec15d6914ca
children 0f9eeb15e636
comparison
equal deleted inserted replaced
2508:fea94949e0a2 2509:16b9a8b5ad39
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.ir;
24
25 import java.util.*;
26
27 import com.sun.c1x.*;
28 import com.sun.c1x.value.*;
29 import com.sun.cri.ci.*;
30
31 /**
32 * Denotes an instruction node in the IR, which is a {@link Value} that
33 * can be added to a basic block (whereas other {@link Value} nodes such as {@link Phi} and
34 * {@link Local} cannot be added to basic blocks).
35 *
36 * Subclasses of instruction represent arithmetic and object operations,
37 * control flow operators, phi statements, method calls, the start of basic blocks, and
38 * the end of basic blocks.
39 *
40 * Instruction nodes are chained together in a basic block through the embedded
41 * {@link Instruction#next} field. An Instruction may also have a list of {@link ExceptionHandler}s.
42 *
43 *
44 * @author Ben L. Titzer
45 */
46 public abstract class Instruction extends Value {
47
48 public static final int INVOCATION_ENTRY_BCI = -1; // XXX: not currently used
49 public static final int SYNCHRONIZATION_ENTRY_BCI = -1;
50
51 /**
52 * Index of bytecode that generated this node when appended in a basic block.
53 * Negative values indicate special cases.
54 */
55 private int bci;
56
57 /**
58 * Links to next instruction in a basic block or to {@code} itself if not in a block.
59 */
60 private Instruction next = this;
61
62 /**
63 * List of associated exception handlers.
64 */
65 private List<ExceptionHandler> exceptionHandlers = ExceptionHandler.ZERO_HANDLERS;
66
67 /**
68 * Constructs a new instruction with the specified value type.
69 * @param kind the value type for this instruction
70 */
71 public Instruction(CiKind kind) {
72 super(kind);
73 C1XMetrics.HIRInstructions++;
74 }
75
76 /**
77 * Gets the bytecode index of this instruction.
78 * @return the bytecode index of this instruction
79 */
80 public final int bci() {
81 return bci;
82 }
83
84 /**
85 * Sets the bytecode index of this instruction.
86 * @param bci the new bytecode index for this instruction
87 */
88 public final void setBCI(int bci) {
89 assert bci >= 0 || bci == SYNCHRONIZATION_ENTRY_BCI;
90 this.bci = bci;
91 }
92
93 /**
94 * Checks whether this instruction has already been added to its basic block.
95 * @return {@code true} if this instruction has been added to the basic block containing it
96 */
97 public final boolean isAppended() {
98 return next != this;
99 }
100
101 /**
102 * Gets the next instruction after this one in the basic block, or {@code null}
103 * if this instruction is the end of a basic block.
104 * @return the next instruction after this one in the basic block
105 */
106 public final Instruction next() {
107 if (next == this) {
108 return null;
109 }
110 return next;
111 }
112
113 /**
114 * Sets the next instruction for this instruction. Note that it is illegal to
115 * set the next field of a phi, block end, or local instruction.
116 * @param next the next instruction
117 * @param bci the bytecode index of the next instruction
118 * @return the new next instruction
119 */
120 public final Instruction setNext(Instruction next, int bci) {
121 this.next = next;
122 if (next != null) {
123 assert !(this instanceof BlockEnd);
124 next.setBCI(bci);
125 if (next.next == next) {
126 next.next = null;
127 }
128 }
129 return next;
130 }
131
132 /**
133 * Re-sets the next instruction for this instruction. Note that it is illegal to
134 * set the next field of a phi, block end, or local instruction.
135 * @param next the next instruction
136 * @return the new next instruction
137 */
138 public final Instruction resetNext(Instruction next) {
139 if (next != null) {
140 assert !(this instanceof BlockEnd);
141 this.next = next;
142 }
143 return next;
144 }
145
146 /**
147 * Gets the instruction preceding this instruction in the specified basic block.
148 * Note that instructions do not directly refer to their previous instructions,
149 * and therefore this operation much search from the beginning of the basic
150 * block, thereby requiring time linear in the size of the basic block in the worst
151 * case. Use with caution!
152 * @param block the basic block that contains this instruction
153 * @return the instruction before this instruction in the basic block
154 */
155 public final Instruction prev(BlockBegin block) {
156 Instruction p = null;
157 Instruction q = block;
158 while (q != this) {
159 assert q != null : "this instruction is not in the specified basic block";
160 p = q;
161 q = q.next();
162 }
163 return p;
164 }
165
166 @Override
167 public BlockBegin block() {
168 // TODO(tw): Make this more efficient.
169 Instruction cur = this;
170 while (!(cur instanceof BlockEnd)) {
171 cur = cur.next;
172 }
173 return ((BlockEnd) cur).begin;
174 }
175
176 /**
177 * Gets the list of exception handlers associated with this instruction.
178 * @return the list of exception handlers for this instruction
179 */
180 public final List<ExceptionHandler> exceptionHandlers() {
181 return exceptionHandlers;
182 }
183
184 /**
185 * Sets the list of exception handlers for this instruction.
186 * @param exceptionHandlers the exception handlers
187 */
188 public final void setExceptionHandlers(List<ExceptionHandler> exceptionHandlers) {
189 this.exceptionHandlers = exceptionHandlers;
190 }
191
192 /**
193 * Compute the value number of this Instruction. Local and global value numbering
194 * optimizations use a hash map, and the value number provides a hash code.
195 * If the instruction cannot be value numbered, then this method should return
196 * {@code 0}.
197 * @return the hashcode of this instruction
198 */
199 public int valueNumber() {
200 return 0;
201 }
202
203 /**
204 * Checks that this instruction is equal to another instruction for the purposes
205 * of value numbering.
206 * @param i the other instruction
207 * @return {@code true} if this instruction is equivalent to the specified
208 * instruction w.r.t. value numbering
209 */
210 public boolean valueEqual(Instruction i) {
211 return false;
212 }
213
214 /**
215 * Gets the name of this instruction as a string.
216 * @return the name of this instruction
217 */
218 public final String name() {
219 return getClass().getSimpleName();
220 }
221
222 /**
223 * Tests whether this instruction can trap. This is conservative; it does not take
224 * into account analysis results that may eliminate the possibility of this
225 * instruction from trapping.
226 *
227 * @return {@code true} if this instruction can cause a trap.
228 */
229 public boolean canTrap() {
230 return false;
231 }
232
233 /**
234 * Apply the specified closure to all the values of this instruction, including
235 * input values, state values, and other values.
236 * @param closure the closure to apply
237 */
238 public final void allValuesDo(ValueClosure closure) {
239 inputValuesDo(closure);
240 FrameState stateBefore = stateBefore();
241 if (stateBefore != null) {
242 stateBefore.valuesDo(closure);
243 }
244 FrameState stateAfter = stateAfter();
245 if (stateAfter != null) {
246 stateAfter.valuesDo(closure);
247 }
248 }
249
250 /**
251 * Gets the state before the instruction, if it is recorded.
252 * @return the state before the instruction
253 */
254 public FrameState stateBefore() {
255 return null;
256 }
257
258 /**
259 * Gets the state after the instruction, if it is recorded. Typically only
260 * instances of {@link BlockEnd} have a non-null state after.
261 * @return the state after the instruction
262 */
263 public FrameState stateAfter() {
264 return null;
265 }
266 }