1 /* 2 * Copyright (c) 2000, 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 #ifndef SHARE_CI_CITYPEFLOW_HPP 26 #define SHARE_CI_CITYPEFLOW_HPP 27 28 #ifdef COMPILER2 29 #include "ci/ciEnv.hpp" 30 #include "ci/ciKlass.hpp" 31 #include "ci/ciMethodBlocks.hpp" 32 #endif 33 34 35 class ciTypeFlow : public ArenaObj { 36 private: 37 ciEnv* _env; 38 ciMethod* _method; 39 int _osr_bci; 40 41 bool _has_irreducible_entry; 42 43 const char* _failure_reason; 44 45 public: 46 class StateVector; 47 class Loop; 48 class Block; 49 50 // Build a type flow analyzer 51 // Do an OSR analysis if osr_bci >= 0. 52 ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci = InvocationEntryBci); 53 54 // Accessors 55 ciMethod* method() const { return _method; } 56 ciEnv* env() { return _env; } 57 Arena* arena() { return _env->arena(); } 58 bool is_osr_flow() const{ return _osr_bci != InvocationEntryBci; } 59 int start_bci() const { return is_osr_flow()? _osr_bci: 0; } 60 int max_locals() const { return method()->max_locals(); } 61 int max_stack() const { return method()->max_stack(); } 62 int max_cells() const { return max_locals() + max_stack(); } 63 int code_size() const { return method()->code_size(); } 64 bool has_irreducible_entry() const { return _has_irreducible_entry; } 65 66 // Represents information about an "active" jsr call. This 67 // class represents a call to the routine at some entry address 68 // with some distinct return address. 69 class JsrRecord : public ArenaObj { 70 private: 71 int _entry_address; 72 int _return_address; 73 public: 74 JsrRecord(int entry_address, int return_address) { 75 _entry_address = entry_address; 76 _return_address = return_address; 77 } 78 79 int entry_address() const { return _entry_address; } 80 int return_address() const { return _return_address; } 81 82 void print_on(outputStream* st) const { 83 #ifndef PRODUCT 84 st->print("%d->%d", entry_address(), return_address()); 85 #endif 86 } 87 }; 88 89 // A JsrSet represents some set of JsrRecords. This class 90 // is used to record a set of all jsr routines which we permit 91 // execution to return (ret) from. 92 // 93 // During abstract interpretation, JsrSets are used to determine 94 // whether two paths which reach a given block are unique, and 95 // should be cloned apart, or are compatible, and should merge 96 // together. 97 // 98 // Note that different amounts of effort can be expended determining 99 // if paths are compatible. <DISCUSSION> 100 class JsrSet : public AnyObj { 101 private: 102 GrowableArray<JsrRecord*> _set; 103 104 JsrRecord* record_at(int i) { 105 return _set.at(i); 106 } 107 108 // Insert the given JsrRecord into the JsrSet, maintaining the order 109 // of the set and replacing any element with the same entry address. 110 void insert_jsr_record(JsrRecord* record); 111 112 // Remove the JsrRecord with the given return address from the JsrSet. 113 void remove_jsr_record(int return_address); 114 115 public: 116 JsrSet(Arena* arena, int default_len = 4); 117 JsrSet(int default_len = 4); 118 119 // Copy this JsrSet. 120 void copy_into(JsrSet* jsrs); 121 122 // Is this JsrSet compatible with some other JsrSet? 123 bool is_compatible_with(JsrSet* other); 124 125 // Apply the effect of a single bytecode to the JsrSet. 126 void apply_control(ciTypeFlow* analyzer, 127 ciBytecodeStream* str, 128 StateVector* state); 129 130 // What is the cardinality of this set? 131 int size() const { return _set.length(); } 132 133 void print_on(outputStream* st) const PRODUCT_RETURN; 134 }; 135 136 class LocalSet { 137 private: 138 enum Constants { max = 63 }; 139 uint64_t _bits; 140 public: 141 LocalSet() : _bits(0) {} 142 void add(uint32_t i) { if (i < (uint32_t)max) _bits |= (1LL << i); } 143 void add(LocalSet* ls) { _bits |= ls->_bits; } 144 bool test(uint32_t i) const { return i < (uint32_t)max ? (_bits>>i)&1U : true; } 145 void clear() { _bits = 0; } 146 void print_on(outputStream* st, int limit) const PRODUCT_RETURN; 147 }; 148 149 // Used as a combined index for locals and temps 150 enum Cell { 151 Cell_0, Cell_max = INT_MAX 152 }; 153 154 // A StateVector summarizes the type information at some 155 // point in the program 156 class StateVector : public AnyObj { 157 private: 158 ciType** _types; 159 int _stack_size; 160 int _monitor_count; 161 ciTypeFlow* _outer; 162 163 int _trap_bci; 164 int _trap_index; 165 166 LocalSet _def_locals; // For entire block 167 168 static ciType* type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer); 169 170 public: 171 // Special elements in our type lattice. 172 enum { 173 T_TOP = T_VOID, // why not? 174 T_BOTTOM = T_CONFLICT, 175 T_LONG2 = T_SHORT, // 2nd word of T_LONG 176 T_DOUBLE2 = T_CHAR, // 2nd word of T_DOUBLE 177 T_NULL = T_BYTE // for now. 178 }; 179 static ciType* top_type() { return ciType::make((BasicType)T_TOP); } 180 static ciType* bottom_type() { return ciType::make((BasicType)T_BOTTOM); } 181 static ciType* long2_type() { return ciType::make((BasicType)T_LONG2); } 182 static ciType* double2_type(){ return ciType::make((BasicType)T_DOUBLE2); } 183 static ciType* null_type() { return ciType::make((BasicType)T_NULL); } 184 185 static ciType* half_type(ciType* t) { 186 switch (t->basic_type()) { 187 case T_LONG: return long2_type(); 188 case T_DOUBLE: return double2_type(); 189 default: ShouldNotReachHere(); return nullptr; 190 } 191 } 192 193 // The meet operation for our type lattice. 194 ciType* type_meet(ciType* t1, ciType* t2) { 195 return type_meet_internal(t1, t2, outer()); 196 } 197 198 // Accessors 199 ciTypeFlow* outer() const { return _outer; } 200 201 int stack_size() const { return _stack_size; } 202 void set_stack_size(int ss) { _stack_size = ss; } 203 204 int monitor_count() const { return _monitor_count; } 205 void set_monitor_count(int mc) { _monitor_count = mc; } 206 207 LocalSet* def_locals() { return &_def_locals; } 208 const LocalSet* def_locals() const { return &_def_locals; } 209 210 static Cell start_cell() { return (Cell)0; } 211 static Cell next_cell(Cell c) { return (Cell)(((int)c) + 1); } 212 Cell limit_cell() const { 213 return (Cell)(outer()->max_locals() + stack_size()); 214 } 215 216 // Cell creation 217 Cell local(int lnum) const { 218 assert(lnum < outer()->max_locals(), "index check"); 219 return (Cell)(lnum); 220 } 221 222 Cell stack(int snum) const { 223 assert(snum < stack_size(), "index check"); 224 return (Cell)(outer()->max_locals() + snum); 225 } 226 227 Cell tos() const { return stack(stack_size()-1); } 228 229 // For external use only: 230 ciType* local_type_at(int i) const { return type_at(local(i)); } 231 ciType* stack_type_at(int i) const { return type_at(stack(i)); } 232 233 // Accessors for the type of some Cell c 234 ciType* type_at(Cell c) const { 235 assert(start_cell() <= c && c < limit_cell(), "out of bounds"); 236 return _types[c]; 237 } 238 239 void set_type_at(Cell c, ciType* type) { 240 assert(start_cell() <= c && c < limit_cell(), "out of bounds"); 241 _types[c] = type; 242 } 243 244 // Top-of-stack operations. 245 void set_type_at_tos(ciType* type) { set_type_at(tos(), type); } 246 ciType* type_at_tos() const { return type_at(tos()); } 247 248 void push(ciType* type) { 249 _stack_size++; 250 set_type_at_tos(type); 251 } 252 void pop() { 253 debug_only(set_type_at_tos(bottom_type())); 254 _stack_size--; 255 } 256 ciType* pop_value() { 257 ciType* t = type_at_tos(); 258 pop(); 259 return t; 260 } 261 262 // Convenience operations. 263 bool is_reference(ciType* type) const { 264 return type == null_type() || !type->is_primitive_type(); 265 } 266 bool is_int(ciType* type) const { 267 return type->basic_type() == T_INT; 268 } 269 bool is_long(ciType* type) const { 270 return type->basic_type() == T_LONG; 271 } 272 bool is_float(ciType* type) const { 273 return type->basic_type() == T_FLOAT; 274 } 275 bool is_double(ciType* type) const { 276 return type->basic_type() == T_DOUBLE; 277 } 278 279 void store_to_local(int lnum) { 280 _def_locals.add((uint) lnum); 281 } 282 283 void push_translate(ciType* type); 284 285 void push_int() { 286 push(ciType::make(T_INT)); 287 } 288 void pop_int() { 289 assert(is_int(type_at_tos()), "must be integer"); 290 pop(); 291 } 292 void check_int(Cell c) { 293 assert(is_int(type_at(c)), "must be integer"); 294 } 295 void push_double() { 296 push(ciType::make(T_DOUBLE)); 297 push(double2_type()); 298 } 299 void pop_double() { 300 assert(type_at_tos() == double2_type(), "must be 2nd half"); 301 pop(); 302 assert(is_double(type_at_tos()), "must be double"); 303 pop(); 304 } 305 void push_float() { 306 push(ciType::make(T_FLOAT)); 307 } 308 void pop_float() { 309 assert(is_float(type_at_tos()), "must be float"); 310 pop(); 311 } 312 void push_long() { 313 push(ciType::make(T_LONG)); 314 push(long2_type()); 315 } 316 void pop_long() { 317 assert(type_at_tos() == long2_type(), "must be 2nd half"); 318 pop(); 319 assert(is_long(type_at_tos()), "must be long"); 320 pop(); 321 } 322 void push_object(ciKlass* klass) { 323 push(klass); 324 } 325 void pop_object() { 326 assert(is_reference(type_at_tos()), "must be reference type"); 327 pop(); 328 } 329 void pop_array() { 330 assert(type_at_tos() == null_type() || 331 type_at_tos()->is_array_klass(), "must be array type"); 332 pop(); 333 } 334 // pop_objOrFlatArray and pop_typeArray narrow the tos to ciObjArrayKlass, 335 // ciFlatArrayKlass or ciTypeArrayKlass (resp.). In the rare case that an explicit 336 // null is popped from the stack, we return null. Caller beware. 337 ciArrayKlass* pop_objOrFlatArray() { 338 ciType* array = pop_value(); 339 if (array == null_type()) return nullptr; 340 assert(array->is_obj_array_klass() || array->is_flat_array_klass(), 341 "must be a flat or an object array type"); 342 return array->as_array_klass(); 343 } 344 ciTypeArrayKlass* pop_typeArray() { 345 ciType* array = pop_value(); 346 if (array == null_type()) return nullptr; 347 assert(array->is_type_array_klass(), "must be prim array type"); 348 return array->as_type_array_klass(); 349 } 350 void push_null() { 351 push(null_type()); 352 } 353 void do_null_assert(ciKlass* unloaded_klass); 354 355 // Helper convenience routines. 356 void do_aload(ciBytecodeStream* str); 357 void do_checkcast(ciBytecodeStream* str); 358 void do_getfield(ciBytecodeStream* str); 359 void do_getstatic(ciBytecodeStream* str); 360 void do_invoke(ciBytecodeStream* str, bool has_receiver); 361 void do_jsr(ciBytecodeStream* str); 362 void do_ldc(ciBytecodeStream* str); 363 void do_multianewarray(ciBytecodeStream* str); 364 void do_new(ciBytecodeStream* str); 365 void do_newarray(ciBytecodeStream* str); 366 void do_putfield(ciBytecodeStream* str); 367 void do_putstatic(ciBytecodeStream* str); 368 void do_ret(ciBytecodeStream* str); 369 370 void overwrite_local_double_long(int index) { 371 // Invalidate the previous local if it contains first half of 372 // a double or long value since its second half is being overwritten. 373 int prev_index = index - 1; 374 if (prev_index >= 0 && 375 (is_double(type_at(local(prev_index))) || 376 is_long(type_at(local(prev_index))))) { 377 set_type_at(local(prev_index), bottom_type()); 378 } 379 } 380 381 void load_local_object(int index) { 382 ciType* type = type_at(local(index)); 383 assert(is_reference(type), "must be reference type"); 384 push(type); 385 } 386 void store_local_object(int index) { 387 ciType* type = pop_value(); 388 assert(is_reference(type) || type->is_return_address(), 389 "must be reference type or return address"); 390 overwrite_local_double_long(index); 391 set_type_at(local(index), type); 392 store_to_local(index); 393 } 394 395 void load_local_double(int index) { 396 ciType* type = type_at(local(index)); 397 ciType* type2 = type_at(local(index+1)); 398 assert(is_double(type), "must be double type"); 399 assert(type2 == double2_type(), "must be 2nd half"); 400 push(type); 401 push(double2_type()); 402 } 403 void store_local_double(int index) { 404 ciType* type2 = pop_value(); 405 ciType* type = pop_value(); 406 assert(is_double(type), "must be double"); 407 assert(type2 == double2_type(), "must be 2nd half"); 408 overwrite_local_double_long(index); 409 set_type_at(local(index), type); 410 set_type_at(local(index+1), type2); 411 store_to_local(index); 412 store_to_local(index+1); 413 } 414 415 void load_local_float(int index) { 416 ciType* type = type_at(local(index)); 417 assert(is_float(type), "must be float type"); 418 push(type); 419 } 420 void store_local_float(int index) { 421 ciType* type = pop_value(); 422 assert(is_float(type), "must be float type"); 423 overwrite_local_double_long(index); 424 set_type_at(local(index), type); 425 store_to_local(index); 426 } 427 428 void load_local_int(int index) { 429 ciType* type = type_at(local(index)); 430 assert(is_int(type), "must be int type"); 431 push(type); 432 } 433 void store_local_int(int index) { 434 ciType* type = pop_value(); 435 assert(is_int(type), "must be int type"); 436 overwrite_local_double_long(index); 437 set_type_at(local(index), type); 438 store_to_local(index); 439 } 440 441 void load_local_long(int index) { 442 ciType* type = type_at(local(index)); 443 ciType* type2 = type_at(local(index+1)); 444 assert(is_long(type), "must be long type"); 445 assert(type2 == long2_type(), "must be 2nd half"); 446 push(type); 447 push(long2_type()); 448 } 449 void store_local_long(int index) { 450 ciType* type2 = pop_value(); 451 ciType* type = pop_value(); 452 assert(is_long(type), "must be long"); 453 assert(type2 == long2_type(), "must be 2nd half"); 454 overwrite_local_double_long(index); 455 set_type_at(local(index), type); 456 set_type_at(local(index+1), type2); 457 store_to_local(index); 458 store_to_local(index+1); 459 } 460 461 // Stop interpretation of this path with a trap. 462 void trap(ciBytecodeStream* str, ciKlass* klass, int index); 463 464 public: 465 StateVector(ciTypeFlow* outer); 466 467 // Copy our value into some other StateVector 468 void copy_into(StateVector* copy) const; 469 470 // Meets this StateVector with another, destructively modifying this 471 // one. Returns true if any modification takes place. 472 bool meet(const StateVector* incoming); 473 474 // Ditto, except that the incoming state is coming from an exception. 475 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming); 476 477 // Apply the effect of one bytecode to this StateVector 478 bool apply_one_bytecode(ciBytecodeStream* stream); 479 480 // What is the bci of the trap? 481 int trap_bci() { return _trap_bci; } 482 483 // What is the index associated with the trap? 484 int trap_index() { return _trap_index; } 485 486 void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN; 487 void print_on(outputStream* st) const PRODUCT_RETURN; 488 }; 489 490 // Parameter for "find_block" calls: 491 // Describes the difference between a public and backedge copy. 492 enum CreateOption { 493 create_public_copy, 494 create_backedge_copy, 495 no_create 496 }; 497 498 // Successor iterator 499 class SuccIter : public StackObj { 500 private: 501 Block* _pred; 502 int _index; 503 Block* _succ; 504 public: 505 SuccIter() : _pred(nullptr), _index(-1), _succ(nullptr) {} 506 SuccIter(Block* pred) : _pred(pred), _index(-1), _succ(nullptr) { next(); } 507 int index() { return _index; } 508 Block* pred() { return _pred; } // Return predecessor 509 bool done() { return _index < 0; } // Finished? 510 Block* succ() { return _succ; } // Return current successor 511 void next(); // Advance 512 void set_succ(Block* succ); // Update current successor 513 bool is_normal_ctrl() { return index() < _pred->successors()->length(); } 514 }; 515 516 // A basic block 517 class Block : public ArenaObj { 518 private: 519 ciBlock* _ciblock; 520 GrowableArray<Block*>* _exceptions; 521 GrowableArray<ciInstanceKlass*>* _exc_klasses; 522 GrowableArray<Block*>* _successors; 523 GrowableArray<Block*> _predecessors; 524 StateVector* _state; 525 JsrSet* _jsrs; 526 527 int _trap_bci; 528 int _trap_index; 529 530 // pre_order, assigned at first visit. Used as block ID and "visited" tag 531 int _pre_order; 532 533 // A post-order, used to compute the reverse post order (RPO) provided to the client 534 int _post_order; // used to compute rpo 535 536 // Has this block been cloned for a loop backedge? 537 bool _backedge_copy; 538 539 // This block is a loop head of an irreducible loop. 540 bool _irreducible_loop_head; 541 542 // This block is a secondary entry to an irreducible loop (entry but not head). 543 bool _irreducible_loop_secondary_entry; 544 545 // This block has monitor entry point. 546 bool _has_monitorenter; 547 548 // A pointer used for our internal work list 549 bool _on_work_list; // on the work list 550 Block* _next; 551 Block* _rpo_next; // Reverse post order list 552 553 // Loop info 554 Loop* _loop; // nearest loop 555 556 ciBlock* ciblock() const { return _ciblock; } 557 StateVector* state() const { return _state; } 558 559 // Compute the exceptional successors and types for this Block. 560 void compute_exceptions(); 561 562 public: 563 // constructors 564 Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs); 565 566 void set_trap(int trap_bci, int trap_index) { 567 _trap_bci = trap_bci; 568 _trap_index = trap_index; 569 assert(has_trap(), ""); 570 } 571 bool has_trap() const { return _trap_bci != -1; } 572 int trap_bci() const { assert(has_trap(), ""); return _trap_bci; } 573 int trap_index() const { assert(has_trap(), ""); return _trap_index; } 574 575 // accessors 576 ciTypeFlow* outer() const { return state()->outer(); } 577 int start() const { return _ciblock->start_bci(); } 578 int limit() const { return _ciblock->limit_bci(); } 579 int control() const { return _ciblock->control_bci(); } 580 JsrSet* jsrs() const { return _jsrs; } 581 582 bool is_backedge_copy() const { return _backedge_copy; } 583 void set_backedge_copy(bool z); 584 int backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); } 585 586 // access to entry state 587 int stack_size() const { return _state->stack_size(); } 588 int monitor_count() const { return _state->monitor_count(); } 589 ciType* local_type_at(int i) const { return _state->local_type_at(i); } 590 ciType* stack_type_at(int i) const { return _state->stack_type_at(i); } 591 592 // Data flow on locals 593 bool is_invariant_local(uint v) const { 594 assert(is_loop_head(), "only loop heads"); 595 // Find outermost loop with same loop head 596 Loop* lp = loop(); 597 while (lp->parent() != nullptr) { 598 if (lp->parent()->head() != lp->head()) break; 599 lp = lp->parent(); 600 } 601 return !lp->def_locals()->test(v); 602 } 603 LocalSet* def_locals() { return _state->def_locals(); } 604 const LocalSet* def_locals() const { return _state->def_locals(); } 605 606 // Get the successors for this Block. 607 GrowableArray<Block*>* successors(ciBytecodeStream* str, 608 StateVector* state, 609 JsrSet* jsrs); 610 GrowableArray<Block*>* successors() { 611 assert(_successors != nullptr, "must be filled in"); 612 return _successors; 613 } 614 615 // Predecessors of this block (including exception edges) 616 GrowableArray<Block*>* predecessors() { 617 return &_predecessors; 618 } 619 620 // Get the exceptional successors for this Block. 621 GrowableArray<Block*>* exceptions() { 622 if (_exceptions == nullptr) { 623 compute_exceptions(); 624 } 625 return _exceptions; 626 } 627 628 // Get the exception klasses corresponding to the 629 // exceptional successors for this Block. 630 GrowableArray<ciInstanceKlass*>* exc_klasses() { 631 if (_exc_klasses == nullptr) { 632 compute_exceptions(); 633 } 634 return _exc_klasses; 635 } 636 637 // Is this Block compatible with a given JsrSet? 638 bool is_compatible_with(JsrSet* other) { 639 return _jsrs->is_compatible_with(other); 640 } 641 642 // Copy the value of our state vector into another. 643 void copy_state_into(StateVector* copy) const { 644 _state->copy_into(copy); 645 } 646 647 // Copy the value of our JsrSet into another 648 void copy_jsrs_into(JsrSet* copy) const { 649 _jsrs->copy_into(copy); 650 } 651 652 // Meets the start state of this block with another state, destructively 653 // modifying this one. Returns true if any modification takes place. 654 bool meet(const StateVector* incoming) { 655 return state()->meet(incoming); 656 } 657 658 // Ditto, except that the incoming state is coming from an 659 // exception path. This means the stack is replaced by the 660 // appropriate exception type. 661 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) { 662 return state()->meet_exception(exc, incoming); 663 } 664 665 // Work list manipulation 666 void set_next(Block* block) { _next = block; } 667 Block* next() const { return _next; } 668 669 void set_on_work_list(bool c) { _on_work_list = c; } 670 bool is_on_work_list() const { return _on_work_list; } 671 672 bool has_pre_order() const { return _pre_order >= 0; } 673 void set_pre_order(int po) { assert(!has_pre_order(), ""); _pre_order = po; } 674 int pre_order() const { assert(has_pre_order(), ""); return _pre_order; } 675 void set_next_pre_order() { set_pre_order(outer()->inc_next_pre_order()); } 676 bool is_start() const { return _pre_order == outer()->start_block_num(); } 677 678 // Reverse post order 679 void df_init(); 680 bool has_post_order() const { return _post_order >= 0; } 681 void set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; } 682 void reset_post_order(int o){ _post_order = o; } 683 int post_order() const { assert(has_post_order(), ""); return _post_order; } 684 685 bool has_rpo() const { return has_post_order() && outer()->have_block_count(); } 686 int rpo() const { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; } 687 void set_rpo_next(Block* b) { _rpo_next = b; } 688 Block* rpo_next() { return _rpo_next; } 689 690 // Loops 691 Loop* loop() const { return _loop; } 692 void set_loop(Loop* lp) { _loop = lp; } 693 bool is_loop_head() const { return _loop && _loop->head() == this; } 694 bool is_in_irreducible_loop() const; 695 void set_irreducible_loop_head() { _irreducible_loop_head = true; } 696 bool is_irreducible_loop_head() const { return _irreducible_loop_head; } 697 void set_irreducible_loop_secondary_entry() { _irreducible_loop_secondary_entry = true; } 698 bool is_irreducible_loop_secondary_entry() const { return _irreducible_loop_secondary_entry; } 699 void set_has_monitorenter() { _has_monitorenter = true; } 700 bool has_monitorenter() const { return _has_monitorenter; } 701 bool is_visited() const { return has_pre_order(); } 702 bool is_post_visited() const { return has_post_order(); } 703 bool is_clonable_exit(Loop* lp); 704 Block* looping_succ(Loop* lp); // Successor inside of loop 705 bool is_single_entry_loop_head() const { 706 if (!is_loop_head()) return false; 707 for (Loop* lp = loop(); lp != nullptr && lp->head() == this; lp = lp->parent()) 708 if (lp->is_irreducible()) return false; 709 return true; 710 } 711 712 void print_value_on(outputStream* st) const PRODUCT_RETURN; 713 void print_on(outputStream* st) const PRODUCT_RETURN; 714 }; 715 716 // Loop 717 class Loop : public ArenaObj { 718 private: 719 Loop* _parent; 720 Loop* _sibling; // List of siblings, null terminated 721 Loop* _child; // Head of child list threaded thru sibling pointer 722 Block* _head; // Head of loop 723 Block* _tail; // Tail of loop 724 bool _irreducible; 725 LocalSet _def_locals; 726 int _profiled_count; 727 728 ciTypeFlow* outer() const { return head()->outer(); } 729 bool at_insertion_point(Loop* lp, Loop* current); 730 731 public: 732 Loop(Block* head, Block* tail) : 733 _parent(nullptr), _sibling(nullptr), _child(nullptr), 734 _head(head), _tail(tail), 735 _irreducible(false), _def_locals(), _profiled_count(-1) {} 736 737 Loop* parent() const { return _parent; } 738 Loop* sibling() const { return _sibling; } 739 Loop* child() const { return _child; } 740 Block* head() const { return _head; } 741 Block* tail() const { return _tail; } 742 void set_parent(Loop* p) { _parent = p; } 743 void set_sibling(Loop* s) { _sibling = s; } 744 void set_child(Loop* c) { _child = c; } 745 void set_head(Block* hd) { _head = hd; } 746 void set_tail(Block* tl) { _tail = tl; } 747 748 int depth() const; // nesting depth 749 750 // Returns true if lp is a nested loop or us. 751 bool contains(Loop* lp) const; 752 bool contains(Block* blk) const { return contains(blk->loop()); } 753 754 // Data flow on locals 755 LocalSet* def_locals() { return &_def_locals; } 756 const LocalSet* def_locals() const { return &_def_locals; } 757 758 // Merge the branch lp into this branch, sorting on the loop head 759 // pre_orders. Returns the new branch. 760 Loop* sorted_merge(Loop* lp); 761 762 // Mark non-single entry to loop 763 void set_irreducible(Block* entry) { 764 _irreducible = true; 765 head()->set_irreducible_loop_head(); 766 entry->set_irreducible_loop_secondary_entry(); 767 } 768 bool is_irreducible() const { return _irreducible; } 769 770 bool is_root() const { return _tail->pre_order() == max_jint; } 771 772 int profiled_count(); 773 774 void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN; 775 }; 776 777 // Preorder iteration over the loop tree. 778 class PreorderLoops : public StackObj { 779 private: 780 Loop* _root; 781 Loop* _current; 782 public: 783 PreorderLoops(Loop* root) : _root(root), _current(root) {} 784 bool done() { return _current == nullptr; } // Finished iterating? 785 void next(); // Advance to next loop 786 Loop* current() { return _current; } // Return current loop. 787 }; 788 789 // Standard indexes of successors, for various bytecodes. 790 enum { 791 FALL_THROUGH = 0, // normal control 792 IF_NOT_TAKEN = 0, // the not-taken branch of an if (i.e., fall-through) 793 IF_TAKEN = 1, // the taken branch of an if 794 GOTO_TARGET = 0, // unique successor for goto, jsr, or ret 795 SWITCH_DEFAULT = 0, // default branch of a switch 796 SWITCH_CASES = 1 // first index for any non-default switch branches 797 // Unlike in other blocks, the successors of a switch are listed uniquely. 798 }; 799 800 private: 801 // A mapping from pre_order to Blocks. This array is created 802 // only at the end of the flow. 803 Block** _block_map; 804 805 // For each ciBlock index, a list of Blocks which share this ciBlock. 806 GrowableArray<Block*>** _idx_to_blocklist; 807 808 // Tells if a given instruction is able to generate an exception edge. 809 bool can_trap(ciBytecodeStream& str); 810 811 // Clone the loop heads. Returns true if any cloning occurred. 812 bool clone_loop_heads(StateVector* temp_vector, JsrSet* temp_set); 813 814 // Clone lp's head and replace tail's successors with clone. 815 Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set); 816 817 public: 818 // Return the block beginning at bci which has a JsrSet compatible 819 // with jsrs. 820 Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy); 821 822 // block factory 823 Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy); 824 825 // How many of the blocks have the backedge_copy bit set? 826 int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const; 827 828 // Return an existing block containing bci which has a JsrSet compatible 829 // with jsrs, or null if there is none. 830 Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); } 831 832 // Tell whether the flow analysis has encountered an error of some sort. 833 bool failing() { return env()->failing() || _failure_reason != nullptr; } 834 835 // Reason this compilation is failing, such as "too many basic blocks". 836 const char* failure_reason() { return _failure_reason; } 837 838 // Note a failure. 839 void record_failure(const char* reason); 840 841 // Return the block of a given pre-order number. 842 int have_block_count() const { return _block_map != nullptr; } 843 int block_count() const { assert(have_block_count(), ""); 844 return _next_pre_order; } 845 Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds"); 846 return _block_map[po]; } 847 Block* start_block() const { return pre_order_at(start_block_num()); } 848 int start_block_num() const { return 0; } 849 Block* rpo_at(int rpo) const { assert(0 <= rpo && rpo < block_count(), "out of bounds"); 850 return _block_map[rpo]; } 851 int inc_next_pre_order() { return _next_pre_order++; } 852 853 ciType* mark_as_null_free(ciType* type); 854 855 private: 856 // A work list used during flow analysis. 857 Block* _work_list; 858 859 // List of blocks in reverse post order 860 Block* _rpo_list; 861 862 // Next Block::_pre_order. After mapping, doubles as block_count. 863 int _next_pre_order; 864 865 // Are there more blocks on the work list? 866 bool work_list_empty() { return _work_list == nullptr; } 867 868 // Get the next basic block from our work list. 869 Block* work_list_next(); 870 871 // Add a basic block to our work list. 872 void add_to_work_list(Block* block); 873 874 // Prepend a basic block to rpo list. 875 void prepend_to_rpo_list(Block* blk) { 876 blk->set_rpo_next(_rpo_list); 877 _rpo_list = blk; 878 } 879 880 // Root of the loop tree 881 Loop* _loop_tree_root; 882 883 // State used for make_jsr_record 884 GrowableArray<JsrRecord*>* _jsr_records; 885 886 public: 887 // Make a JsrRecord for a given (entry, return) pair, if such a record 888 // does not already exist. 889 JsrRecord* make_jsr_record(int entry_address, int return_address); 890 891 void set_loop_tree_root(Loop* ltr) { _loop_tree_root = ltr; } 892 Loop* loop_tree_root() const { return _loop_tree_root; } 893 894 private: 895 // Get the initial state for start_bci: 896 const StateVector* get_start_state(); 897 898 // Merge the current state into all exceptional successors at the 899 // current point in the code. 900 void flow_exceptions(GrowableArray<Block*>* exceptions, 901 GrowableArray<ciInstanceKlass*>* exc_klasses, 902 StateVector* state); 903 904 // Merge the current state into all successors at the current point 905 // in the code. 906 void flow_successors(GrowableArray<Block*>* successors, 907 StateVector* state); 908 909 // Interpret the effects of the bytecodes on the incoming state 910 // vector of a basic block. Push the changed state to succeeding 911 // basic blocks. 912 void flow_block(Block* block, 913 StateVector* scratch_state, 914 JsrSet* scratch_jsrs); 915 916 // Perform the type flow analysis, creating and cloning Blocks as 917 // necessary. 918 void flow_types(); 919 920 // Perform the depth first type flow analysis. Helper for flow_types. 921 void df_flow_types(Block* start, 922 bool do_flow, 923 StateVector* temp_vector, 924 JsrSet* temp_set); 925 926 // Incrementally build loop tree. 927 void build_loop_tree(Block* blk); 928 929 // Create the block map, which indexes blocks in pre_order. 930 void map_blocks(); 931 932 public: 933 // Perform type inference flow analysis. 934 void do_flow(); 935 936 // Determine if bci is dominated by dom_bci 937 bool is_dominated_by(int bci, int dom_bci); 938 939 void print() const PRODUCT_RETURN; 940 void print_on(outputStream* st) const PRODUCT_RETURN; 941 942 void rpo_print_on(outputStream* st) const PRODUCT_RETURN; 943 }; 944 945 #endif // SHARE_CI_CITYPEFLOW_HPP