1 /*
  2  * Copyright (c) 1999, 2026, 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 "c1/c1_Canonicalizer.hpp"
 26 #include "c1/c1_InstructionPrinter.hpp"
 27 #include "c1/c1_ValueStack.hpp"
 28 #include "ci/ciArray.hpp"
 29 #include "runtime/sharedRuntime.hpp"
 30 
 31 
 32 class PrintValueVisitor: public ValueVisitor {
 33   void visit(Value* vp) {
 34     (*vp)->print_line();
 35   }
 36 };
 37 
 38 void Canonicalizer::set_canonical(Value x) {
 39   assert(x != nullptr, "value must exist");
 40   // Note: we can not currently substitute root nodes which show up in
 41   // the instruction stream (because the instruction list is embedded
 42   // in the instructions).
 43   if (canonical() != x) {
 44 #ifndef PRODUCT
 45     if (!x->has_printable_bci()) {
 46       x->set_printable_bci(bci());
 47     }
 48 #endif
 49     if (PrintCanonicalization) {
 50       PrintValueVisitor do_print_value;
 51       canonical()->input_values_do(&do_print_value);
 52       canonical()->print_line();
 53       tty->print_cr("canonicalized to:");
 54       x->input_values_do(&do_print_value);
 55       x->print_line();
 56       tty->cr();
 57     }
 58     assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
 59     _canonical = x;
 60   }
 61 }
 62 
 63 
 64 void Canonicalizer::move_const_to_right(Op2* x) {
 65   if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
 66 }
 67 
 68 
 69 void Canonicalizer::do_Op2(Op2* x) {
 70   if (x->x() == x->y()) {
 71     switch (x->op()) {
 72     case Bytecodes::_isub: set_constant(0); return;
 73     case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
 74     case Bytecodes::_iand: // fall through
 75     case Bytecodes::_land: // fall through
 76     case Bytecodes::_ior : // fall through
 77     case Bytecodes::_lor : set_canonical(x->x()); return;
 78     case Bytecodes::_ixor: set_constant(0); return;
 79     case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
 80     default              : break;
 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(java_add(a, b)); return;
 92             case Bytecodes::_isub: set_constant(java_subtract(a, b)); return;
 93             case Bytecodes::_imul: set_constant(java_multiply(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             default              : break;
118           }
119         }
120         break;
121       case longTag:
122         { jlong a = x->x()->type()->as_LongConstant()->value();
123           jlong b = x->y()->type()->as_LongConstant()->value();
124           switch (x->op()) {
125             case Bytecodes::_ladd: set_constant(java_add(a, b)); return;
126             case Bytecodes::_lsub: set_constant(java_subtract(a, b)); return;
127             case Bytecodes::_lmul: set_constant(java_multiply(a, b)); return;
128             case Bytecodes::_ldiv:
129               if (b != 0) {
130                 set_constant(SharedRuntime::ldiv(b, a));
131                 return;
132               }
133               break;
134             case Bytecodes::_lrem:
135               if (b != 0) {
136                 set_constant(SharedRuntime::lrem(b, a));
137                 return;
138               }
139               break;
140             case Bytecodes::_land: set_constant(a & b); return;
141             case Bytecodes::_lor : set_constant(a | b); return;
142             case Bytecodes::_lxor: set_constant(a ^ b); return;
143             default              : break;
144           }
145         }
146         break;
147       default:
148         // other cases not implemented (must be extremely careful with floats & doubles!)
149         break;
150     }
151   }
152   // make sure constant is on the right side, if any
153   move_const_to_right(x);
154 
155   if (x->y()->type()->is_constant()) {
156     // do constant folding for selected operations
157     switch (x->type()->tag()) {
158       case intTag:
159         if (x->y()->type()->as_IntConstant()->value() == 0) {
160           switch (x->op()) {
161             case Bytecodes::_iadd: set_canonical(x->x()); return;
162             case Bytecodes::_isub: set_canonical(x->x()); return;
163             case Bytecodes::_imul: set_constant(0); return;
164               // Note: for div and rem, make sure that C semantics
165               //       corresponds to Java semantics!
166             case Bytecodes::_iand: set_constant(0); return;
167             case Bytecodes::_ior : set_canonical(x->x()); return;
168             default              : break;
169           }
170         }
171         break;
172       case longTag:
173         if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
174           switch (x->op()) {
175             case Bytecodes::_ladd: set_canonical(x->x()); return;
176             case Bytecodes::_lsub: set_canonical(x->x()); return;
177             case Bytecodes::_lmul: set_constant((jlong)0); return;
178               // Note: for div and rem, make sure that C semantics
179               //       corresponds to Java semantics!
180             case Bytecodes::_land: set_constant((jlong)0); return;
181             case Bytecodes::_lor : set_canonical(x->x()); return;
182             default              : break;
183           }
184         }
185         break;
186       default:
187         break;
188     }
189   }
190 }
191 
192 
193 void Canonicalizer::do_Phi            (Phi*             x) {}
194 void Canonicalizer::do_Constant       (Constant*        x) {}
195 void Canonicalizer::do_Local          (Local*           x) {}
196 void Canonicalizer::do_LoadField      (LoadField*       x) {}
197 
198 // checks if v is in the block that is currently processed by
199 // GraphBuilder. This is the only block that has not BlockEnd yet.
200 static bool in_current_block(Value v) {
201   int max_distance = 4;
202   while (max_distance > 0 && v != nullptr && v->as_BlockEnd() == nullptr) {
203     v = v->next();
204     max_distance--;
205   }
206   return v == nullptr;
207 }
208 
209 void Canonicalizer::do_StoreField     (StoreField*      x) {
210   // If a value is going to be stored into a field or array some of
211   // the conversions emitted by javac are unneeded because the fields
212   // are packed to their natural size.
213   Convert* conv = x->value()->as_Convert();
214   if (conv) {
215     Value value = nullptr;
216     BasicType type = x->field()->type()->basic_type();
217     switch (conv->op()) {
218     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
219     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
220     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE)  value = conv->value(); break;
221     default             : break;
222     }
223     // limit this optimization to current block
224     if (value != nullptr && in_current_block(conv)) {
225       set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
226                                    x->state_before(), x->needs_patching()));
227       return;
228     }
229   }
230 
231 }
232 
233 void Canonicalizer::do_ArrayLength    (ArrayLength*     x) {
234   NewArray*  na;
235   Constant*  ct;
236   LoadField* lf;
237 
238   if ((na = x->array()->as_NewArray()) != nullptr) {
239     // New arrays might have the known length.
240     // Do not use the Constant itself, but create a new Constant
241     // with same value Otherwise a Constant is live over multiple
242     // blocks without being registered in a state array.
243     Constant* length;
244     NewMultiArray* nma;
245     if (na->length() != nullptr &&
246         (length = na->length()->as_Constant()) != nullptr) {
247       assert(length->type()->as_IntConstant() != nullptr, "array length must be integer");
248       set_constant(length->type()->as_IntConstant()->value());
249     } else if ((nma = x->array()->as_NewMultiArray()) != nullptr &&
250                (length = nma->dims()->at(0)->as_Constant()) != nullptr) {
251       assert(length->type()->as_IntConstant() != nullptr, "array length must be integer");
252       set_constant(length->type()->as_IntConstant()->value());
253     }
254 
255   } else if ((ct = x->array()->as_Constant()) != nullptr) {
256     // Constant arrays have constant lengths.
257     ArrayConstant* cnst = ct->type()->as_ArrayConstant();
258     if (cnst != nullptr) {
259       set_constant(cnst->value()->length());
260     }
261 
262   } else if ((lf = x->array()->as_LoadField()) != nullptr) {
263     ciField* field = lf->field();
264     if (field->is_static_constant()) {
265       // Constant field loads are usually folded during parsing.
266       // But it doesn't happen with PatchALot, ScavengeRootsInCode < 2, or when
267       // holder class is being initialized during parsing (for static fields).
268       ciObject* c = field->constant_value().as_object();
269       if (!c->is_null_object()) {
270         set_constant(c->as_array()->length());
271       }
272     }
273   }
274 }
275 
276 void Canonicalizer::do_LoadIndexed    (LoadIndexed*     x) {
277   StableArrayConstant* array = x->array()->type()->as_StableArrayConstant();
278   IntConstant* index = x->index()->type()->as_IntConstant();
279 
280   assert(array == nullptr || FoldStableValues, "not enabled");
281 
282   // Constant fold loads from stable arrays.
283   if (!x->should_profile() && !x->mismatched() && array != nullptr && index != nullptr) {
284     jint idx = index->value();
285     if (idx < 0 || idx >= array->value()->length()) {
286       // Leave the load as is. The range check will handle it.
287       return;
288     }
289 
290     ciConstant field_val = array->value()->element_value(idx);
291     if (!field_val.is_null_or_zero()) {
292       jint dimension = array->dimension();
293       assert(dimension <= array->value()->array_type()->dimension(), "inconsistent info");
294       ValueType* value = nullptr;
295       if (dimension > 1) {
296         // Preserve information about the dimension for the element.
297         assert(field_val.as_object()->is_array(), "not an array");
298         value = new StableArrayConstant(field_val.as_object()->as_array(), dimension - 1);
299       } else {
300         assert(dimension == 1, "sanity");
301         value = as_ValueType(field_val);
302       }
303       set_canonical(new Constant(value));
304     }
305   }
306 }
307 
308 void Canonicalizer::do_StoreIndexed   (StoreIndexed*    x) {
309   // If a value is going to be stored into a field or array some of
310   // the conversions emitted by javac are unneeded because the fields
311   // are packed to their natural size.
312   Convert* conv = x->value()->as_Convert();
313   if (conv) {
314     Value value = nullptr;
315     BasicType type = x->elt_type();
316     switch (conv->op()) {
317     case Bytecodes::_i2b: if (type == T_BYTE)  value = conv->value(); break;
318     case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
319     case Bytecodes::_i2c: if (type == T_CHAR  || type == T_BYTE) value = conv->value(); break;
320     default             : break;
321     }
322     // limit this optimization to current block
323     if (value != nullptr && in_current_block(conv)) {
324       set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
325                                      x->elt_type(), value, x->state_before(),
326                                      x->check_boolean()));
327       return;
328     }
329   }
330 }
331 
332 
333 void Canonicalizer::do_NegateOp(NegateOp* x) {
334   ValueType* t = x->x()->type();
335   if (t->is_constant()) {
336     switch (t->tag()) {
337       case intTag   : set_constant(java_negate(t->as_IntConstant()->value())); return;
338       case longTag  : set_constant(java_negate(t->as_LongConstant()->value())); return;
339       case floatTag : set_constant(-t->as_FloatConstant()->value()); return;
340       case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
341       default       : ShouldNotReachHere();
342     }
343   }
344 }
345 
346 
347 void Canonicalizer::do_ArithmeticOp   (ArithmeticOp*    x) { do_Op2(x); }
348 
349 
350 void Canonicalizer::do_ShiftOp        (ShiftOp*         x) {
351   ValueType* t = x->x()->type();
352   ValueType* t2 = x->y()->type();
353   if (t->is_constant()) {
354     switch (t->tag()) {
355     case intTag   : if (t->as_IntConstant()->value() == 0)         { set_constant(0); return; } break;
356     case longTag  : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
357     default       : ShouldNotReachHere();
358     }
359     if (t2->is_constant()) {
360       if (t->tag() == intTag) {
361         jint value = t->as_IntConstant()->value();
362         jint shift = t2->as_IntConstant()->value();
363         switch (x->op()) {
364           case Bytecodes::_ishl:  set_constant(java_shift_left(value, shift)); return;
365           case Bytecodes::_ishr:  set_constant(java_shift_right(value, shift)); return;
366           case Bytecodes::_iushr: set_constant(java_shift_right_unsigned(value, shift)); return;
367           default:                break;
368         }
369       } else if (t->tag() == longTag) {
370         jlong value = t->as_LongConstant()->value();
371         jint shift = t2->as_IntConstant()->value();
372         switch (x->op()) {
373           case Bytecodes::_lshl:  set_constant(java_shift_left(value, shift)); return;
374           case Bytecodes::_lshr:  set_constant(java_shift_right(value, shift)); return;
375           case Bytecodes::_lushr: set_constant(java_shift_right_unsigned(value, shift)); 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(); return;
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: {
397         FloatConstant* fc = x->x()->type()->as_FloatConstant();
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);
440         else if (vx == vy)
441           set_constant(0);
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_IfOp(IfOp* x) {
471   // Currently, Canonicalizer is only used by GraphBuilder, and IfOp is only created by
472   // GraphBuilder when loading/storing flat fields, do nothing for now.
473 }
474 
475 
476 void Canonicalizer::do_Intrinsic      (Intrinsic*       x) {
477   switch (x->id()) {
478   case vmIntrinsics::_floatToRawIntBits   : {
479     FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
480     if (c != nullptr) {
481       JavaValue v;
482       v.set_jfloat(c->value());
483       set_constant(v.get_jint());
484     }
485     break;
486   }
487   case vmIntrinsics::_intBitsToFloat      : {
488     IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
489     if (c != nullptr) {
490       JavaValue v;
491       v.set_jint(c->value());
492       set_constant(v.get_jfloat());
493     }
494     break;
495   }
496   case vmIntrinsics::_doubleToRawLongBits : {
497     DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
498     if (c != nullptr) {
499       JavaValue v;
500       v.set_jdouble(c->value());
501       set_constant(v.get_jlong());
502     }
503     break;
504   }
505   case vmIntrinsics::_longBitsToDouble    : {
506     LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
507     if (c != nullptr) {
508       JavaValue v;
509       v.set_jlong(c->value());
510       set_constant(v.get_jdouble());
511     }
512     break;
513   }
514   case vmIntrinsics::_isInstance          : {
515     assert(x->number_of_arguments() == 2, "wrong type");
516 
517     InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();
518     if (c != nullptr && !c->value()->is_null_object()) {
519       // ciInstance::java_mirror_type() returns non-null only for Java mirrors
520       ciType* t = c->value()->java_mirror_type();
521       if (t->is_klass()) {
522         // substitute cls.isInstance(obj) of a constant Class into
523         // an InstanceOf instruction
524         InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before());
525         set_canonical(i);
526         // and try to canonicalize even further
527         do_InstanceOf(i);
528       } else {
529         assert(t->is_primitive_type(), "should be a primitive type");
530         // cls.isInstance(obj) always returns false for primitive classes
531         set_constant(0);
532       }
533     }
534     break;
535   }
536   default:
537     break;
538   }
539 }
540 
541 void Canonicalizer::do_Convert        (Convert*         x) {
542   if (x->value()->type()->is_constant()) {
543     switch (x->op()) {
544     case Bytecodes::_i2b:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
545     case Bytecodes::_i2s:  set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
546     case Bytecodes::_i2c:  set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
547     case Bytecodes::_i2l:  set_constant((jlong)(x->value()->type()->as_IntConstant()->value()));             break;
548     case Bytecodes::_i2f:  set_constant((float)(x->value()->type()->as_IntConstant()->value()));             break;
549     case Bytecodes::_i2d:  set_constant((double)(x->value()->type()->as_IntConstant()->value()));            break;
550     case Bytecodes::_l2i:  set_constant((int)(x->value()->type()->as_LongConstant()->value()));              break;
551     case Bytecodes::_l2f:  set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
552     case Bytecodes::_l2d:  set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
553     case Bytecodes::_f2d:  set_constant((double)(x->value()->type()->as_FloatConstant()->value()));          break;
554     case Bytecodes::_f2i:  set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
555     case Bytecodes::_f2l:  set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
556     case Bytecodes::_d2f:  set_constant((float)(x->value()->type()->as_DoubleConstant()->value()));          break;
557     case Bytecodes::_d2i:  set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
558     case Bytecodes::_d2l:  set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
559     default:
560       ShouldNotReachHere();
561     }
562   }
563 
564   Value value = x->value();
565   BasicType type = T_ILLEGAL;
566   LoadField* lf = value->as_LoadField();
567   if (lf) {
568     type = lf->field_type();
569   } else {
570     LoadIndexed* li = value->as_LoadIndexed();
571     if (li) {
572       type = li->elt_type();
573     } else {
574       Convert* conv = value->as_Convert();
575       if (conv) {
576         switch (conv->op()) {
577           case Bytecodes::_i2b: type = T_BYTE;  break;
578           case Bytecodes::_i2s: type = T_SHORT; break;
579           case Bytecodes::_i2c: type = T_CHAR;  break;
580           default             :                 break;
581         }
582       }
583     }
584   }
585   if (type != T_ILLEGAL) {
586     switch (x->op()) {
587       case Bytecodes::_i2b: if (type == T_BYTE)                    set_canonical(x->value()); break;
588       case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
589       case Bytecodes::_i2c: if (type == T_CHAR)                    set_canonical(x->value()); break;
590       default             :                                                                   break;
591     }
592   } else {
593     Op2* op2 = x->value()->as_Op2();
594     if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
595       jint safebits = 0;
596       jint mask = op2->y()->type()->as_IntConstant()->value();
597       switch (x->op()) {
598         case Bytecodes::_i2b: safebits = 0x7f;   break;
599         case Bytecodes::_i2s: safebits = 0x7fff; break;
600         case Bytecodes::_i2c: safebits = 0xffff; break;
601         default             :                    break;
602       }
603       // When casting a masked integer to a smaller signed type, if
604       // the mask doesn't include the sign bit the cast isn't needed.
605       if (safebits && (mask & ~safebits) == 0) {
606         set_canonical(x->value());
607       }
608     }
609   }
610 
611 }
612 
613 void Canonicalizer::do_NullCheck      (NullCheck*       x) {
614   if (x->obj()->as_NewArray() != nullptr || x->obj()->as_NewInstance() != nullptr) {
615     set_canonical(x->obj());
616   } else {
617     Constant* con = x->obj()->as_Constant();
618     if (con) {
619       ObjectType* c = con->type()->as_ObjectType();
620       if (c && c->is_loaded()) {
621         ObjectConstant* oc = c->as_ObjectConstant();
622         if (!oc || !oc->value()->is_null_object()) {
623           set_canonical(con);
624         }
625       }
626     }
627   }
628 }
629 
630 void Canonicalizer::do_TypeCast       (TypeCast*        x) {}
631 void Canonicalizer::do_Invoke         (Invoke*          x) {}
632 void Canonicalizer::do_NewInstance    (NewInstance*     x) {}
633 void Canonicalizer::do_NewTypeArray   (NewTypeArray*    x) {}
634 void Canonicalizer::do_NewObjectArray (NewObjectArray*  x) {}
635 void Canonicalizer::do_NewMultiArray  (NewMultiArray*   x) {}
636 void Canonicalizer::do_CheckCast      (CheckCast*       x) {
637   if (x->klass()->is_loaded()) {
638     Value obj = x->obj();
639     ciType* klass = obj->exact_type();
640     if (klass == nullptr) {
641       klass = obj->declared_type();
642     }
643     if (klass != nullptr && klass->is_loaded()) {
644       bool is_interface = klass->is_instance_klass() &&
645                           klass->as_instance_klass()->is_interface();
646       // Interface casts can't be statically optimized away since verifier doesn't
647       // enforce interface types in bytecode.
648       if (!is_interface && klass->is_subtype_of(x->klass()) && (!x->is_null_free() || obj->is_null_free())) {
649         assert(!x->klass()->is_inlinetype() || x->klass() == klass, "Inline klasses can't have subtypes");
650         set_canonical(obj);
651         return;
652       }
653     }
654     // checkcast of null returns null for non null-free klasses
655     if (!x->is_null_free() && obj->is_null_obj()) {
656       set_canonical(obj);
657     }
658   }
659 }
660 void Canonicalizer::do_InstanceOf     (InstanceOf*      x) {
661   if (x->klass()->is_loaded()) {
662     Value obj = x->obj();
663     ciType* exact = obj->exact_type();
664     if (exact != nullptr && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
665       set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
666       return;
667     }
668     // instanceof null returns false
669     if (obj->as_Constant() && obj->is_null_obj()) {
670       set_constant(0);
671     }
672   }
673 
674 }
675 void Canonicalizer::do_MonitorEnter   (MonitorEnter*    x) {}
676 void Canonicalizer::do_MonitorExit    (MonitorExit*     x) {}
677 void Canonicalizer::do_BlockBegin     (BlockBegin*      x) {}
678 void Canonicalizer::do_Goto           (Goto*            x) {}
679 
680 
681 static bool is_true(jlong x, If::Condition cond, jlong y) {
682   switch (cond) {
683     case If::eql: return x == y;
684     case If::neq: return x != y;
685     case If::lss: return x <  y;
686     case If::leq: return x <= y;
687     case If::gtr: return x >  y;
688     case If::geq: return x >= y;
689     default:
690       ShouldNotReachHere();
691       return false;
692   }
693 }
694 
695 static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {
696   // An Instruction with multiple successors, x, is replaced by a Goto
697   // to a single successor, sux. Is a safepoint check needed = was the
698   // instruction being replaced a safepoint and the single remaining
699   // successor a back branch?
700   return x->is_safepoint() && (sux->bci() < x->state_before()->bci());
701 }
702 
703 void Canonicalizer::do_If(If* x) {
704   // move const to right
705   if (x->x()->type()->is_constant()) x->swap_operands();
706   // simplify
707   const Value l = x->x(); ValueType* lt = l->type();
708   const Value r = x->y(); ValueType* rt = r->type();
709 
710   if (l == r && !lt->is_float_kind()) {
711     // pattern: If (a cond a) => simplify to Goto
712     BlockBegin* sux = nullptr;
713     switch (x->cond()) {
714     case If::eql: sux = x->sux_for(true);  break;
715     case If::neq: sux = x->sux_for(false); break;
716     case If::lss: sux = x->sux_for(false); break;
717     case If::leq: sux = x->sux_for(true);  break;
718     case If::gtr: sux = x->sux_for(false); break;
719     case If::geq: sux = x->sux_for(true);  break;
720     default: ShouldNotReachHere();
721     }
722     // If is a safepoint then the debug information should come from the state_before of the If.
723     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
724     return;
725   }
726 
727   if (lt->is_constant() && rt->is_constant()) {
728     if (x->x()->as_Constant() != nullptr) {
729       // pattern: If (lc cond rc) => simplify to: Goto
730       BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
731                                                        x->sux_for(true),
732                                                        x->sux_for(false));
733       if (sux != nullptr) {
734         // If is a safepoint then the debug information should come from the state_before of the If.
735         set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
736       }
737     }
738   } else if (rt->as_IntConstant() != nullptr) {
739     // pattern: If (l cond rc) => investigate further
740     const jint rc = rt->as_IntConstant()->value();
741     if (l->as_CompareOp() != nullptr) {
742       // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
743       CompareOp* cmp = l->as_CompareOp();
744       bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
745       BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
746       BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
747       BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
748       BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
749       // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
750       //       equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
751       //       lss_sux or gtr_sux.
752       if (lss_sux == eql_sux && eql_sux == gtr_sux) {
753         // all successors identical => simplify to: Goto
754         set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
755       } else {
756         // two successors differ and two successors are the same => simplify to: If (x cmp y)
757         // determine new condition & successors
758         If::Condition cond = If::eql;
759         BlockBegin* tsux = nullptr;
760         BlockBegin* fsux = nullptr;
761              if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
762         else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
763         else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
764         else                         { ShouldNotReachHere();                           }
765         If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, x->state_before(), x->is_safepoint());
766         if (cmp->x() == cmp->y()) {
767           do_If(canon);
768         } else {
769           if (compilation()->profile_branches() || compilation()->is_profiling()) {
770             // TODO: If profiling, leave floating point comparisons unoptimized.
771             // We currently do not support profiling of the unordered case.
772             switch(cmp->op()) {
773               case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:
774               case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:
775                 set_canonical(x);
776                 return;
777               default:
778                 break;
779             }
780           }
781           set_bci(cmp->state_before()->bci());
782           set_canonical(canon);
783         }
784       }
785     }
786   } else if (rt == objectNull &&
787            (l->as_NewInstance() || l->as_NewArray() ||
788              (l->as_Local() && l->as_Local()->is_receiver()))) {
789     if (x->cond() == Instruction::eql) {
790       BlockBegin* sux = x->fsux();
791       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
792     } else {
793       assert(x->cond() == Instruction::neq, "only other valid case");
794       BlockBegin* sux = x->tsux();
795       set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
796     }
797   }
798 }
799 
800 
801 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
802   if (x->tag()->type()->is_constant()) {
803     int v = x->tag()->type()->as_IntConstant()->value();
804     BlockBegin* sux = x->default_sux();
805     if (v >= x->lo_key() && v <= x->hi_key()) {
806       sux = x->sux_at(v - x->lo_key());
807     }
808     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
809   }
810 }
811 
812 
813 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
814   if (x->tag()->type()->is_constant()) {
815     int v = x->tag()->type()->as_IntConstant()->value();
816     BlockBegin* sux = x->default_sux();
817     int low = 0;
818     int high = x->length() - 1;
819     while (low <= high) {
820       int mid = low + ((high - low) >> 1);
821       int key = x->key_at(mid);
822       if (key == v) {
823         sux = x->sux_at(mid);
824         break;
825       } else if (key > v) {
826         high = mid - 1;
827       } else {
828         low = mid + 1;
829       }
830     }
831     set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));
832   }
833 }
834 
835 
836 void Canonicalizer::do_Return         (Return*          x) {}
837 void Canonicalizer::do_Throw          (Throw*           x) {}
838 void Canonicalizer::do_Base           (Base*            x) {}
839 void Canonicalizer::do_OsrEntry       (OsrEntry*        x) {}
840 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
841 void Canonicalizer::do_UnsafeGet      (UnsafeGet*       x) {}
842 void Canonicalizer::do_UnsafePut      (UnsafePut*       x) {}
843 void Canonicalizer::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
844 void Canonicalizer::do_ProfileCall    (ProfileCall*     x) {}
845 void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {}
846 void Canonicalizer::do_ProfileInvoke    (ProfileInvoke* x) {}
847 void Canonicalizer::do_ProfileACmpTypes (ProfileACmpTypes* x) {}
848 void Canonicalizer::do_RuntimeCall      (RuntimeCall* x) {}
849 void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
850 #ifdef ASSERT
851 void Canonicalizer::do_Assert         (Assert*          x) {}
852 #endif
853 void Canonicalizer::do_MemBar         (MemBar*          x) {}