1164 // SubNode::Value
1165 // CmpPNode::sub
1166 // MemNode::detect_ptr_independence
1167 // MemNode::all_controls_dominate
1168 // We find all controls of a pointer load, and see if they dominate the control of
1169 // an allocation. If they all dominate, we know the allocation is after (independent)
1170 // of the pointer load, and we can say the pointers are different. For this we call
1171 // n->dominates(sub, nlist) to check if controls n of the pointer load dominate the
1172 // control sub of the allocation. The problems is that sometimes dominates answers
1173 // false conservatively, and later it can determine that it is indeed true. Loops with
1174 // Region heads can lead to giving up, whereas LoopNodes can be skipped easier, and
1175 // so the traversal becomes more powerful. This is difficult to remidy, we would have
1176 // to notify the CmpP of CFG updates. Luckily, we recompute CmpP::Value during CCP
1177 // after loop-opts, so that should take care of many of these cases.
1178 return false;
1179 }
1180
1181 stringStream ss; // Print as a block without tty lock.
1182 ss.cr();
1183 ss.print_cr("Missed Value optimization:");
1184 n->dump_bfs(1, nullptr, "", &ss);
1185 ss.print_cr("Current type:");
1186 told->dump_on(&ss);
1187 ss.cr();
1188 ss.print_cr("Optimized type:");
1189 tnew->dump_on(&ss);
1190 ss.cr();
1191 tty->print_cr("%s", ss.as_string());
1192 return true;
1193 }
1194
1195 // Check that all Ideal optimizations that could be done were done.
1196 // Returns true if it found missed optimization opportunities and
1197 // false otherwise (no missed optimization, or skipped verification).
1198 bool PhaseIterGVN::verify_Ideal_for(Node* n, bool can_reshape) {
1199 // First, we check a list of exceptions, where we skip verification,
1200 // because there are known cases where Ideal can optimize after IGVN.
1201 // Some may be expected and cannot be fixed, and others should be fixed.
1202 switch (n->Opcode()) {
1203 // RangeCheckNode::Ideal looks up the chain for about 999 nodes
1204 // (see "Range-Check scan limit"). So, it is possible that something
2057 i->dump_bfs(1, nullptr, "", &ss);
2058 tty->print_cr("%s", ss.as_string());
2059 return true;
2060 }
2061 #endif
2062
2063 /**
2064 * Register a new node with the optimizer. Update the types array, the def-use
2065 * info. Put on worklist.
2066 */
2067 Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) {
2068 set_type_bottom(n);
2069 _worklist.push(n);
2070 if (orig != nullptr) C->copy_node_notes_to(n, orig);
2071 return n;
2072 }
2073
2074 //------------------------------transform--------------------------------------
2075 // Non-recursive: idealize Node 'n' with respect to its inputs and its value
2076 Node *PhaseIterGVN::transform( Node *n ) {
2077 if (_delay_transform) {
2078 // Register the node but don't optimize for now
2079 register_new_node_with_optimizer(n);
2080 return n;
2081 }
2082
2083 // If brand new node, make space in type array, and give it a type.
2084 ensure_type_or_null(n);
2085 if (type_or_null(n) == nullptr) {
2086 set_type_bottom(n);
2087 }
2088
2089 return transform_old(n);
2090 }
2091
2092 Node *PhaseIterGVN::transform_old(Node* n) {
2093 NOT_PRODUCT(set_transforms());
2094 // Remove 'n' from hash table in case it gets modified
2095 _table.hash_delete(n);
2096 #ifdef ASSERT
2097 if (is_verify_def_use()) {
2098 assert(!_table.find_index(n->_idx), "found duplicate entry in table");
2099 }
2100 #endif
2101
2102 // Allow Bool -> Cmp idealisation in late inlining intrinsics that return a bool
2103 if (n->is_Cmp()) {
2104 add_users_to_worklist(n);
2105 }
2106
2107 // Apply the Ideal call in a loop until it no longer applies
2108 Node* k = n;
2341
2342 // Smash all inputs to 'old', isolating him completely
2343 Node *temp = new Node(1);
2344 temp->init_req(0,nn); // Add a use to nn to prevent him from dying
2345 remove_dead_node( old );
2346 temp->del_req(0); // Yank bogus edge
2347 if (nn != nullptr && nn->outcnt() == 0) {
2348 _worklist.push(nn);
2349 }
2350 #ifndef PRODUCT
2351 if (is_verify_def_use()) {
2352 for ( int i = 0; i < _verify_window_size; i++ ) {
2353 if ( _verify_window[i] == old )
2354 _verify_window[i] = nn;
2355 }
2356 }
2357 #endif
2358 temp->destruct(this); // reuse the _idx of this little guy
2359 }
2360
2361 //------------------------------add_users_to_worklist--------------------------
2362 void PhaseIterGVN::add_users_to_worklist0(Node* n, Unique_Node_List& worklist) {
2363 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2364 worklist.push(n->fast_out(i)); // Push on worklist
2365 }
2366 }
2367
2368 // Return counted loop Phi if as a counted loop exit condition, cmp
2369 // compares the induction variable with n
2370 static PhiNode* countedloop_phi_from_cmp(CmpNode* cmp, Node* n) {
2371 for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) {
2372 Node* bol = cmp->fast_out(i);
2373 for (DUIterator_Fast i2max, i2 = bol->fast_outs(i2max); i2 < i2max; i2++) {
2374 Node* iff = bol->fast_out(i2);
2375 if (iff->is_BaseCountedLoopEnd()) {
2376 BaseCountedLoopEndNode* cle = iff->as_BaseCountedLoopEnd();
2377 if (cle->limit() == n) {
2378 PhiNode* phi = cle->phi();
2379 if (phi != nullptr) {
2380 return phi;
2396 add_users_of_use_to_worklist(n, use, worklist);
2397 }
2398 }
2399
2400 void PhaseIterGVN::add_users_of_use_to_worklist(Node* n, Node* use, Unique_Node_List& worklist) {
2401 if(use->is_Multi() || // Multi-definer? Push projs on worklist
2402 use->is_Store() ) // Enable store/load same address
2403 add_users_to_worklist0(use, worklist);
2404
2405 // If we changed the receiver type to a call, we need to revisit
2406 // the Catch following the call. It's looking for a non-null
2407 // receiver to know when to enable the regular fall-through path
2408 // in addition to the NullPtrException path.
2409 if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) {
2410 Node* p = use->as_CallDynamicJava()->proj_out_or_null(TypeFunc::Control);
2411 if (p != nullptr) {
2412 add_users_to_worklist0(p, worklist);
2413 }
2414 }
2415
2416 uint use_op = use->Opcode();
2417 if(use->is_Cmp()) { // Enable CMP/BOOL optimization
2418 add_users_to_worklist0(use, worklist); // Put Bool on worklist
2419 if (use->outcnt() > 0) {
2420 Node* bol = use->raw_out(0);
2421 if (bol->outcnt() > 0) {
2422 Node* iff = bol->raw_out(0);
2423 if (iff->outcnt() == 2) {
2424 // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the
2425 // phi merging either 0 or 1 onto the worklist
2426 Node* ifproj0 = iff->raw_out(0);
2427 Node* ifproj1 = iff->raw_out(1);
2428 if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) {
2429 Node* region0 = ifproj0->raw_out(0);
2430 Node* region1 = ifproj1->raw_out(0);
2431 if( region0 == region1 )
2432 add_users_to_worklist0(region0, worklist);
2433 }
2434 }
2435 }
2493 assert(n == in2, "only in2 modified");
2494 // Find all CastII with input in1.
2495 for (DUIterator_Fast jmax, j = in1->fast_outs(jmax); j < jmax; j++) {
2496 Node* castii = in1->fast_out(j);
2497 if (castii->is_CastII() && castii->as_CastII()->carry_dependency()) {
2498 // Find If.
2499 if (castii->in(0) != nullptr && castii->in(0)->in(0) != nullptr && castii->in(0)->in(0)->is_If()) {
2500 Node* ifnode = castii->in(0)->in(0);
2501 // Check that if connects to the cmp
2502 if (ifnode->in(1) != nullptr && ifnode->in(1)->is_Bool() && ifnode->in(1)->in(1) == cmp) {
2503 worklist.push(castii);
2504 }
2505 }
2506 }
2507 }
2508 }
2509 }
2510 }
2511 }
2512
2513 // If changed Cast input, notify down for Phi, Sub, and Xor - all do "uncast"
2514 // Patterns:
2515 // ConstraintCast+ -> Sub
2516 // ConstraintCast+ -> Phi
2517 // ConstraintCast+ -> Xor
2518 if (use->is_ConstraintCast()) {
2519 auto push_the_uses_to_worklist = [&](Node* n){
2520 if (n->is_Phi() || n->is_Sub() || n->Opcode() == Op_XorI || n->Opcode() == Op_XorL) {
2521 worklist.push(n);
2522 }
2523 };
2524 auto is_boundary = [](Node* n){ return !n->is_ConstraintCast(); };
2525 use->visit_uses(push_the_uses_to_worklist, is_boundary);
2526 }
2527 // If changed LShift inputs, check RShift users for useless sign-ext
2528 if (use_op == Op_LShiftI || use_op == Op_LShiftL) {
2529 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2530 Node* u = use->fast_out(i2);
2531 if (u->Opcode() == Op_RShiftI || u->Opcode() == Op_RShiftL)
2532 worklist.push(u);
2603 // If the ValidLengthTest input changes then the fallthrough path out of the AllocateArray may have become dead.
2604 // CatchNode::Value() is responsible for killing that path. The CatchNode has to be explicitly enqueued for igvn
2605 // to guarantee the change is not missed.
2606 if (use_op == Op_AllocateArray && n == use->in(AllocateNode::ValidLengthTest)) {
2607 Node* p = use->as_AllocateArray()->proj_out_or_null(TypeFunc::Control);
2608 if (p != nullptr) {
2609 add_users_to_worklist0(p, worklist);
2610 }
2611 }
2612
2613 if (use_op == Op_Initialize) {
2614 Node* imem = use->as_Initialize()->proj_out_or_null(TypeFunc::Memory);
2615 if (imem != nullptr) add_users_to_worklist0(imem, worklist);
2616 }
2617 // Loading the java mirror from a Klass requires two loads and the type
2618 // of the mirror load depends on the type of 'n'. See LoadNode::Value().
2619 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
2620 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2621 bool has_load_barrier_nodes = bs->has_load_barrier_nodes();
2622
2623 if (use_op == Op_LoadP && use->bottom_type()->isa_rawptr()) {
2624 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2625 Node* u = use->fast_out(i2);
2626 const Type* ut = u->bottom_type();
2627 if (u->Opcode() == Op_LoadP && ut->isa_instptr()) {
2628 if (has_load_barrier_nodes) {
2629 // Search for load barriers behind the load
2630 for (DUIterator_Fast i3max, i3 = u->fast_outs(i3max); i3 < i3max; i3++) {
2631 Node* b = u->fast_out(i3);
2632 if (bs->is_gc_barrier_node(b)) {
2633 worklist.push(b);
2634 }
2635 }
2636 }
2637 worklist.push(u);
2638 }
2639 }
2640 }
2641 if (use->Opcode() == Op_OpaqueZeroTripGuard) {
2642 assert(use->outcnt() <= 1, "OpaqueZeroTripGuard can't be shared");
2643 if (use->outcnt() == 1) {
2644 Node* cmp = use->unique_out();
2645 worklist.push(cmp);
2646 }
2647 }
2648
2649 // From CastX2PNode::Ideal
2650 // CastX2P(AddX(x, y))
2651 // CastX2P(SubX(x, y))
2652 if (use->Opcode() == Op_AddX || use->Opcode() == Op_SubX) {
2653 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2654 Node* u = use->fast_out(i2);
2655 if (u->Opcode() == Op_CastX2P) {
2656 worklist.push(u);
2657 }
2658 }
2659 }
2660
2740 //------------------------------PhaseCCP---------------------------------------
2741 // Conditional Constant Propagation, ala Wegman & Zadeck
2742 PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) {
2743 NOT_PRODUCT( clear_constants(); )
2744 assert( _worklist.size() == 0, "" );
2745 analyze();
2746 }
2747
2748 #ifndef PRODUCT
2749 //------------------------------~PhaseCCP--------------------------------------
2750 PhaseCCP::~PhaseCCP() {
2751 inc_invokes();
2752 _total_constants += count_constants();
2753 }
2754 #endif
2755
2756
2757 #ifdef ASSERT
2758 void PhaseCCP::verify_type(Node* n, const Type* tnew, const Type* told) {
2759 if (tnew->meet(told) != tnew->remove_speculative()) {
2760 n->dump(1);
2761 tty->print("told = "); told->dump(); tty->cr();
2762 tty->print("tnew = "); tnew->dump(); tty->cr();
2763 fatal("Not monotonic");
2764 }
2765 assert(!told->isa_int() || !tnew->isa_int() || told->is_int()->_widen <= tnew->is_int()->_widen, "widen increases");
2766 assert(!told->isa_long() || !tnew->isa_long() || told->is_long()->_widen <= tnew->is_long()->_widen, "widen increases");
2767 }
2768 #endif //ASSERT
2769
2770 // In this analysis, all types are initially set to TOP. We iteratively call Value() on all nodes of the graph until
2771 // we reach a fixed-point (i.e. no types change anymore). We start with a list that only contains the root node. Each time
2772 // a new type is set, we push all uses of that node back to the worklist (in some cases, we also push grandchildren
2773 // or nodes even further down back to the worklist because their type could change as a result of the current type
2774 // change).
2775 void PhaseCCP::analyze() {
2776 // Initialize all types to TOP, optimistic analysis
2777 for (uint i = 0; i < C->unique(); i++) {
2778 _types.map(i, Type::TOP);
2779 }
2780
2862 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2863 Node* use = n->fast_out(i);
2864 push_if_not_bottom_type(worklist, use);
2865 push_more_uses(worklist, n, use);
2866 }
2867 }
2868
2869 void PhaseCCP::push_if_not_bottom_type(Unique_Node_List& worklist, Node* n) const {
2870 if (n->bottom_type() != type(n)) {
2871 worklist.push(n);
2872 }
2873 }
2874
2875 // For some nodes, we need to propagate the type change to grandchildren or even further down.
2876 // Add them back to the worklist.
2877 void PhaseCCP::push_more_uses(Unique_Node_List& worklist, Node* parent, const Node* use) const {
2878 push_phis(worklist, use);
2879 push_catch(worklist, use);
2880 push_cmpu(worklist, use);
2881 push_counted_loop_phi(worklist, parent, use);
2882 push_loadp(worklist, use);
2883 push_and(worklist, parent, use);
2884 push_cast_ii(worklist, parent, use);
2885 push_opaque_zero_trip_guard(worklist, use);
2886 push_bool_with_cmpu_and_mask(worklist, use);
2887 }
2888
2889
2890 // We must recheck Phis too if use is a Region.
2891 void PhaseCCP::push_phis(Unique_Node_List& worklist, const Node* use) const {
2892 if (use->is_Region()) {
2893 for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
2894 push_if_not_bottom_type(worklist, use->fast_out(i));
2895 }
2896 }
2897 }
2898
2899 // If we changed the receiver type to a call, we need to revisit the Catch node following the call. It's looking for a
2900 // non-null receiver to know when to enable the regular fall-through path in addition to the NullPtrException path.
2901 // Same is true if the type of a ValidLengthTest input to an AllocateArrayNode changes.
2973 continue;
2974 }
2975
2976 Node* m = addI->in(1);
2977 if (m == andI->in(1) || m == andI->in(2)) {
2978 // Is "m" shared? Matched (1b) and thus we revisit Bool.
2979 push_if_not_bottom_type(worklist, bol);
2980 }
2981 }
2982 }
2983
2984 // If n is used in a counted loop exit condition, then the type of the counted loop's Phi depends on the type of 'n'.
2985 // Seem PhiNode::Value().
2986 void PhaseCCP::push_counted_loop_phi(Unique_Node_List& worklist, Node* parent, const Node* use) {
2987 uint use_op = use->Opcode();
2988 if (use_op == Op_CmpI || use_op == Op_CmpL) {
2989 PhiNode* phi = countedloop_phi_from_cmp(use->as_Cmp(), parent);
2990 if (phi != nullptr) {
2991 worklist.push(phi);
2992 }
2993 }
2994 }
2995
2996 // Loading the java mirror from a Klass requires two loads and the type of the mirror load depends on the type of 'n'.
2997 // See LoadNode::Value().
2998 void PhaseCCP::push_loadp(Unique_Node_List& worklist, const Node* use) const {
2999 BarrierSetC2* barrier_set = BarrierSet::barrier_set()->barrier_set_c2();
3000 bool has_load_barrier_nodes = barrier_set->has_load_barrier_nodes();
3001
3002 if (use->Opcode() == Op_LoadP && use->bottom_type()->isa_rawptr()) {
3003 for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
3004 Node* loadp = use->fast_out(i);
3005 const Type* ut = loadp->bottom_type();
3006 if (loadp->Opcode() == Op_LoadP && ut->isa_instptr() && ut != type(loadp)) {
3007 if (has_load_barrier_nodes) {
3008 // Search for load barriers behind the load
3009 push_load_barrier(worklist, barrier_set, loadp);
3010 }
3011 worklist.push(loadp);
3012 }
|
1164 // SubNode::Value
1165 // CmpPNode::sub
1166 // MemNode::detect_ptr_independence
1167 // MemNode::all_controls_dominate
1168 // We find all controls of a pointer load, and see if they dominate the control of
1169 // an allocation. If they all dominate, we know the allocation is after (independent)
1170 // of the pointer load, and we can say the pointers are different. For this we call
1171 // n->dominates(sub, nlist) to check if controls n of the pointer load dominate the
1172 // control sub of the allocation. The problems is that sometimes dominates answers
1173 // false conservatively, and later it can determine that it is indeed true. Loops with
1174 // Region heads can lead to giving up, whereas LoopNodes can be skipped easier, and
1175 // so the traversal becomes more powerful. This is difficult to remidy, we would have
1176 // to notify the CmpP of CFG updates. Luckily, we recompute CmpP::Value during CCP
1177 // after loop-opts, so that should take care of many of these cases.
1178 return false;
1179 }
1180
1181 stringStream ss; // Print as a block without tty lock.
1182 ss.cr();
1183 ss.print_cr("Missed Value optimization:");
1184 n->dump_bfs(3, nullptr, "", &ss);
1185 ss.print_cr("Current type:");
1186 told->dump_on(&ss);
1187 ss.cr();
1188 ss.print_cr("Optimized type:");
1189 tnew->dump_on(&ss);
1190 ss.cr();
1191 tty->print_cr("%s", ss.as_string());
1192 return true;
1193 }
1194
1195 // Check that all Ideal optimizations that could be done were done.
1196 // Returns true if it found missed optimization opportunities and
1197 // false otherwise (no missed optimization, or skipped verification).
1198 bool PhaseIterGVN::verify_Ideal_for(Node* n, bool can_reshape) {
1199 // First, we check a list of exceptions, where we skip verification,
1200 // because there are known cases where Ideal can optimize after IGVN.
1201 // Some may be expected and cannot be fixed, and others should be fixed.
1202 switch (n->Opcode()) {
1203 // RangeCheckNode::Ideal looks up the chain for about 999 nodes
1204 // (see "Range-Check scan limit"). So, it is possible that something
2057 i->dump_bfs(1, nullptr, "", &ss);
2058 tty->print_cr("%s", ss.as_string());
2059 return true;
2060 }
2061 #endif
2062
2063 /**
2064 * Register a new node with the optimizer. Update the types array, the def-use
2065 * info. Put on worklist.
2066 */
2067 Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) {
2068 set_type_bottom(n);
2069 _worklist.push(n);
2070 if (orig != nullptr) C->copy_node_notes_to(n, orig);
2071 return n;
2072 }
2073
2074 //------------------------------transform--------------------------------------
2075 // Non-recursive: idealize Node 'n' with respect to its inputs and its value
2076 Node *PhaseIterGVN::transform( Node *n ) {
2077 // If brand new node, make space in type array, and give it a type.
2078 ensure_type_or_null(n);
2079 if (type_or_null(n) == nullptr) {
2080 set_type_bottom(n);
2081 }
2082
2083 if (_delay_transform) {
2084 // Add the node to the worklist but don't optimize for now
2085 _worklist.push(n);
2086 return n;
2087 }
2088
2089 return transform_old(n);
2090 }
2091
2092 Node *PhaseIterGVN::transform_old(Node* n) {
2093 NOT_PRODUCT(set_transforms());
2094 // Remove 'n' from hash table in case it gets modified
2095 _table.hash_delete(n);
2096 #ifdef ASSERT
2097 if (is_verify_def_use()) {
2098 assert(!_table.find_index(n->_idx), "found duplicate entry in table");
2099 }
2100 #endif
2101
2102 // Allow Bool -> Cmp idealisation in late inlining intrinsics that return a bool
2103 if (n->is_Cmp()) {
2104 add_users_to_worklist(n);
2105 }
2106
2107 // Apply the Ideal call in a loop until it no longer applies
2108 Node* k = n;
2341
2342 // Smash all inputs to 'old', isolating him completely
2343 Node *temp = new Node(1);
2344 temp->init_req(0,nn); // Add a use to nn to prevent him from dying
2345 remove_dead_node( old );
2346 temp->del_req(0); // Yank bogus edge
2347 if (nn != nullptr && nn->outcnt() == 0) {
2348 _worklist.push(nn);
2349 }
2350 #ifndef PRODUCT
2351 if (is_verify_def_use()) {
2352 for ( int i = 0; i < _verify_window_size; i++ ) {
2353 if ( _verify_window[i] == old )
2354 _verify_window[i] = nn;
2355 }
2356 }
2357 #endif
2358 temp->destruct(this); // reuse the _idx of this little guy
2359 }
2360
2361 void PhaseIterGVN::replace_in_uses(Node* n, Node* m) {
2362 assert(n != nullptr, "sanity");
2363 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2364 Node* u = n->fast_out(i);
2365 if (u != n) {
2366 rehash_node_delayed(u);
2367 int nb = u->replace_edge(n, m);
2368 --i, imax -= nb;
2369 }
2370 }
2371 assert(n->outcnt() == 0, "all uses must be deleted");
2372 }
2373
2374 //------------------------------add_users_to_worklist--------------------------
2375 void PhaseIterGVN::add_users_to_worklist0(Node* n, Unique_Node_List& worklist) {
2376 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2377 worklist.push(n->fast_out(i)); // Push on worklist
2378 }
2379 }
2380
2381 // Return counted loop Phi if as a counted loop exit condition, cmp
2382 // compares the induction variable with n
2383 static PhiNode* countedloop_phi_from_cmp(CmpNode* cmp, Node* n) {
2384 for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) {
2385 Node* bol = cmp->fast_out(i);
2386 for (DUIterator_Fast i2max, i2 = bol->fast_outs(i2max); i2 < i2max; i2++) {
2387 Node* iff = bol->fast_out(i2);
2388 if (iff->is_BaseCountedLoopEnd()) {
2389 BaseCountedLoopEndNode* cle = iff->as_BaseCountedLoopEnd();
2390 if (cle->limit() == n) {
2391 PhiNode* phi = cle->phi();
2392 if (phi != nullptr) {
2393 return phi;
2409 add_users_of_use_to_worklist(n, use, worklist);
2410 }
2411 }
2412
2413 void PhaseIterGVN::add_users_of_use_to_worklist(Node* n, Node* use, Unique_Node_List& worklist) {
2414 if(use->is_Multi() || // Multi-definer? Push projs on worklist
2415 use->is_Store() ) // Enable store/load same address
2416 add_users_to_worklist0(use, worklist);
2417
2418 // If we changed the receiver type to a call, we need to revisit
2419 // the Catch following the call. It's looking for a non-null
2420 // receiver to know when to enable the regular fall-through path
2421 // in addition to the NullPtrException path.
2422 if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) {
2423 Node* p = use->as_CallDynamicJava()->proj_out_or_null(TypeFunc::Control);
2424 if (p != nullptr) {
2425 add_users_to_worklist0(p, worklist);
2426 }
2427 }
2428
2429 // AndLNode::Ideal folds GraphKit::mark_word_test patterns. Give it a chance to run.
2430 if (n->is_Load() && use->is_Phi()) {
2431 for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
2432 Node* u = use->fast_out(i);
2433 if (u->Opcode() == Op_AndL) {
2434 worklist.push(u);
2435 }
2436 }
2437 }
2438
2439 uint use_op = use->Opcode();
2440 if(use->is_Cmp()) { // Enable CMP/BOOL optimization
2441 add_users_to_worklist0(use, worklist); // Put Bool on worklist
2442 if (use->outcnt() > 0) {
2443 Node* bol = use->raw_out(0);
2444 if (bol->outcnt() > 0) {
2445 Node* iff = bol->raw_out(0);
2446 if (iff->outcnt() == 2) {
2447 // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the
2448 // phi merging either 0 or 1 onto the worklist
2449 Node* ifproj0 = iff->raw_out(0);
2450 Node* ifproj1 = iff->raw_out(1);
2451 if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) {
2452 Node* region0 = ifproj0->raw_out(0);
2453 Node* region1 = ifproj1->raw_out(0);
2454 if( region0 == region1 )
2455 add_users_to_worklist0(region0, worklist);
2456 }
2457 }
2458 }
2516 assert(n == in2, "only in2 modified");
2517 // Find all CastII with input in1.
2518 for (DUIterator_Fast jmax, j = in1->fast_outs(jmax); j < jmax; j++) {
2519 Node* castii = in1->fast_out(j);
2520 if (castii->is_CastII() && castii->as_CastII()->carry_dependency()) {
2521 // Find If.
2522 if (castii->in(0) != nullptr && castii->in(0)->in(0) != nullptr && castii->in(0)->in(0)->is_If()) {
2523 Node* ifnode = castii->in(0)->in(0);
2524 // Check that if connects to the cmp
2525 if (ifnode->in(1) != nullptr && ifnode->in(1)->is_Bool() && ifnode->in(1)->in(1) == cmp) {
2526 worklist.push(castii);
2527 }
2528 }
2529 }
2530 }
2531 }
2532 }
2533 }
2534 }
2535
2536 // Inline type nodes can have other inline types as users. If an input gets
2537 // updated, make sure that inline type users get a chance for optimization.
2538 if (use->is_InlineType()) {
2539 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2540 Node* u = use->fast_out(i2);
2541 if (u->is_InlineType())
2542 worklist.push(u);
2543 }
2544 }
2545 // If changed Cast input, notify down for Phi, Sub, and Xor - all do "uncast"
2546 // Patterns:
2547 // ConstraintCast+ -> Sub
2548 // ConstraintCast+ -> Phi
2549 // ConstraintCast+ -> Xor
2550 if (use->is_ConstraintCast()) {
2551 auto push_the_uses_to_worklist = [&](Node* n){
2552 if (n->is_Phi() || n->is_Sub() || n->Opcode() == Op_XorI || n->Opcode() == Op_XorL) {
2553 worklist.push(n);
2554 }
2555 };
2556 auto is_boundary = [](Node* n){ return !n->is_ConstraintCast(); };
2557 use->visit_uses(push_the_uses_to_worklist, is_boundary);
2558 }
2559 // If changed LShift inputs, check RShift users for useless sign-ext
2560 if (use_op == Op_LShiftI || use_op == Op_LShiftL) {
2561 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2562 Node* u = use->fast_out(i2);
2563 if (u->Opcode() == Op_RShiftI || u->Opcode() == Op_RShiftL)
2564 worklist.push(u);
2635 // If the ValidLengthTest input changes then the fallthrough path out of the AllocateArray may have become dead.
2636 // CatchNode::Value() is responsible for killing that path. The CatchNode has to be explicitly enqueued for igvn
2637 // to guarantee the change is not missed.
2638 if (use_op == Op_AllocateArray && n == use->in(AllocateNode::ValidLengthTest)) {
2639 Node* p = use->as_AllocateArray()->proj_out_or_null(TypeFunc::Control);
2640 if (p != nullptr) {
2641 add_users_to_worklist0(p, worklist);
2642 }
2643 }
2644
2645 if (use_op == Op_Initialize) {
2646 Node* imem = use->as_Initialize()->proj_out_or_null(TypeFunc::Memory);
2647 if (imem != nullptr) add_users_to_worklist0(imem, worklist);
2648 }
2649 // Loading the java mirror from a Klass requires two loads and the type
2650 // of the mirror load depends on the type of 'n'. See LoadNode::Value().
2651 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
2652 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2653 bool has_load_barrier_nodes = bs->has_load_barrier_nodes();
2654
2655 if (use_op == Op_CastP2X) {
2656 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2657 Node* u = use->fast_out(i2);
2658 if (u->Opcode() == Op_AndX) {
2659 worklist.push(u);
2660 }
2661 }
2662 }
2663 if (use_op == Op_LoadP && use->bottom_type()->isa_rawptr()) {
2664 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2665 Node* u = use->fast_out(i2);
2666 const Type* ut = u->bottom_type();
2667 if (u->Opcode() == Op_LoadP && ut->isa_instptr()) {
2668 if (has_load_barrier_nodes) {
2669 // Search for load barriers behind the load
2670 for (DUIterator_Fast i3max, i3 = u->fast_outs(i3max); i3 < i3max; i3++) {
2671 Node* b = u->fast_out(i3);
2672 if (bs->is_gc_barrier_node(b)) {
2673 worklist.push(b);
2674 }
2675 }
2676 }
2677 worklist.push(u);
2678 }
2679 }
2680 }
2681 // Give CallStaticJavaNode::remove_useless_allocation a chance to run
2682 if (use->is_Region()) {
2683 Node* c = use;
2684 do {
2685 c = c->unique_ctrl_out_or_null();
2686 } while (c != nullptr && c->is_Region());
2687 if (c != nullptr && c->is_CallStaticJava() && c->as_CallStaticJava()->uncommon_trap_request() != 0) {
2688 worklist.push(c);
2689 }
2690 }
2691 if (use->Opcode() == Op_OpaqueZeroTripGuard) {
2692 assert(use->outcnt() <= 1, "OpaqueZeroTripGuard can't be shared");
2693 if (use->outcnt() == 1) {
2694 Node* cmp = use->unique_out();
2695 worklist.push(cmp);
2696 }
2697 }
2698
2699 // From CastX2PNode::Ideal
2700 // CastX2P(AddX(x, y))
2701 // CastX2P(SubX(x, y))
2702 if (use->Opcode() == Op_AddX || use->Opcode() == Op_SubX) {
2703 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2704 Node* u = use->fast_out(i2);
2705 if (u->Opcode() == Op_CastX2P) {
2706 worklist.push(u);
2707 }
2708 }
2709 }
2710
2790 //------------------------------PhaseCCP---------------------------------------
2791 // Conditional Constant Propagation, ala Wegman & Zadeck
2792 PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) {
2793 NOT_PRODUCT( clear_constants(); )
2794 assert( _worklist.size() == 0, "" );
2795 analyze();
2796 }
2797
2798 #ifndef PRODUCT
2799 //------------------------------~PhaseCCP--------------------------------------
2800 PhaseCCP::~PhaseCCP() {
2801 inc_invokes();
2802 _total_constants += count_constants();
2803 }
2804 #endif
2805
2806
2807 #ifdef ASSERT
2808 void PhaseCCP::verify_type(Node* n, const Type* tnew, const Type* told) {
2809 if (tnew->meet(told) != tnew->remove_speculative()) {
2810 n->dump(3);
2811 tty->print("told = "); told->dump(); tty->cr();
2812 tty->print("tnew = "); tnew->dump(); tty->cr();
2813 fatal("Not monotonic");
2814 }
2815 assert(!told->isa_int() || !tnew->isa_int() || told->is_int()->_widen <= tnew->is_int()->_widen, "widen increases");
2816 assert(!told->isa_long() || !tnew->isa_long() || told->is_long()->_widen <= tnew->is_long()->_widen, "widen increases");
2817 }
2818 #endif //ASSERT
2819
2820 // In this analysis, all types are initially set to TOP. We iteratively call Value() on all nodes of the graph until
2821 // we reach a fixed-point (i.e. no types change anymore). We start with a list that only contains the root node. Each time
2822 // a new type is set, we push all uses of that node back to the worklist (in some cases, we also push grandchildren
2823 // or nodes even further down back to the worklist because their type could change as a result of the current type
2824 // change).
2825 void PhaseCCP::analyze() {
2826 // Initialize all types to TOP, optimistic analysis
2827 for (uint i = 0; i < C->unique(); i++) {
2828 _types.map(i, Type::TOP);
2829 }
2830
2912 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2913 Node* use = n->fast_out(i);
2914 push_if_not_bottom_type(worklist, use);
2915 push_more_uses(worklist, n, use);
2916 }
2917 }
2918
2919 void PhaseCCP::push_if_not_bottom_type(Unique_Node_List& worklist, Node* n) const {
2920 if (n->bottom_type() != type(n)) {
2921 worklist.push(n);
2922 }
2923 }
2924
2925 // For some nodes, we need to propagate the type change to grandchildren or even further down.
2926 // Add them back to the worklist.
2927 void PhaseCCP::push_more_uses(Unique_Node_List& worklist, Node* parent, const Node* use) const {
2928 push_phis(worklist, use);
2929 push_catch(worklist, use);
2930 push_cmpu(worklist, use);
2931 push_counted_loop_phi(worklist, parent, use);
2932 push_cast(worklist, use);
2933 push_loadp(worklist, use);
2934 push_and(worklist, parent, use);
2935 push_cast_ii(worklist, parent, use);
2936 push_opaque_zero_trip_guard(worklist, use);
2937 push_bool_with_cmpu_and_mask(worklist, use);
2938 }
2939
2940
2941 // We must recheck Phis too if use is a Region.
2942 void PhaseCCP::push_phis(Unique_Node_List& worklist, const Node* use) const {
2943 if (use->is_Region()) {
2944 for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
2945 push_if_not_bottom_type(worklist, use->fast_out(i));
2946 }
2947 }
2948 }
2949
2950 // If we changed the receiver type to a call, we need to revisit the Catch node following the call. It's looking for a
2951 // non-null receiver to know when to enable the regular fall-through path in addition to the NullPtrException path.
2952 // Same is true if the type of a ValidLengthTest input to an AllocateArrayNode changes.
3024 continue;
3025 }
3026
3027 Node* m = addI->in(1);
3028 if (m == andI->in(1) || m == andI->in(2)) {
3029 // Is "m" shared? Matched (1b) and thus we revisit Bool.
3030 push_if_not_bottom_type(worklist, bol);
3031 }
3032 }
3033 }
3034
3035 // If n is used in a counted loop exit condition, then the type of the counted loop's Phi depends on the type of 'n'.
3036 // Seem PhiNode::Value().
3037 void PhaseCCP::push_counted_loop_phi(Unique_Node_List& worklist, Node* parent, const Node* use) {
3038 uint use_op = use->Opcode();
3039 if (use_op == Op_CmpI || use_op == Op_CmpL) {
3040 PhiNode* phi = countedloop_phi_from_cmp(use->as_Cmp(), parent);
3041 if (phi != nullptr) {
3042 worklist.push(phi);
3043 }
3044 }
3045 }
3046
3047 void PhaseCCP::push_cast(Unique_Node_List& worklist, const Node* use) {
3048 uint use_op = use->Opcode();
3049 if (use_op == Op_CastP2X) {
3050 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
3051 Node* u = use->fast_out(i2);
3052 if (u->Opcode() == Op_AndX) {
3053 worklist.push(u);
3054 }
3055 }
3056 }
3057 }
3058
3059 // Loading the java mirror from a Klass requires two loads and the type of the mirror load depends on the type of 'n'.
3060 // See LoadNode::Value().
3061 void PhaseCCP::push_loadp(Unique_Node_List& worklist, const Node* use) const {
3062 BarrierSetC2* barrier_set = BarrierSet::barrier_set()->barrier_set_c2();
3063 bool has_load_barrier_nodes = barrier_set->has_load_barrier_nodes();
3064
3065 if (use->Opcode() == Op_LoadP && use->bottom_type()->isa_rawptr()) {
3066 for (DUIterator_Fast imax, i = use->fast_outs(imax); i < imax; i++) {
3067 Node* loadp = use->fast_out(i);
3068 const Type* ut = loadp->bottom_type();
3069 if (loadp->Opcode() == Op_LoadP && ut->isa_instptr() && ut != type(loadp)) {
3070 if (has_load_barrier_nodes) {
3071 // Search for load barriers behind the load
3072 push_load_barrier(worklist, barrier_set, loadp);
3073 }
3074 worklist.push(loadp);
3075 }
|