1 /*
  2  * Copyright (c) 1999, 2023, 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 {
 34   void visit(Value* vp) {
 35     (*vp)->print_line();
 36   }
 37 };
 38 
 39 void Canonicalizer::set_canonical(Value x) {
 40   assert(x != nullptr, "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 
 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(java_add(a, b)); return;
 93             case Bytecodes::_isub: set_constant(java_subtract(a, b)); return;
 94             case Bytecodes::_imul: set_constant(java_multiply(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(java_add(a, b)); return;
127             case Bytecodes::_lsub: set_constant(java_subtract(a, b)); return;
128             case Bytecodes::_lmul: set_constant(java_multiply(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 != nullptr && v->as_BlockEnd() == nullptr) {
204     v = v->next();
205     max_distance--;
206   }
207   return v == nullptr;
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 = nullptr;
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 != nullptr && 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()) != nullptr) {
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     NewMultiArray* nma;
246     if (na->length() != nullptr &&
247         (length = na->length()->as_Constant()) != nullptr) {
248       assert(length->type()->as_IntConstant() != nullptr, "array length must be integer");
249       set_constant(length->type()->as_IntConstant()->value());
250     } else if ((nma = x->array()->as_NewMultiArray()) != nullptr &&
251                (length = nma->dims()->at(0)->as_Constant()) != nullptr) {
252       assert(length->type()->as_IntConstant() != nullptr, "array length must be integer");
253       set_constant(length->type()->as_IntConstant()->value());
254     }
255 
256   } else if ((ct = x->array()->as_Constant()) != nullptr) {
257     // Constant arrays have constant lengths.
258     ArrayConstant* cnst = ct->type()->as_ArrayConstant();
259     if (cnst != nullptr) {
260       set_constant(cnst->value()->length());
261     }
262 
263   } else if ((lf = x->array()->as_LoadField()) != nullptr) {
264     ciField* field = lf->field();
265     if (field->is_static_constant()) {
266       // Constant field loads are usually folded during parsing.
267       // But it doesn't happen with PatchALot, ScavengeRootsInCode < 2, or when
268       // holder class is being initialized during parsing (for static fields).
269       ciObject* c = field->constant_value().as_object();
270       if (!c->is_null_object()) {
271         set_constant(c->as_array()->length());
272       }
273     }
274   }
275 }
276 
277 void Canonicalizer::do_LoadIndexed    (LoadIndexed*     x) {
278   StableArrayConstant* array = x->array()->type()->as_StableArrayConstant();
279   IntConstant* index = x->index()->type()->as_IntConstant();
280 
281   assert(array == nullptr || FoldStableValues, "not enabled");
282 
283   // Constant fold loads from stable arrays.
284   if (!x->mismatched() && array != nullptr && index != nullptr) {
285     jint idx = index->value();
286     if (idx < 0 || idx >= array->value()->length()) {
287       // Leave the load as is. The range check will handle it.
288       return;
289     }
290 
291     ciConstant field_val = array->value()->element_value(idx);
292     if (!field_val.is_null_or_zero()) {
293       jint dimension = array->dimension();
294       assert(dimension <= array->value()->array_type()->dimension(), "inconsistent info");
295       ValueType* value = nullptr;
296       if (dimension > 1) {
297         // Preserve information about the dimension for the element.
298         assert(field_val.as_object()->is_array(), "not an array");
299         value = new StableArrayConstant(field_val.as_object()->as_array(), dimension - 1);
300       } else {
301         assert(dimension == 1, "sanity");
302         value = as_ValueType(field_val);
303       }
304       set_canonical(new Constant(value));
305     }
306   }
307 }
308 
309 void Canonicalizer::do_StoreIndexed   (StoreIndexed*    x) {
310   // If a value is going to be stored into a field or array some of
311   // the conversions emitted by javac are unneeded because the fields
312   // are packed to their natural size.
313   Convert* conv = x->value()->as_Convert();
314   if (conv) {
315     Value value = nullptr;
316     BasicType type = x->elt_type();
317     switch (conv->op()) {
318     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
319     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
320     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
321     default             : break;
322     }
323     // limit this optimization to current block
324     if (value != nullptr && in_current_block(conv)) {
325       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
326                                      x->elt_type(), value, x->state_before(),
327                                      x->check_boolean()));
328       return;
329     }
330   }
331 }
332 
333 
334 void Canonicalizer::do_NegateOp(NegateOp* x) {
335   ValueType* t = x->x()->type();
336   if (t->is_constant()) {
337     switch (t->tag()) {
338       case intTag   : set_constant(java_negate(t->as_IntConstant()->value())); return;
339       case longTag  : set_constant(java_negate(t->as_LongConstant()->value())); return;
340       case floatTag : set_constant(-t->as_FloatConstant()->value()); return;
341       case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
342       default       : ShouldNotReachHere();
343     }
344   }
345 }
346 
347 
348 void Canonicalizer::do_ArithmeticOp   (ArithmeticOp*    x) { do_Op2(x); }
349 
350 
351 void Canonicalizer::do_ShiftOp        (ShiftOp*         x) {
352   ValueType* t = x->x()->type();
353   ValueType* t2 = x->y()->type();
354   if (t->is_constant()) {
355     switch (t->tag()) {
356     case intTag   : if (t->as_IntConstant()->value() == 0)         { set_constant(0); return; } break;
357     case longTag  : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
358     default       : ShouldNotReachHere();
359     }
360     if (t2->is_constant()) {
361       if (t->tag() == intTag) {
362         jint value = t->as_IntConstant()->value();
363         jint shift = t2->as_IntConstant()->value();
364         switch (x->op()) {
365           case Bytecodes::_ishl:  set_constant(java_shift_left(value, shift)); return;
366           case Bytecodes::_ishr:  set_constant(java_shift_right(value, shift)); return;
367           case Bytecodes::_iushr: set_constant(java_shift_right_unsigned(value, shift)); return;
368           default:                break;
369         }
370       } else if (t->tag() == longTag) {
371         jlong value = t->as_LongConstant()->value();
372         jint shift = t2->as_IntConstant()->value();
373         switch (x->op()) {
374           case Bytecodes::_lshl:  set_constant(java_shift_left(value, shift)); return;
375           case Bytecodes::_lshr:  set_constant(java_shift_right(value, shift)); return;
376           case Bytecodes::_lushr: set_constant(java_shift_right_unsigned(value, shift)); return;
377           default:                break;
378         }
379       }
380     }
381   }
382   if (t2->is_constant()) {
383     switch (t2->tag()) {
384       case intTag   : if (t2->as_IntConstant()->value() == 0)  set_canonical(x->x()); return;
385       case longTag  : if (t2->as_LongConstant()->value() == (jlong)0)  set_canonical(x->x()); return;
386       default       : ShouldNotReachHere(); return;
387     }
388   }
389 }
390 
391 
392 void Canonicalizer::do_LogicOp        (LogicOp*         x) { do_Op2(x); }
393 void Canonicalizer::do_CompareOp      (CompareOp*       x) {
394   if (x->x() == x->y()) {
395     switch (x->x()->type()->tag()) {
396       case longTag: set_constant(0); break;
397       case floatTag: {
398         FloatConstant* fc = x->x()->type()->as_FloatConstant();
399         if (fc) {
400           if (g_isnan(fc->value())) {
401             set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
402           } else {
403             set_constant(0);
404           }
405         }
406         break;
407       }
408       case doubleTag: {
409         DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
410         if (dc) {
411           if (g_isnan(dc->value())) {
412             set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
413           } else {
414             set_constant(0);
415           }
416         }
417         break;
418       }
419       default:
420         break;
421     }
422   } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
423     switch (x->x()->type()->tag()) {
424       case longTag: {
425         jlong vx = x->x()->type()->as_LongConstant()->value();
426         jlong vy = x->y()->type()->as_LongConstant()->value();
427         if (vx == vy)
428           set_constant(0);
429         else if (vx < vy)
430           set_constant(-1);
431         else
432           set_constant(1);
433         break;
434       }
435 
436       case floatTag: {
437         float vx = x->x()->type()->as_FloatConstant()->value();
438         float vy = x->y()->type()->as_FloatConstant()->value();
439         if (g_isnan(vx) || g_isnan(vy))
440           set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
441         else if (vx == vy)
442           set_constant(0);
443         else if (vx < vy)
444           set_constant(-1);
445         else
446           set_constant(1);
447         break;
448       }
449 
450       case doubleTag: {
451         double vx = x->x()->type()->as_DoubleConstant()->value();
452         double vy = x->y()->type()->as_DoubleConstant()->value();
453         if (g_isnan(vx) || g_isnan(vy))
454           set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
455         else if (vx == vy)
456           set_constant(0);
457         else if (vx < vy)
458           set_constant(-1);
459         else
460           set_constant(1);
461         break;
462       }
463 
464       default:
465         break;
466     }
467   }
468 }
469 
470 
471 void Canonicalizer::do_IfOp(IfOp* x) {
472   // Currently, Canonicalizer is only used by GraphBuilder,
473   // and IfOp is not created by GraphBuilder but only later
474   // when eliminating conditional expressions with CE_Eliminator,
475   // so this method will not be called.
476   ShouldNotReachHere();
477 }
478 
479 
480 void Canonicalizer::do_Intrinsic      (Intrinsic*       x) {
481   switch (x->id()) {
482   case vmIntrinsics::_floatToRawIntBits   : {
483     FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
484     if (c != nullptr) {
485       JavaValue v;
486       v.set_jfloat(c->value());
487       set_constant(v.get_jint());
488     }
489     break;
490   }
491   case vmIntrinsics::_intBitsToFloat      : {
492     IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
493     if (c != nullptr) {
494       JavaValue v;
495       v.set_jint(c->value());
496       set_constant(v.get_jfloat());
497     }
498     break;
499   }
500   case vmIntrinsics::_doubleToRawLongBits : {
501     DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
502     if (c != nullptr) {
503       JavaValue v;
504       v.set_jdouble(c->value());
505       set_constant(v.get_jlong());
506     }
507     break;
508   }
509   case vmIntrinsics::_longBitsToDouble    : {
510     LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
511     if (c != nullptr) {
512       JavaValue v;
513       v.set_jlong(c->value());
514       set_constant(v.get_jdouble());
515     }
516     break;
517   }
518   case vmIntrinsics::_isInstance          : {
519     assert(x->number_of_arguments() == 2, "wrong type");
520 
521     InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
522     if (c != nullptr && !c->value()->is_null_object()) {
523       // ciInstance::java_mirror_type() returns non-null only for Java mirrors
524       ciType* t = c->value()->java_mirror_type();
525       if (t->is_klass()) {
526         // substitute cls.isInstance(obj) of a constant Class into
527         // an InstantOf instruction
528         InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before());
529         set_canonical(i);
530         // and try to canonicalize even further
531         do_InstanceOf(i);
532       } else {
533         assert(t->is_primitive_type(), "should be a primitive type");
534         // cls.isInstance(obj) always returns false for primitive classes
535         set_constant(0);
536       }
537     }
538     break;
539   }
540   case vmIntrinsics::_isPrimitive        : {
541     assert(x->number_of_arguments() == 1, "wrong type");
542 
543     // Class.isPrimitive is known on constant classes:
544     InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
545     if (c != nullptr && !c->value()->is_null_object()) {
546       ciType* t = c->value()->java_mirror_type();
547       set_constant(t->is_primitive_type());
548     }
549     break;
550   }
551   case vmIntrinsics::_getModifiers: {
552     assert(x->number_of_arguments() == 1, "wrong type");
553 
554     // Optimize for Foo.class.getModifier()
555     InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
556     if (c != nullptr && !c->value()->is_null_object()) {
557       ciType* t = c->value()->java_mirror_type();
558       if (t->is_klass()) {
559         set_constant(t->as_klass()->modifier_flags());
560       } else {
561         assert(t->is_primitive_type(), "should be a primitive type");
562         set_constant(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
563       }
564     }
565     break;
566   }
567   default:
568     break;
569   }
570 }
571 
572 void Canonicalizer::do_Convert        (Convert*         x) {
573   if (x->value()->type()->is_constant()) {
574     switch (x->op()) {
575     case Bytecodes::_i2b:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
576     case Bytecodes::_i2s:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
577     case Bytecodes::_i2c:  set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
578     case Bytecodes::_i2l:  set_constant((jlong)(x->value()->type()->as_IntConstant()->value()));             break;
579     case Bytecodes::_i2f:  set_constant((float)(x->value()->type()->as_IntConstant()->value()));             break;
580     case Bytecodes::_i2d:  set_constant((double)(x->value()->type()->as_IntConstant()->value()));            break;
581     case Bytecodes::_l2i:  set_constant((int)(x->value()->type()->as_LongConstant()->value()));              break;
582     case Bytecodes::_l2f:  set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
583     case Bytecodes::_l2d:  set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
584     case Bytecodes::_f2d:  set_constant((double)(x->value()->type()->as_FloatConstant()->value()));          break;
585     case Bytecodes::_f2i:  set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
586     case Bytecodes::_f2l:  set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
587     case Bytecodes::_d2f:  set_constant((float)(x->value()->type()->as_DoubleConstant()->value()));          break;
588     case Bytecodes::_d2i:  set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
589     case Bytecodes::_d2l:  set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
590     default:
591       ShouldNotReachHere();
592     }
593   }
594 
595   Value value = x->value();
596   BasicType type = T_ILLEGAL;
597   LoadField* lf = value->as_LoadField();
598   if (lf) {
599     type = lf->field_type();
600   } else {
601     LoadIndexed* li = value->as_LoadIndexed();
602     if (li) {
603       type = li->elt_type();
604     } else {
605       Convert* conv = value->as_Convert();
606       if (conv) {
607         switch (conv->op()) {
608           case Bytecodes::_i2b: type = T_BYTE;  break;
609           case Bytecodes::_i2s: type = T_SHORT; break;
610           case Bytecodes::_i2c: type = T_CHAR;  break;
611           default             :                 break;
612         }
613       }
614     }
615   }
616   if (type != T_ILLEGAL) {
617     switch (x->op()) {
618       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
619       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
620       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
621       default             :                                                                   break;
622     }
623   } else {
624     Op2* op2 = x->value()->as_Op2();
625     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
626       jint safebits = 0;
627       jint mask = op2->y()->type()->as_IntConstant()->value();
628       switch (x->op()) {
629         case Bytecodes::_i2b: safebits = 0x7f;   break;
630         case Bytecodes::_i2s: safebits = 0x7fff; break;
631         case Bytecodes::_i2c: safebits = 0xffff; break;
632         default             :                    break;
633       }
634       // When casting a masked integer to a smaller signed type, if
635       // the mask doesn't include the sign bit the cast isn't needed.
636       if (safebits && (mask & ~safebits) == 0) {
637         set_canonical(x->value());
638       }
639     }
640   }
641 
642 }
643 
644 void Canonicalizer::do_NullCheck      (NullCheck*       x) {
645   if (x->obj()->as_NewArray() != nullptr || x->obj()->as_NewInstance() != nullptr) {
646     set_canonical(x->obj());
647   } else {
648     Constant* con = x->obj()->as_Constant();
649     if (con) {
650       ObjectType* c = con->type()->as_ObjectType();
651       if (c && c->is_loaded()) {
652         ObjectConstant* oc = c->as_ObjectConstant();
653         if (!oc || !oc->value()->is_null_object()) {
654           set_canonical(con);
655         }
656       }
657     }
658   }
659 }
660 
661 void Canonicalizer::do_TypeCast       (TypeCast*        x) {}
662 void Canonicalizer::do_Invoke         (Invoke*          x) {}
663 void Canonicalizer::do_NewInstance    (NewInstance*     x) {}
664 void Canonicalizer::do_NewTypeArray   (NewTypeArray*    x) {}
665 void Canonicalizer::do_NewObjectArray (NewObjectArray*  x) {}
666 void Canonicalizer::do_NewMultiArray  (NewMultiArray*   x) {}
667 void Canonicalizer::do_CheckCast      (CheckCast*       x) {
668   if (x->klass()->is_loaded()) {
669     Value obj = x->obj();
670     ciType* klass = obj->exact_type();
671     if (klass == nullptr) {
672       klass = obj->declared_type();
673     }
674     if (klass != nullptr && klass->is_loaded()) {
675       bool is_interface = klass->is_instance_klass() &&
676                           klass->as_instance_klass()->is_interface();
677       // Interface casts can't be statically optimized away since verifier doesn't
678       // enforce interface types in bytecode.
679       if (!is_interface && klass->is_subtype_of(x->klass())) {
680         set_canonical(obj);
681         return;
682       }
683     }
684     // checkcast of null returns null
685     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
686       set_canonical(obj);
687     }
688   }
689 }
690 void Canonicalizer::do_InstanceOf     (InstanceOf*      x) {
691   if (x->klass()->is_loaded()) {
692     Value obj = x->obj();
693     ciType* exact = obj->exact_type();
694     if (exact != nullptr && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
695       set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
696       return;
697     }
698     // instanceof null returns false
699     if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
700       set_constant(0);
701     }
702   }
703 
704 }
705 void Canonicalizer::do_MonitorEnter   (MonitorEnter*    x) {}
706 void Canonicalizer::do_MonitorExit    (MonitorExit*     x) {}
707 void Canonicalizer::do_BlockBegin     (BlockBegin*      x) {}
708 void Canonicalizer::do_Goto           (Goto*            x) {}
709 
710 
711 static bool is_true(jlong x, If::Condition cond, jlong y) {
712   switch (cond) {
713     case If::eql: return x == y;
714     case If::neq: return x != y;
715     case If::lss: return x <  y;
716     case If::leq: return x <= y;
717     case If::gtr: return x >  y;
718     case If::geq: return x >= y;
719     default:
720       ShouldNotReachHere();
721       return false;
722   }
723 }
724 
725 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
726   // An Instruction with multiple successors, x, is replaced by a Goto
727   // to a single successor, sux. Is a safepoint check needed = was the
728   // instruction being replaced a safepoint and the single remaining
729   // successor a back branch?
730   return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
731 }
732 
733 void Canonicalizer::do_If(If* x) {
734   // move const to right
735   if (x->x()->type()->is_constant()) x->swap_operands();
736   // simplify
737   const Value l = x->x(); ValueType* lt = l->type();
738   const Value r = x->y(); ValueType* rt = r->type();
739 
740   if (l == r && !lt->is_float_kind()) {
741     // pattern: If (a cond a) => simplify to Goto
742     BlockBegin* sux = nullptr;
743     switch (x->cond()) {
744     case If::eql: sux = x->sux_for(true);  break;
745     case If::neq: sux = x->sux_for(false); break;
746     case If::lss: sux = x->sux_for(false); break;
747     case If::leq: sux = x->sux_for(true);  break;
748     case If::gtr: sux = x->sux_for(false); break;
749     case If::geq: sux = x->sux_for(true);  break;
750     default: ShouldNotReachHere();
751     }
752     // If is a safepoint then the debug information should come from the state_before of the If.
753     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
754     return;
755   }
756 
757   if (lt->is_constant() && rt->is_constant()) {
758     if (x->x()->as_Constant() != nullptr) {
759       // pattern: If (lc cond rc) => simplify to: Goto
760       BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
761                                                        x->sux_for(true),
762                                                        x->sux_for(false));
763       if (sux != nullptr) {
764         // If is a safepoint then the debug information should come from the state_before of the If.
765         set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
766       }
767     }
768   } else if (rt->as_IntConstant() != nullptr) {
769     // pattern: If (l cond rc) => investigate further
770     const jint rc = rt->as_IntConstant()->value();
771     if (l->as_CompareOp() != nullptr) {
772       // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
773       CompareOp* cmp = l->as_CompareOp();
774       bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
775       BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
776       BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
777       BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
778       BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
779       // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
780       //       equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
781       //       lss_sux or gtr_sux.
782       if (lss_sux == eql_sux && eql_sux == gtr_sux) {
783         // all successors identical => simplify to: Goto
784         set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
785       } else {
786         // two successors differ and two successors are the same => simplify to: If (x cmp y)
787         // determine new condition & successors
788         If::Condition cond = If::eql;
789         BlockBegin* tsux = nullptr;
790         BlockBegin* fsux = nullptr;
791              if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
792         else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
793         else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
794         else                         { ShouldNotReachHere();                           }
795         If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, x->state_before(), x->is_safepoint());
796         if (cmp->x() == cmp->y()) {
797           do_If(canon);
798         } else {
799           if (compilation()->profile_branches() || compilation()->is_profiling()) {
800             // TODO: If profiling, leave floating point comparisons unoptimized.
801             // We currently do not support profiling of the unordered case.
802             switch(cmp->op()) {
803               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
804               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
805                 set_canonical(x);
806                 return;
807               default:
808                 break;
809             }
810           }
811           set_bci(cmp->state_before()->bci());
812           set_canonical(canon);
813         }
814       }
815     }
816   } else if (rt == objectNull &&
817            (l->as_NewInstance() || l->as_NewArray() ||
818              (l->as_Local() && l->as_Local()->is_receiver()))) {
819     if (x->cond() == Instruction::eql) {
820       BlockBegin* sux = x->fsux();
821       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
822     } else {
823       assert(x->cond() == Instruction::neq, "only other valid case");
824       BlockBegin* sux = x->tsux();
825       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
826     }
827   }
828 }
829 
830 
831 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
832   if (x->tag()->type()->is_constant()) {
833     int v = x->tag()->type()->as_IntConstant()->value();
834     BlockBegin* sux = x->default_sux();
835     if (v >= x->lo_key() && v <= x->hi_key()) {
836       sux = x->sux_at(v - x->lo_key());
837     }
838     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
839   }
840 }
841 
842 
843 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
844   if (x->tag()->type()->is_constant()) {
845     int v = x->tag()->type()->as_IntConstant()->value();
846     BlockBegin* sux = x->default_sux();
847     int low = 0;
848     int high = x->length() - 1;
849     while (low <= high) {
850       int mid = low + ((high - low) >> 1);
851       int key = x->key_at(mid);
852       if (key == v) {
853         sux = x->sux_at(mid);
854         break;
855       } else if (key > v) {
856         high = mid - 1;
857       } else {
858         low = mid + 1;
859       }
860     }
861     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
862   }
863 }
864 
865 
866 void Canonicalizer::do_Return         (Return*          x) {}
867 void Canonicalizer::do_Throw          (Throw*           x) {}
868 void Canonicalizer::do_Base           (Base*            x) {}
869 void Canonicalizer::do_OsrEntry       (OsrEntry*        x) {}
870 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
871 void Canonicalizer::do_RoundFP        (RoundFP*         x) {}
872 void Canonicalizer::do_UnsafeGet      (UnsafeGet*       x) {}
873 void Canonicalizer::do_UnsafePut      (UnsafePut*       x) {}
874 void Canonicalizer::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
875 void Canonicalizer::do_ProfileCall    (ProfileCall*     x) {}
876 void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {}
877 void Canonicalizer::do_ProfileInvoke  (ProfileInvoke*   x) {}
878 void Canonicalizer::do_RuntimeCall    (RuntimeCall*     x) {}
879 void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
880 #ifdef ASSERT
881 void Canonicalizer::do_Assert         (Assert*          x) {}
882 #endif
883 void Canonicalizer::do_MemBar         (MemBar*          x) {}