1 /*
2 * Copyright (c) 1999, 2016, 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 *
57 tty->cr();
58 }
59 assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
60 _canonical = x;
61 }
62 }
63
64
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
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 }
82 }
83
84 if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
85 // do constant folding for selected operations
86 switch (x->type()->tag()) {
87 case intTag:
88 { jint a = x->x()->type()->as_IntConstant()->value();
89 jint b = x->y()->type()->as_IntConstant()->value();
90 switch (x->op()) {
91 case Bytecodes::_iadd: set_constant(a + b); return;
92 case Bytecodes::_isub: set_constant(a - b); return;
93 case Bytecodes::_imul: set_constant(a * b); return;
94 case Bytecodes::_idiv:
95 if (b != 0) {
96 if (a == min_jint && b == -1) {
97 set_constant(min_jint);
98 } else {
99 set_constant(a / b);
100 }
101 return;
102 }
103 break;
104 case Bytecodes::_irem:
105 if (b != 0) {
106 if (a == min_jint && b == -1) {
107 set_constant(0);
108 } else {
109 set_constant(a % b);
110 }
111 return;
112 }
113 break;
114 case Bytecodes::_iand: set_constant(a & b); return;
115 case Bytecodes::_ior : set_constant(a | b); return;
116 case Bytecodes::_ixor: set_constant(a ^ b); return;
117 }
118 }
119 break;
120 case longTag:
121 { jlong a = x->x()->type()->as_LongConstant()->value();
122 jlong b = x->y()->type()->as_LongConstant()->value();
123 switch (x->op()) {
124 case Bytecodes::_ladd: set_constant(a + b); return;
125 case Bytecodes::_lsub: set_constant(a - b); return;
126 case Bytecodes::_lmul: set_constant(a * b); return;
127 case Bytecodes::_ldiv:
128 if (b != 0) {
129 set_constant(SharedRuntime::ldiv(b, a));
130 return;
131 }
132 break;
133 case Bytecodes::_lrem:
134 if (b != 0) {
135 set_constant(SharedRuntime::lrem(b, a));
136 return;
137 }
138 break;
139 case Bytecodes::_land: set_constant(a & b); return;
140 case Bytecodes::_lor : set_constant(a | b); return;
141 case Bytecodes::_lxor: set_constant(a ^ b); return;
142 }
143 }
144 break;
145 // other cases not implemented (must be extremely careful with floats & doubles!)
146 }
147 }
148 // make sure constant is on the right side, if any
149 move_const_to_right(x);
150
151 if (x->y()->type()->is_constant()) {
152 // do constant folding for selected operations
153 switch (x->type()->tag()) {
154 case intTag:
155 if (x->y()->type()->as_IntConstant()->value() == 0) {
156 switch (x->op()) {
157 case Bytecodes::_iadd: set_canonical(x->x()); return;
158 case Bytecodes::_isub: set_canonical(x->x()); return;
159 case Bytecodes::_imul: set_constant(0); return;
160 // Note: for div and rem, make sure that C semantics
161 // corresponds to Java semantics!
162 case Bytecodes::_iand: set_constant(0); return;
163 case Bytecodes::_ior : set_canonical(x->x()); return;
164 }
165 }
166 break;
167 case longTag:
168 if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
169 switch (x->op()) {
170 case Bytecodes::_ladd: set_canonical(x->x()); return;
171 case Bytecodes::_lsub: set_canonical(x->x()); return;
172 case Bytecodes::_lmul: set_constant((jlong)0); return;
173 // Note: for div and rem, make sure that C semantics
174 // corresponds to Java semantics!
175 case Bytecodes::_land: set_constant((jlong)0); return;
176 case Bytecodes::_lor : set_canonical(x->x()); return;
177 }
178 }
179 break;
180 }
181 }
182 }
183
184
185 void Canonicalizer::do_Phi (Phi* x) {}
186 void Canonicalizer::do_Constant (Constant* x) {}
187 void Canonicalizer::do_Local (Local* x) {}
188 void Canonicalizer::do_LoadField (LoadField* x) {}
189
190 // checks if v is in the block that is currently processed by
191 // GraphBuilder. This is the only block that has not BlockEnd yet.
192 static bool in_current_block(Value v) {
193 int max_distance = 4;
194 while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {
195 v = v->next();
196 max_distance--;
197 }
198 return v == NULL;
199 }
200
201 void Canonicalizer::do_StoreField (StoreField* x) {
202 // If a value is going to be stored into a field or array some of
203 // the conversions emitted by javac are unneeded because the fields
204 // are packed to their natural size.
205 Convert* conv = x->value()->as_Convert();
206 if (conv) {
207 Value value = NULL;
208 BasicType type = x->field()->type()->basic_type();
209 switch (conv->op()) {
210 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
211 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
212 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
213 }
214 // limit this optimization to current block
215 if (value != NULL && in_current_block(conv)) {
216 set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
217 x->state_before(), x->needs_patching()));
218 return;
219 }
220 }
221
222 }
223
224 void Canonicalizer::do_ArrayLength (ArrayLength* x) {
225 NewArray* na;
226 Constant* ct;
227 LoadField* lf;
228
229 if ((na = x->array()->as_NewArray()) != NULL) {
230 // New arrays might have the known length.
231 // Do not use the Constant itself, but create a new Constant
232 // with same value Otherwise a Constant is live over multiple
286 assert(dimension == 1, "sanity");
287 value = as_ValueType(field_val);
288 }
289 set_canonical(new Constant(value));
290 }
291 }
292 }
293
294 void Canonicalizer::do_StoreIndexed (StoreIndexed* x) {
295 // If a value is going to be stored into a field or array some of
296 // the conversions emitted by javac are unneeded because the fields
297 // are packed to their natural size.
298 Convert* conv = x->value()->as_Convert();
299 if (conv) {
300 Value value = NULL;
301 BasicType type = x->elt_type();
302 switch (conv->op()) {
303 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
304 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
305 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
306 }
307 // limit this optimization to current block
308 if (value != NULL && in_current_block(conv)) {
309 set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
310 x->elt_type(), value, x->state_before(),
311 x->check_boolean()));
312 return;
313 }
314 }
315 }
316
317
318 void Canonicalizer::do_NegateOp(NegateOp* x) {
319 ValueType* t = x->x()->type();
320 if (t->is_constant()) {
321 switch (t->tag()) {
322 case intTag : set_constant(-t->as_IntConstant ()->value()); return;
323 case longTag : set_constant(-t->as_LongConstant ()->value()); return;
324 case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
325 case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
334
335 void Canonicalizer::do_ShiftOp (ShiftOp* x) {
336 ValueType* t = x->x()->type();
337 ValueType* t2 = x->y()->type();
338 if (t->is_constant()) {
339 switch (t->tag()) {
340 case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break;
341 case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
342 default : ShouldNotReachHere();
343 }
344 if (t2->is_constant()) {
345 if (t->tag() == intTag) {
346 int value = t->as_IntConstant()->value();
347 int shift = t2->as_IntConstant()->value() & 31;
348 jint mask = ~(~0 << (32 - shift));
349 if (shift == 0) mask = ~0;
350 switch (x->op()) {
351 case Bytecodes::_ishl: set_constant(value << shift); return;
352 case Bytecodes::_ishr: set_constant(value >> shift); return;
353 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
354 }
355 } else if (t->tag() == longTag) {
356 jlong value = t->as_LongConstant()->value();
357 int shift = t2->as_IntConstant()->value() & 63;
358 jlong mask = ~(~jlong_cast(0) << (64 - shift));
359 if (shift == 0) mask = ~jlong_cast(0);
360 switch (x->op()) {
361 case Bytecodes::_lshl: set_constant(value << shift); return;
362 case Bytecodes::_lshr: set_constant(value >> shift); return;
363 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
364 }
365 }
366 }
367 }
368 if (t2->is_constant()) {
369 switch (t2->tag()) {
370 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
371 case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;
372 default : ShouldNotReachHere();
373 }
374 }
375 }
376
377
378 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
379 void Canonicalizer::do_CompareOp (CompareOp* x) {
380 if (x->x() == x->y()) {
381 switch (x->x()->type()->tag()) {
382 case longTag: set_constant(0); break;
383 case floatTag: {
385 if (fc) {
386 if (g_isnan(fc->value())) {
387 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
388 } else {
389 set_constant(0);
390 }
391 }
392 break;
393 }
394 case doubleTag: {
395 DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
396 if (dc) {
397 if (g_isnan(dc->value())) {
398 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
399 } else {
400 set_constant(0);
401 }
402 }
403 break;
404 }
405 }
406 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
407 switch (x->x()->type()->tag()) {
408 case longTag: {
409 jlong vx = x->x()->type()->as_LongConstant()->value();
410 jlong vy = x->y()->type()->as_LongConstant()->value();
411 if (vx == vy)
412 set_constant(0);
413 else if (vx < vy)
414 set_constant(-1);
415 else
416 set_constant(1);
417 break;
418 }
419
420 case floatTag: {
421 float vx = x->x()->type()->as_FloatConstant()->value();
422 float vy = x->y()->type()->as_FloatConstant()->value();
423 if (g_isnan(vx) || g_isnan(vy))
424 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
427 else if (vx < vy)
428 set_constant(-1);
429 else
430 set_constant(1);
431 break;
432 }
433
434 case doubleTag: {
435 double vx = x->x()->type()->as_DoubleConstant()->value();
436 double vy = x->y()->type()->as_DoubleConstant()->value();
437 if (g_isnan(vx) || g_isnan(vy))
438 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
439 else if (vx == vy)
440 set_constant(0);
441 else if (vx < vy)
442 set_constant(-1);
443 else
444 set_constant(1);
445 break;
446 }
447 }
448
449 }
450 }
451
452
453 void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {}
454
455 void Canonicalizer::do_IfOp(IfOp* x) {
456 // Caution: do not use do_Op2(x) here for now since
457 // we map the condition to the op for now!
458 move_const_to_right(x);
459 }
460
461
462 void Canonicalizer::do_Intrinsic (Intrinsic* x) {
463 switch (x->id()) {
464 case vmIntrinsics::_floatToRawIntBits : {
465 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
466 if (c != NULL) {
467 JavaValue v;
468 v.set_jfloat(c->value());
513 do_InstanceOf(i);
514 } else {
515 assert(t->is_primitive_type(), "should be a primitive type");
516 // cls.isInstance(obj) always returns false for primitive classes
517 set_constant(0);
518 }
519 }
520 break;
521 }
522 case vmIntrinsics::_isPrimitive : {
523 assert(x->number_of_arguments() == 1, "wrong type");
524
525 // Class.isPrimitive is known on constant classes:
526 InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
527 if (c != NULL && !c->value()->is_null_object()) {
528 ciType* t = c->value()->java_mirror_type();
529 set_constant(t->is_primitive_type());
530 }
531 break;
532 }
533 }
534 }
535
536 void Canonicalizer::do_Convert (Convert* x) {
537 if (x->value()->type()->is_constant()) {
538 switch (x->op()) {
539 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
540 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
541 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
542 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;
543 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;
544 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;
545 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;
546 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
547 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
548 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;
549 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
550 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
551 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;
552 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
555 ShouldNotReachHere();
556 }
557 }
558
559 Value value = x->value();
560 BasicType type = T_ILLEGAL;
561 LoadField* lf = value->as_LoadField();
562 if (lf) {
563 type = lf->field_type();
564 } else {
565 LoadIndexed* li = value->as_LoadIndexed();
566 if (li) {
567 type = li->elt_type();
568 } else {
569 Convert* conv = value->as_Convert();
570 if (conv) {
571 switch (conv->op()) {
572 case Bytecodes::_i2b: type = T_BYTE; break;
573 case Bytecodes::_i2s: type = T_SHORT; break;
574 case Bytecodes::_i2c: type = T_CHAR; break;
575 }
576 }
577 }
578 }
579 if (type != T_ILLEGAL) {
580 switch (x->op()) {
581 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;
582 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
583 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;
584 }
585 } else {
586 Op2* op2 = x->value()->as_Op2();
587 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
588 jint safebits = 0;
589 jint mask = op2->y()->type()->as_IntConstant()->value();
590 switch (x->op()) {
591 case Bytecodes::_i2b: safebits = 0x7f; break;
592 case Bytecodes::_i2s: safebits = 0x7fff; break;
593 case Bytecodes::_i2c: safebits = 0xffff; break;
594 }
595 // When casting a masked integer to a smaller signed type, if
596 // the mask doesn't include the sign bit the cast isn't needed.
597 if (safebits && (mask & ~safebits) == 0) {
598 set_canonical(x->value());
599 }
600 }
601 }
602
603 }
604
605 void Canonicalizer::do_NullCheck (NullCheck* x) {
606 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
607 set_canonical(x->obj());
608 } else {
609 Constant* con = x->obj()->as_Constant();
610 if (con) {
611 ObjectType* c = con->type()->as_ObjectType();
612 if (c && c->is_loaded()) {
613 ObjectConstant* oc = c->as_ObjectConstant();
652 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
653 set_constant(0);
654 }
655 }
656
657 }
658 void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}
659 void Canonicalizer::do_MonitorExit (MonitorExit* x) {}
660 void Canonicalizer::do_BlockBegin (BlockBegin* x) {}
661 void Canonicalizer::do_Goto (Goto* x) {}
662
663
664 static bool is_true(jlong x, If::Condition cond, jlong y) {
665 switch (cond) {
666 case If::eql: return x == y;
667 case If::neq: return x != y;
668 case If::lss: return x < y;
669 case If::leq: return x <= y;
670 case If::gtr: return x > y;
671 case If::geq: return x >= y;
672 }
673 ShouldNotReachHere();
674 return false;
675 }
676
677 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
678 // An Instruction with multiple successors, x, is replaced by a Goto
679 // to a single successor, sux. Is a safepoint check needed = was the
680 // instruction being replaced a safepoint and the single remaining
681 // successor a back branch?
682 return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
683 }
684
685 void Canonicalizer::do_If(If* x) {
686 // move const to right
687 if (x->x()->type()->is_constant()) x->swap_operands();
688 // simplify
689 const Value l = x->x(); ValueType* lt = l->type();
690 const Value r = x->y(); ValueType* rt = r->type();
691
692 if (l == r && !lt->is_float_kind()) {
693 // pattern: If (a cond a) => simplify to Goto
694 BlockBegin* sux = NULL;
739 // determine new condition & successors
740 If::Condition cond = If::eql;
741 BlockBegin* tsux = NULL;
742 BlockBegin* fsux = NULL;
743 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
744 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
745 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
746 else { ShouldNotReachHere(); }
747 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
748 if (cmp->x() == cmp->y()) {
749 do_If(canon);
750 } else {
751 if (compilation()->profile_branches()) {
752 // TODO: If profiling, leave floating point comparisons unoptimized.
753 // We currently do not support profiling of the unordered case.
754 switch(cmp->op()) {
755 case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
756 case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
757 set_canonical(x);
758 return;
759 }
760 }
761 set_bci(cmp->state_before()->bci());
762 set_canonical(canon);
763 }
764 }
765 } else if (l->as_InstanceOf() != NULL) {
766 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
767 // instruction in the graph (it is pinned). Need to fix this at some point.
768 // It should also be left in the graph when generating a profiled method version or Goto
769 // has to know that it was an InstanceOf.
770 return;
771 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
772 InstanceOf* inst = l->as_InstanceOf();
773 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
774 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
775 if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
776 // both successors identical and klass is loaded => simplify to: Goto
777 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
778 } else {
|
1 /*
2 * Copyright (c) 1999, 2017, 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 *
57 tty->cr();
58 }
59 assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
60 _canonical = x;
61 }
62 }
63
64
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
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
194 void Canonicalizer::do_Phi (Phi* x) {}
195 void Canonicalizer::do_Constant (Constant* x) {}
196 void Canonicalizer::do_Local (Local* 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.
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
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
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
296 assert(dimension == 1, "sanity");
297 value = as_ValueType(field_val);
298 }
299 set_canonical(new Constant(value));
300 }
301 }
302 }
303
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
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;
345
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 int value = t->as_IntConstant()->value();
358 int shift = t2->as_IntConstant()->value() & 31;
359 jint mask = ~(~0 << (32 - shift));
360 if (shift == 0) mask = ~0;
361 switch (x->op()) {
362 case Bytecodes::_ishl: set_constant(value << shift); return;
363 case Bytecodes::_ishr: set_constant(value >> shift); return;
364 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
365 default: break;
366 }
367 } else if (t->tag() == longTag) {
368 jlong value = t->as_LongConstant()->value();
369 int shift = t2->as_IntConstant()->value() & 63;
370 jlong mask = ~(~jlong_cast(0) << (64 - shift));
371 if (shift == 0) mask = ~jlong_cast(0);
372 switch (x->op()) {
373 case Bytecodes::_lshl: set_constant(value << shift); return;
374 case Bytecodes::_lshr: set_constant(value >> shift); return;
375 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
376 default: break;
377 }
378 }
379 }
380 }
381 if (t2->is_constant()) {
382 switch (t2->tag()) {
383 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
384 case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;
385 default : ShouldNotReachHere();
386 }
387 }
388 }
389
390
391 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
392 void Canonicalizer::do_CompareOp (CompareOp* x) {
393 if (x->x() == x->y()) {
394 switch (x->x()->type()->tag()) {
395 case longTag: set_constant(0); break;
396 case floatTag: {
398 if (fc) {
399 if (g_isnan(fc->value())) {
400 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
401 } else {
402 set_constant(0);
403 }
404 }
405 break;
406 }
407 case doubleTag: {
408 DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
409 if (dc) {
410 if (g_isnan(dc->value())) {
411 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
412 } else {
413 set_constant(0);
414 }
415 }
416 break;
417 }
418 default:
419 break;
420 }
421 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
422 switch (x->x()->type()->tag()) {
423 case longTag: {
424 jlong vx = x->x()->type()->as_LongConstant()->value();
425 jlong vy = x->y()->type()->as_LongConstant()->value();
426 if (vx == vy)
427 set_constant(0);
428 else if (vx < vy)
429 set_constant(-1);
430 else
431 set_constant(1);
432 break;
433 }
434
435 case floatTag: {
436 float vx = x->x()->type()->as_FloatConstant()->value();
437 float vy = x->y()->type()->as_FloatConstant()->value();
438 if (g_isnan(vx) || g_isnan(vy))
439 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
442 else if (vx < vy)
443 set_constant(-1);
444 else
445 set_constant(1);
446 break;
447 }
448
449 case doubleTag: {
450 double vx = x->x()->type()->as_DoubleConstant()->value();
451 double vy = x->y()->type()->as_DoubleConstant()->value();
452 if (g_isnan(vx) || g_isnan(vy))
453 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
454 else if (vx == vy)
455 set_constant(0);
456 else if (vx < vy)
457 set_constant(-1);
458 else
459 set_constant(1);
460 break;
461 }
462
463 default:
464 break;
465 }
466 }
467 }
468
469
470 void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {}
471
472 void Canonicalizer::do_IfOp(IfOp* x) {
473 // Caution: do not use do_Op2(x) here for now since
474 // we map the condition to the op for now!
475 move_const_to_right(x);
476 }
477
478
479 void Canonicalizer::do_Intrinsic (Intrinsic* x) {
480 switch (x->id()) {
481 case vmIntrinsics::_floatToRawIntBits : {
482 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
483 if (c != NULL) {
484 JavaValue v;
485 v.set_jfloat(c->value());
530 do_InstanceOf(i);
531 } else {
532 assert(t->is_primitive_type(), "should be a primitive type");
533 // cls.isInstance(obj) always returns false for primitive classes
534 set_constant(0);
535 }
536 }
537 break;
538 }
539 case vmIntrinsics::_isPrimitive : {
540 assert(x->number_of_arguments() == 1, "wrong type");
541
542 // Class.isPrimitive is known on constant classes:
543 InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
544 if (c != NULL && !c->value()->is_null_object()) {
545 ciType* t = c->value()->java_mirror_type();
546 set_constant(t->is_primitive_type());
547 }
548 break;
549 }
550 default:
551 break;
552 }
553 }
554
555 void Canonicalizer::do_Convert (Convert* x) {
556 if (x->value()->type()->is_constant()) {
557 switch (x->op()) {
558 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
559 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
560 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
561 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;
562 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;
563 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;
564 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;
565 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
566 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
567 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;
568 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
569 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
570 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;
571 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
574 ShouldNotReachHere();
575 }
576 }
577
578 Value value = x->value();
579 BasicType type = T_ILLEGAL;
580 LoadField* lf = value->as_LoadField();
581 if (lf) {
582 type = lf->field_type();
583 } else {
584 LoadIndexed* li = value->as_LoadIndexed();
585 if (li) {
586 type = li->elt_type();
587 } else {
588 Convert* conv = value->as_Convert();
589 if (conv) {
590 switch (conv->op()) {
591 case Bytecodes::_i2b: type = T_BYTE; break;
592 case Bytecodes::_i2s: type = T_SHORT; break;
593 case Bytecodes::_i2c: type = T_CHAR; break;
594 default : break;
595 }
596 }
597 }
598 }
599 if (type != T_ILLEGAL) {
600 switch (x->op()) {
601 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;
602 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
603 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;
604 default : break;
605 }
606 } else {
607 Op2* op2 = x->value()->as_Op2();
608 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
609 jint safebits = 0;
610 jint mask = op2->y()->type()->as_IntConstant()->value();
611 switch (x->op()) {
612 case Bytecodes::_i2b: safebits = 0x7f; break;
613 case Bytecodes::_i2s: safebits = 0x7fff; break;
614 case Bytecodes::_i2c: safebits = 0xffff; break;
615 default : break;
616 }
617 // When casting a masked integer to a smaller signed type, if
618 // the mask doesn't include the sign bit the cast isn't needed.
619 if (safebits && (mask & ~safebits) == 0) {
620 set_canonical(x->value());
621 }
622 }
623 }
624
625 }
626
627 void Canonicalizer::do_NullCheck (NullCheck* x) {
628 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
629 set_canonical(x->obj());
630 } else {
631 Constant* con = x->obj()->as_Constant();
632 if (con) {
633 ObjectType* c = con->type()->as_ObjectType();
634 if (c && c->is_loaded()) {
635 ObjectConstant* oc = c->as_ObjectConstant();
674 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
675 set_constant(0);
676 }
677 }
678
679 }
680 void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}
681 void Canonicalizer::do_MonitorExit (MonitorExit* x) {}
682 void Canonicalizer::do_BlockBegin (BlockBegin* x) {}
683 void Canonicalizer::do_Goto (Goto* x) {}
684
685
686 static bool is_true(jlong x, If::Condition cond, jlong y) {
687 switch (cond) {
688 case If::eql: return x == y;
689 case If::neq: return x != y;
690 case If::lss: return x < y;
691 case If::leq: return x <= y;
692 case If::gtr: return x > y;
693 case If::geq: return x >= y;
694 default:
695 ShouldNotReachHere();
696 return false;
697 }
698 }
699
700 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
701 // An Instruction with multiple successors, x, is replaced by a Goto
702 // to a single successor, sux. Is a safepoint check needed = was the
703 // instruction being replaced a safepoint and the single remaining
704 // successor a back branch?
705 return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
706 }
707
708 void Canonicalizer::do_If(If* x) {
709 // move const to right
710 if (x->x()->type()->is_constant()) x->swap_operands();
711 // simplify
712 const Value l = x->x(); ValueType* lt = l->type();
713 const Value r = x->y(); ValueType* rt = r->type();
714
715 if (l == r && !lt->is_float_kind()) {
716 // pattern: If (a cond a) => simplify to Goto
717 BlockBegin* sux = NULL;
762 // determine new condition & successors
763 If::Condition cond = If::eql;
764 BlockBegin* tsux = NULL;
765 BlockBegin* fsux = NULL;
766 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
767 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
768 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
769 else { ShouldNotReachHere(); }
770 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
771 if (cmp->x() == cmp->y()) {
772 do_If(canon);
773 } else {
774 if (compilation()->profile_branches()) {
775 // TODO: If profiling, leave floating point comparisons unoptimized.
776 // We currently do not support profiling of the unordered case.
777 switch(cmp->op()) {
778 case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
779 case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
780 set_canonical(x);
781 return;
782 default:
783 break;
784 }
785 }
786 set_bci(cmp->state_before()->bci());
787 set_canonical(canon);
788 }
789 }
790 } else if (l->as_InstanceOf() != NULL) {
791 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
792 // instruction in the graph (it is pinned). Need to fix this at some point.
793 // It should also be left in the graph when generating a profiled method version or Goto
794 // has to know that it was an InstanceOf.
795 return;
796 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
797 InstanceOf* inst = l->as_InstanceOf();
798 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
799 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
800 if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
801 // both successors identical and klass is loaded => simplify to: Goto
802 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
803 } else {
|