1 /*
2 * Copyright (c) 1999, 2019, 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 #include "precompiled.hpp"
26 #include "c1/c1_Canonicalizer.hpp"
27 #include "c1/c1_InstructionPrinter.hpp"
28 #include "c1/c1_ValueStack.hpp"
29 #include "ci/ciArray.hpp"
30 #include "runtime/sharedRuntime.hpp"
31
32
33 class PrintValueVisitor: public ValueVisitor {
visit(Value * vp)34 void visit(Value* vp) {
35 (*vp)->print_line();
36 }
37 };
38
set_canonical(Value x)39 void Canonicalizer::set_canonical(Value x) {
40 assert(x != NULL, "value must exist");
41 // Note: we can not currently substitute root nodes which show up in
42 // the instruction stream (because the instruction list is embedded
43 // in the instructions).
44 if (canonical() != x) {
45 #ifndef PRODUCT
46 if (!x->has_printable_bci()) {
47 x->set_printable_bci(bci());
48 }
49 #endif
50 if (PrintCanonicalization) {
51 PrintValueVisitor do_print_value;
52 canonical()->input_values_do(&do_print_value);
53 canonical()->print_line();
54 tty->print_cr("canonicalized to:");
55 x->input_values_do(&do_print_value);
56 x->print_line();
57 tty->cr();
58 }
59 assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
60 _canonical = x;
61 }
62 }
63
64
move_const_to_right(Op2 * x)65 void Canonicalizer::move_const_to_right(Op2* x) {
66 if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
67 }
68
69
do_Op2(Op2 * x)70 void Canonicalizer::do_Op2(Op2* x) {
71 if (x->x() == x->y()) {
72 switch (x->op()) {
73 case Bytecodes::_isub: set_constant(0); return;
74 case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
75 case Bytecodes::_iand: // fall through
76 case Bytecodes::_land: // fall through
77 case Bytecodes::_ior : // fall through
78 case Bytecodes::_lor : set_canonical(x->x()); return;
79 case Bytecodes::_ixor: set_constant(0); return;
80 case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
81 default : break;
82 }
83 }
84
85 if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
86 // do constant folding for selected operations
87 switch (x->type()->tag()) {
88 case intTag:
89 { jint a = x->x()->type()->as_IntConstant()->value();
90 jint b = x->y()->type()->as_IntConstant()->value();
91 switch (x->op()) {
92 case Bytecodes::_iadd: set_constant(a + b); return;
93 case Bytecodes::_isub: set_constant(a - b); return;
94 case Bytecodes::_imul: set_constant(a * b); return;
95 case Bytecodes::_idiv:
96 if (b != 0) {
97 if (a == min_jint && b == -1) {
98 set_constant(min_jint);
99 } else {
100 set_constant(a / b);
101 }
102 return;
103 }
104 break;
105 case Bytecodes::_irem:
106 if (b != 0) {
107 if (a == min_jint && b == -1) {
108 set_constant(0);
109 } else {
110 set_constant(a % b);
111 }
112 return;
113 }
114 break;
115 case Bytecodes::_iand: set_constant(a & b); return;
116 case Bytecodes::_ior : set_constant(a | b); return;
117 case Bytecodes::_ixor: set_constant(a ^ b); return;
118 default : break;
119 }
120 }
121 break;
122 case longTag:
123 { jlong a = x->x()->type()->as_LongConstant()->value();
124 jlong b = x->y()->type()->as_LongConstant()->value();
125 switch (x->op()) {
126 case Bytecodes::_ladd: set_constant(a + b); return;
127 case Bytecodes::_lsub: set_constant(a - b); return;
128 case Bytecodes::_lmul: set_constant(a * b); return;
129 case Bytecodes::_ldiv:
130 if (b != 0) {
131 set_constant(SharedRuntime::ldiv(b, a));
132 return;
133 }
134 break;
135 case Bytecodes::_lrem:
136 if (b != 0) {
137 set_constant(SharedRuntime::lrem(b, a));
138 return;
139 }
140 break;
141 case Bytecodes::_land: set_constant(a & b); return;
142 case Bytecodes::_lor : set_constant(a | b); return;
143 case Bytecodes::_lxor: set_constant(a ^ b); return;
144 default : break;
145 }
146 }
147 break;
148 default:
149 // other cases not implemented (must be extremely careful with floats & doubles!)
150 break;
151 }
152 }
153 // make sure constant is on the right side, if any
154 move_const_to_right(x);
155
156 if (x->y()->type()->is_constant()) {
157 // do constant folding for selected operations
158 switch (x->type()->tag()) {
159 case intTag:
160 if (x->y()->type()->as_IntConstant()->value() == 0) {
161 switch (x->op()) {
162 case Bytecodes::_iadd: set_canonical(x->x()); return;
163 case Bytecodes::_isub: set_canonical(x->x()); return;
164 case Bytecodes::_imul: set_constant(0); return;
165 // Note: for div and rem, make sure that C semantics
166 // corresponds to Java semantics!
167 case Bytecodes::_iand: set_constant(0); return;
168 case Bytecodes::_ior : set_canonical(x->x()); return;
169 default : break;
170 }
171 }
172 break;
173 case longTag:
174 if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
175 switch (x->op()) {
176 case Bytecodes::_ladd: set_canonical(x->x()); return;
177 case Bytecodes::_lsub: set_canonical(x->x()); return;
178 case Bytecodes::_lmul: set_constant((jlong)0); return;
179 // Note: for div and rem, make sure that C semantics
180 // corresponds to Java semantics!
181 case Bytecodes::_land: set_constant((jlong)0); return;
182 case Bytecodes::_lor : set_canonical(x->x()); return;
183 default : break;
184 }
185 }
186 break;
187 default:
188 break;
189 }
190 }
191 }
192
193
do_Phi(Phi * x)194 void Canonicalizer::do_Phi (Phi* x) {}
do_Constant(Constant * x)195 void Canonicalizer::do_Constant (Constant* x) {}
do_Local(Local * x)196 void Canonicalizer::do_Local (Local* x) {}
do_LoadField(LoadField * x)197 void Canonicalizer::do_LoadField (LoadField* x) {}
198
199 // checks if v is in the block that is currently processed by
200 // GraphBuilder. This is the only block that has not BlockEnd yet.
in_current_block(Value v)201 static bool in_current_block(Value v) {
202 int max_distance = 4;
203 while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {
204 v = v->next();
205 max_distance--;
206 }
207 return v == NULL;
208 }
209
do_StoreField(StoreField * x)210 void Canonicalizer::do_StoreField (StoreField* x) {
211 // If a value is going to be stored into a field or array some of
212 // the conversions emitted by javac are unneeded because the fields
213 // are packed to their natural size.
214 Convert* conv = x->value()->as_Convert();
215 if (conv) {
216 Value value = NULL;
217 BasicType type = x->field()->type()->basic_type();
218 switch (conv->op()) {
219 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
220 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
221 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
222 default : break;
223 }
224 // limit this optimization to current block
225 if (value != NULL && in_current_block(conv)) {
226 set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
227 x->state_before(), x->needs_patching()));
228 return;
229 }
230 }
231
232 }
233
do_ArrayLength(ArrayLength * x)234 void Canonicalizer::do_ArrayLength (ArrayLength* x) {
235 NewArray* na;
236 Constant* ct;
237 LoadField* lf;
238
239 if ((na = x->array()->as_NewArray()) != NULL) {
240 // New arrays might have the known length.
241 // Do not use the Constant itself, but create a new Constant
242 // with same value Otherwise a Constant is live over multiple
243 // blocks without being registered in a state array.
244 Constant* length;
245 if (na->length() != NULL &&
246 (length = na->length()->as_Constant()) != NULL) {
247 assert(length->type()->as_IntConstant() != NULL, "array length must be integer");
248 set_constant(length->type()->as_IntConstant()->value());
249 }
250
251 } else if ((ct = x->array()->as_Constant()) != NULL) {
252 // Constant arrays have constant lengths.
253 ArrayConstant* cnst = ct->type()->as_ArrayConstant();
254 if (cnst != NULL) {
255 set_constant(cnst->value()->length());
256 }
257
258 } else if ((lf = x->array()->as_LoadField()) != NULL) {
259 ciField* field = lf->field();
260 if (field->is_static_constant()) {
261 // Constant field loads are usually folded during parsing.
262 // But it doesn't happen with PatchALot, ScavengeRootsInCode < 2, or when
263 // holder class is being initialized during parsing (for static fields).
264 ciObject* c = field->constant_value().as_object();
265 if (!c->is_null_object()) {
266 set_constant(c->as_array()->length());
267 }
268 }
269 }
270 }
271
do_LoadIndexed(LoadIndexed * x)272 void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {
273 StableArrayConstant* array = x->array()->type()->as_StableArrayConstant();
274 IntConstant* index = x->index()->type()->as_IntConstant();
275
276 assert(array == NULL || FoldStableValues, "not enabled");
277
278 // Constant fold loads from stable arrays.
279 if (!x->mismatched() && array != NULL && index != NULL) {
280 jint idx = index->value();
281 if (idx < 0 || idx >= array->value()->length()) {
282 // Leave the load as is. The range check will handle it.
283 return;
284 }
285
286 ciConstant field_val = array->value()->element_value(idx);
287 if (!field_val.is_null_or_zero()) {
288 jint dimension = array->dimension();
289 assert(dimension <= array->value()->array_type()->dimension(), "inconsistent info");
290 ValueType* value = NULL;
291 if (dimension > 1) {
292 // Preserve information about the dimension for the element.
293 assert(field_val.as_object()->is_array(), "not an array");
294 value = new StableArrayConstant(field_val.as_object()->as_array(), dimension - 1);
295 } else {
296 assert(dimension == 1, "sanity");
297 value = as_ValueType(field_val);
298 }
299 set_canonical(new Constant(value));
300 }
301 }
302 }
303
do_StoreIndexed(StoreIndexed * x)304 void Canonicalizer::do_StoreIndexed (StoreIndexed* x) {
305 // If a value is going to be stored into a field or array some of
306 // the conversions emitted by javac are unneeded because the fields
307 // are packed to their natural size.
308 Convert* conv = x->value()->as_Convert();
309 if (conv) {
310 Value value = NULL;
311 BasicType type = x->elt_type();
312 switch (conv->op()) {
313 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
314 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
315 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
316 default : break;
317 }
318 // limit this optimization to current block
319 if (value != NULL && in_current_block(conv)) {
320 set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
321 x->elt_type(), value, x->state_before(),
322 x->check_boolean()));
323 return;
324 }
325 }
326 }
327
328
do_NegateOp(NegateOp * x)329 void Canonicalizer::do_NegateOp(NegateOp* x) {
330 ValueType* t = x->x()->type();
331 if (t->is_constant()) {
332 switch (t->tag()) {
333 case intTag : set_constant(-t->as_IntConstant ()->value()); return;
334 case longTag : set_constant(-t->as_LongConstant ()->value()); return;
335 case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
336 case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
337 default : ShouldNotReachHere();
338 }
339 }
340 }
341
342
do_ArithmeticOp(ArithmeticOp * x)343 void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); }
344
345
do_ShiftOp(ShiftOp * x)346 void Canonicalizer::do_ShiftOp (ShiftOp* x) {
347 ValueType* t = x->x()->type();
348 ValueType* t2 = x->y()->type();
349 if (t->is_constant()) {
350 switch (t->tag()) {
351 case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break;
352 case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
353 default : ShouldNotReachHere();
354 }
355 if (t2->is_constant()) {
356 if (t->tag() == intTag) {
357 jint value = t->as_IntConstant()->value();
358 jint shift = t2->as_IntConstant()->value();
359 switch (x->op()) {
360 case Bytecodes::_ishl: set_constant(java_shift_left(value, shift)); return;
361 case Bytecodes::_ishr: set_constant(java_shift_right(value, shift)); return;
362 case Bytecodes::_iushr: set_constant(java_shift_right_unsigned(value, shift)); return;
363 default: break;
364 }
365 } else if (t->tag() == longTag) {
366 jlong value = t->as_LongConstant()->value();
367 jint shift = t2->as_IntConstant()->value();
368 switch (x->op()) {
369 case Bytecodes::_lshl: set_constant(java_shift_left(value, shift)); return;
370 case Bytecodes::_lshr: set_constant(java_shift_right(value, shift)); return;
371 case Bytecodes::_lushr: set_constant(java_shift_right_unsigned(value, shift)); return;
372 default: break;
373 }
374 }
375 }
376 }
377 if (t2->is_constant()) {
378 switch (t2->tag()) {
379 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
380 case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;
381 default : ShouldNotReachHere(); return;
382 }
383 }
384 }
385
386
do_LogicOp(LogicOp * x)387 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
do_CompareOp(CompareOp * x)388 void Canonicalizer::do_CompareOp (CompareOp* x) {
389 if (x->x() == x->y()) {
390 switch (x->x()->type()->tag()) {
391 case longTag: set_constant(0); break;
392 case floatTag: {
393 FloatConstant* fc = x->x()->type()->as_FloatConstant();
394 if (fc) {
395 if (g_isnan(fc->value())) {
396 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
397 } else {
398 set_constant(0);
399 }
400 }
401 break;
402 }
403 case doubleTag: {
404 DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
405 if (dc) {
406 if (g_isnan(dc->value())) {
407 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
408 } else {
409 set_constant(0);
410 }
411 }
412 break;
413 }
414 default:
415 break;
416 }
417 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
418 switch (x->x()->type()->tag()) {
419 case longTag: {
420 jlong vx = x->x()->type()->as_LongConstant()->value();
421 jlong vy = x->y()->type()->as_LongConstant()->value();
422 if (vx == vy)
423 set_constant(0);
424 else if (vx < vy)
425 set_constant(-1);
426 else
427 set_constant(1);
428 break;
429 }
430
431 case floatTag: {
432 float vx = x->x()->type()->as_FloatConstant()->value();
433 float vy = x->y()->type()->as_FloatConstant()->value();
434 if (g_isnan(vx) || g_isnan(vy))
435 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
436 else if (vx == vy)
437 set_constant(0);
438 else if (vx < vy)
439 set_constant(-1);
440 else
441 set_constant(1);
442 break;
443 }
444
445 case doubleTag: {
446 double vx = x->x()->type()->as_DoubleConstant()->value();
447 double vy = x->y()->type()->as_DoubleConstant()->value();
448 if (g_isnan(vx) || g_isnan(vy))
449 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
450 else if (vx == vy)
451 set_constant(0);
452 else if (vx < vy)
453 set_constant(-1);
454 else
455 set_constant(1);
456 break;
457 }
458
459 default:
460 break;
461 }
462 }
463 }
464
465
do_IfInstanceOf(IfInstanceOf * x)466 void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {}
467
do_IfOp(IfOp * x)468 void Canonicalizer::do_IfOp(IfOp* x) {
469 // Caution: do not use do_Op2(x) here for now since
470 // we map the condition to the op for now!
471 move_const_to_right(x);
472 }
473
474
do_Intrinsic(Intrinsic * x)475 void Canonicalizer::do_Intrinsic (Intrinsic* x) {
476 switch (x->id()) {
477 case vmIntrinsics::_floatToRawIntBits : {
478 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
479 if (c != NULL) {
480 JavaValue v;
481 v.set_jfloat(c->value());
482 set_constant(v.get_jint());
483 }
484 break;
485 }
486 case vmIntrinsics::_intBitsToFloat : {
487 IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
488 if (c != NULL) {
489 JavaValue v;
490 v.set_jint(c->value());
491 set_constant(v.get_jfloat());
492 }
493 break;
494 }
495 case vmIntrinsics::_doubleToRawLongBits : {
496 DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
497 if (c != NULL) {
498 JavaValue v;
499 v.set_jdouble(c->value());
500 set_constant(v.get_jlong());
501 }
502 break;
503 }
504 case vmIntrinsics::_longBitsToDouble : {
505 LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
506 if (c != NULL) {
507 JavaValue v;
508 v.set_jlong(c->value());
509 set_constant(v.get_jdouble());
510 }
511 break;
512 }
513 case vmIntrinsics::_isInstance : {
514 assert(x->number_of_arguments() == 2, "wrong type");
515
516 InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
517 if (c != NULL && !c->value()->is_null_object()) {
518 // ciInstance::java_mirror_type() returns non-NULL only for Java mirrors
519 ciType* t = c->value()->java_mirror_type();
520 if (t->is_klass()) {
521 // substitute cls.isInstance(obj) of a constant Class into
522 // an InstantOf instruction
523 InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before());
524 set_canonical(i);
525 // and try to canonicalize even further
526 do_InstanceOf(i);
527 } else {
528 assert(t->is_primitive_type(), "should be a primitive type");
529 // cls.isInstance(obj) always returns false for primitive classes
530 set_constant(0);
531 }
532 }
533 break;
534 }
535 case vmIntrinsics::_isPrimitive : {
536 assert(x->number_of_arguments() == 1, "wrong type");
537
538 // Class.isPrimitive is known on constant classes:
539 InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
540 if (c != NULL && !c->value()->is_null_object()) {
541 ciType* t = c->value()->java_mirror_type();
542 set_constant(t->is_primitive_type());
543 }
544 break;
545 }
546 default:
547 break;
548 }
549 }
550
do_Convert(Convert * x)551 void Canonicalizer::do_Convert (Convert* x) {
552 if (x->value()->type()->is_constant()) {
553 switch (x->op()) {
554 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
555 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
556 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
557 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;
558 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;
559 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;
560 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;
561 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
562 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
563 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;
564 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
565 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
566 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;
567 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
568 case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
569 default:
570 ShouldNotReachHere();
571 }
572 }
573
574 Value value = x->value();
575 BasicType type = T_ILLEGAL;
576 LoadField* lf = value->as_LoadField();
577 if (lf) {
578 type = lf->field_type();
579 } else {
580 LoadIndexed* li = value->as_LoadIndexed();
581 if (li) {
582 type = li->elt_type();
583 } else {
584 Convert* conv = value->as_Convert();
585 if (conv) {
586 switch (conv->op()) {
587 case Bytecodes::_i2b: type = T_BYTE; break;
588 case Bytecodes::_i2s: type = T_SHORT; break;
589 case Bytecodes::_i2c: type = T_CHAR; break;
590 default : break;
591 }
592 }
593 }
594 }
595 if (type != T_ILLEGAL) {
596 switch (x->op()) {
597 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;
598 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
599 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;
600 default : break;
601 }
602 } else {
603 Op2* op2 = x->value()->as_Op2();
604 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
605 jint safebits = 0;
606 jint mask = op2->y()->type()->as_IntConstant()->value();
607 switch (x->op()) {
608 case Bytecodes::_i2b: safebits = 0x7f; break;
609 case Bytecodes::_i2s: safebits = 0x7fff; break;
610 case Bytecodes::_i2c: safebits = 0xffff; break;
611 default : break;
612 }
613 // When casting a masked integer to a smaller signed type, if
614 // the mask doesn't include the sign bit the cast isn't needed.
615 if (safebits && (mask & ~safebits) == 0) {
616 set_canonical(x->value());
617 }
618 }
619 }
620
621 }
622
do_NullCheck(NullCheck * x)623 void Canonicalizer::do_NullCheck (NullCheck* x) {
624 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
625 set_canonical(x->obj());
626 } else {
627 Constant* con = x->obj()->as_Constant();
628 if (con) {
629 ObjectType* c = con->type()->as_ObjectType();
630 if (c && c->is_loaded()) {
631 ObjectConstant* oc = c->as_ObjectConstant();
632 if (!oc || !oc->value()->is_null_object()) {
633 set_canonical(con);
634 }
635 }
636 }
637 }
638 }
639
do_TypeCast(TypeCast * x)640 void Canonicalizer::do_TypeCast (TypeCast* x) {}
do_Invoke(Invoke * x)641 void Canonicalizer::do_Invoke (Invoke* x) {}
do_NewInstance(NewInstance * x)642 void Canonicalizer::do_NewInstance (NewInstance* x) {}
do_NewTypeArray(NewTypeArray * x)643 void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {}
do_NewObjectArray(NewObjectArray * x)644 void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {}
do_NewMultiArray(NewMultiArray * x)645 void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {}
do_CheckCast(CheckCast * x)646 void Canonicalizer::do_CheckCast (CheckCast* x) {
647 if (x->klass()->is_loaded()) {
648 Value obj = x->obj();
649 ciType* klass = obj->exact_type();
650 if (klass == NULL) {
651 klass = obj->declared_type();
652 }
653 if (klass != NULL && klass->is_loaded()) {
654 bool is_interface = klass->is_instance_klass() &&
655 klass->as_instance_klass()->is_interface();
656 // Interface casts can't be statically optimized away since verifier doesn't
657 // enforce interface types in bytecode.
658 if (!is_interface && klass->is_subtype_of(x->klass())) {
659 set_canonical(obj);
660 return;
661 }
662 }
663 // checkcast of null returns null
664 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
665 set_canonical(obj);
666 }
667 }
668 }
do_InstanceOf(InstanceOf * x)669 void Canonicalizer::do_InstanceOf (InstanceOf* x) {
670 if (x->klass()->is_loaded()) {
671 Value obj = x->obj();
672 ciType* exact = obj->exact_type();
673 if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
674 set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
675 return;
676 }
677 // instanceof null returns false
678 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
679 set_constant(0);
680 }
681 }
682
683 }
do_MonitorEnter(MonitorEnter * x)684 void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}
do_MonitorExit(MonitorExit * x)685 void Canonicalizer::do_MonitorExit (MonitorExit* x) {}
do_BlockBegin(BlockBegin * x)686 void Canonicalizer::do_BlockBegin (BlockBegin* x) {}
do_Goto(Goto * x)687 void Canonicalizer::do_Goto (Goto* x) {}
688
689
is_true(jlong x,If::Condition cond,jlong y)690 static bool is_true(jlong x, If::Condition cond, jlong y) {
691 switch (cond) {
692 case If::eql: return x == y;
693 case If::neq: return x != y;
694 case If::lss: return x < y;
695 case If::leq: return x <= y;
696 case If::gtr: return x > y;
697 case If::geq: return x >= y;
698 default:
699 ShouldNotReachHere();
700 return false;
701 }
702 }
703
is_safepoint(BlockEnd * x,BlockBegin * sux)704 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
705 // An Instruction with multiple successors, x, is replaced by a Goto
706 // to a single successor, sux. Is a safepoint check needed = was the
707 // instruction being replaced a safepoint and the single remaining
708 // successor a back branch?
709 return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
710 }
711
do_If(If * x)712 void Canonicalizer::do_If(If* x) {
713 // move const to right
714 if (x->x()->type()->is_constant()) x->swap_operands();
715 // simplify
716 const Value l = x->x(); ValueType* lt = l->type();
717 const Value r = x->y(); ValueType* rt = r->type();
718
719 if (l == r && !lt->is_float_kind()) {
720 // pattern: If (a cond a) => simplify to Goto
721 BlockBegin* sux = NULL;
722 switch (x->cond()) {
723 case If::eql: sux = x->sux_for(true); break;
724 case If::neq: sux = x->sux_for(false); break;
725 case If::lss: sux = x->sux_for(false); break;
726 case If::leq: sux = x->sux_for(true); break;
727 case If::gtr: sux = x->sux_for(false); break;
728 case If::geq: sux = x->sux_for(true); break;
729 default: ShouldNotReachHere();
730 }
731 // If is a safepoint then the debug information should come from the state_before of the If.
732 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
733 return;
734 }
735
736 if (lt->is_constant() && rt->is_constant()) {
737 if (x->x()->as_Constant() != NULL) {
738 // pattern: If (lc cond rc) => simplify to: Goto
739 BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
740 x->sux_for(true),
741 x->sux_for(false));
742 if (sux != NULL) {
743 // If is a safepoint then the debug information should come from the state_before of the If.
744 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
745 }
746 }
747 } else if (rt->as_IntConstant() != NULL) {
748 // pattern: If (l cond rc) => investigate further
749 const jint rc = rt->as_IntConstant()->value();
750 if (l->as_CompareOp() != NULL) {
751 // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
752 CompareOp* cmp = l->as_CompareOp();
753 bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
754 BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
755 BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
756 BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
757 BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
758 // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
759 // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
760 // lss_sux or gtr_sux.
761 if (lss_sux == eql_sux && eql_sux == gtr_sux) {
762 // all successors identical => simplify to: Goto
763 set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
764 } else {
765 // two successors differ and two successors are the same => simplify to: If (x cmp y)
766 // determine new condition & successors
767 If::Condition cond = If::eql;
768 BlockBegin* tsux = NULL;
769 BlockBegin* fsux = NULL;
770 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
771 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
772 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
773 else { ShouldNotReachHere(); }
774 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
775 if (cmp->x() == cmp->y()) {
776 do_If(canon);
777 } else {
778 if (compilation()->profile_branches() || compilation()->count_backedges()) {
779 // TODO: If profiling, leave floating point comparisons unoptimized.
780 // We currently do not support profiling of the unordered case.
781 switch(cmp->op()) {
782 case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
783 case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
784 set_canonical(x);
785 return;
786 default:
787 break;
788 }
789 }
790 set_bci(cmp->state_before()->bci());
791 set_canonical(canon);
792 }
793 }
794 } else if (l->as_InstanceOf() != NULL) {
795 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
796 // instruction in the graph (it is pinned). Need to fix this at some point.
797 // It should also be left in the graph when generating a profiled method version or Goto
798 // has to know that it was an InstanceOf.
799 return;
800 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
801 InstanceOf* inst = l->as_InstanceOf();
802 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
803 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
804 if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
805 // both successors identical and klass is loaded => simplify to: Goto
806 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
807 } else {
808 // successors differ => simplify to: IfInstanceOf
809 set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->state_before()->bci(), is_inst_sux, no_inst_sux));
810 }
811 }
812 } else if (rt == objectNull &&
813 (l->as_NewInstance() || l->as_NewArray() ||
814 (l->as_Local() && l->as_Local()->is_receiver()))) {
815 if (x->cond() == Instruction::eql) {
816 BlockBegin* sux = x->fsux();
817 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
818 } else {
819 assert(x->cond() == Instruction::neq, "only other valid case");
820 BlockBegin* sux = x->tsux();
821 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
822 }
823 }
824 }
825
826
do_TableSwitch(TableSwitch * x)827 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
828 if (x->tag()->type()->is_constant()) {
829 int v = x->tag()->type()->as_IntConstant()->value();
830 BlockBegin* sux = x->default_sux();
831 if (v >= x->lo_key() && v <= x->hi_key()) {
832 sux = x->sux_at(v - x->lo_key());
833 }
834 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
835 } else if (x->number_of_sux() == 1) {
836 // NOTE: Code permanently disabled for now since the switch statement's
837 // tag expression may produce side-effects in which case it must
838 // be executed.
839 return;
840 // simplify to Goto
841 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
842 } else if (x->number_of_sux() == 2) {
843 // NOTE: Code permanently disabled for now since it produces two new nodes
844 // (Constant & If) and the Canonicalizer cannot return them correctly
845 // yet. For now we copied the corresponding code directly into the
846 // GraphBuilder (i.e., we should never reach here).
847 return;
848 // simplify to If
849 assert(x->lo_key() == x->hi_key(), "keys must be the same");
850 Constant* key = new Constant(new IntConstant(x->lo_key()));
851 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
852 }
853 }
854
855
do_LookupSwitch(LookupSwitch * x)856 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
857 if (x->tag()->type()->is_constant()) {
858 int v = x->tag()->type()->as_IntConstant()->value();
859 BlockBegin* sux = x->default_sux();
860 for (int i = 0; i < x->length(); i++) {
861 if (v == x->key_at(i)) {
862 sux = x->sux_at(i);
863 }
864 }
865 set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
866 } else if (x->number_of_sux() == 1) {
867 // NOTE: Code permanently disabled for now since the switch statement's
868 // tag expression may produce side-effects in which case it must
869 // be executed.
870 return;
871 // simplify to Goto
872 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
873 } else if (x->number_of_sux() == 2) {
874 // NOTE: Code permanently disabled for now since it produces two new nodes
875 // (Constant & If) and the Canonicalizer cannot return them correctly
876 // yet. For now we copied the corresponding code directly into the
877 // GraphBuilder (i.e., we should never reach here).
878 return;
879 // simplify to If
880 assert(x->length() == 1, "length must be the same");
881 Constant* key = new Constant(new IntConstant(x->key_at(0)));
882 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
883 }
884 }
885
886
do_Return(Return * x)887 void Canonicalizer::do_Return (Return* x) {}
do_Throw(Throw * x)888 void Canonicalizer::do_Throw (Throw* x) {}
do_Base(Base * x)889 void Canonicalizer::do_Base (Base* x) {}
do_OsrEntry(OsrEntry * x)890 void Canonicalizer::do_OsrEntry (OsrEntry* x) {}
do_ExceptionObject(ExceptionObject * x)891 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
892
match_index_and_scale(Instruction * instr,Instruction ** index,int * log2_scale)893 static bool match_index_and_scale(Instruction* instr,
894 Instruction** index,
895 int* log2_scale) {
896 // Skip conversion ops. This works only on 32bit because of the implicit l2i that the
897 // unsafe performs.
898 #ifndef _LP64
899 Convert* convert = instr->as_Convert();
900 if (convert != NULL && convert->op() == Bytecodes::_i2l) {
901 assert(convert->value()->type() == intType, "invalid input type");
902 instr = convert->value();
903 }
904 #endif
905
906 ShiftOp* shift = instr->as_ShiftOp();
907 if (shift != NULL) {
908 if (shift->op() == Bytecodes::_lshl) {
909 assert(shift->x()->type() == longType, "invalid input type");
910 } else {
911 #ifndef _LP64
912 if (shift->op() == Bytecodes::_ishl) {
913 assert(shift->x()->type() == intType, "invalid input type");
914 } else {
915 return false;
916 }
917 #else
918 return false;
919 #endif
920 }
921
922
923 // Constant shift value?
924 Constant* con = shift->y()->as_Constant();
925 if (con == NULL) return false;
926 // Well-known type and value?
927 IntConstant* val = con->type()->as_IntConstant();
928 assert(val != NULL, "Should be an int constant");
929
930 *index = shift->x();
931 int tmp_scale = val->value();
932 if (tmp_scale >= 0 && tmp_scale < 4) {
933 *log2_scale = tmp_scale;
934 return true;
935 } else {
936 return false;
937 }
938 }
939
940 ArithmeticOp* arith = instr->as_ArithmeticOp();
941 if (arith != NULL) {
942 // See if either arg is a known constant
943 Constant* con = arith->x()->as_Constant();
944 if (con != NULL) {
945 *index = arith->y();
946 } else {
947 con = arith->y()->as_Constant();
948 if (con == NULL) return false;
949 *index = arith->x();
950 }
951 long const_value;
952 // Check for integer multiply
953 if (arith->op() == Bytecodes::_lmul) {
954 assert((*index)->type() == longType, "invalid input type");
955 LongConstant* val = con->type()->as_LongConstant();
956 assert(val != NULL, "expecting a long constant");
957 const_value = val->value();
958 } else {
959 #ifndef _LP64
960 if (arith->op() == Bytecodes::_imul) {
961 assert((*index)->type() == intType, "invalid input type");
962 IntConstant* val = con->type()->as_IntConstant();
963 assert(val != NULL, "expecting an int constant");
964 const_value = val->value();
965 } else {
966 return false;
967 }
968 #else
969 return false;
970 #endif
971 }
972 switch (const_value) {
973 case 1: *log2_scale = 0; return true;
974 case 2: *log2_scale = 1; return true;
975 case 4: *log2_scale = 2; return true;
976 case 8: *log2_scale = 3; return true;
977 default: return false;
978 }
979 }
980
981 // Unknown instruction sequence; don't touch it
982 return false;
983 }
984
985
match(UnsafeRawOp * x,Instruction ** base,Instruction ** index,int * log2_scale)986 static bool match(UnsafeRawOp* x,
987 Instruction** base,
988 Instruction** index,
989 int* log2_scale) {
990 ArithmeticOp* root = x->base()->as_ArithmeticOp();
991 if (root == NULL) return false;
992 // Limit ourselves to addition for now
993 if (root->op() != Bytecodes::_ladd) return false;
994
995 bool match_found = false;
996 // Try to find shift or scale op
997 if (match_index_and_scale(root->y(), index, log2_scale)) {
998 *base = root->x();
999 match_found = true;
1000 } else if (match_index_and_scale(root->x(), index, log2_scale)) {
1001 *base = root->y();
1002 match_found = true;
1003 } else if (NOT_LP64(root->y()->as_Convert() != NULL) LP64_ONLY(false)) {
1004 // Skipping i2l works only on 32bit because of the implicit l2i that the unsafe performs.
1005 // 64bit needs a real sign-extending conversion.
1006 Convert* convert = root->y()->as_Convert();
1007 if (convert->op() == Bytecodes::_i2l) {
1008 assert(convert->value()->type() == intType, "should be an int");
1009 // pick base and index, setting scale at 1
1010 *base = root->x();
1011 *index = convert->value();
1012 *log2_scale = 0;
1013 match_found = true;
1014 }
1015 }
1016 // The default solution
1017 if (!match_found) {
1018 *base = root->x();
1019 *index = root->y();
1020 *log2_scale = 0;
1021 }
1022
1023 // If the value is pinned then it will be always be computed so
1024 // there's no profit to reshaping the expression.
1025 return !root->is_pinned();
1026 }
1027
1028
do_UnsafeRawOp(UnsafeRawOp * x)1029 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
1030 Instruction* base = NULL;
1031 Instruction* index = NULL;
1032 int log2_scale;
1033
1034 if (match(x, &base, &index, &log2_scale)) {
1035 x->set_base(base);
1036 x->set_index(index);
1037 x->set_log2_scale(log2_scale);
1038 if (PrintUnsafeOptimization) {
1039 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
1040 x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
1041 }
1042 }
1043 }
1044
do_RoundFP(RoundFP * x)1045 void Canonicalizer::do_RoundFP(RoundFP* x) {}
do_UnsafeGetRaw(UnsafeGetRaw * x)1046 void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
do_UnsafePutRaw(UnsafePutRaw * x)1047 void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
do_UnsafeGetObject(UnsafeGetObject * x)1048 void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}
do_UnsafePutObject(UnsafePutObject * x)1049 void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}
do_UnsafeGetAndSetObject(UnsafeGetAndSetObject * x)1050 void Canonicalizer::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}
do_ProfileCall(ProfileCall * x)1051 void Canonicalizer::do_ProfileCall(ProfileCall* x) {}
do_ProfileReturnType(ProfileReturnType * x)1052 void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {}
do_ProfileInvoke(ProfileInvoke * x)1053 void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {}
do_RuntimeCall(RuntimeCall * x)1054 void Canonicalizer::do_RuntimeCall(RuntimeCall* x) {}
do_RangeCheckPredicate(RangeCheckPredicate * x)1055 void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
1056 #ifdef ASSERT
do_Assert(Assert * x)1057 void Canonicalizer::do_Assert(Assert* x) {}
1058 #endif
do_MemBar(MemBar * x)1059 void Canonicalizer::do_MemBar(MemBar* x) {}
1060