15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "gc/shared/barrierSet.hpp"
26 #include "gc/shared/c2/barrierSetC2.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/objArrayKlass.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/castnode.hpp"
32 #include "opto/cfgnode.hpp"
33 #include "opto/connode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/loopnode.hpp"
36 #include "opto/machnode.hpp"
37 #include "opto/movenode.hpp"
38 #include "opto/narrowptrnode.hpp"
39 #include "opto/mulnode.hpp"
40 #include "opto/phaseX.hpp"
41 #include "opto/regalloc.hpp"
42 #include "opto/regmask.hpp"
43 #include "opto/runtime.hpp"
44 #include "opto/subnode.hpp"
45 #include "opto/vectornode.hpp"
46 #include "utilities/vmError.hpp"
47
48 // Portions of code courtesy of Clifford Click
49
50 // Optimization - Graph Style
51
52 //=============================================================================
53 //------------------------------Value------------------------------------------
54 // Compute the type of the RegionNode.
503 if (left_path == nullptr || right_path == nullptr) {
504 return false;
505 }
506 Node* diamond_if = left_path->in(0);
507 if (diamond_if == nullptr || !diamond_if->is_If() || diamond_if != right_path->in(0)) {
508 // Not an IfNode merging a diamond or TOP.
509 return false;
510 }
511
512 // Check for a proper bool/cmp
513 const Node* bol = diamond_if->in(1);
514 if (!bol->is_Bool()) {
515 return false;
516 }
517 const Node* cmp = bol->in(1);
518 if (!cmp->is_Cmp()) {
519 return false;
520 }
521 return true;
522 }
523 //------------------------------Ideal------------------------------------------
524 // Return a node which is more "ideal" than the current node. Must preserve
525 // the CFG, but we can still strip out dead paths.
526 Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {
527 if( !can_reshape && !in(0) ) return nullptr; // Already degraded to a Copy
528 assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");
529
530 // Check for RegionNode with no Phi users and both inputs come from either
531 // arm of the same IF. If found, then the control-flow split is useless.
532 bool has_phis = false;
533 if (can_reshape) { // Need DU info to check for Phi users
534 try_clean_mem_phis(phase->is_IterGVN());
535 has_phis = (has_phi() != nullptr); // Cache result
536
537 if (!has_phis) { // No Phi users? Nothing merging?
538 for (uint i = 1; i < req()-1; i++) {
539 Node *if1 = in(i);
540 if( !if1 ) continue;
541 Node *iff = if1->in(0);
542 if( !iff || !iff->is_If() ) continue;
949 if (iff1 == iff2) {
950 igvn->add_users_to_worklist(iff1); // Make sure dead if is eliminated
951 igvn->replace_input_of(region, idx1, iff1->in(0));
952 igvn->replace_input_of(region, idx2, igvn->C->top());
953 return (region == this); // Remove useless if (both projections map to the same control/value)
954 }
955 BoolNode* bol1 = iff1->in(1)->isa_Bool();
956 BoolNode* bol2 = iff2->in(1)->isa_Bool();
957 if (bol1 == nullptr || bol2 == nullptr) {
958 return false; // No bool inputs found
959 }
960 Node* cmp1 = bol1->in(1);
961 Node* cmp2 = bol2->in(1);
962 bool commute = false;
963 if (!cmp1->is_Cmp() || !cmp2->is_Cmp()) {
964 return false; // No comparison
965 } else if (cmp1->Opcode() == Op_CmpF || cmp1->Opcode() == Op_CmpD ||
966 cmp2->Opcode() == Op_CmpF || cmp2->Opcode() == Op_CmpD ||
967 cmp1->Opcode() == Op_CmpP || cmp1->Opcode() == Op_CmpN ||
968 cmp2->Opcode() == Op_CmpP || cmp2->Opcode() == Op_CmpN ||
969 cmp1->is_SubTypeCheck() || cmp2->is_SubTypeCheck()) {
970 // Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.
971 // SubTypeCheck is not commutative
972 return false;
973 } else if (cmp1 != cmp2) {
974 if (cmp1->in(1) == cmp2->in(2) &&
975 cmp1->in(2) == cmp2->in(1)) {
976 commute = true; // Same but swapped inputs, commute the test
977 } else {
978 return false; // Ifs are not comparing the same values
979 }
980 }
981 proj1 = proj1->other_if_proj();
982 proj2 = proj2->other_if_proj();
983 if (!((proj1->unique_ctrl_out_or_null() == iff2 &&
984 proj2->unique_ctrl_out_or_null() == this) ||
985 (proj2->unique_ctrl_out_or_null() == iff1 &&
986 proj1->unique_ctrl_out_or_null() == this))) {
987 return false; // Ifs are not connected through other projs
988 }
989 // Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged
1028 st->print("#reducible ");
1029 break;
1030 case RegionNode::LoopStatus::NeverIrreducibleEntry:
1031 break; // nothing
1032 }
1033 }
1034 #endif
1035
1036 // Find the one non-null required input. RegionNode only
1037 Node *Node::nonnull_req() const {
1038 assert( is_Region(), "" );
1039 for( uint i = 1; i < _cnt; i++ )
1040 if( in(i) )
1041 return in(i);
1042 ShouldNotReachHere();
1043 return nullptr;
1044 }
1045
1046
1047 //=============================================================================
1048 // note that these functions assume that the _adr_type field is flattened
1049 uint PhiNode::hash() const {
1050 const Type* at = _adr_type;
1051 return TypeNode::hash() + (at ? at->hash() : 0);
1052 }
1053 bool PhiNode::cmp( const Node &n ) const {
1054 return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
1055 }
1056 static inline
1057 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
1058 if (at == nullptr || at == TypePtr::BOTTOM) return at;
1059 return Compile::current()->alias_type(at)->adr_type();
1060 }
1061
1062 //----------------------------make---------------------------------------------
1063 // create a new phi with edges matching r and set (initially) to x
1064 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
1065 uint preds = r->req(); // Number of predecessor paths
1066 assert(t != Type::MEMORY || at == flatten_phi_adr_type(at), "flatten at");
1067 PhiNode* p = new PhiNode(r, t, at);
1068 for (uint j = 1; j < preds; j++) {
1069 // Fill in all inputs, except those which the region does not yet have
1070 if (r->in(j) != nullptr)
1071 p->init_req(j, x);
1072 }
1073 return p;
1074 }
1075 PhiNode* PhiNode::make(Node* r, Node* x) {
1076 const Type* t = x->bottom_type();
1077 const TypePtr* at = nullptr;
1078 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
1079 return make(r, x, t, at);
1080 }
1081 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
1082 const Type* t = x->bottom_type();
1083 const TypePtr* at = nullptr;
1084 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
1085 return new PhiNode(r, t, at);
1086 }
1175 np->as_Phi()->verify_adr_type(visited, at);
1176 } else if (n->bottom_type() == Type::TOP
1177 || (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {
1178 // ignore top inputs
1179 } else {
1180 const TypePtr* nat = flatten_phi_adr_type(n->adr_type());
1181 // recheck phi/non-phi consistency at leaves:
1182 assert((nat != nullptr) == (at != nullptr), "");
1183 assert(nat == at || nat == TypePtr::BOTTOM,
1184 "adr_type must be consistent at leaves of phi nest");
1185 }
1186 }
1187 }
1188
1189 // Verify a whole nest of phis rooted at this one.
1190 void PhiNode::verify_adr_type(bool recursive) const {
1191 if (VMError::is_error_reported()) return; // muzzle asserts when debugging an error
1192 if (Node::in_dump()) return; // muzzle asserts when printing
1193
1194 assert((_type == Type::MEMORY) == (_adr_type != nullptr), "adr_type for memory phis only");
1195
1196 if (!VerifyAliases) return; // verify thoroughly only if requested
1197
1198 assert(_adr_type == flatten_phi_adr_type(_adr_type),
1199 "Phi::adr_type must be pre-normalized");
1200
1201 if (recursive) {
1202 VectorSet visited;
1203 verify_adr_type(visited, _adr_type);
1204 }
1205 }
1206 #endif
1207
1208
1209 //------------------------------Value------------------------------------------
1210 // Compute the type of the PhiNode
1211 const Type* PhiNode::Value(PhaseGVN* phase) const {
1212 Node *r = in(0); // RegionNode
1213 if( !r ) // Copy or dead
1214 return in(1) ? phase->type(in(1)) : Type::TOP;
1393 assert(is_diamond_phi() > 0, "sanity");
1394 assert(req() == 3, "same as region");
1395 const Node* region = in(0);
1396 for (uint i = 1; i < 3; i++) {
1397 Node* phi_input = in(i);
1398 if (phi_input != nullptr && phi_input->is_MergeMem() && region->in(i)->outcnt() == 1) {
1399 // Nothing is control-dependent on path #i except the region itself.
1400 MergeMemNode* merge_mem = phi_input->as_MergeMem();
1401 uint j = 3 - i;
1402 Node* other_phi_input = in(j);
1403 if (other_phi_input != nullptr && other_phi_input == merge_mem->base_memory()) {
1404 // merge_mem is a successor memory to other_phi_input, and is not pinned inside the diamond, so push it out.
1405 // This will allow the diamond to collapse completely if there are no other phis left.
1406 igvn->replace_node(this, merge_mem);
1407 return true;
1408 }
1409 }
1410 }
1411 return false;
1412 }
1413 //----------------------------check_cmove_id-----------------------------------
1414 // Check for CMove'ing a constant after comparing against the constant.
1415 // Happens all the time now, since if we compare equality vs a constant in
1416 // the parser, we "know" the variable is constant on one path and we force
1417 // it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
1418 // conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more
1419 // general in that we don't need constants. Since CMove's are only inserted
1420 // in very special circumstances, we do it here on generic Phi's.
1421 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
1422 assert(true_path !=0, "only diamond shape graph expected");
1423
1424 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1425 // phi->region->if_proj->ifnode->bool->cmp
1426 Node* region = in(0);
1427 Node* iff = region->in(1)->in(0);
1428 BoolNode* b = iff->in(1)->as_Bool();
1429 Node* cmp = b->in(1);
1430 Node* tval = in(true_path);
1431 Node* fval = in(3-true_path);
1432 Node* id = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);
2013
2014 if (rc->in(0)->in(1) == nullptr || !rc->in(0)->in(1)->is_Bool()) { continue; }
2015 if (worklist.member(rc->in(0)->in(1))) {
2016 delay = true;
2017 break;
2018 }
2019
2020 if (rc->in(0)->in(1)->in(1) == nullptr || !rc->in(0)->in(1)->in(1)->is_Cmp()) { continue; }
2021 if (worklist.member(rc->in(0)->in(1)->in(1))) {
2022 delay = true;
2023 break;
2024 }
2025 }
2026
2027 if (delay) {
2028 worklist.push(this);
2029 }
2030 return delay;
2031 }
2032
2033 // If the Phi's Region is in an irreducible loop, and the Region
2034 // has had an input removed, but not yet transformed, it could be
2035 // that the Region (and this Phi) are not reachable from Root.
2036 // If we allow the Phi to collapse before the Region, this may lead
2037 // to dead-loop data. Wait for the Region to check for reachability,
2038 // and potentially remove the dead code.
2039 bool PhiNode::must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const {
2040 RegionNode* region = in(0)->as_Region();
2041 if (region->loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
2042 Node* top = phase->C->top();
2043 for (uint j = 1; j < req(); j++) {
2044 Node* rc = region->in(j); // for each control input
2045 if (rc == nullptr || phase->type(rc) == Type::TOP) {
2046 // Region is missing a control input
2047 Node* n = in(j);
2048 if (n != nullptr && n != top) {
2049 // Phi still has its input, so region just lost its input
2050 return true;
2051 }
2052 }
2413 // Phi (this) |
2414 // | |
2415 // +-----------+
2416 //
2417 // Generally, there are issues with non-termination with such circularity
2418 // (see comment further below). However, if there is a direct loop to self,
2419 // splitting the Phi through the MergeMem will result in the below.
2420 //
2421 // +---+
2422 // | |
2423 // v |
2424 // Phi |
2425 // |\ |
2426 // | +-+
2427 // (base_memory) v
2428 // MergeMem
2429 //
2430 // This split breaks the circularity and consequently does not lead to
2431 // non-termination.
2432 uint merge_width = 0;
2433 bool split_always_terminates = false; // Is splitting guaranteed to terminate?
2434 for( uint i=1; i<req(); ++i ) {// For all paths in
2435 Node *ii = in(i);
2436 // TOP inputs should not be counted as safe inputs because if the
2437 // Phi references itself through all other inputs then splitting the
2438 // Phi through memory merges would create dead loop at later stage.
2439 if (ii == top) {
2440 return nullptr; // Delay optimization until graph is cleaned.
2441 }
2442 if (ii->is_MergeMem()) {
2443 MergeMemNode* n = ii->as_MergeMem();
2444 merge_width = MAX2(merge_width, n->req());
2445 if (n->base_memory() == this) {
2446 split_always_terminates = true;
2447 }
2448 }
2449 }
2450
2451 // There are cases with circular dependencies between bottom Phis
2452 // and MergeMems. Below is a minimal example.
2453 //
2454 // +------------+
2455 // | |
2456 // (base_memory) v |
2457 // MergeMem |
2458 // | |
2459 // v |
2460 // Phi (this) |
2461 // | |
2462 // v |
2463 // Phi |
2464 // | |
2465 // +----------+
2466 //
2467 // Here, we cannot break the circularity through a self-loop as there
2468 // are two Phis involved. Repeatedly splitting the Phis through the
2469 // MergeMem leads to non-termination. We check for non-termination below.
2470 // Only check for non-termination if necessary.
2471 if (!split_always_terminates && adr_type() == TypePtr::BOTTOM &&
2472 merge_width > Compile::AliasIdxRaw) {
2473 split_always_terminates = is_split_through_mergemem_terminating();
2474 }
2475
2476 if (merge_width > Compile::AliasIdxRaw) {
2477 // found at least one non-empty MergeMem
2478 const TypePtr* at = adr_type();
2479 if (at != TypePtr::BOTTOM) {
2480 // Patch the existing phi to select an input from the merge:
2481 // Phi:AT1(...MergeMem(m0, m1, m2)...) into
2482 // Phi:AT1(...m1...)
2483 int alias_idx = phase->C->get_alias_index(at);
2484 for (uint i=1; i<req(); ++i) {
2485 Node *ii = in(i);
2486 if (ii->is_MergeMem()) {
2487 MergeMemNode* n = ii->as_MergeMem();
2488 // compress paths and change unreachable cycles to TOP
2489 // If not, we can update the input infinitely along a MergeMem cycle
2490 // Equivalent code is in MemNode::Ideal_common
2491 Node *m = phase->transform(n);
2492 if (outcnt() == 0) { // Above transform() may kill us!
2493 return top;
2494 }
2495 // If transformed to a MergeMem, get the desired slice
2496 // Otherwise the returned node represents memory for every slice
2497 Node *new_mem = (m->is_MergeMem()) ?
2498 m->as_MergeMem()->memory_at(alias_idx) : m;
2499 // Update input if it is progress over what we have now
2500 if (new_mem != ii) {
2501 set_req_X(i, new_mem, phase->is_IterGVN());
2502 progress = this;
2503 }
2504 }
2505 }
2506 } else if (split_always_terminates) {
2507 // If all inputs reference this phi (directly or through data nodes) -
2508 // it is a dead loop.
2509 bool saw_safe_input = false;
2510 for (uint j = 1; j < req(); ++j) {
2511 Node* n = in(j);
2512 if (n->is_MergeMem()) {
2513 MergeMemNode* mm = n->as_MergeMem();
2514 if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {
2515 // Skip this input if it references back to this phi or if the memory path is dead
2516 continue;
2517 }
2518 }
2519 if (!is_unsafe_data_reference(n)) {
2520 saw_safe_input = true; // found safe input
2521 break;
2522 }
2523 }
2524 if (!saw_safe_input) {
2525 // There is a dead loop: All inputs are either dead or reference back to this phi
2526 return top;
2527 }
2528
2529 // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
2530 // MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
2531 PhaseIterGVN* igvn = phase->is_IterGVN();
2532 assert(igvn != nullptr, "sanity check");
2533 Node* hook = new Node(1);
2534 PhiNode* new_base = (PhiNode*) clone();
2535 // Must eagerly register phis, since they participate in loops.
2536 igvn->register_new_node_with_optimizer(new_base);
2537 hook->add_req(new_base);
2538
2539 MergeMemNode* result = MergeMemNode::make(new_base);
2540 for (uint i = 1; i < req(); ++i) {
2541 Node *ii = in(i);
2542 if (ii->is_MergeMem()) {
2543 MergeMemNode* n = ii->as_MergeMem();
2544 for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
2545 // If we have not seen this slice yet, make a phi for it.
2546 bool made_new_phi = false;
2547 if (mms.is_empty()) {
2548 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
2549 made_new_phi = true;
2550 igvn->register_new_node_with_optimizer(new_phi);
2551 hook->add_req(new_phi);
2552 mms.set_memory(new_phi);
2553 }
2554 Node* phi = mms.memory();
2555 assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
2556 phi->set_req(i, mms.memory2());
2557 }
2558 }
2559 }
2560 // Distribute all self-loops.
2561 { // (Extra braces to hide mms.)
2562 for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2563 Node* phi = mms.memory();
2642 if (is_decodeN) {
2643 new_ii = new EncodePNode(ii, narrow_t);
2644 } else {
2645 new_ii = new EncodePKlassNode(ii, narrow_t);
2646 }
2647 igvn->register_new_node_with_optimizer(new_ii);
2648 }
2649 }
2650 new_phi->set_req(i, new_ii);
2651 }
2652 igvn->register_new_node_with_optimizer(new_phi, this);
2653 if (is_decodeN) {
2654 progress = new DecodeNNode(new_phi, bottom_type());
2655 } else {
2656 progress = new DecodeNKlassNode(new_phi, bottom_type());
2657 }
2658 }
2659 }
2660 #endif
2661
2662 // Try to convert a Phi with two duplicated convert nodes into a phi of the pre-conversion type and the convert node
2663 // proceeding the phi, to de-duplicate the convert node and compact the IR.
2664 if (can_reshape && progress == nullptr) {
2665 ConvertNode* convert = in(1)->isa_Convert();
2666 if (convert != nullptr) {
2667 int conv_op = convert->Opcode();
2668 bool ok = true;
2669
2670 // Check the rest of the inputs
2671 for (uint i = 2; i < req(); i++) {
2672 // Make sure that all inputs are of the same type of convert node
2673 if (in(i)->Opcode() != conv_op) {
2674 ok = false;
2675 break;
2676 }
2677 }
2678
2679 if (ok) {
2680 // Find the local bottom type to set as the type of the phi
2681 const Type* source_type = Type::get_const_basic_type(convert->in_type()->basic_type());
2685 // Set inputs to the new phi be the inputs of the convert
2686 for (uint i = 1; i < req(); i++) {
2687 newphi->init_req(i, in(i)->in(1));
2688 }
2689
2690 phase->is_IterGVN()->register_new_node_with_optimizer(newphi, this);
2691
2692 return ConvertNode::create_convert(get_convert_type(convert, source_type), get_convert_type(convert, dest_type), newphi);
2693 }
2694 }
2695 }
2696
2697 // Phi (VB ... VB) => VB (Phi ...) (Phi ...)
2698 if (EnableVectorReboxing && can_reshape && progress == nullptr && type()->isa_oopptr()) {
2699 progress = merge_through_phi(this, phase->is_IterGVN());
2700 }
2701
2702 return progress; // Return any progress
2703 }
2704
2705 static int compare_types(const Type* const& e1, const Type* const& e2) {
2706 return (intptr_t)e1 - (intptr_t)e2;
2707 }
2708
2709 // Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
2710 // Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
2711 // pointer comparison is enough to tell if 2 list of types are the same or not)
2712 const TypeTuple* PhiNode::collect_types(PhaseGVN* phase) const {
2713 const Node* region = in(0);
2714 const Type* phi_type = bottom_type();
2715 ResourceMark rm;
2716 GrowableArray<const Type*> types;
2717 for (uint i = 1; i < req(); i++) {
2718 if (region->in(i) == nullptr || phase->type(region->in(i)) == Type::TOP) {
2719 continue;
2720 }
2721 Node* in = Node::in(i);
2722 const Type* t = phase->type(in);
2723 if (in == nullptr || in == this || t == Type::TOP) {
2724 continue;
3067 #ifndef PRODUCT
3068 void CatchProjNode::dump_spec(outputStream *st) const {
3069 ProjNode::dump_spec(st);
3070 st->print("@bci %d ",_handler_bci);
3071 }
3072 #endif
3073
3074 //=============================================================================
3075 //------------------------------Identity---------------------------------------
3076 // Check for CreateEx being Identity.
3077 Node* CreateExNode::Identity(PhaseGVN* phase) {
3078 if( phase->type(in(1)) == Type::TOP ) return in(1);
3079 if( phase->type(in(0)) == Type::TOP ) return in(0);
3080 if (phase->type(in(0)->in(0)) == Type::TOP) {
3081 assert(in(0)->is_CatchProj(), "control is CatchProj");
3082 return phase->C->top(); // dead code
3083 }
3084 // We only come from CatchProj, unless the CatchProj goes away.
3085 // If the CatchProj is optimized away, then we just carry the
3086 // exception oop through.
3087 CallNode *call = in(1)->in(0)->as_Call();
3088
3089 return (in(0)->is_CatchProj() && in(0)->in(0)->is_Catch() &&
3090 in(0)->in(0)->in(1) == in(1)) ? this : call->in(TypeFunc::Parms);
3091 }
3092
3093 //=============================================================================
3094 //------------------------------Value------------------------------------------
3095 // Check for being unreachable.
3096 const Type* NeverBranchNode::Value(PhaseGVN* phase) const {
3097 if (!in(0) || in(0)->is_top()) return Type::TOP;
3098 return bottom_type();
3099 }
3100
3101 //------------------------------Ideal------------------------------------------
3102 // Check for no longer being part of a loop
3103 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3104 if (can_reshape && !in(0)->is_Region()) {
3105 // Dead code elimination can sometimes delete this projection so
3106 // if it's not there, there's nothing to do.
|
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "gc/shared/barrierSet.hpp"
26 #include "gc/shared/c2/barrierSetC2.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/objArrayKlass.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/castnode.hpp"
32 #include "opto/cfgnode.hpp"
33 #include "opto/connode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/inlinetypenode.hpp"
36 #include "opto/loopnode.hpp"
37 #include "opto/machnode.hpp"
38 #include "opto/movenode.hpp"
39 #include "opto/narrowptrnode.hpp"
40 #include "opto/mulnode.hpp"
41 #include "opto/phaseX.hpp"
42 #include "opto/regalloc.hpp"
43 #include "opto/regmask.hpp"
44 #include "opto/runtime.hpp"
45 #include "opto/subnode.hpp"
46 #include "opto/vectornode.hpp"
47 #include "utilities/vmError.hpp"
48
49 // Portions of code courtesy of Clifford Click
50
51 // Optimization - Graph Style
52
53 //=============================================================================
54 //------------------------------Value------------------------------------------
55 // Compute the type of the RegionNode.
504 if (left_path == nullptr || right_path == nullptr) {
505 return false;
506 }
507 Node* diamond_if = left_path->in(0);
508 if (diamond_if == nullptr || !diamond_if->is_If() || diamond_if != right_path->in(0)) {
509 // Not an IfNode merging a diamond or TOP.
510 return false;
511 }
512
513 // Check for a proper bool/cmp
514 const Node* bol = diamond_if->in(1);
515 if (!bol->is_Bool()) {
516 return false;
517 }
518 const Node* cmp = bol->in(1);
519 if (!cmp->is_Cmp()) {
520 return false;
521 }
522 return true;
523 }
524
525 //------------------------------Ideal------------------------------------------
526 // Return a node which is more "ideal" than the current node. Must preserve
527 // the CFG, but we can still strip out dead paths.
528 Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {
529 if( !can_reshape && !in(0) ) return nullptr; // Already degraded to a Copy
530 assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");
531
532 // Check for RegionNode with no Phi users and both inputs come from either
533 // arm of the same IF. If found, then the control-flow split is useless.
534 bool has_phis = false;
535 if (can_reshape) { // Need DU info to check for Phi users
536 try_clean_mem_phis(phase->is_IterGVN());
537 has_phis = (has_phi() != nullptr); // Cache result
538
539 if (!has_phis) { // No Phi users? Nothing merging?
540 for (uint i = 1; i < req()-1; i++) {
541 Node *if1 = in(i);
542 if( !if1 ) continue;
543 Node *iff = if1->in(0);
544 if( !iff || !iff->is_If() ) continue;
951 if (iff1 == iff2) {
952 igvn->add_users_to_worklist(iff1); // Make sure dead if is eliminated
953 igvn->replace_input_of(region, idx1, iff1->in(0));
954 igvn->replace_input_of(region, idx2, igvn->C->top());
955 return (region == this); // Remove useless if (both projections map to the same control/value)
956 }
957 BoolNode* bol1 = iff1->in(1)->isa_Bool();
958 BoolNode* bol2 = iff2->in(1)->isa_Bool();
959 if (bol1 == nullptr || bol2 == nullptr) {
960 return false; // No bool inputs found
961 }
962 Node* cmp1 = bol1->in(1);
963 Node* cmp2 = bol2->in(1);
964 bool commute = false;
965 if (!cmp1->is_Cmp() || !cmp2->is_Cmp()) {
966 return false; // No comparison
967 } else if (cmp1->Opcode() == Op_CmpF || cmp1->Opcode() == Op_CmpD ||
968 cmp2->Opcode() == Op_CmpF || cmp2->Opcode() == Op_CmpD ||
969 cmp1->Opcode() == Op_CmpP || cmp1->Opcode() == Op_CmpN ||
970 cmp2->Opcode() == Op_CmpP || cmp2->Opcode() == Op_CmpN ||
971 cmp1->is_SubTypeCheck() || cmp2->is_SubTypeCheck() ||
972 cmp1->is_FlatArrayCheck() || cmp2->is_FlatArrayCheck()) {
973 // Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.
974 // SubTypeCheck is not commutative
975 return false;
976 } else if (cmp1 != cmp2) {
977 if (cmp1->in(1) == cmp2->in(2) &&
978 cmp1->in(2) == cmp2->in(1)) {
979 commute = true; // Same but swapped inputs, commute the test
980 } else {
981 return false; // Ifs are not comparing the same values
982 }
983 }
984 proj1 = proj1->other_if_proj();
985 proj2 = proj2->other_if_proj();
986 if (!((proj1->unique_ctrl_out_or_null() == iff2 &&
987 proj2->unique_ctrl_out_or_null() == this) ||
988 (proj2->unique_ctrl_out_or_null() == iff1 &&
989 proj1->unique_ctrl_out_or_null() == this))) {
990 return false; // Ifs are not connected through other projs
991 }
992 // Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged
1031 st->print("#reducible ");
1032 break;
1033 case RegionNode::LoopStatus::NeverIrreducibleEntry:
1034 break; // nothing
1035 }
1036 }
1037 #endif
1038
1039 // Find the one non-null required input. RegionNode only
1040 Node *Node::nonnull_req() const {
1041 assert( is_Region(), "" );
1042 for( uint i = 1; i < _cnt; i++ )
1043 if( in(i) )
1044 return in(i);
1045 ShouldNotReachHere();
1046 return nullptr;
1047 }
1048
1049
1050 //=============================================================================
1051 // note that these functions assume that the _adr_type field is flat
1052 uint PhiNode::hash() const {
1053 const Type* at = _adr_type;
1054 return TypeNode::hash() + (at ? at->hash() : 0);
1055 }
1056 bool PhiNode::cmp( const Node &n ) const {
1057 return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
1058 }
1059 static inline
1060 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
1061 if (at == nullptr || at == TypePtr::BOTTOM) return at;
1062 return Compile::current()->alias_type(at)->adr_type();
1063 }
1064
1065 //----------------------------make---------------------------------------------
1066 // create a new phi with edges matching r and set (initially) to x
1067 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
1068 uint preds = r->req(); // Number of predecessor paths
1069 assert(t != Type::MEMORY || at == flatten_phi_adr_type(at) || (flatten_phi_adr_type(at) == TypeAryPtr::INLINES && Compile::current()->flat_accesses_share_alias()), "flatten at");
1070 PhiNode* p = new PhiNode(r, t, at);
1071 for (uint j = 1; j < preds; j++) {
1072 // Fill in all inputs, except those which the region does not yet have
1073 if (r->in(j) != nullptr)
1074 p->init_req(j, x);
1075 }
1076 return p;
1077 }
1078 PhiNode* PhiNode::make(Node* r, Node* x) {
1079 const Type* t = x->bottom_type();
1080 const TypePtr* at = nullptr;
1081 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
1082 return make(r, x, t, at);
1083 }
1084 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
1085 const Type* t = x->bottom_type();
1086 const TypePtr* at = nullptr;
1087 if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());
1088 return new PhiNode(r, t, at);
1089 }
1178 np->as_Phi()->verify_adr_type(visited, at);
1179 } else if (n->bottom_type() == Type::TOP
1180 || (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {
1181 // ignore top inputs
1182 } else {
1183 const TypePtr* nat = flatten_phi_adr_type(n->adr_type());
1184 // recheck phi/non-phi consistency at leaves:
1185 assert((nat != nullptr) == (at != nullptr), "");
1186 assert(nat == at || nat == TypePtr::BOTTOM,
1187 "adr_type must be consistent at leaves of phi nest");
1188 }
1189 }
1190 }
1191
1192 // Verify a whole nest of phis rooted at this one.
1193 void PhiNode::verify_adr_type(bool recursive) const {
1194 if (VMError::is_error_reported()) return; // muzzle asserts when debugging an error
1195 if (Node::in_dump()) return; // muzzle asserts when printing
1196
1197 assert((_type == Type::MEMORY) == (_adr_type != nullptr), "adr_type for memory phis only");
1198 // Flat array element shouldn't get their own memory slice until flat_accesses_share_alias is cleared.
1199 // It could be the graph has no loads/stores and flat_accesses_share_alias is never cleared. EA could still
1200 // creates per element Phis but that wouldn't be a problem as there are no memory accesses for that array.
1201 assert(_adr_type == nullptr || _adr_type->isa_aryptr() == nullptr ||
1202 _adr_type->is_aryptr()->is_known_instance() ||
1203 !_adr_type->is_aryptr()->is_flat() ||
1204 !Compile::current()->flat_accesses_share_alias() ||
1205 _adr_type == TypeAryPtr::INLINES, "flat array element shouldn't get its own slice yet");
1206
1207 if (!VerifyAliases) return; // verify thoroughly only if requested
1208
1209 assert(_adr_type == flatten_phi_adr_type(_adr_type),
1210 "Phi::adr_type must be pre-normalized");
1211
1212 if (recursive) {
1213 VectorSet visited;
1214 verify_adr_type(visited, _adr_type);
1215 }
1216 }
1217 #endif
1218
1219
1220 //------------------------------Value------------------------------------------
1221 // Compute the type of the PhiNode
1222 const Type* PhiNode::Value(PhaseGVN* phase) const {
1223 Node *r = in(0); // RegionNode
1224 if( !r ) // Copy or dead
1225 return in(1) ? phase->type(in(1)) : Type::TOP;
1404 assert(is_diamond_phi() > 0, "sanity");
1405 assert(req() == 3, "same as region");
1406 const Node* region = in(0);
1407 for (uint i = 1; i < 3; i++) {
1408 Node* phi_input = in(i);
1409 if (phi_input != nullptr && phi_input->is_MergeMem() && region->in(i)->outcnt() == 1) {
1410 // Nothing is control-dependent on path #i except the region itself.
1411 MergeMemNode* merge_mem = phi_input->as_MergeMem();
1412 uint j = 3 - i;
1413 Node* other_phi_input = in(j);
1414 if (other_phi_input != nullptr && other_phi_input == merge_mem->base_memory()) {
1415 // merge_mem is a successor memory to other_phi_input, and is not pinned inside the diamond, so push it out.
1416 // This will allow the diamond to collapse completely if there are no other phis left.
1417 igvn->replace_node(this, merge_mem);
1418 return true;
1419 }
1420 }
1421 }
1422 return false;
1423 }
1424
1425 //----------------------------check_cmove_id-----------------------------------
1426 // Check for CMove'ing a constant after comparing against the constant.
1427 // Happens all the time now, since if we compare equality vs a constant in
1428 // the parser, we "know" the variable is constant on one path and we force
1429 // it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
1430 // conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more
1431 // general in that we don't need constants. Since CMove's are only inserted
1432 // in very special circumstances, we do it here on generic Phi's.
1433 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
1434 assert(true_path !=0, "only diamond shape graph expected");
1435
1436 // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1437 // phi->region->if_proj->ifnode->bool->cmp
1438 Node* region = in(0);
1439 Node* iff = region->in(1)->in(0);
1440 BoolNode* b = iff->in(1)->as_Bool();
1441 Node* cmp = b->in(1);
1442 Node* tval = in(true_path);
1443 Node* fval = in(3-true_path);
1444 Node* id = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);
2025
2026 if (rc->in(0)->in(1) == nullptr || !rc->in(0)->in(1)->is_Bool()) { continue; }
2027 if (worklist.member(rc->in(0)->in(1))) {
2028 delay = true;
2029 break;
2030 }
2031
2032 if (rc->in(0)->in(1)->in(1) == nullptr || !rc->in(0)->in(1)->in(1)->is_Cmp()) { continue; }
2033 if (worklist.member(rc->in(0)->in(1)->in(1))) {
2034 delay = true;
2035 break;
2036 }
2037 }
2038
2039 if (delay) {
2040 worklist.push(this);
2041 }
2042 return delay;
2043 }
2044
2045 // Push inline type input nodes (and null) down through the phi recursively (can handle data loops).
2046 InlineTypeNode* PhiNode::push_inline_types_down(PhaseGVN* phase, bool can_reshape, ciInlineKlass* inline_klass) {
2047 assert(inline_klass != nullptr, "must be");
2048 InlineTypeNode* vt = InlineTypeNode::make_null(*phase, inline_klass, /* transform = */ false)->clone_with_phis(phase, in(0), nullptr, !_type->maybe_null());
2049 if (can_reshape) {
2050 // Replace phi right away to be able to use the inline
2051 // type node when reaching the phi again through data loops.
2052 PhaseIterGVN* igvn = phase->is_IterGVN();
2053 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
2054 Node* u = fast_out(i);
2055 igvn->rehash_node_delayed(u);
2056 imax -= u->replace_edge(this, vt);
2057 --i;
2058 }
2059 igvn->rehash_node_delayed(this);
2060 assert(outcnt() == 0, "should be dead now");
2061 }
2062 ResourceMark rm;
2063 Node_List casts;
2064 for (uint i = 1; i < req(); ++i) {
2065 Node* n = in(i);
2066 while (n->is_ConstraintCast()) {
2067 casts.push(n);
2068 n = n->in(1);
2069 }
2070 if (phase->type(n)->is_zero_type()) {
2071 n = InlineTypeNode::make_null(*phase, inline_klass);
2072 } else if (n->is_Phi()) {
2073 assert(can_reshape, "can only handle phis during IGVN");
2074 n = phase->transform(n->as_Phi()->push_inline_types_down(phase, can_reshape, inline_klass));
2075 }
2076 while (casts.size() != 0) {
2077 // Push the cast(s) through the InlineTypeNode
2078 // TODO 8302217 Can we avoid cloning? See InlineTypeNode::clone_if_required
2079 Node* cast = casts.pop()->clone();
2080 cast->set_req_X(1, n->as_InlineType()->get_oop(), phase);
2081 n = n->clone();
2082 n->as_InlineType()->set_oop(*phase, phase->transform(cast));
2083 n = phase->transform(n);
2084 }
2085 bool transform = !can_reshape && (i == (req()-1)); // Transform phis on last merge
2086 vt->merge_with(phase, n->as_InlineType(), i, transform);
2087 }
2088 return vt;
2089 }
2090
2091 // If the Phi's Region is in an irreducible loop, and the Region
2092 // has had an input removed, but not yet transformed, it could be
2093 // that the Region (and this Phi) are not reachable from Root.
2094 // If we allow the Phi to collapse before the Region, this may lead
2095 // to dead-loop data. Wait for the Region to check for reachability,
2096 // and potentially remove the dead code.
2097 bool PhiNode::must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const {
2098 RegionNode* region = in(0)->as_Region();
2099 if (region->loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
2100 Node* top = phase->C->top();
2101 for (uint j = 1; j < req(); j++) {
2102 Node* rc = region->in(j); // for each control input
2103 if (rc == nullptr || phase->type(rc) == Type::TOP) {
2104 // Region is missing a control input
2105 Node* n = in(j);
2106 if (n != nullptr && n != top) {
2107 // Phi still has its input, so region just lost its input
2108 return true;
2109 }
2110 }
2471 // Phi (this) |
2472 // | |
2473 // +-----------+
2474 //
2475 // Generally, there are issues with non-termination with such circularity
2476 // (see comment further below). However, if there is a direct loop to self,
2477 // splitting the Phi through the MergeMem will result in the below.
2478 //
2479 // +---+
2480 // | |
2481 // v |
2482 // Phi |
2483 // |\ |
2484 // | +-+
2485 // (base_memory) v
2486 // MergeMem
2487 //
2488 // This split breaks the circularity and consequently does not lead to
2489 // non-termination.
2490 uint merge_width = 0;
2491 // TODO revisit this with JDK-8247216
2492 bool mergemem_only = true;
2493 bool split_always_terminates = false; // Is splitting guaranteed to terminate?
2494 for( uint i=1; i<req(); ++i ) {// For all paths in
2495 Node *ii = in(i);
2496 // TOP inputs should not be counted as safe inputs because if the
2497 // Phi references itself through all other inputs then splitting the
2498 // Phi through memory merges would create dead loop at later stage.
2499 if (ii == top) {
2500 return nullptr; // Delay optimization until graph is cleaned.
2501 }
2502 if (ii->is_MergeMem()) {
2503 MergeMemNode* n = ii->as_MergeMem();
2504 merge_width = MAX2(merge_width, n->req());
2505 if (n->base_memory() == this) {
2506 split_always_terminates = true;
2507 }
2508 } else {
2509 mergemem_only = false;
2510 }
2511 }
2512
2513 // There are cases with circular dependencies between bottom Phis
2514 // and MergeMems. Below is a minimal example.
2515 //
2516 // +------------+
2517 // | |
2518 // (base_memory) v |
2519 // MergeMem |
2520 // | |
2521 // v |
2522 // Phi (this) |
2523 // | |
2524 // v |
2525 // Phi |
2526 // | |
2527 // +----------+
2528 //
2529 // Here, we cannot break the circularity through a self-loop as there
2530 // are two Phis involved. Repeatedly splitting the Phis through the
2531 // MergeMem leads to non-termination. We check for non-termination below.
2532 // Only check for non-termination if necessary.
2533 if (!mergemem_only && !split_always_terminates && adr_type() == TypePtr::BOTTOM &&
2534 merge_width > Compile::AliasIdxRaw) {
2535 split_always_terminates = is_split_through_mergemem_terminating();
2536 }
2537
2538 if (merge_width > Compile::AliasIdxRaw) {
2539 // found at least one non-empty MergeMem
2540 const TypePtr* at = adr_type();
2541 if (at != TypePtr::BOTTOM) {
2542 // Patch the existing phi to select an input from the merge:
2543 // Phi:AT1(...MergeMem(m0, m1, m2)...) into
2544 // Phi:AT1(...m1...)
2545 int alias_idx = phase->C->get_alias_index(at);
2546 for (uint i=1; i<req(); ++i) {
2547 Node *ii = in(i);
2548 if (ii->is_MergeMem()) {
2549 MergeMemNode* n = ii->as_MergeMem();
2550 // compress paths and change unreachable cycles to TOP
2551 // If not, we can update the input infinitely along a MergeMem cycle
2552 // Equivalent code is in MemNode::Ideal_common
2553 Node *m = phase->transform(n);
2554 if (outcnt() == 0) { // Above transform() may kill us!
2555 return top;
2556 }
2557 // If transformed to a MergeMem, get the desired slice
2558 // Otherwise the returned node represents memory for every slice
2559 Node *new_mem = (m->is_MergeMem()) ?
2560 m->as_MergeMem()->memory_at(alias_idx) : m;
2561 // Update input if it is progress over what we have now
2562 if (new_mem != ii) {
2563 set_req_X(i, new_mem, phase->is_IterGVN());
2564 progress = this;
2565 }
2566 }
2567 }
2568 } else if (mergemem_only || split_always_terminates) {
2569 // If all inputs reference this phi (directly or through data nodes) -
2570 // it is a dead loop.
2571 bool saw_safe_input = false;
2572 for (uint j = 1; j < req(); ++j) {
2573 Node* n = in(j);
2574 if (n->is_MergeMem()) {
2575 MergeMemNode* mm = n->as_MergeMem();
2576 if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {
2577 // Skip this input if it references back to this phi or if the memory path is dead
2578 continue;
2579 }
2580 }
2581 if (!is_unsafe_data_reference(n)) {
2582 saw_safe_input = true; // found safe input
2583 break;
2584 }
2585 }
2586 if (!saw_safe_input) {
2587 // There is a dead loop: All inputs are either dead or reference back to this phi
2588 return top;
2589 }
2590
2591 // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
2592 // MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
2593 PhaseIterGVN* igvn = phase->is_IterGVN();
2594 assert(igvn != nullptr, "sanity check");
2595 Node* hook = new Node(1);
2596 PhiNode* new_base = (PhiNode*) clone();
2597 // Must eagerly register phis, since they participate in loops.
2598 igvn->register_new_node_with_optimizer(new_base);
2599 hook->add_req(new_base);
2600
2601 MergeMemNode* result = MergeMemNode::make(new_base);
2602 for (uint i = 1; i < req(); ++i) {
2603 Node *ii = in(i);
2604 if (ii->is_MergeMem()) {
2605 MergeMemNode* n = ii->as_MergeMem();
2606 if (igvn) {
2607 // TODO revisit this with JDK-8247216
2608 // Put 'n' on the worklist because it might be modified by MergeMemStream::iteration_setup
2609 igvn->_worklist.push(n);
2610 }
2611 for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
2612 // If we have not seen this slice yet, make a phi for it.
2613 bool made_new_phi = false;
2614 if (mms.is_empty()) {
2615 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
2616 made_new_phi = true;
2617 igvn->register_new_node_with_optimizer(new_phi);
2618 hook->add_req(new_phi);
2619 mms.set_memory(new_phi);
2620 }
2621 Node* phi = mms.memory();
2622 assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
2623 phi->set_req(i, mms.memory2());
2624 }
2625 }
2626 }
2627 // Distribute all self-loops.
2628 { // (Extra braces to hide mms.)
2629 for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2630 Node* phi = mms.memory();
2709 if (is_decodeN) {
2710 new_ii = new EncodePNode(ii, narrow_t);
2711 } else {
2712 new_ii = new EncodePKlassNode(ii, narrow_t);
2713 }
2714 igvn->register_new_node_with_optimizer(new_ii);
2715 }
2716 }
2717 new_phi->set_req(i, new_ii);
2718 }
2719 igvn->register_new_node_with_optimizer(new_phi, this);
2720 if (is_decodeN) {
2721 progress = new DecodeNNode(new_phi, bottom_type());
2722 } else {
2723 progress = new DecodeNKlassNode(new_phi, bottom_type());
2724 }
2725 }
2726 }
2727 #endif
2728
2729 Node* inline_type = try_push_inline_types_down(phase, can_reshape);
2730 if (inline_type != this) {
2731 return inline_type;
2732 }
2733
2734 // Try to convert a Phi with two duplicated convert nodes into a phi of the pre-conversion type and the convert node
2735 // proceeding the phi, to de-duplicate the convert node and compact the IR.
2736 if (can_reshape && progress == nullptr) {
2737 ConvertNode* convert = in(1)->isa_Convert();
2738 if (convert != nullptr) {
2739 int conv_op = convert->Opcode();
2740 bool ok = true;
2741
2742 // Check the rest of the inputs
2743 for (uint i = 2; i < req(); i++) {
2744 // Make sure that all inputs are of the same type of convert node
2745 if (in(i)->Opcode() != conv_op) {
2746 ok = false;
2747 break;
2748 }
2749 }
2750
2751 if (ok) {
2752 // Find the local bottom type to set as the type of the phi
2753 const Type* source_type = Type::get_const_basic_type(convert->in_type()->basic_type());
2757 // Set inputs to the new phi be the inputs of the convert
2758 for (uint i = 1; i < req(); i++) {
2759 newphi->init_req(i, in(i)->in(1));
2760 }
2761
2762 phase->is_IterGVN()->register_new_node_with_optimizer(newphi, this);
2763
2764 return ConvertNode::create_convert(get_convert_type(convert, source_type), get_convert_type(convert, dest_type), newphi);
2765 }
2766 }
2767 }
2768
2769 // Phi (VB ... VB) => VB (Phi ...) (Phi ...)
2770 if (EnableVectorReboxing && can_reshape && progress == nullptr && type()->isa_oopptr()) {
2771 progress = merge_through_phi(this, phase->is_IterGVN());
2772 }
2773
2774 return progress; // Return any progress
2775 }
2776
2777 // Check recursively if inputs are either an inline type, constant null
2778 // or another Phi (including self references through data loops). If so,
2779 // push the inline types down through the phis to enable folding of loads.
2780 Node* PhiNode::try_push_inline_types_down(PhaseGVN* phase, const bool can_reshape) {
2781 if (!can_be_inline_type()) {
2782 return this;
2783 }
2784
2785 ciInlineKlass* inline_klass;
2786 if (can_push_inline_types_down(phase, can_reshape, inline_klass)) {
2787 assert(inline_klass != nullptr, "must be");
2788 return push_inline_types_down(phase, can_reshape, inline_klass);
2789 }
2790 return this;
2791 }
2792
2793 bool PhiNode::can_push_inline_types_down(PhaseGVN* phase, const bool can_reshape, ciInlineKlass*& inline_klass) {
2794 if (req() <= 2) {
2795 // Dead phi.
2796 return false;
2797 }
2798 inline_klass = nullptr;
2799
2800 // TODO 8302217 We need to prevent endless pushing through
2801 bool only_phi = (outcnt() != 0);
2802 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
2803 Node* n = fast_out(i);
2804 if (n->is_InlineType() && n->in(1) == this) {
2805 return false;
2806 }
2807 if (!n->is_Phi()) {
2808 only_phi = false;
2809 }
2810 }
2811 if (only_phi) {
2812 return false;
2813 }
2814
2815 ResourceMark rm;
2816 Unique_Node_List worklist;
2817 worklist.push(this);
2818 Node_List casts;
2819
2820 for (uint next = 0; next < worklist.size(); next++) {
2821 Node* phi = worklist.at(next);
2822 for (uint i = 1; i < phi->req(); i++) {
2823 Node* n = phi->in(i);
2824 if (n == nullptr) {
2825 return false;
2826 }
2827 while (n->is_ConstraintCast()) {
2828 if (n->in(0) != nullptr && n->in(0)->is_top()) {
2829 // Will die, don't optimize
2830 return false;
2831 }
2832 casts.push(n);
2833 n = n->in(1);
2834 }
2835 const Type* type = phase->type(n);
2836 if (n->is_InlineType() && (inline_klass == nullptr || inline_klass == type->inline_klass())) {
2837 inline_klass = type->inline_klass();
2838 } else if (n->is_Phi() && can_reshape && n->bottom_type()->isa_ptr()) {
2839 worklist.push(n);
2840 } else if (!type->is_zero_type()) {
2841 return false;
2842 }
2843 }
2844 }
2845 if (inline_klass == nullptr) {
2846 return false;
2847 }
2848
2849 // Check if cast nodes can be pushed through
2850 const Type* t = Type::get_const_type(inline_klass);
2851 while (casts.size() != 0 && t != nullptr) {
2852 Node* cast = casts.pop();
2853 if (t->filter(cast->bottom_type()) == Type::TOP) {
2854 return false;
2855 }
2856 }
2857
2858 return true;
2859 }
2860
2861 #ifdef ASSERT
2862 bool PhiNode::can_push_inline_types_down(PhaseGVN* phase) {
2863 if (!can_be_inline_type()) {
2864 return false;
2865 }
2866
2867 ciInlineKlass* inline_klass;
2868 return can_push_inline_types_down(phase, true, inline_klass);
2869 }
2870 #endif // ASSERT
2871
2872 static int compare_types(const Type* const& e1, const Type* const& e2) {
2873 return (intptr_t)e1 - (intptr_t)e2;
2874 }
2875
2876 // Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
2877 // Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
2878 // pointer comparison is enough to tell if 2 list of types are the same or not)
2879 const TypeTuple* PhiNode::collect_types(PhaseGVN* phase) const {
2880 const Node* region = in(0);
2881 const Type* phi_type = bottom_type();
2882 ResourceMark rm;
2883 GrowableArray<const Type*> types;
2884 for (uint i = 1; i < req(); i++) {
2885 if (region->in(i) == nullptr || phase->type(region->in(i)) == Type::TOP) {
2886 continue;
2887 }
2888 Node* in = Node::in(i);
2889 const Type* t = phase->type(in);
2890 if (in == nullptr || in == this || t == Type::TOP) {
2891 continue;
3234 #ifndef PRODUCT
3235 void CatchProjNode::dump_spec(outputStream *st) const {
3236 ProjNode::dump_spec(st);
3237 st->print("@bci %d ",_handler_bci);
3238 }
3239 #endif
3240
3241 //=============================================================================
3242 //------------------------------Identity---------------------------------------
3243 // Check for CreateEx being Identity.
3244 Node* CreateExNode::Identity(PhaseGVN* phase) {
3245 if( phase->type(in(1)) == Type::TOP ) return in(1);
3246 if( phase->type(in(0)) == Type::TOP ) return in(0);
3247 if (phase->type(in(0)->in(0)) == Type::TOP) {
3248 assert(in(0)->is_CatchProj(), "control is CatchProj");
3249 return phase->C->top(); // dead code
3250 }
3251 // We only come from CatchProj, unless the CatchProj goes away.
3252 // If the CatchProj is optimized away, then we just carry the
3253 // exception oop through.
3254
3255 // CheckCastPPNode::Ideal() for inline types reuses the exception
3256 // paths of a call to perform an allocation: we can see a Phi here.
3257 if (in(1)->is_Phi()) {
3258 return this;
3259 }
3260 CallNode *call = in(1)->in(0)->as_Call();
3261
3262 return (in(0)->is_CatchProj() && in(0)->in(0)->is_Catch() &&
3263 in(0)->in(0)->in(1) == in(1)) ? this : call->in(TypeFunc::Parms);
3264 }
3265
3266 //=============================================================================
3267 //------------------------------Value------------------------------------------
3268 // Check for being unreachable.
3269 const Type* NeverBranchNode::Value(PhaseGVN* phase) const {
3270 if (!in(0) || in(0)->is_top()) return Type::TOP;
3271 return bottom_type();
3272 }
3273
3274 //------------------------------Ideal------------------------------------------
3275 // Check for no longer being part of a loop
3276 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3277 if (can_reshape && !in(0)->is_Region()) {
3278 // Dead code elimination can sometimes delete this projection so
3279 // if it's not there, there's nothing to do.
|