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 local_limit_cell() const { return (Cell) outer()->max_locals(); } 217 218 // Cell creation 219 Cell local(int lnum) const { 220 assert(lnum < outer()->max_locals(), "index check"); 221 assert(Cell_0 <= lnum && lnum <= Cell_max, "out of Cell's range"); 222 return (Cell)(lnum); 223 } 224 225 Cell stack(int snum) const { 226 assert(snum < stack_size(), "index check"); 227 return (Cell)(outer()->max_locals() + snum); 228 } 229 230 Cell tos() const { return stack(stack_size()-1); } 231 232 // For external use only: 233 ciType* local_type_at(int i) const { return type_at(local(i)); } 234 ciType* stack_type_at(int i) const { return type_at(stack(i)); } 235 236 // Accessors for the type of some Cell c 237 ciType* type_at(Cell c) const { 238 assert(start_cell() <= c && c < limit_cell(), "out of bounds"); 239 return _types[c]; 240 } 241 242 void set_type_at(Cell c, ciType* type) { 243 assert(start_cell() <= c && c < limit_cell(), "out of bounds"); 244 _types[c] = type; 245 } 246 247 // Top-of-stack operations. 248 void set_type_at_tos(ciType* type) { set_type_at(tos(), type); } 249 ciType* type_at_tos() const { return type_at(tos()); } 250 251 void push(ciType* type) { 252 _stack_size++; 253 set_type_at_tos(type); 254 } 255 void pop() { 256 debug_only(set_type_at_tos(bottom_type())); 257 _stack_size--; 258 } 259 ciType* pop_value() { 260 ciType* t = type_at_tos(); 261 pop(); 262 return t; 263 } 264 265 // Convenience operations. 266 bool is_reference(ciType* type) const { 267 return type == null_type() || !type->is_primitive_type(); 268 } 269 bool is_int(ciType* type) const { 270 return type->basic_type() == T_INT; 271 } 272 bool is_long(ciType* type) const { 273 return type->basic_type() == T_LONG; 274 } 275 bool is_float(ciType* type) const { 276 return type->basic_type() == T_FLOAT; 277 } 278 bool is_double(ciType* type) const { 279 return type->basic_type() == T_DOUBLE; 280 } 281 282 void store_to_local(int lnum) { 283 _def_locals.add((uint) lnum); 284 } 285 286 void push_translate(ciType* type); 287 288 void push_int() { 289 push(ciType::make(T_INT)); 290 } 291 void pop_int() { 292 assert(is_int(type_at_tos()), "must be integer"); 293 pop(); 294 } 295 void check_int(Cell c) { 296 assert(is_int(type_at(c)), "must be integer"); 297 } 298 void push_double() { 299 push(ciType::make(T_DOUBLE)); 300 push(double2_type()); 301 } 302 void pop_double() { 303 assert(type_at_tos() == double2_type(), "must be 2nd half"); 304 pop(); 305 assert(is_double(type_at_tos()), "must be double"); 306 pop(); 307 } 308 void push_float() { 309 push(ciType::make(T_FLOAT)); 310 } 311 void pop_float() { 312 assert(is_float(type_at_tos()), "must be float"); 313 pop(); 314 } 315 void push_long() { 316 push(ciType::make(T_LONG)); 317 push(long2_type()); 318 } 319 void pop_long() { 320 assert(type_at_tos() == long2_type(), "must be 2nd half"); 321 pop(); 322 assert(is_long(type_at_tos()), "must be long"); 323 pop(); 324 } 325 void push_object(ciKlass* klass) { 326 push(klass); 327 } 328 void pop_object() { 329 assert(is_reference(type_at_tos()), "must be reference type"); 330 pop(); 331 } 332 void pop_array() { 333 assert(type_at_tos() == null_type() || 334 type_at_tos()->is_array_klass(), "must be array type"); 335 pop(); 336 } 337 // pop_objArray and pop_typeArray narrow the tos to ciObjArrayKlass 338 // or ciTypeArrayKlass (resp.). In the rare case that an explicit 339 // null is popped from the stack, we return null. Caller beware. 340 ciObjArrayKlass* pop_objArray() { 341 ciType* array = pop_value(); 342 if (array == null_type()) return nullptr; 343 assert(array->is_obj_array_klass(), "must be object array type"); 344 return array->as_obj_array_klass(); 345 } 346 ciTypeArrayKlass* pop_typeArray() { 347 ciType* array = pop_value(); 348 if (array == null_type()) return nullptr; 349 assert(array->is_type_array_klass(), "must be prim array type"); 350 return array->as_type_array_klass(); 351 } 352 void push_null() { 353 push(null_type()); 354 } 355 void do_null_assert(ciKlass* unloaded_klass); 356 357 // Helper convenience routines. 358 void do_aaload(ciBytecodeStream* str); 359 void do_checkcast(ciBytecodeStream* str); 360 void do_getfield(ciBytecodeStream* str); 361 void do_getstatic(ciBytecodeStream* str); 362 void do_invoke(ciBytecodeStream* str, bool has_receiver); 363 void do_jsr(ciBytecodeStream* str); 364 void do_ldc(ciBytecodeStream* str); 365 void do_multianewarray(ciBytecodeStream* str); 366 void do_new(ciBytecodeStream* str); 367 void do_newarray(ciBytecodeStream* str); 368 void do_putfield(ciBytecodeStream* str); 369 void do_putstatic(ciBytecodeStream* str); 370 void do_ret(ciBytecodeStream* str); 371 372 void overwrite_local_double_long(int index) { 373 // Invalidate the previous local if it contains first half of 374 // a double or long value since its second half is being overwritten. 375 int prev_index = index - 1; 376 if (prev_index >= 0 && 377 (is_double(type_at(local(prev_index))) || 378 is_long(type_at(local(prev_index))))) { 379 set_type_at(local(prev_index), bottom_type()); 380 } 381 } 382 383 void load_local_object(int index) { 384 ciType* type = type_at(local(index)); 385 assert(is_reference(type), "must be reference type"); 386 push(type); 387 } 388 void store_local_object(int index) { 389 ciType* type = pop_value(); 390 assert(is_reference(type) || type->is_return_address(), 391 "must be reference type or return address"); 392 overwrite_local_double_long(index); 393 set_type_at(local(index), type); 394 store_to_local(index); 395 } 396 397 void load_local_double(int index) { 398 ciType* type = type_at(local(index)); 399 ciType* type2 = type_at(local(index+1)); 400 assert(is_double(type), "must be double type"); 401 assert(type2 == double2_type(), "must be 2nd half"); 402 push(type); 403 push(double2_type()); 404 } 405 void store_local_double(int index) { 406 ciType* type2 = pop_value(); 407 ciType* type = pop_value(); 408 assert(is_double(type), "must be double"); 409 assert(type2 == double2_type(), "must be 2nd half"); 410 overwrite_local_double_long(index); 411 set_type_at(local(index), type); 412 set_type_at(local(index+1), type2); 413 store_to_local(index); 414 store_to_local(index+1); 415 } 416 417 void load_local_float(int index) { 418 ciType* type = type_at(local(index)); 419 assert(is_float(type), "must be float type"); 420 push(type); 421 } 422 void store_local_float(int index) { 423 ciType* type = pop_value(); 424 assert(is_float(type), "must be float type"); 425 overwrite_local_double_long(index); 426 set_type_at(local(index), type); 427 store_to_local(index); 428 } 429 430 void load_local_int(int index) { 431 ciType* type = type_at(local(index)); 432 assert(is_int(type), "must be int type"); 433 push(type); 434 } 435 void store_local_int(int index) { 436 ciType* type = pop_value(); 437 assert(is_int(type), "must be int type"); 438 overwrite_local_double_long(index); 439 set_type_at(local(index), type); 440 store_to_local(index); 441 } 442 443 void load_local_long(int index) { 444 ciType* type = type_at(local(index)); 445 ciType* type2 = type_at(local(index+1)); 446 assert(is_long(type), "must be long type"); 447 assert(type2 == long2_type(), "must be 2nd half"); 448 push(type); 449 push(long2_type()); 450 } 451 void store_local_long(int index) { 452 ciType* type2 = pop_value(); 453 ciType* type = pop_value(); 454 assert(is_long(type), "must be long"); 455 assert(type2 == long2_type(), "must be 2nd half"); 456 overwrite_local_double_long(index); 457 set_type_at(local(index), type); 458 set_type_at(local(index+1), type2); 459 store_to_local(index); 460 store_to_local(index+1); 461 } 462 463 // Stop interpretation of this path with a trap. 464 void trap(ciBytecodeStream* str, ciKlass* klass, int index); 465 466 public: 467 StateVector(ciTypeFlow* outer); 468 469 // Copy our value into some other StateVector 470 void copy_into(StateVector* copy) const; 471 472 // Meets this StateVector with another, destructively modifying this 473 // one. Returns true if any modification takes place. 474 bool meet(const StateVector* incoming); 475 476 // Ditto, except that the incoming state is coming from an exception. 477 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming); 478 479 // Apply the effect of one bytecode to this StateVector 480 bool apply_one_bytecode(ciBytecodeStream* stream); 481 482 // What is the bci of the trap? 483 int trap_bci() { return _trap_bci; } 484 485 // What is the index associated with the trap? 486 int trap_index() { return _trap_index; } 487 488 void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN; 489 void print_on(outputStream* st) const PRODUCT_RETURN; 490 }; 491 492 // Parameter for "find_block" calls: 493 // Describes the difference between a public and backedge copy. 494 enum CreateOption { 495 create_public_copy, 496 create_backedge_copy, 497 no_create 498 }; 499 500 // Successor iterator 501 class SuccIter : public StackObj { 502 private: 503 Block* _pred; 504 int _index; 505 Block* _succ; 506 public: 507 SuccIter() : _pred(nullptr), _index(-1), _succ(nullptr) {} 508 SuccIter(Block* pred) : _pred(pred), _index(-1), _succ(nullptr) { next(); } 509 int index() { return _index; } 510 Block* pred() { return _pred; } // Return predecessor 511 bool done() { return _index < 0; } // Finished? 512 Block* succ() { return _succ; } // Return current successor 513 void next(); // Advance 514 void set_succ(Block* succ); // Update current successor 515 bool is_normal_ctrl() { return index() < _pred->successors()->length(); } 516 }; 517 518 // A basic block 519 class Block : public ArenaObj { 520 private: 521 ciBlock* _ciblock; 522 GrowableArray<Block*>* _exceptions; 523 GrowableArray<ciInstanceKlass*>* _exc_klasses; 524 GrowableArray<Block*>* _successors; 525 GrowableArray<Block*> _predecessors; 526 StateVector* _state; 527 JsrSet* _jsrs; 528 529 int _trap_bci; 530 int _trap_index; 531 532 // pre_order, assigned at first visit. Used as block ID and "visited" tag 533 int _pre_order; 534 535 // A post-order, used to compute the reverse post order (RPO) provided to the client 536 int _post_order; // used to compute rpo 537 538 // Has this block been cloned for a loop backedge? 539 bool _backedge_copy; 540 541 // This block is a loop head of an irreducible loop. 542 bool _irreducible_loop_head; 543 544 // This block is a secondary entry to an irreducible loop (entry but not head). 545 bool _irreducible_loop_secondary_entry; 546 547 // This block has monitor entry point. 548 bool _has_monitorenter; 549 550 // A pointer used for our internal work list 551 bool _on_work_list; // on the work list 552 Block* _next; 553 Block* _rpo_next; // Reverse post order list 554 555 // Loop info 556 Loop* _loop; // nearest loop 557 558 ciBlock* ciblock() const { return _ciblock; } 559 StateVector* state() const { return _state; } 560 561 // Compute the exceptional successors and types for this Block. 562 void compute_exceptions(); 563 564 public: 565 // constructors 566 Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs); 567 568 void set_trap(int trap_bci, int trap_index) { 569 _trap_bci = trap_bci; 570 _trap_index = trap_index; 571 assert(has_trap(), ""); 572 } 573 bool has_trap() const { return _trap_bci != -1; } 574 int trap_bci() const { assert(has_trap(), ""); return _trap_bci; } 575 int trap_index() const { assert(has_trap(), ""); return _trap_index; } 576 577 // accessors 578 ciTypeFlow* outer() const { return state()->outer(); } 579 int start() const { return _ciblock->start_bci(); } 580 int limit() const { return _ciblock->limit_bci(); } 581 int control() const { return _ciblock->control_bci(); } 582 JsrSet* jsrs() const { return _jsrs; } 583 584 bool is_backedge_copy() const { return _backedge_copy; } 585 void set_backedge_copy(bool z); 586 int backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); } 587 588 // access to entry state 589 int stack_size() const { return _state->stack_size(); } 590 int monitor_count() const { return _state->monitor_count(); } 591 ciType* local_type_at(int i) const { return _state->local_type_at(i); } 592 ciType* stack_type_at(int i) const { return _state->stack_type_at(i); } 593 594 // Data flow on locals 595 bool is_invariant_local(uint v) const { 596 assert(is_loop_head(), "only loop heads"); 597 // Find outermost loop with same loop head 598 Loop* lp = loop(); 599 while (lp->parent() != nullptr) { 600 if (lp->parent()->head() != lp->head()) break; 601 lp = lp->parent(); 602 } 603 return !lp->def_locals()->test(v); 604 } 605 LocalSet* def_locals() { return _state->def_locals(); } 606 const LocalSet* def_locals() const { return _state->def_locals(); } 607 608 // Get the successors for this Block. 609 GrowableArray<Block*>* successors(ciBytecodeStream* str, 610 StateVector* state, 611 JsrSet* jsrs); 612 GrowableArray<Block*>* successors() { 613 assert(_successors != nullptr, "must be filled in"); 614 return _successors; 615 } 616 617 // Predecessors of this block (including exception edges) 618 GrowableArray<Block*>* predecessors() { 619 return &_predecessors; 620 } 621 622 // Get the exceptional successors for this Block. 623 GrowableArray<Block*>* exceptions() { 624 if (_exceptions == nullptr) { 625 compute_exceptions(); 626 } 627 return _exceptions; 628 } 629 630 // Get the exception klasses corresponding to the 631 // exceptional successors for this Block. 632 GrowableArray<ciInstanceKlass*>* exc_klasses() { 633 if (_exc_klasses == nullptr) { 634 compute_exceptions(); 635 } 636 return _exc_klasses; 637 } 638 639 // Is this Block compatible with a given JsrSet? 640 bool is_compatible_with(JsrSet* other) { 641 return _jsrs->is_compatible_with(other); 642 } 643 644 // Copy the value of our state vector into another. 645 void copy_state_into(StateVector* copy) const { 646 _state->copy_into(copy); 647 } 648 649 // Copy the value of our JsrSet into another 650 void copy_jsrs_into(JsrSet* copy) const { 651 _jsrs->copy_into(copy); 652 } 653 654 // Meets the start state of this block with another state, destructively 655 // modifying this one. Returns true if any modification takes place. 656 bool meet(const StateVector* incoming) { 657 return state()->meet(incoming); 658 } 659 660 // Ditto, except that the incoming state is coming from an 661 // exception path. This means the stack is replaced by the 662 // appropriate exception type. 663 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) { 664 return state()->meet_exception(exc, incoming); 665 } 666 667 // Work list manipulation 668 void set_next(Block* block) { _next = block; } 669 Block* next() const { return _next; } 670 671 void set_on_work_list(bool c) { _on_work_list = c; } 672 bool is_on_work_list() const { return _on_work_list; } 673 674 bool has_pre_order() const { return _pre_order >= 0; } 675 void set_pre_order(int po) { assert(!has_pre_order(), ""); _pre_order = po; } 676 int pre_order() const { assert(has_pre_order(), ""); return _pre_order; } 677 void set_next_pre_order() { set_pre_order(outer()->inc_next_pre_order()); } 678 bool is_start() const { return _pre_order == outer()->start_block_num(); } 679 680 // Reverse post order 681 void df_init(); 682 bool has_post_order() const { return _post_order >= 0; } 683 void set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; } 684 void reset_post_order(int o){ _post_order = o; } 685 int post_order() const { assert(has_post_order(), ""); return _post_order; } 686 687 bool has_rpo() const { return has_post_order() && outer()->have_block_count(); } 688 int rpo() const { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; } 689 void set_rpo_next(Block* b) { _rpo_next = b; } 690 Block* rpo_next() { return _rpo_next; } 691 692 // Loops 693 Loop* loop() const { return _loop; } 694 void set_loop(Loop* lp) { _loop = lp; } 695 bool is_loop_head() const { return _loop && _loop->head() == this; } 696 bool is_in_irreducible_loop() const; 697 void set_irreducible_loop_head() { _irreducible_loop_head = true; } 698 bool is_irreducible_loop_head() const { return _irreducible_loop_head; } 699 void set_irreducible_loop_secondary_entry() { _irreducible_loop_secondary_entry = true; } 700 bool is_irreducible_loop_secondary_entry() const { return _irreducible_loop_secondary_entry; } 701 void set_has_monitorenter() { _has_monitorenter = true; } 702 bool has_monitorenter() const { return _has_monitorenter; } 703 bool is_visited() const { return has_pre_order(); } 704 bool is_post_visited() const { return has_post_order(); } 705 bool is_clonable_exit(Loop* lp); 706 Block* looping_succ(Loop* lp); // Successor inside of loop 707 bool is_single_entry_loop_head() const { 708 if (!is_loop_head()) return false; 709 for (Loop* lp = loop(); lp != nullptr && lp->head() == this; lp = lp->parent()) 710 if (lp->is_irreducible()) return false; 711 return true; 712 } 713 714 void print_value_on(outputStream* st) const PRODUCT_RETURN; 715 void print_on(outputStream* st) const PRODUCT_RETURN; 716 }; 717 718 // Loop 719 class Loop : public ArenaObj { 720 private: 721 Loop* _parent; 722 Loop* _sibling; // List of siblings, null terminated 723 Loop* _child; // Head of child list threaded thru sibling pointer 724 Block* _head; // Head of loop 725 Block* _tail; // Tail of loop 726 bool _irreducible; 727 LocalSet _def_locals; 728 int _profiled_count; 729 730 ciTypeFlow* outer() const { return head()->outer(); } 731 bool at_insertion_point(Loop* lp, Loop* current); 732 733 public: 734 Loop(Block* head, Block* tail) : 735 _parent(nullptr), _sibling(nullptr), _child(nullptr), 736 _head(head), _tail(tail), 737 _irreducible(false), _def_locals(), _profiled_count(-1) {} 738 739 Loop* parent() const { return _parent; } 740 Loop* sibling() const { return _sibling; } 741 Loop* child() const { return _child; } 742 Block* head() const { return _head; } 743 Block* tail() const { return _tail; } 744 void set_parent(Loop* p) { _parent = p; } 745 void set_sibling(Loop* s) { _sibling = s; } 746 void set_child(Loop* c) { _child = c; } 747 void set_head(Block* hd) { _head = hd; } 748 void set_tail(Block* tl) { _tail = tl; } 749 750 int depth() const; // nesting depth 751 752 // Returns true if lp is a nested loop or us. 753 bool contains(Loop* lp) const; 754 bool contains(Block* blk) const { return contains(blk->loop()); } 755 756 // Data flow on locals 757 LocalSet* def_locals() { return &_def_locals; } 758 const LocalSet* def_locals() const { return &_def_locals; } 759 760 // Merge the branch lp into this branch, sorting on the loop head 761 // pre_orders. Returns the new branch. 762 Loop* sorted_merge(Loop* lp); 763 764 // Mark non-single entry to loop 765 void set_irreducible(Block* entry) { 766 _irreducible = true; 767 head()->set_irreducible_loop_head(); 768 entry->set_irreducible_loop_secondary_entry(); 769 } 770 bool is_irreducible() const { return _irreducible; } 771 772 bool is_root() const { return _tail->pre_order() == max_jint; } 773 774 int profiled_count(); 775 776 void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN; 777 }; 778 779 // Preorder iteration over the loop tree. 780 class PreorderLoops : public StackObj { 781 private: 782 Loop* _root; 783 Loop* _current; 784 public: 785 PreorderLoops(Loop* root) : _root(root), _current(root) {} 786 bool done() { return _current == nullptr; } // Finished iterating? 787 void next(); // Advance to next loop 788 Loop* current() { return _current; } // Return current loop. 789 }; 790 791 // Standard indexes of successors, for various bytecodes. 792 enum { 793 FALL_THROUGH = 0, // normal control 794 IF_NOT_TAKEN = 0, // the not-taken branch of an if (i.e., fall-through) 795 IF_TAKEN = 1, // the taken branch of an if 796 GOTO_TARGET = 0, // unique successor for goto, jsr, or ret 797 SWITCH_DEFAULT = 0, // default branch of a switch 798 SWITCH_CASES = 1 // first index for any non-default switch branches 799 // Unlike in other blocks, the successors of a switch are listed uniquely. 800 }; 801 802 private: 803 // A mapping from pre_order to Blocks. This array is created 804 // only at the end of the flow. 805 Block** _block_map; 806 807 // For each ciBlock index, a list of Blocks which share this ciBlock. 808 GrowableArray<Block*>** _idx_to_blocklist; 809 810 // Tells if a given instruction is able to generate an exception edge. 811 bool can_trap(ciBytecodeStream& str); 812 813 // Clone the loop heads. Returns true if any cloning occurred. 814 bool clone_loop_heads(StateVector* temp_vector, JsrSet* temp_set); 815 816 // Clone lp's head and replace tail's successors with clone. 817 Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set); 818 819 public: 820 // Return the block beginning at bci which has a JsrSet compatible 821 // with jsrs. 822 Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy); 823 824 // block factory 825 Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy); 826 827 // How many of the blocks have the backedge_copy bit set? 828 int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const; 829 830 // Return an existing block containing bci which has a JsrSet compatible 831 // with jsrs, or null if there is none. 832 Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); } 833 834 // Tell whether the flow analysis has encountered an error of some sort. 835 bool failing() { return env()->failing() || _failure_reason != nullptr; } 836 837 // Reason this compilation is failing, such as "too many basic blocks". 838 const char* failure_reason() { return _failure_reason; } 839 840 // Note a failure. 841 void record_failure(const char* reason); 842 843 // Return the block of a given pre-order number. 844 int have_block_count() const { return _block_map != nullptr; } 845 int block_count() const { assert(have_block_count(), ""); 846 return _next_pre_order; } 847 Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds"); 848 return _block_map[po]; } 849 Block* start_block() const { return pre_order_at(start_block_num()); } 850 int start_block_num() const { return 0; } 851 Block* rpo_at(int rpo) const { assert(0 <= rpo && rpo < block_count(), "out of bounds"); 852 return _block_map[rpo]; } 853 int inc_next_pre_order() { return _next_pre_order++; } 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