Mercurial > hg > truffle
annotate src/share/vm/c1/c1_Canonicalizer.cpp @ 1579:e9ff18c4ace7
Merge
author | jrose |
---|---|
date | Wed, 02 Jun 2010 22:45:42 -0700 |
parents | c18cbe5936b8 |
children | b812ff5abc73 |
rev | line source |
---|---|
0 | 1 /* |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
diff
changeset
|
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 #include "incls/_precompiled.incl" | |
26 #include "incls/_c1_Canonicalizer.cpp.incl" | |
27 | |
28 | |
29 static void do_print_value(Value* vp) { | |
30 (*vp)->print_line(); | |
31 } | |
32 | |
33 void Canonicalizer::set_canonical(Value x) { | |
34 assert(x != NULL, "value must exist"); | |
35 // Note: we can not currently substitute root nodes which show up in | |
36 // the instruction stream (because the instruction list is embedded | |
37 // in the instructions). | |
38 if (canonical() != x) { | |
39 if (PrintCanonicalization) { | |
40 canonical()->input_values_do(do_print_value); | |
41 canonical()->print_line(); | |
42 tty->print_cr("canonicalized to:"); | |
43 x->input_values_do(do_print_value); | |
44 x->print_line(); | |
45 tty->cr(); | |
46 } | |
47 assert(_canonical->type()->tag() == x->type()->tag(), "types must match"); | |
48 _canonical = x; | |
49 } | |
50 } | |
51 | |
52 | |
53 void Canonicalizer::move_const_to_right(Op2* x) { | |
54 if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands(); | |
55 } | |
56 | |
57 | |
58 void Canonicalizer::do_Op2(Op2* x) { | |
59 if (x->x() == x->y()) { | |
60 switch (x->op()) { | |
61 case Bytecodes::_isub: set_constant(0); return; | |
62 case Bytecodes::_lsub: set_constant(jlong_cast(0)); return; | |
63 case Bytecodes::_iand: // fall through | |
64 case Bytecodes::_land: // fall through | |
65 case Bytecodes::_ior: // fall through | |
66 case Bytecodes::_lor : set_canonical(x->x()); return; | |
67 case Bytecodes::_ixor: set_constant(0); return; | |
68 case Bytecodes::_lxor: set_constant(jlong_cast(0)); return; | |
69 } | |
70 } | |
71 | |
72 if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { | |
73 // do constant folding for selected operations | |
74 switch (x->type()->tag()) { | |
75 case intTag: | |
76 { jint a = x->x()->type()->as_IntConstant()->value(); | |
77 jint b = x->y()->type()->as_IntConstant()->value(); | |
78 switch (x->op()) { | |
79 case Bytecodes::_iadd: set_constant(a + b); return; | |
80 case Bytecodes::_isub: set_constant(a - b); return; | |
81 case Bytecodes::_imul: set_constant(a * b); return; | |
82 case Bytecodes::_idiv: | |
83 if (b != 0) { | |
84 if (a == min_jint && b == -1) { | |
85 set_constant(min_jint); | |
86 } else { | |
87 set_constant(a / b); | |
88 } | |
89 return; | |
90 } | |
91 break; | |
92 case Bytecodes::_irem: | |
93 if (b != 0) { | |
94 if (a == min_jint && b == -1) { | |
95 set_constant(0); | |
96 } else { | |
97 set_constant(a % b); | |
98 } | |
99 return; | |
100 } | |
101 break; | |
102 case Bytecodes::_iand: set_constant(a & b); return; | |
103 case Bytecodes::_ior : set_constant(a | b); return; | |
104 case Bytecodes::_ixor: set_constant(a ^ b); return; | |
105 } | |
106 } | |
107 break; | |
108 case longTag: | |
109 { jlong a = x->x()->type()->as_LongConstant()->value(); | |
110 jlong b = x->y()->type()->as_LongConstant()->value(); | |
111 switch (x->op()) { | |
112 case Bytecodes::_ladd: set_constant(a + b); return; | |
113 case Bytecodes::_lsub: set_constant(a - b); return; | |
114 case Bytecodes::_lmul: set_constant(a * b); return; | |
115 case Bytecodes::_ldiv: | |
116 if (b != 0) { | |
117 set_constant(SharedRuntime::ldiv(b, a)); | |
118 return; | |
119 } | |
120 break; | |
121 case Bytecodes::_lrem: | |
122 if (b != 0) { | |
123 set_constant(SharedRuntime::lrem(b, a)); | |
124 return; | |
125 } | |
126 break; | |
127 case Bytecodes::_land: set_constant(a & b); return; | |
128 case Bytecodes::_lor : set_constant(a | b); return; | |
129 case Bytecodes::_lxor: set_constant(a ^ b); return; | |
130 } | |
131 } | |
132 break; | |
133 // other cases not implemented (must be extremely careful with floats & doubles!) | |
134 } | |
135 } | |
136 // make sure constant is on the right side, if any | |
137 move_const_to_right(x); | |
138 | |
139 if (x->y()->type()->is_constant()) { | |
140 // do constant folding for selected operations | |
141 switch (x->type()->tag()) { | |
142 case intTag: | |
143 if (x->y()->type()->as_IntConstant()->value() == 0) { | |
144 switch (x->op()) { | |
145 case Bytecodes::_iadd: set_canonical(x->x()); return; | |
146 case Bytecodes::_isub: set_canonical(x->x()); return; | |
147 case Bytecodes::_imul: set_constant(0); return; | |
148 // Note: for div and rem, make sure that C semantics | |
149 // corresponds to Java semantics! | |
150 case Bytecodes::_iand: set_constant(0); return; | |
151 case Bytecodes::_ior : set_canonical(x->x()); return; | |
152 } | |
153 } | |
154 break; | |
155 case longTag: | |
156 if (x->y()->type()->as_LongConstant()->value() == (jlong)0) { | |
157 switch (x->op()) { | |
158 case Bytecodes::_ladd: set_canonical(x->x()); return; | |
159 case Bytecodes::_lsub: set_canonical(x->x()); return; | |
160 case Bytecodes::_lmul: set_constant((jlong)0); return; | |
161 // Note: for div and rem, make sure that C semantics | |
162 // corresponds to Java semantics! | |
163 case Bytecodes::_land: set_constant((jlong)0); return; | |
164 case Bytecodes::_lor : set_canonical(x->x()); return; | |
165 } | |
166 } | |
167 break; | |
168 } | |
169 } | |
170 } | |
171 | |
172 | |
173 void Canonicalizer::do_Phi (Phi* x) {} | |
174 void Canonicalizer::do_Constant (Constant* x) {} | |
175 void Canonicalizer::do_Local (Local* x) {} | |
176 void Canonicalizer::do_LoadField (LoadField* x) {} | |
177 | |
178 // checks if v is in the block that is currently processed by | |
179 // GraphBuilder. This is the only block that has not BlockEnd yet. | |
180 static bool in_current_block(Value v) { | |
181 int max_distance = 4; | |
182 while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) { | |
183 v = v->next(); | |
184 max_distance--; | |
185 } | |
186 return v == NULL; | |
187 } | |
188 | |
189 void Canonicalizer::do_StoreField (StoreField* x) { | |
190 // If a value is going to be stored into a field or array some of | |
191 // the conversions emitted by javac are unneeded because the fields | |
192 // are packed to their natural size. | |
193 Convert* conv = x->value()->as_Convert(); | |
194 if (conv) { | |
195 Value value = NULL; | |
196 BasicType type = x->field()->type()->basic_type(); | |
197 switch (conv->op()) { | |
198 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; | |
199 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; | |
200 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; | |
201 } | |
202 // limit this optimization to current block | |
203 if (value != NULL && in_current_block(conv)) { | |
204 set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(), | |
205 x->lock_stack(), x->state_before(), x->is_loaded(), x->is_initialized())); | |
206 return; | |
207 } | |
208 } | |
209 | |
210 } | |
211 | |
212 void Canonicalizer::do_ArrayLength (ArrayLength* x) { | |
213 NewArray* array = x->array()->as_NewArray(); | |
214 if (array != NULL && array->length() != NULL) { | |
215 Constant* length = array->length()->as_Constant(); | |
216 if (length != NULL) { | |
217 // do not use the Constant itself, but create a new Constant | |
218 // with same value Otherwise a Constant is live over multiple | |
219 // blocks without being registered in a state array. | |
220 assert(length->type()->as_IntConstant() != NULL, "array length must be integer"); | |
221 set_constant(length->type()->as_IntConstant()->value()); | |
222 } | |
223 } else { | |
224 LoadField* lf = x->array()->as_LoadField(); | |
1295 | 225 if (lf != NULL) { |
226 ciField* field = lf->field(); | |
227 if (field->is_constant() && field->is_static()) { | |
228 // final static field | |
229 ciObject* c = field->constant_value().as_object(); | |
230 if (c->is_array()) { | |
231 ciArray* array = (ciArray*) c; | |
232 set_constant(array->length()); | |
233 } | |
0 | 234 } |
235 } | |
236 } | |
237 } | |
238 | |
239 void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {} | |
240 void Canonicalizer::do_StoreIndexed (StoreIndexed* x) { | |
241 // If a value is going to be stored into a field or array some of | |
242 // the conversions emitted by javac are unneeded because the fields | |
243 // are packed to their natural size. | |
244 Convert* conv = x->value()->as_Convert(); | |
245 if (conv) { | |
246 Value value = NULL; | |
247 BasicType type = x->elt_type(); | |
248 switch (conv->op()) { | |
249 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; | |
250 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; | |
251 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; | |
252 } | |
253 // limit this optimization to current block | |
254 if (value != NULL && in_current_block(conv)) { | |
255 set_canonical(new StoreIndexed(x->array(), x->index(), x->length(), | |
256 x->elt_type(), value, x->lock_stack())); | |
257 return; | |
258 } | |
259 } | |
260 | |
261 | |
262 } | |
263 | |
264 | |
265 void Canonicalizer::do_NegateOp(NegateOp* x) { | |
266 ValueType* t = x->x()->type(); | |
267 if (t->is_constant()) { | |
268 switch (t->tag()) { | |
269 case intTag : set_constant(-t->as_IntConstant ()->value()); return; | |
270 case longTag : set_constant(-t->as_LongConstant ()->value()); return; | |
271 case floatTag : set_constant(-t->as_FloatConstant ()->value()); return; | |
272 case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return; | |
273 default : ShouldNotReachHere(); | |
274 } | |
275 } | |
276 } | |
277 | |
278 | |
279 void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); } | |
280 | |
281 | |
282 void Canonicalizer::do_ShiftOp (ShiftOp* x) { | |
283 ValueType* t = x->x()->type(); | |
284 ValueType* t2 = x->y()->type(); | |
285 if (t->is_constant()) { | |
286 switch (t->tag()) { | |
287 case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break; | |
288 case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break; | |
289 default : ShouldNotReachHere(); | |
290 } | |
291 if (t2->is_constant()) { | |
292 if (t->tag() == intTag) { | |
293 int value = t->as_IntConstant()->value(); | |
294 int shift = t2->as_IntConstant()->value() & 31; | |
295 jint mask = ~(~0 << (32 - shift)); | |
296 if (shift == 0) mask = ~0; | |
297 switch (x->op()) { | |
298 case Bytecodes::_ishl: set_constant(value << shift); return; | |
299 case Bytecodes::_ishr: set_constant(value >> shift); return; | |
300 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return; | |
301 } | |
302 } else if (t->tag() == longTag) { | |
303 jlong value = t->as_LongConstant()->value(); | |
304 int shift = t2->as_IntConstant()->value() & 63; | |
305 jlong mask = ~(~jlong_cast(0) << (64 - shift)); | |
306 if (shift == 0) mask = ~jlong_cast(0); | |
307 switch (x->op()) { | |
308 case Bytecodes::_lshl: set_constant(value << shift); return; | |
309 case Bytecodes::_lshr: set_constant(value >> shift); return; | |
310 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return; | |
311 } | |
312 } | |
313 } | |
314 } | |
315 if (t2->is_constant()) { | |
316 switch (t2->tag()) { | |
317 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; | |
318 case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; | |
319 default : ShouldNotReachHere(); | |
320 } | |
321 } | |
322 } | |
323 | |
324 | |
325 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); } | |
326 void Canonicalizer::do_CompareOp (CompareOp* x) { | |
327 if (x->x() == x->y()) { | |
328 switch (x->x()->type()->tag()) { | |
329 case longTag: set_constant(0); break; | |
330 case floatTag: { | |
331 FloatConstant* fc = x->x()->type()->as_FloatConstant(); | |
332 if (fc) { | |
333 if (g_isnan(fc->value())) { | |
334 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); | |
335 } else { | |
336 set_constant(0); | |
337 } | |
338 } | |
339 break; | |
340 } | |
341 case doubleTag: { | |
342 DoubleConstant* dc = x->x()->type()->as_DoubleConstant(); | |
343 if (dc) { | |
344 if (g_isnan(dc->value())) { | |
345 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); | |
346 } else { | |
347 set_constant(0); | |
348 } | |
349 } | |
350 break; | |
351 } | |
352 } | |
353 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { | |
354 switch (x->x()->type()->tag()) { | |
355 case longTag: { | |
356 jlong vx = x->x()->type()->as_LongConstant()->value(); | |
357 jlong vy = x->y()->type()->as_LongConstant()->value(); | |
358 if (vx == vy) | |
359 set_constant(0); | |
360 else if (vx < vy) | |
361 set_constant(-1); | |
362 else | |
363 set_constant(1); | |
364 break; | |
365 } | |
366 | |
367 case floatTag: { | |
368 float vx = x->x()->type()->as_FloatConstant()->value(); | |
369 float vy = x->y()->type()->as_FloatConstant()->value(); | |
370 if (g_isnan(vx) || g_isnan(vy)) | |
371 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); | |
372 else if (vx == vy) | |
373 set_constant(0); | |
374 else if (vx < vy) | |
375 set_constant(-1); | |
376 else | |
377 set_constant(1); | |
378 break; | |
379 } | |
380 | |
381 case doubleTag: { | |
382 double vx = x->x()->type()->as_DoubleConstant()->value(); | |
383 double vy = x->y()->type()->as_DoubleConstant()->value(); | |
384 if (g_isnan(vx) || g_isnan(vy)) | |
385 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); | |
386 else if (vx == vy) | |
387 set_constant(0); | |
388 else if (vx < vy) | |
389 set_constant(-1); | |
390 else | |
391 set_constant(1); | |
392 break; | |
393 } | |
394 } | |
395 | |
396 } | |
397 } | |
398 | |
399 | |
400 void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {} | |
401 | |
402 void Canonicalizer::do_IfOp(IfOp* x) { | |
403 // Caution: do not use do_Op2(x) here for now since | |
404 // we map the condition to the op for now! | |
405 move_const_to_right(x); | |
406 } | |
407 | |
408 | |
409 void Canonicalizer::do_Intrinsic (Intrinsic* x) { | |
410 switch (x->id()) { | |
411 case vmIntrinsics::_floatToRawIntBits : { | |
412 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant(); | |
413 if (c != NULL) { | |
414 JavaValue v; | |
415 v.set_jfloat(c->value()); | |
416 set_constant(v.get_jint()); | |
417 } | |
418 break; | |
419 } | |
420 case vmIntrinsics::_intBitsToFloat : { | |
421 IntConstant* c = x->argument_at(0)->type()->as_IntConstant(); | |
422 if (c != NULL) { | |
423 JavaValue v; | |
424 v.set_jint(c->value()); | |
425 set_constant(v.get_jfloat()); | |
426 } | |
427 break; | |
428 } | |
429 case vmIntrinsics::_doubleToRawLongBits : { | |
430 DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant(); | |
431 if (c != NULL) { | |
432 JavaValue v; | |
433 v.set_jdouble(c->value()); | |
434 set_constant(v.get_jlong()); | |
435 } | |
436 break; | |
437 } | |
438 case vmIntrinsics::_longBitsToDouble : { | |
439 LongConstant* c = x->argument_at(0)->type()->as_LongConstant(); | |
440 if (c != NULL) { | |
441 JavaValue v; | |
442 v.set_jlong(c->value()); | |
443 set_constant(v.get_jdouble()); | |
444 } | |
445 break; | |
446 } | |
447 } | |
448 } | |
449 | |
450 void Canonicalizer::do_Convert (Convert* x) { | |
451 if (x->value()->type()->is_constant()) { | |
452 switch (x->op()) { | |
453 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break; | |
454 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break; | |
455 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break; | |
456 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break; | |
457 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break; | |
458 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break; | |
459 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break; | |
460 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break; | |
461 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break; | |
462 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break; | |
463 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break; | |
464 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break; | |
465 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break; | |
466 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break; | |
467 case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break; | |
468 default: | |
469 ShouldNotReachHere(); | |
470 } | |
471 } | |
472 | |
473 Value value = x->value(); | |
474 BasicType type = T_ILLEGAL; | |
475 LoadField* lf = value->as_LoadField(); | |
476 if (lf) { | |
477 type = lf->field_type(); | |
478 } else { | |
479 LoadIndexed* li = value->as_LoadIndexed(); | |
480 if (li) { | |
481 type = li->elt_type(); | |
482 } else { | |
483 Convert* conv = value->as_Convert(); | |
484 if (conv) { | |
485 switch (conv->op()) { | |
486 case Bytecodes::_i2b: type = T_BYTE; break; | |
487 case Bytecodes::_i2s: type = T_SHORT; break; | |
488 case Bytecodes::_i2c: type = T_CHAR; break; | |
489 } | |
490 } | |
491 } | |
492 } | |
493 if (type != T_ILLEGAL) { | |
494 switch (x->op()) { | |
495 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break; | |
496 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break; | |
497 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break; | |
498 } | |
499 } else { | |
500 Op2* op2 = x->value()->as_Op2(); | |
501 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) { | |
502 jint safebits = 0; | |
503 jint mask = op2->y()->type()->as_IntConstant()->value(); | |
504 switch (x->op()) { | |
505 case Bytecodes::_i2b: safebits = 0x7f; break; | |
506 case Bytecodes::_i2s: safebits = 0x7fff; break; | |
507 case Bytecodes::_i2c: safebits = 0xffff; break; | |
508 } | |
509 // When casting a masked integer to a smaller signed type, if | |
510 // the mask doesn't include the sign bit the cast isn't needed. | |
511 if (safebits && (mask & ~safebits) == 0) { | |
512 set_canonical(x->value()); | |
513 } | |
514 } | |
515 } | |
516 | |
517 } | |
518 | |
519 void Canonicalizer::do_NullCheck (NullCheck* x) { | |
520 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) { | |
521 set_canonical(x->obj()); | |
522 } else { | |
523 Constant* con = x->obj()->as_Constant(); | |
524 if (con) { | |
525 ObjectType* c = con->type()->as_ObjectType(); | |
526 if (c && c->is_loaded()) { | |
527 ObjectConstant* oc = c->as_ObjectConstant(); | |
528 if (!oc || !oc->value()->is_null_object()) { | |
529 set_canonical(con); | |
530 } | |
531 } | |
532 } | |
533 } | |
534 } | |
535 | |
536 void Canonicalizer::do_Invoke (Invoke* x) {} | |
537 void Canonicalizer::do_NewInstance (NewInstance* x) {} | |
538 void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {} | |
539 void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {} | |
540 void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {} | |
541 void Canonicalizer::do_CheckCast (CheckCast* x) { | |
542 if (x->klass()->is_loaded()) { | |
543 Value obj = x->obj(); | |
544 ciType* klass = obj->exact_type(); | |
545 if (klass == NULL) klass = obj->declared_type(); | |
546 if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) { | |
547 set_canonical(obj); | |
548 return; | |
549 } | |
550 // checkcast of null returns null | |
551 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { | |
552 set_canonical(obj); | |
553 } | |
554 } | |
555 } | |
556 void Canonicalizer::do_InstanceOf (InstanceOf* x) { | |
557 if (x->klass()->is_loaded()) { | |
558 Value obj = x->obj(); | |
559 ciType* exact = obj->exact_type(); | |
560 if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) { | |
561 set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0); | |
562 return; | |
563 } | |
564 // instanceof null returns false | |
565 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { | |
566 set_constant(0); | |
567 } | |
568 } | |
569 | |
570 } | |
571 void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {} | |
572 void Canonicalizer::do_MonitorExit (MonitorExit* x) {} | |
573 void Canonicalizer::do_BlockBegin (BlockBegin* x) {} | |
574 void Canonicalizer::do_Goto (Goto* x) {} | |
575 | |
576 | |
577 static bool is_true(jlong x, If::Condition cond, jlong y) { | |
578 switch (cond) { | |
579 case If::eql: return x == y; | |
580 case If::neq: return x != y; | |
581 case If::lss: return x < y; | |
582 case If::leq: return x <= y; | |
583 case If::gtr: return x > y; | |
584 case If::geq: return x >= y; | |
585 } | |
586 ShouldNotReachHere(); | |
587 return false; | |
588 } | |
589 | |
590 | |
591 void Canonicalizer::do_If(If* x) { | |
592 // move const to right | |
593 if (x->x()->type()->is_constant()) x->swap_operands(); | |
594 // simplify | |
595 const Value l = x->x(); ValueType* lt = l->type(); | |
596 const Value r = x->y(); ValueType* rt = r->type(); | |
597 | |
598 if (l == r && !lt->is_float_kind()) { | |
599 // pattern: If (a cond a) => simplify to Goto | |
600 BlockBegin* sux; | |
601 switch (x->cond()) { | |
602 case If::eql: sux = x->sux_for(true); break; | |
603 case If::neq: sux = x->sux_for(false); break; | |
604 case If::lss: sux = x->sux_for(false); break; | |
605 case If::leq: sux = x->sux_for(true); break; | |
606 case If::gtr: sux = x->sux_for(false); break; | |
607 case If::geq: sux = x->sux_for(true); break; | |
608 } | |
609 // If is a safepoint then the debug information should come from the state_before of the If. | |
610 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); | |
611 return; | |
612 } | |
613 | |
614 if (lt->is_constant() && rt->is_constant()) { | |
615 if (x->x()->as_Constant() != NULL) { | |
616 // pattern: If (lc cond rc) => simplify to: Goto | |
617 BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(), | |
618 x->sux_for(true), | |
619 x->sux_for(false)); | |
620 if (sux != NULL) { | |
621 // If is a safepoint then the debug information should come from the state_before of the If. | |
622 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); | |
623 } | |
624 } | |
625 } else if (rt->as_IntConstant() != NULL) { | |
626 // pattern: If (l cond rc) => investigate further | |
627 const jint rc = rt->as_IntConstant()->value(); | |
628 if (l->as_CompareOp() != NULL) { | |
629 // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto | |
630 CompareOp* cmp = l->as_CompareOp(); | |
631 bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl; | |
632 BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b | |
633 BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b | |
634 BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b | |
635 BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered | |
636 // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are | |
637 // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either | |
638 // lss_sux or gtr_sux. | |
639 if (lss_sux == eql_sux && eql_sux == gtr_sux) { | |
640 // all successors identical => simplify to: Goto | |
641 set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint())); | |
642 } else { | |
643 // two successors differ and two successors are the same => simplify to: If (x cmp y) | |
644 // determine new condition & successors | |
645 If::Condition cond; | |
646 BlockBegin* tsux = NULL; | |
647 BlockBegin* fsux = NULL; | |
648 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; } | |
649 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; } | |
650 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; } | |
651 else { ShouldNotReachHere(); } | |
652 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint()); | |
653 if (cmp->x() == cmp->y()) { | |
654 do_If(canon); | |
655 } else { | |
656 set_canonical(canon); | |
657 set_bci(cmp->bci()); | |
658 } | |
659 } | |
660 } else if (l->as_InstanceOf() != NULL) { | |
661 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf | |
662 // instruction in the graph (it is pinned). Need to fix this at some point. | |
663 return; | |
664 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto | |
665 InstanceOf* inst = l->as_InstanceOf(); | |
666 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1 | |
667 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0 | |
668 if (is_inst_sux == no_inst_sux && inst->is_loaded()) { | |
669 // both successors identical and klass is loaded => simplify to: Goto | |
670 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint())); | |
671 } else { | |
672 // successors differ => simplify to: IfInstanceOf | |
673 set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux)); | |
674 } | |
675 } | |
676 } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) { | |
677 if (x->cond() == Instruction::eql) { | |
678 set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint())); | |
679 } else { | |
680 assert(x->cond() == Instruction::neq, "only other valid case"); | |
681 set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint())); | |
682 } | |
683 } | |
684 } | |
685 | |
686 | |
687 void Canonicalizer::do_TableSwitch(TableSwitch* x) { | |
688 if (x->tag()->type()->is_constant()) { | |
689 int v = x->tag()->type()->as_IntConstant()->value(); | |
690 BlockBegin* sux = x->default_sux(); | |
691 if (v >= x->lo_key() && v <= x->hi_key()) { | |
692 sux = x->sux_at(v - x->lo_key()); | |
693 } | |
694 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); | |
695 } else if (x->number_of_sux() == 1) { | |
696 // NOTE: Code permanently disabled for now since the switch statement's | |
697 // tag expression may produce side-effects in which case it must | |
698 // be executed. | |
699 return; | |
700 // simplify to Goto | |
701 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); | |
702 } else if (x->number_of_sux() == 2) { | |
703 // NOTE: Code permanently disabled for now since it produces two new nodes | |
704 // (Constant & If) and the Canonicalizer cannot return them correctly | |
705 // yet. For now we copied the corresponding code directly into the | |
706 // GraphBuilder (i.e., we should never reach here). | |
707 return; | |
708 // simplify to If | |
709 assert(x->lo_key() == x->hi_key(), "keys must be the same"); | |
710 Constant* key = new Constant(new IntConstant(x->lo_key())); | |
711 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); | |
712 } | |
713 } | |
714 | |
715 | |
716 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) { | |
717 if (x->tag()->type()->is_constant()) { | |
718 int v = x->tag()->type()->as_IntConstant()->value(); | |
719 BlockBegin* sux = x->default_sux(); | |
720 for (int i = 0; i < x->length(); i++) { | |
721 if (v == x->key_at(i)) { | |
722 sux = x->sux_at(i); | |
723 } | |
724 } | |
725 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); | |
726 } else if (x->number_of_sux() == 1) { | |
727 // NOTE: Code permanently disabled for now since the switch statement's | |
728 // tag expression may produce side-effects in which case it must | |
729 // be executed. | |
730 return; | |
731 // simplify to Goto | |
732 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); | |
733 } else if (x->number_of_sux() == 2) { | |
734 // NOTE: Code permanently disabled for now since it produces two new nodes | |
735 // (Constant & If) and the Canonicalizer cannot return them correctly | |
736 // yet. For now we copied the corresponding code directly into the | |
737 // GraphBuilder (i.e., we should never reach here). | |
738 return; | |
739 // simplify to If | |
740 assert(x->length() == 1, "length must be the same"); | |
741 Constant* key = new Constant(new IntConstant(x->key_at(0))); | |
742 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); | |
743 } | |
744 } | |
745 | |
746 | |
747 void Canonicalizer::do_Return (Return* x) {} | |
748 void Canonicalizer::do_Throw (Throw* x) {} | |
749 void Canonicalizer::do_Base (Base* x) {} | |
750 void Canonicalizer::do_OsrEntry (OsrEntry* x) {} | |
751 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {} | |
752 | |
753 static bool match_index_and_scale(Instruction* instr, | |
754 Instruction** index, | |
755 int* log2_scale, | |
756 Instruction** instr_to_unpin) { | |
757 *instr_to_unpin = NULL; | |
758 | |
759 // Skip conversion ops | |
760 Convert* convert = instr->as_Convert(); | |
761 if (convert != NULL) { | |
762 instr = convert->value(); | |
763 } | |
764 | |
765 ShiftOp* shift = instr->as_ShiftOp(); | |
766 if (shift != NULL) { | |
767 if (shift->is_pinned()) { | |
768 *instr_to_unpin = shift; | |
769 } | |
770 // Constant shift value? | |
771 Constant* con = shift->y()->as_Constant(); | |
772 if (con == NULL) return false; | |
773 // Well-known type and value? | |
774 IntConstant* val = con->type()->as_IntConstant(); | |
775 if (val == NULL) return false; | |
776 if (shift->x()->type() != intType) return false; | |
777 *index = shift->x(); | |
778 int tmp_scale = val->value(); | |
779 if (tmp_scale >= 0 && tmp_scale < 4) { | |
780 *log2_scale = tmp_scale; | |
781 return true; | |
782 } else { | |
783 return false; | |
784 } | |
785 } | |
786 | |
787 ArithmeticOp* arith = instr->as_ArithmeticOp(); | |
788 if (arith != NULL) { | |
789 if (arith->is_pinned()) { | |
790 *instr_to_unpin = arith; | |
791 } | |
792 // Check for integer multiply | |
793 if (arith->op() == Bytecodes::_imul) { | |
794 // See if either arg is a known constant | |
795 Constant* con = arith->x()->as_Constant(); | |
796 if (con != NULL) { | |
797 *index = arith->y(); | |
798 } else { | |
799 con = arith->y()->as_Constant(); | |
800 if (con == NULL) return false; | |
801 *index = arith->x(); | |
802 } | |
803 if ((*index)->type() != intType) return false; | |
804 // Well-known type and value? | |
805 IntConstant* val = con->type()->as_IntConstant(); | |
806 if (val == NULL) return false; | |
807 switch (val->value()) { | |
808 case 1: *log2_scale = 0; return true; | |
809 case 2: *log2_scale = 1; return true; | |
810 case 4: *log2_scale = 2; return true; | |
811 case 8: *log2_scale = 3; return true; | |
812 default: return false; | |
813 } | |
814 } | |
815 } | |
816 | |
817 // Unknown instruction sequence; don't touch it | |
818 return false; | |
819 } | |
820 | |
821 | |
822 static bool match(UnsafeRawOp* x, | |
823 Instruction** base, | |
824 Instruction** index, | |
825 int* log2_scale) { | |
826 Instruction* instr_to_unpin = NULL; | |
827 ArithmeticOp* root = x->base()->as_ArithmeticOp(); | |
828 if (root == NULL) return false; | |
829 // Limit ourselves to addition for now | |
830 if (root->op() != Bytecodes::_ladd) return false; | |
831 // Try to find shift or scale op | |
832 if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) { | |
833 *base = root->x(); | |
834 } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) { | |
835 *base = root->y(); | |
836 } else if (root->y()->as_Convert() != NULL) { | |
837 Convert* convert = root->y()->as_Convert(); | |
838 if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) { | |
839 // pick base and index, setting scale at 1 | |
840 *base = root->x(); | |
841 *index = convert->value(); | |
842 *log2_scale = 0; | |
843 } else { | |
844 return false; | |
845 } | |
846 } else { | |
847 // doesn't match any expected sequences | |
848 return false; | |
849 } | |
850 | |
851 // If the value is pinned then it will be always be computed so | |
852 // there's no profit to reshaping the expression. | |
853 return !root->is_pinned(); | |
854 } | |
855 | |
856 | |
857 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) { | |
858 Instruction* base = NULL; | |
859 Instruction* index = NULL; | |
860 int log2_scale; | |
861 | |
862 if (match(x, &base, &index, &log2_scale)) { | |
863 x->set_base(base); | |
864 x->set_index(index); | |
865 x->set_log2_scale(log2_scale); | |
866 if (PrintUnsafeOptimization) { | |
867 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d", | |
868 x->id(), x->base()->id(), x->index()->id(), x->log2_scale()); | |
869 } | |
870 } | |
871 } | |
872 | |
873 void Canonicalizer::do_RoundFP(RoundFP* x) {} | |
874 void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } | |
875 void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } | |
876 void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {} | |
877 void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {} | |
878 void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} | |
879 void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} | |
880 void Canonicalizer::do_ProfileCall(ProfileCall* x) {} | |
881 void Canonicalizer::do_ProfileCounter(ProfileCounter* x) {} |