< prev index next >

src/hotspot/share/c1/c1_Instruction.cpp

Print this page

   1 /*
   2  * Copyright (c) 1999, 2025, 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_Instruction.hpp"
  26 #include "c1/c1_InstructionPrinter.hpp"
  27 #include "c1/c1_IR.hpp"
  28 #include "c1/c1_ValueStack.hpp"


  29 #include "ci/ciObjArrayKlass.hpp"
  30 #include "ci/ciTypeArrayKlass.hpp"
  31 #include "utilities/bitMap.inline.hpp"
  32 
  33 
  34 // Implementation of Instruction
  35 
  36 
  37 int Instruction::dominator_depth() {
  38   int result = -1;
  39   if (block()) {
  40     result = block()->dominator_depth();
  41   }
  42   assert(result != -1 || this->as_Local(), "Only locals have dominator depth -1");
  43   return result;
  44 }
  45 
  46 Instruction::Condition Instruction::mirror(Condition cond) {
  47   switch (cond) {
  48     case eql: return eql;

  88   Instruction* p = nullptr;
  89   Instruction* q = block();
  90   while (q != this) {
  91     assert(q != nullptr, "this is not in the block's instruction list");
  92     p = q; q = q->next();
  93   }
  94   return p;
  95 }
  96 
  97 
  98 void Instruction::state_values_do(ValueVisitor* f) {
  99   if (state_before() != nullptr) {
 100     state_before()->values_do(f);
 101   }
 102   if (exception_state() != nullptr) {
 103     exception_state()->values_do(f);
 104   }
 105 }
 106 
 107 ciType* Instruction::exact_type() const {
 108   ciType* t =  declared_type();
 109   if (t != nullptr && t->is_klass()) {
 110     return t->as_klass()->exact_klass();
 111   }
 112   return nullptr;
 113 }
 114 
































































 115 
 116 #ifndef PRODUCT
 117 void Instruction::check_state(ValueStack* state) {
 118   if (state != nullptr) {
 119     state->verify();
 120   }
 121 }
 122 
 123 
 124 void Instruction::print() {
 125   InstructionPrinter ip;
 126   print(ip);
 127 }
 128 
 129 
 130 void Instruction::print_line() {
 131   InstructionPrinter ip;
 132   ip.print_line(this);
 133 }
 134 

 155     }
 156   }
 157 
 158   if (!this->check_flag(NeedsRangeCheckFlag)) {
 159     return false;
 160   }
 161 
 162   return true;
 163 }
 164 
 165 
 166 ciType* Constant::exact_type() const {
 167   if (type()->is_object() && type()->as_ObjectType()->is_loaded()) {
 168     return type()->as_ObjectType()->exact_type();
 169   }
 170   return nullptr;
 171 }
 172 
 173 ciType* LoadIndexed::exact_type() const {
 174   ciType* array_type = array()->exact_type();
 175   if (array_type != nullptr) {
 176     assert(array_type->is_array_klass(), "what else?");
 177     ciArrayKlass* ak = (ciArrayKlass*)array_type;
 178 
 179     if (ak->element_type()->is_instance_klass()) {
 180       ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type();
 181       if (ik->is_loaded() && ik->is_final()) {
 182         return ik;
 183       }
 184     }
 185   }
 186   return Instruction::exact_type();
 187 }
 188 
 189 
 190 ciType* LoadIndexed::declared_type() const {



 191   ciType* array_type = array()->declared_type();
 192   if (array_type == nullptr || !array_type->is_loaded()) {
 193     return nullptr;
 194   }
 195   assert(array_type->is_array_klass(), "what else?");
 196   ciArrayKlass* ak = (ciArrayKlass*)array_type;
 197   return ak->element_type();
 198 }
 199 














 200 
 201 ciType* LoadField::declared_type() const {
 202   return field()->type();
 203 }
 204 
 205 
 206 ciType* NewTypeArray::exact_type() const {
 207   return ciTypeArrayKlass::make(elt_type());
 208 }
 209 
 210 ciType* NewObjectArray::exact_type() const {

 211   return ciObjArrayKlass::make(klass());
 212 }
 213 




 214 ciType* NewArray::declared_type() const {
 215   return exact_type();
 216 }
 217 
 218 ciType* NewInstance::exact_type() const {
 219   return klass();
 220 }
 221 
 222 ciType* NewInstance::declared_type() const {
 223   return exact_type();
 224 }
 225 
 226 ciType* CheckCast::declared_type() const {
 227   return klass();
 228 }
 229 
 230 // Implementation of ArithmeticOp
 231 
 232 bool ArithmeticOp::is_commutative() const {
 233   switch (op()) {

 301 }
 302 
 303 
 304 void StateSplit::state_values_do(ValueVisitor* f) {
 305   Instruction::state_values_do(f);
 306   if (state() != nullptr) state()->values_do(f);
 307 }
 308 
 309 
 310 void BlockBegin::state_values_do(ValueVisitor* f) {
 311   StateSplit::state_values_do(f);
 312 
 313   if (is_set(BlockBegin::exception_entry_flag)) {
 314     for (int i = 0; i < number_of_exception_states(); i++) {
 315       exception_state_at(i)->values_do(f);
 316     }
 317   }
 318 }
 319 
 320 


























 321 // Implementation of Invoke
 322 
 323 
 324 Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args,
 325                ciMethod* target, ValueStack* state_before)
 326   : StateSplit(result_type, state_before)
 327   , _code(code)
 328   , _recv(recv)
 329   , _args(args)
 330   , _target(target)

 331 {
 332   set_flag(TargetIsLoadedFlag,   target->is_loaded());
 333   set_flag(TargetIsFinalFlag,    target_is_loaded() && target->is_final_method());
 334 
 335   assert(args != nullptr, "args must exist");
 336 #ifdef ASSERT
 337   AssertValues assert_value;
 338   values_do(&assert_value);
 339 #endif
 340 
 341   // provide an initial guess of signature size.
 342   _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0));
 343   if (has_receiver()) {
 344     _signature->append(as_BasicType(receiver()->type()));
 345   }
 346   for (int i = 0; i < number_of_arguments(); i++) {
 347     ValueType* t = argument_at(i)->type();

 348     BasicType bt = as_BasicType(t);
 349     _signature->append(bt);
 350   }
 351 }
 352 
 353 
 354 void Invoke::state_values_do(ValueVisitor* f) {
 355   StateSplit::state_values_do(f);
 356   if (state_before() != nullptr) state_before()->values_do(f);
 357   if (state()        != nullptr) state()->values_do(f);
 358 }
 359 
 360 ciType* Invoke::declared_type() const {
 361   ciSignature* declared_signature = state()->scope()->method()->get_declared_signature_at_bci(state()->bci());
 362   ciType *t = declared_signature->return_type();
 363   assert(t->basic_type() != T_VOID, "need return value of void method?");
 364   return t;
 365 }
 366 
 367 // Implementation of Constant
 368 intx Constant::hash() const {
 369   if (state_before() == nullptr) {
 370     switch (type()->tag()) {
 371     case intTag:
 372       return HASH2(name(), type()->as_IntConstant()->value());
 373     case addressTag:
 374       return HASH2(name(), type()->as_AddressConstant()->value());
 375     case longTag:
 376       {
 377         jlong temp = type()->as_LongConstant()->value();
 378         return HASH3(name(), high(temp), low(temp));
 379       }
 380     case floatTag:
 381       return HASH2(name(), jint_cast(type()->as_FloatConstant()->value()));
 382     case doubleTag:
 383       {
 384         jlong temp = jlong_cast(type()->as_DoubleConstant()->value());

 972   ip1.print_instr(x);
 973 
 974   stringStream strStream2;
 975   InstructionPrinter ip2(1, &strStream2);
 976   ip2.print_instr(y);
 977 
 978   stringStream ss;
 979   ss.print("Assertion %s %s %s in method %s", strStream1.freeze(), ip2.cond_name(cond), strStream2.freeze(), strStream.freeze());
 980 
 981   _message = ss.as_string();
 982 }
 983 #endif
 984 
 985 void RangeCheckPredicate::check_state() {
 986   assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state");
 987 }
 988 
 989 void ProfileInvoke::state_values_do(ValueVisitor* f) {
 990   if (state() != nullptr) state()->values_do(f);
 991 }


   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_Instruction.hpp"
  26 #include "c1/c1_InstructionPrinter.hpp"
  27 #include "c1/c1_IR.hpp"
  28 #include "c1/c1_ValueStack.hpp"
  29 #include "ci/ciFlatArrayKlass.hpp"
  30 #include "ci/ciInlineKlass.hpp"
  31 #include "ci/ciObjArrayKlass.hpp"
  32 #include "ci/ciTypeArrayKlass.hpp"
  33 #include "utilities/bitMap.inline.hpp"
  34 
  35 
  36 // Implementation of Instruction
  37 
  38 
  39 int Instruction::dominator_depth() {
  40   int result = -1;
  41   if (block()) {
  42     result = block()->dominator_depth();
  43   }
  44   assert(result != -1 || this->as_Local(), "Only locals have dominator depth -1");
  45   return result;
  46 }
  47 
  48 Instruction::Condition Instruction::mirror(Condition cond) {
  49   switch (cond) {
  50     case eql: return eql;

  90   Instruction* p = nullptr;
  91   Instruction* q = block();
  92   while (q != this) {
  93     assert(q != nullptr, "this is not in the block's instruction list");
  94     p = q; q = q->next();
  95   }
  96   return p;
  97 }
  98 
  99 
 100 void Instruction::state_values_do(ValueVisitor* f) {
 101   if (state_before() != nullptr) {
 102     state_before()->values_do(f);
 103   }
 104   if (exception_state() != nullptr) {
 105     exception_state()->values_do(f);
 106   }
 107 }
 108 
 109 ciType* Instruction::exact_type() const {
 110   ciType* t = declared_type();
 111   if (t != nullptr && t->is_klass()) {
 112     return t->as_klass()->exact_klass();
 113   }
 114   return nullptr;
 115 }
 116 
 117 ciKlass* Instruction::as_loaded_klass_or_null() const {
 118   ciType* type = declared_type();
 119   if (type != nullptr && type->is_klass()) {
 120     ciKlass* klass = type->as_klass();
 121     if (klass->is_loaded()) {
 122       return klass;
 123     }
 124   }
 125   return nullptr;
 126 }
 127 
 128 bool Instruction::is_loaded_flat_array() const {
 129   if (UseArrayFlattening) {
 130     ciType* type = declared_type();
 131     return type != nullptr && type->is_flat_array_klass();
 132   }
 133   return false;
 134 }
 135 
 136 bool Instruction::maybe_flat_array() const {
 137   if (UseArrayFlattening) {
 138     ciType* type = declared_type();
 139     if (type != nullptr) {
 140       if (type->is_ref_array_klass()) {
 141         return false;
 142       } else if (type->is_flat_array_klass()) {
 143         return true;
 144       } else if (type->is_obj_array_klass()) {
 145         // This is the unrefined array type
 146         ciKlass* element_klass = type->as_obj_array_klass()->element_klass();
 147         if (element_klass->can_be_inline_klass() && (!element_klass->is_inlinetype() || element_klass->as_inline_klass()->maybe_flat_in_array())) {
 148           return true;
 149         }
 150       } else if (type->is_klass() && type->as_klass()->is_java_lang_Object()) {
 151         // This can happen as a parameter to System.arraycopy()
 152         return true;
 153       }
 154     } else {
 155       // Type info gets lost during Phi merging (Phi, IfOp, etc), but we might be storing into a
 156       // flat array, so we should do a runtime check.
 157       return true;
 158     }
 159   }
 160   return false;
 161 }
 162 
 163 bool Instruction::maybe_null_free_array() const {
 164   ciType* type = declared_type();
 165   if (type != nullptr) {
 166     if (type->is_loaded() && type->is_array_klass() && type->as_array_klass()->is_refined()) {
 167       return type->as_array_klass()->is_elem_null_free();
 168     } else if (type->is_obj_array_klass()) {
 169       // Due to array covariance, the runtime type might be a null-free array.
 170       if (type->as_obj_array_klass()->can_be_inline_array_klass()) {
 171         return true;
 172       }
 173     }
 174   } else {
 175     // Type info gets lost during Phi merging (Phi, IfOp, etc), but we might be storing into a
 176     // null-free array, so we should do a runtime check.
 177     return true;
 178   }
 179   return false;
 180 }
 181 
 182 #ifndef PRODUCT
 183 void Instruction::check_state(ValueStack* state) {
 184   if (state != nullptr) {
 185     state->verify();
 186   }
 187 }
 188 
 189 
 190 void Instruction::print() {
 191   InstructionPrinter ip;
 192   print(ip);
 193 }
 194 
 195 
 196 void Instruction::print_line() {
 197   InstructionPrinter ip;
 198   ip.print_line(this);
 199 }
 200 

 221     }
 222   }
 223 
 224   if (!this->check_flag(NeedsRangeCheckFlag)) {
 225     return false;
 226   }
 227 
 228   return true;
 229 }
 230 
 231 
 232 ciType* Constant::exact_type() const {
 233   if (type()->is_object() && type()->as_ObjectType()->is_loaded()) {
 234     return type()->as_ObjectType()->exact_type();
 235   }
 236   return nullptr;
 237 }
 238 
 239 ciType* LoadIndexed::exact_type() const {
 240   ciType* array_type = array()->exact_type();
 241   if (delayed() == nullptr && array_type != nullptr) {
 242     assert(array_type->is_array_klass(), "what else?");
 243     ciArrayKlass* ak = (ciArrayKlass*)array_type;
 244 
 245     if (ak->element_type()->is_instance_klass()) {
 246       ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type();
 247       if (ik->is_loaded() && ik->is_final()) {
 248         return ik;
 249       }
 250     }
 251   }
 252   return Instruction::exact_type();
 253 }
 254 

 255 ciType* LoadIndexed::declared_type() const {
 256   if (delayed() != nullptr) {
 257     return delayed()->field()->type();
 258   }
 259   ciType* array_type = array()->declared_type();
 260   if (array_type == nullptr || !array_type->is_loaded()) {
 261     return nullptr;
 262   }
 263   assert(array_type->is_array_klass(), "what else?");
 264   ciArrayKlass* ak = (ciArrayKlass*)array_type;
 265   return ak->element_type();
 266 }
 267 
 268 bool StoreIndexed::is_exact_flat_array_store() const {
 269   if (array()->is_loaded_flat_array() && value()->as_Constant() == nullptr && value()->declared_type() != nullptr) {
 270     ciKlass* element_klass = array()->declared_type()->as_flat_array_klass()->element_klass();
 271     ciKlass* actual_klass = value()->declared_type()->as_klass();
 272 
 273     // The following check can fail with inlining:
 274     //     void test45_inline(Object[] oa, Object o, int index) { oa[index] = o; }
 275     //     void test45(MyValue1[] va, int index, MyValue2 v) { test45_inline(va, v, index); }
 276     if (element_klass == actual_klass) {
 277       return true;
 278     }
 279   }
 280   return false;
 281 }
 282 
 283 ciType* LoadField::declared_type() const {
 284   return field()->type();
 285 }
 286 
 287 
 288 ciType* NewTypeArray::exact_type() const {
 289   return ciTypeArrayKlass::make(elt_type());
 290 }
 291 
 292 ciType* NewObjectArray::exact_type() const {
 293   // Returns the refined type
 294   return ciObjArrayKlass::make(klass());
 295 }
 296 
 297 ciType* NewMultiArray::exact_type() const {
 298   return _klass;
 299 }
 300 
 301 ciType* NewArray::declared_type() const {
 302   return exact_type();
 303 }
 304 
 305 ciType* NewInstance::exact_type() const {
 306   return klass();
 307 }
 308 
 309 ciType* NewInstance::declared_type() const {
 310   return exact_type();
 311 }
 312 
 313 ciType* CheckCast::declared_type() const {
 314   return klass();
 315 }
 316 
 317 // Implementation of ArithmeticOp
 318 
 319 bool ArithmeticOp::is_commutative() const {
 320   switch (op()) {

 388 }
 389 
 390 
 391 void StateSplit::state_values_do(ValueVisitor* f) {
 392   Instruction::state_values_do(f);
 393   if (state() != nullptr) state()->values_do(f);
 394 }
 395 
 396 
 397 void BlockBegin::state_values_do(ValueVisitor* f) {
 398   StateSplit::state_values_do(f);
 399 
 400   if (is_set(BlockBegin::exception_entry_flag)) {
 401     for (int i = 0; i < number_of_exception_states(); i++) {
 402       exception_state_at(i)->values_do(f);
 403     }
 404   }
 405 }
 406 
 407 
 408 StoreField::StoreField(Value obj, int offset, ciField* field, Value value, bool is_static,
 409                        ValueStack* state_before, bool needs_patching)
 410   : AccessField(obj, offset, field, is_static, state_before, needs_patching)
 411   , _value(value)
 412   , _enclosing_field(nullptr)
 413 {
 414 #ifdef ASSERT
 415   AssertValues assert_value;
 416   values_do(&assert_value);
 417 #endif
 418   pin();
 419 }
 420 
 421 StoreIndexed::StoreIndexed(Value array, Value index, Value length, BasicType elt_type, Value value,
 422                            ValueStack* state_before, bool check_boolean, bool mismatched)
 423   : AccessIndexed(array, index, length, elt_type, state_before, mismatched)
 424   , _value(value), _check_boolean(check_boolean)
 425 {
 426 #ifdef ASSERT
 427   AssertValues assert_value;
 428   values_do(&assert_value);
 429 #endif
 430   pin();
 431 }
 432 
 433 
 434 // Implementation of Invoke
 435 
 436 
 437 Invoke::Invoke(Bytecodes::Code code, ciType* return_type, Value recv, Values* args,
 438                ciMethod* target, ValueStack* state_before)
 439   : StateSplit(as_ValueType(return_type), state_before)
 440   , _code(code)
 441   , _recv(recv)
 442   , _args(args)
 443   , _target(target)
 444   , _return_type(return_type)
 445 {
 446   set_flag(TargetIsLoadedFlag,   target->is_loaded());
 447   set_flag(TargetIsFinalFlag,    target_is_loaded() && target->is_final_method());
 448 
 449   assert(args != nullptr, "args must exist");
 450 #ifdef ASSERT
 451   AssertValues assert_value;
 452   values_do(&assert_value);
 453 #endif
 454 
 455   // provide an initial guess of signature size.
 456   _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0));
 457   if (has_receiver()) {
 458     _signature->append(as_BasicType(receiver()->type()));
 459   }
 460   for (int i = 0; i < number_of_arguments(); i++) {
 461     Value v = argument_at(i);
 462     ValueType* t = v->type();
 463     BasicType bt = as_BasicType(t);
 464     _signature->append(bt);
 465   }
 466 }
 467 
 468 
 469 void Invoke::state_values_do(ValueVisitor* f) {
 470   StateSplit::state_values_do(f);
 471   if (state_before() != nullptr) state_before()->values_do(f);
 472   if (state()        != nullptr) state()->values_do(f);
 473 }
 474 
 475 ciType* Invoke::declared_type() const {
 476   assert(_return_type->basic_type() != T_VOID, "need return value of void method?");
 477   return _return_type;


 478 }
 479 
 480 // Implementation of Constant
 481 intx Constant::hash() const {
 482   if (state_before() == nullptr) {
 483     switch (type()->tag()) {
 484     case intTag:
 485       return HASH2(name(), type()->as_IntConstant()->value());
 486     case addressTag:
 487       return HASH2(name(), type()->as_AddressConstant()->value());
 488     case longTag:
 489       {
 490         jlong temp = type()->as_LongConstant()->value();
 491         return HASH3(name(), high(temp), low(temp));
 492       }
 493     case floatTag:
 494       return HASH2(name(), jint_cast(type()->as_FloatConstant()->value()));
 495     case doubleTag:
 496       {
 497         jlong temp = jlong_cast(type()->as_DoubleConstant()->value());

1085   ip1.print_instr(x);
1086 
1087   stringStream strStream2;
1088   InstructionPrinter ip2(1, &strStream2);
1089   ip2.print_instr(y);
1090 
1091   stringStream ss;
1092   ss.print("Assertion %s %s %s in method %s", strStream1.freeze(), ip2.cond_name(cond), strStream2.freeze(), strStream.freeze());
1093 
1094   _message = ss.as_string();
1095 }
1096 #endif
1097 
1098 void RangeCheckPredicate::check_state() {
1099   assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state");
1100 }
1101 
1102 void ProfileInvoke::state_values_do(ValueVisitor* f) {
1103   if (state() != nullptr) state()->values_do(f);
1104 }
1105 
< prev index next >