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