comparison graal/GraalCompiler/src/com/sun/c1x/ir/Merge.java @ 2795:0fd105ff30f1

Add merge
author Gilles Duboscq <gilles.duboscq@oracle.com>
date Fri, 27 May 2011 11:29:28 +0200
parents
children e1dad0edd57a e3a0630a1dab 530366123e46
comparison
equal deleted inserted replaced
2794:bec08f6d56ad 2795:0fd105ff30f1
1 /*
2 * Copyright (c) 2009, 2011, 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 com.oracle.graal.graph.*;
26 import com.sun.c1x.debug.*;
27 import com.sun.c1x.util.*;
28 import com.sun.c1x.value.*;
29 import com.sun.cri.ci.*;
30
31 /**
32 * Denotes the beginning of a basic block, and holds information
33 * about the basic block, including the successor and
34 * predecessor blocks, exception handlers, liveness information, etc.
35 */
36 public final class Merge extends StateSplit {
37
38 private static final int INPUT_COUNT = 0;
39
40 private static final int SUCCESSOR_COUNT = 0;
41
42 @Override
43 protected int inputCount() {
44 return super.inputCount() + INPUT_COUNT;
45 }
46
47 @Override
48 protected int successorCount() {
49 return super.successorCount() + SUCCESSOR_COUNT;
50 }
51
52 @Override
53 public boolean needsStateAfter() {
54 return false;
55 }
56
57 public final boolean isLoopHeader;
58
59 /**
60 * Index of bytecode that generated this node when appended in a basic block.
61 * Negative values indicate special cases.
62 */
63 private int bci;
64
65 /**
66 * Constructs a new Merge at the specified bytecode index.
67 * @param bci the bytecode index of the start
68 * @param blockID the ID of the block
69 * @param graph
70 */
71 public Merge(int bci, boolean isLoopHeader, Graph graph) {
72 super(CiKind.Illegal, INPUT_COUNT, SUCCESSOR_COUNT, graph);
73 this.bci = bci;
74 this.isLoopHeader = isLoopHeader;
75 }
76
77 /**
78 * Gets the bytecode index of this instruction.
79 * @return the bytecode index of this instruction
80 */
81 public int bci() {
82 return bci;
83 }
84
85 @Override
86 public void accept(ValueVisitor v) {
87 v.visitMerge(this);
88 }
89
90 @Override
91 public String toString() {
92 StringBuilder builder = new StringBuilder();
93 builder.append("merge #");
94 builder.append(id());
95 builder.append(" [");
96
97 builder.append("]");
98 //if (end() != null) {
99 builder.append(" -> ");
100 boolean hasSucc = false;
101 for (Node s : this.successors()) {
102 if (hasSucc) {
103 builder.append(", ");
104 }
105 builder.append("#");
106 builder.append(s.id());
107 hasSucc = true;
108 }
109 //}
110 return builder.toString();
111 }
112
113 public void printWithoutPhis(LogStream out) {
114 // print block id
115 out.print("B").print(id()).print(" ");
116
117 // print flags
118 StringBuilder sb = new StringBuilder(8);
119 if (sb.length() != 0) {
120 out.print('(').print(sb.toString()).print(')');
121 }
122
123 // print block bci range
124 out.print('[').print(-1).print(", ").print(-1).print(']');
125
126 // print block successors
127 //if (end != null && end.blockSuccessors().size() > 0) {
128 out.print(" .");
129 for (Node successor : this.successors()) {
130 if (successor instanceof Value) {
131 out.print((Value) successor);
132 } else {
133 out.print(successor.toString());
134 }
135 }
136 //}
137
138 // print predecessors
139 if (!blockPredecessors().isEmpty()) {
140 out.print(" pred:");
141 for (Instruction pred : blockPredecessors()) {
142 out.print(pred.block());
143 }
144 }
145 }
146
147 @Override
148 public void print(LogStream out) {
149
150 printWithoutPhis(out);
151
152 // print phi functions
153 boolean hasPhisInLocals = false;
154 boolean hasPhisOnStack = false;
155
156 //if (end() != null && end().stateAfter() != null) {
157 FrameState state = stateBefore();
158
159 int i = 0;
160 while (!hasPhisOnStack && i < state.stackSize()) {
161 Value value = state.stackAt(i);
162 hasPhisOnStack = isPhiAtBlock(value);
163 if (value != null && !value.isIllegal()) {
164 i += value.kind.sizeInSlots();
165 } else {
166 i++;
167 }
168 }
169
170 for (i = 0; !hasPhisInLocals && i < state.localsSize();) {
171 Value value = state.localAt(i);
172 hasPhisInLocals = isPhiAtBlock(value);
173 // also ignore illegal HiWords
174 if (value != null && !value.isIllegal()) {
175 i += value.kind.sizeInSlots();
176 } else {
177 i++;
178 }
179 }
180 //}
181
182 // print values in locals
183 if (hasPhisInLocals) {
184 out.println();
185 out.println("Locals:");
186
187 int j = 0;
188 while (j < state.localsSize()) {
189 Value value = state.localAt(j);
190 if (value != null) {
191 out.println(stateString(j, value));
192 // also ignore illegal HiWords
193 j += value.isIllegal() ? 1 : value.kind.sizeInSlots();
194 } else {
195 j++;
196 }
197 }
198 out.println();
199 }
200
201 // print values on stack
202 if (hasPhisOnStack) {
203 out.println();
204 out.println("Stack:");
205 int j = 0;
206 while (j < stateBefore().stackSize()) {
207 Value value = stateBefore().stackAt(j);
208 if (value != null) {
209 out.println(stateString(j, value));
210 j += value.kind.sizeInSlots();
211 } else {
212 j++;
213 }
214 }
215 }
216
217 }
218
219 /**
220 * Determines if a given instruction is a phi whose {@linkplain Phi#block() join block} is a given block.
221 *
222 * @param value the instruction to test
223 * @param block the block that may be the join block of {@code value} if {@code value} is a phi
224 * @return {@code true} if {@code value} is a phi and its join block is {@code block}
225 */
226 private boolean isPhiAtBlock(Value value) {
227 return value instanceof Phi && ((Phi) value).block() == this;
228 }
229
230
231 /**
232 * Formats a given instruction as a value in a {@linkplain FrameState frame state}. If the instruction is a phi defined at a given
233 * block, its {@linkplain Phi#valueCount() inputs} are appended to the returned string.
234 *
235 * @param index the index of the value in the frame state
236 * @param value the frame state value
237 * @param block if {@code value} is a phi, then its inputs are formatted if {@code block} is its
238 * {@linkplain Phi#block() join point}
239 * @return the instruction representation as a string
240 */
241 public String stateString(int index, Value value) {
242 StringBuilder sb = new StringBuilder(30);
243 sb.append(String.format("%2d %s", index, Util.valueString(value)));
244 if (value instanceof Phi) {
245 Phi phi = (Phi) value;
246 // print phi operands
247 if (phi.block() == this) {
248 sb.append(" [");
249 for (int j = 0; j < phi.valueCount(); j++) {
250 sb.append(' ');
251 Value operand = phi.valueAt(j);
252 if (operand != null) {
253 sb.append(Util.valueString(operand));
254 } else {
255 sb.append("NULL");
256 }
257 }
258 sb.append("] ");
259 }
260 }
261 if (value != null && value.hasSubst()) {
262 sb.append("alias ").append(Util.valueString(value.subst()));
263 }
264 return sb.toString();
265 }
266
267 @Override
268 public String shortName() {
269 return "Merge #" + id();
270 }
271 }