1931 break;
1932 }
1933 } else {
1934 break; // found an interesting control
1935 }
1936 }
1937 return ctrl;
1938 }
1939 //
1940 // Given a control, see if it's the control projection of an Unlock which
1941 // operating on the same object as lock.
1942 //
1943 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
1944 GrowableArray<AbstractLockNode*> &lock_ops) {
1945 ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : nullptr;
1946 if (ctrl_proj != nullptr && ctrl_proj->_con == TypeFunc::Control) {
1947 Node *n = ctrl_proj->in(0);
1948 if (n != nullptr && n->is_Unlock()) {
1949 UnlockNode *unlock = n->as_Unlock();
1950 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1951 Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
1952 Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
1953 if (lock_obj->eqv_uncast(unlock_obj) &&
1954 BoxLockNode::same_slot(lock->box_node(), unlock->box_node()) &&
1955 !unlock->is_eliminated()) {
1956 lock_ops.append(unlock);
1957 return true;
1958 }
1959 }
1960 }
1961 return false;
1962 }
1963
1964 //
1965 // Find the lock matching an unlock. Returns null if a safepoint
1966 // or complicated control is encountered first.
1967 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
1968 LockNode *lock_result = nullptr;
1969 // find the matching lock, or an intervening safepoint
1970 Node *ctrl = next_control(unlock->in(0));
1971 while (1) {
1972 assert(ctrl != nullptr, "invalid control graph");
1979 // Check for a simple diamond pattern. Punt on anything more complicated
1980 if (ctrl->req() == 3 && ctrl->in(1) != nullptr && ctrl->in(2) != nullptr) {
1981 Node *in1 = next_control(ctrl->in(1));
1982 Node *in2 = next_control(ctrl->in(2));
1983 if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
1984 (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
1985 ctrl = next_control(in1->in(0)->in(0));
1986 } else {
1987 break;
1988 }
1989 } else {
1990 break;
1991 }
1992 } else {
1993 ctrl = next_control(ctrl->in(0)); // keep searching
1994 }
1995 }
1996 if (ctrl->is_Lock()) {
1997 LockNode *lock = ctrl->as_Lock();
1998 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1999 Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
2000 Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
2001 if (lock_obj->eqv_uncast(unlock_obj) &&
2002 BoxLockNode::same_slot(lock->box_node(), unlock->box_node())) {
2003 lock_result = lock;
2004 }
2005 }
2006 return lock_result;
2007 }
2008
2009 // This code corresponds to case 3 above.
2010
2011 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
2012 GrowableArray<AbstractLockNode*> &lock_ops) {
2013 Node* if_node = node->in(0);
2014 bool if_true = node->is_IfTrue();
2015
2016 if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
2017 Node *lock_ctrl = next_control(if_node->in(0));
2018 if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
2019 Node* lock1_node = nullptr;
2020 ProjNode* proj = if_node->as_If()->proj_out(!if_true);
2021 if (if_true) {
2022 if (proj->is_IfFalse() && proj->outcnt() == 1) {
2023 lock1_node = proj->unique_out();
2024 }
2025 } else {
2026 if (proj->is_IfTrue() && proj->outcnt() == 1) {
2027 lock1_node = proj->unique_out();
2028 }
2029 }
2030 if (lock1_node != nullptr && lock1_node->is_Lock()) {
2031 LockNode *lock1 = lock1_node->as_Lock();
2032 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2033 Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
2034 Node* lock1_obj = bs->step_over_gc_barrier(lock1->obj_node());
2035 if (lock_obj->eqv_uncast(lock1_obj) &&
2036 BoxLockNode::same_slot(lock->box_node(), lock1->box_node()) &&
2037 !lock1->is_eliminated()) {
2038 lock_ops.append(lock1);
2039 return true;
2040 }
2041 }
2042 }
2043 }
2044
2045 lock_ops.trunc_to(0);
2046 return false;
2047 }
2048
2049 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
2050 GrowableArray<AbstractLockNode*> &lock_ops) {
2051 // check each control merging at this point for a matching unlock.
2052 // in(0) should be self edge so skip it.
2053 for (int i = 1; i < (int)region->req(); i++) {
2054 Node *in_node = next_control(region->in(i));
2274 tty->print(" this: ");
2275 this->dump();
2276 tty->print(" box: ");
2277 box->dump();
2278 tty->print(" obj: ");
2279 obj->dump();
2280 if (unique_lock != nullptr) {
2281 tty->print(" unique_lock: ");
2282 unique_lock->dump();
2283 }
2284 if (bad_lock != nullptr) {
2285 tty->print(" bad_lock: ");
2286 bad_lock->dump();
2287 }
2288 tty->print_cr("===============");
2289 }
2290 #endif
2291 return false;
2292 }
2293
2294 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2295 obj = bs->step_over_gc_barrier(obj);
2296 // Look for external lock for the same object.
2297 SafePointNode* sfn = this->as_SafePoint();
2298 JVMState* youngest_jvms = sfn->jvms();
2299 int max_depth = youngest_jvms->depth();
2300 for (int depth = 1; depth <= max_depth; depth++) {
2301 JVMState* jvms = youngest_jvms->of_depth(depth);
2302 int num_mon = jvms->nof_monitors();
2303 // Loop over monitors
2304 for (int idx = 0; idx < num_mon; idx++) {
2305 Node* obj_node = sfn->monitor_obj(jvms, idx);
2306 obj_node = bs->step_over_gc_barrier(obj_node);
2307 BoxLockNode* box_node = sfn->monitor_box(jvms, idx)->as_BoxLock();
2308 if ((box_node->stack_slot() < stk_slot) && obj_node->eqv_uncast(obj)) {
2309 box->set_nested();
2310 return true;
2311 }
2312 }
2313 }
2314 #ifdef ASSERT
2315 this->log_lock_optimization(c, "eliminate_lock_INLR_3");
2316 #endif
2317 return false;
2318 }
2319
2320 //=============================================================================
2321 uint UnlockNode::size_of() const { return sizeof(*this); }
2322
2323 //=============================================================================
2324 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2325
2326 // perform any generic optimizations first (returns 'this' or null)
|
1931 break;
1932 }
1933 } else {
1934 break; // found an interesting control
1935 }
1936 }
1937 return ctrl;
1938 }
1939 //
1940 // Given a control, see if it's the control projection of an Unlock which
1941 // operating on the same object as lock.
1942 //
1943 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
1944 GrowableArray<AbstractLockNode*> &lock_ops) {
1945 ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : nullptr;
1946 if (ctrl_proj != nullptr && ctrl_proj->_con == TypeFunc::Control) {
1947 Node *n = ctrl_proj->in(0);
1948 if (n != nullptr && n->is_Unlock()) {
1949 UnlockNode *unlock = n->as_Unlock();
1950 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1951 Node* lock_obj = lock->obj_node();
1952 Node* unlock_obj = unlock->obj_node();
1953 if (lock_obj->eqv_uncast(unlock_obj) &&
1954 BoxLockNode::same_slot(lock->box_node(), unlock->box_node()) &&
1955 !unlock->is_eliminated()) {
1956 lock_ops.append(unlock);
1957 return true;
1958 }
1959 }
1960 }
1961 return false;
1962 }
1963
1964 //
1965 // Find the lock matching an unlock. Returns null if a safepoint
1966 // or complicated control is encountered first.
1967 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
1968 LockNode *lock_result = nullptr;
1969 // find the matching lock, or an intervening safepoint
1970 Node *ctrl = next_control(unlock->in(0));
1971 while (1) {
1972 assert(ctrl != nullptr, "invalid control graph");
1979 // Check for a simple diamond pattern. Punt on anything more complicated
1980 if (ctrl->req() == 3 && ctrl->in(1) != nullptr && ctrl->in(2) != nullptr) {
1981 Node *in1 = next_control(ctrl->in(1));
1982 Node *in2 = next_control(ctrl->in(2));
1983 if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
1984 (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
1985 ctrl = next_control(in1->in(0)->in(0));
1986 } else {
1987 break;
1988 }
1989 } else {
1990 break;
1991 }
1992 } else {
1993 ctrl = next_control(ctrl->in(0)); // keep searching
1994 }
1995 }
1996 if (ctrl->is_Lock()) {
1997 LockNode *lock = ctrl->as_Lock();
1998 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1999 Node* lock_obj = lock->obj_node();
2000 Node* unlock_obj = unlock->obj_node();
2001 if (lock_obj->eqv_uncast(unlock_obj) &&
2002 BoxLockNode::same_slot(lock->box_node(), unlock->box_node())) {
2003 lock_result = lock;
2004 }
2005 }
2006 return lock_result;
2007 }
2008
2009 // This code corresponds to case 3 above.
2010
2011 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
2012 GrowableArray<AbstractLockNode*> &lock_ops) {
2013 Node* if_node = node->in(0);
2014 bool if_true = node->is_IfTrue();
2015
2016 if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
2017 Node *lock_ctrl = next_control(if_node->in(0));
2018 if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
2019 Node* lock1_node = nullptr;
2020 ProjNode* proj = if_node->as_If()->proj_out(!if_true);
2021 if (if_true) {
2022 if (proj->is_IfFalse() && proj->outcnt() == 1) {
2023 lock1_node = proj->unique_out();
2024 }
2025 } else {
2026 if (proj->is_IfTrue() && proj->outcnt() == 1) {
2027 lock1_node = proj->unique_out();
2028 }
2029 }
2030 if (lock1_node != nullptr && lock1_node->is_Lock()) {
2031 LockNode *lock1 = lock1_node->as_Lock();
2032 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2033 Node* lock_obj = lock->obj_node();
2034 Node* lock1_obj = lock1->obj_node();
2035 if (lock_obj->eqv_uncast(lock1_obj) &&
2036 BoxLockNode::same_slot(lock->box_node(), lock1->box_node()) &&
2037 !lock1->is_eliminated()) {
2038 lock_ops.append(lock1);
2039 return true;
2040 }
2041 }
2042 }
2043 }
2044
2045 lock_ops.trunc_to(0);
2046 return false;
2047 }
2048
2049 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
2050 GrowableArray<AbstractLockNode*> &lock_ops) {
2051 // check each control merging at this point for a matching unlock.
2052 // in(0) should be self edge so skip it.
2053 for (int i = 1; i < (int)region->req(); i++) {
2054 Node *in_node = next_control(region->in(i));
2274 tty->print(" this: ");
2275 this->dump();
2276 tty->print(" box: ");
2277 box->dump();
2278 tty->print(" obj: ");
2279 obj->dump();
2280 if (unique_lock != nullptr) {
2281 tty->print(" unique_lock: ");
2282 unique_lock->dump();
2283 }
2284 if (bad_lock != nullptr) {
2285 tty->print(" bad_lock: ");
2286 bad_lock->dump();
2287 }
2288 tty->print_cr("===============");
2289 }
2290 #endif
2291 return false;
2292 }
2293
2294 // Look for external lock for the same object.
2295 SafePointNode* sfn = this->as_SafePoint();
2296 JVMState* youngest_jvms = sfn->jvms();
2297 int max_depth = youngest_jvms->depth();
2298 for (int depth = 1; depth <= max_depth; depth++) {
2299 JVMState* jvms = youngest_jvms->of_depth(depth);
2300 int num_mon = jvms->nof_monitors();
2301 // Loop over monitors
2302 for (int idx = 0; idx < num_mon; idx++) {
2303 Node* obj_node = sfn->monitor_obj(jvms, idx);
2304 BoxLockNode* box_node = sfn->monitor_box(jvms, idx)->as_BoxLock();
2305 if ((box_node->stack_slot() < stk_slot) && obj_node->eqv_uncast(obj)) {
2306 box->set_nested();
2307 return true;
2308 }
2309 }
2310 }
2311 #ifdef ASSERT
2312 this->log_lock_optimization(c, "eliminate_lock_INLR_3");
2313 #endif
2314 return false;
2315 }
2316
2317 //=============================================================================
2318 uint UnlockNode::size_of() const { return sizeof(*this); }
2319
2320 //=============================================================================
2321 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2322
2323 // perform any generic optimizations first (returns 'this' or null)
|