< 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()) {

 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());

   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   // A delayed load produces a field within the array element. Let
 242   // Instruction::exact_type() below derive its type from declared_type().
 243   if (delayed() == nullptr && array_type != nullptr) {
 244     assert(array_type->is_array_klass(), "what else?");
 245     ciArrayKlass* ak = (ciArrayKlass*)array_type;
 246 
 247     if (ak->element_type()->is_instance_klass()) {
 248       ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type();
 249       if (ik->is_loaded() && ik->is_final()) {
 250         return ik;
 251       }
 252     }
 253   }
 254   return Instruction::exact_type();
 255 }
 256 

 257 ciType* LoadIndexed::declared_type() const {
 258   if (delayed() != nullptr) {
 259     // The LoadIndexed is fused with one or more following getfield bytecodes
 260     // and produces the final field value instead of the array element.
 261     return delayed()->field()->type();
 262   }
 263   ciType* array_type = array()->declared_type();
 264   if (array_type == nullptr || !array_type->is_loaded()) {
 265     return nullptr;
 266   }
 267   assert(array_type->is_array_klass(), "what else?");
 268   ciArrayKlass* ak = (ciArrayKlass*)array_type;
 269   return ak->element_type();
 270 }
 271 
 272 bool StoreIndexed::is_exact_flat_array_store() const {
 273   if (array()->is_loaded_flat_array() && value()->as_Constant() == nullptr && value()->declared_type() != nullptr) {
 274     ciKlass* element_klass = array()->declared_type()->as_flat_array_klass()->element_klass();
 275     ciKlass* actual_klass = value()->declared_type()->as_klass();
 276 
 277     // Inlining can expose more specific types than the callee's signature. In
 278     // this example, element_klass is MyValue1 and actual_klass is MyValue2, so
 279     // the array store check must be kept:
 280     //     void test45_inline(Object[] oa, Object o, int index) { oa[index] = o; }
 281     //     void test45(MyValue1[] va, int index, MyValue2 v) { test45_inline(va, v, index); }
 282     if (element_klass == actual_klass) {
 283       return true;
 284     }
 285   }
 286   return false;
 287 }
 288 
 289 ciType* LoadField::declared_type() const {
 290   return field()->type();
 291 }
 292 
 293 
 294 ciType* NewTypeArray::exact_type() const {
 295   return ciTypeArrayKlass::make(elt_type());
 296 }
 297 
 298 ciType* NewObjectArray::exact_type() const {
 299   // Resolve the default array properties used by anewarray to a concrete
 300   // layout klass (reference or flat), which is the exact type of the allocation.
 301   return ciObjArrayKlass::make(klass());
 302 }
 303 
 304 ciType* NewMultiArray::exact_type() const {
 305   return _klass;
 306 }
 307 
 308 ciType* NewArray::declared_type() const {
 309   return exact_type();
 310 }
 311 
 312 ciType* NewInstance::exact_type() const {
 313   return klass();
 314 }
 315 
 316 ciType* NewInstance::declared_type() const {
 317   return exact_type();
 318 }
 319 
 320 ciType* CheckCast::declared_type() const {
 321   return klass();
 322 }
 323 
 324 // Implementation of ArithmeticOp
 325 
 326 bool ArithmeticOp::is_commutative() const {
 327   switch (op()) {

 398 void StateSplit::state_values_do(ValueVisitor* f) {
 399   Instruction::state_values_do(f);
 400   if (state() != nullptr) state()->values_do(f);
 401 }
 402 
 403 
 404 void BlockBegin::state_values_do(ValueVisitor* f) {
 405   StateSplit::state_values_do(f);
 406 
 407   if (is_set(BlockBegin::exception_entry_flag)) {
 408     for (int i = 0; i < number_of_exception_states(); i++) {
 409       exception_state_at(i)->values_do(f);
 410     }
 411   }
 412 }
 413 
 414 
 415 // Implementation of Invoke
 416 
 417 
 418 Invoke::Invoke(Bytecodes::Code code, ciType* return_type, Value recv, Values* args,
 419                ciMethod* target, ValueStack* state_before)
 420   : StateSplit(as_ValueType(return_type), state_before)
 421   , _code(code)
 422   , _recv(recv)
 423   , _args(args)
 424   , _target(target)
 425   , _return_type(return_type)
 426 {
 427   set_flag(TargetIsLoadedFlag,   target->is_loaded());
 428   set_flag(TargetIsFinalFlag,    target_is_loaded() && target->is_final_method());
 429 
 430   assert(args != nullptr, "args must exist");
 431 #ifdef ASSERT
 432   AssertValues assert_value;
 433   values_do(&assert_value);
 434 #endif
 435 
 436   // provide an initial guess of signature size.
 437   _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0));
 438   if (has_receiver()) {
 439     _signature->append(as_BasicType(receiver()->type()));
 440   }
 441   for (int i = 0; i < number_of_arguments(); i++) {
 442     ValueType* t = argument_at(i)->type();
 443     BasicType bt = as_BasicType(t);
 444     _signature->append(bt);
 445   }
 446 }
 447 
 448 
 449 void Invoke::state_values_do(ValueVisitor* f) {
 450   StateSplit::state_values_do(f);
 451   if (state_before() != nullptr) state_before()->values_do(f);
 452   if (state()        != nullptr) state()->values_do(f);
 453 }
 454 
 455 ciType* Invoke::declared_type() const {
 456   assert(_return_type->basic_type() != T_VOID, "need return value of void method?");
 457   return _return_type;


 458 }
 459 
 460 // Implementation of Constant
 461 intx Constant::hash() const {
 462   if (state_before() == nullptr) {
 463     switch (type()->tag()) {
 464     case intTag:
 465       return HASH2(name(), type()->as_IntConstant()->value());
 466     case addressTag:
 467       return HASH2(name(), type()->as_AddressConstant()->value());
 468     case longTag:
 469       {
 470         jlong temp = type()->as_LongConstant()->value();
 471         return HASH3(name(), high(temp), low(temp));
 472       }
 473     case floatTag:
 474       return HASH2(name(), jint_cast(type()->as_FloatConstant()->value()));
 475     case doubleTag:
 476       {
 477         jlong temp = jlong_cast(type()->as_DoubleConstant()->value());
< prev index next >