src/hotspot/cpu/x86/macroAssembler_x86.cpp
changeset 51996 84743156e780
parent 51976 390f529f4f22
child 52003 be4614f04eb6
equal deleted inserted replaced
51995:f7babf9d1592 51996:84743156e780
  1028 void MacroAssembler::andptr(Register dst, int32_t imm32) {
  1028 void MacroAssembler::andptr(Register dst, int32_t imm32) {
  1029   LP64_ONLY(andq(dst, imm32)) NOT_LP64(andl(dst, imm32));
  1029   LP64_ONLY(andq(dst, imm32)) NOT_LP64(andl(dst, imm32));
  1030 }
  1030 }
  1031 
  1031 
  1032 void MacroAssembler::atomic_incl(Address counter_addr) {
  1032 void MacroAssembler::atomic_incl(Address counter_addr) {
  1033   if (os::is_MP())
  1033   lock();
  1034     lock();
       
  1035   incrementl(counter_addr);
  1034   incrementl(counter_addr);
  1036 }
  1035 }
  1037 
  1036 
  1038 void MacroAssembler::atomic_incl(AddressLiteral counter_addr, Register scr) {
  1037 void MacroAssembler::atomic_incl(AddressLiteral counter_addr, Register scr) {
  1039   if (reachable(counter_addr)) {
  1038   if (reachable(counter_addr)) {
  1044   }
  1043   }
  1045 }
  1044 }
  1046 
  1045 
  1047 #ifdef _LP64
  1046 #ifdef _LP64
  1048 void MacroAssembler::atomic_incq(Address counter_addr) {
  1047 void MacroAssembler::atomic_incq(Address counter_addr) {
  1049   if (os::is_MP())
  1048   lock();
  1050     lock();
       
  1051   incrementq(counter_addr);
  1049   incrementq(counter_addr);
  1052 }
  1050 }
  1053 
  1051 
  1054 void MacroAssembler::atomic_incq(AddressLiteral counter_addr, Register scr) {
  1052 void MacroAssembler::atomic_incq(AddressLiteral counter_addr, Register scr) {
  1055   if (reachable(counter_addr)) {
  1053   if (reachable(counter_addr)) {
  1211   orptr(tmp_reg, r15_thread);
  1209   orptr(tmp_reg, r15_thread);
  1212 #else
  1210 #else
  1213   get_thread(tmp_reg);
  1211   get_thread(tmp_reg);
  1214   orptr(tmp_reg, swap_reg);
  1212   orptr(tmp_reg, swap_reg);
  1215 #endif
  1213 #endif
  1216   if (os::is_MP()) {
  1214   lock();
  1217     lock();
       
  1218   }
       
  1219   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1215   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1220   // If the biasing toward our thread failed, this means that
  1216   // If the biasing toward our thread failed, this means that
  1221   // another thread succeeded in biasing it toward itself and we
  1217   // another thread succeeded in biasing it toward itself and we
  1222   // need to revoke that bias. The revocation will occur in the
  1218   // need to revoke that bias. The revocation will occur in the
  1223   // interpreter runtime in the slow case.
  1219   // interpreter runtime in the slow case.
  1246 #else
  1242 #else
  1247   get_thread(swap_reg);
  1243   get_thread(swap_reg);
  1248   orptr(tmp_reg, swap_reg);
  1244   orptr(tmp_reg, swap_reg);
  1249   movptr(swap_reg, saved_mark_addr);
  1245   movptr(swap_reg, saved_mark_addr);
  1250 #endif
  1246 #endif
  1251   if (os::is_MP()) {
  1247   lock();
  1252     lock();
       
  1253   }
       
  1254   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1248   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1255   // If the biasing toward our thread failed, then another thread
  1249   // If the biasing toward our thread failed, then another thread
  1256   // succeeded in biasing it toward itself and we need to revoke that
  1250   // succeeded in biasing it toward itself and we need to revoke that
  1257   // bias. The revocation will occur in the runtime in the slow case.
  1251   // bias. The revocation will occur in the runtime in the slow case.
  1258   if (counters != NULL) {
  1252   if (counters != NULL) {
  1276   //
  1270   //
  1277   // FIXME: due to a lack of registers we currently blow away the age
  1271   // FIXME: due to a lack of registers we currently blow away the age
  1278   // bits in this situation. Should attempt to preserve them.
  1272   // bits in this situation. Should attempt to preserve them.
  1279   NOT_LP64( movptr(swap_reg, saved_mark_addr); )
  1273   NOT_LP64( movptr(swap_reg, saved_mark_addr); )
  1280   load_prototype_header(tmp_reg, obj_reg);
  1274   load_prototype_header(tmp_reg, obj_reg);
  1281   if (os::is_MP()) {
  1275   lock();
  1282     lock();
       
  1283   }
       
  1284   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1276   cmpxchgptr(tmp_reg, mark_addr); // compare tmp_reg and swap_reg
  1285   // Fall through to the normal CAS-based lock, because no matter what
  1277   // Fall through to the normal CAS-based lock, because no matter what
  1286   // the result of the above CAS, some thread must have succeeded in
  1278   // the result of the above CAS, some thread must have succeeded in
  1287   // removing the bias bit from the object's header.
  1279   // removing the bias bit from the object's header.
  1288   if (counters != NULL) {
  1280   if (counters != NULL) {
  1374   cmpptr(tmpReg, scrReg);
  1366   cmpptr(tmpReg, scrReg);
  1375   jccb(Assembler::below, L_check_always_rtm1);
  1367   jccb(Assembler::below, L_check_always_rtm1);
  1376   if (method_data != NULL) {
  1368   if (method_data != NULL) {
  1377     // set rtm_state to "no rtm" in MDO
  1369     // set rtm_state to "no rtm" in MDO
  1378     mov_metadata(tmpReg, method_data);
  1370     mov_metadata(tmpReg, method_data);
  1379     if (os::is_MP()) {
  1371     lock();
  1380       lock();
       
  1381     }
       
  1382     orl(Address(tmpReg, MethodData::rtm_state_offset_in_bytes()), NoRTM);
  1372     orl(Address(tmpReg, MethodData::rtm_state_offset_in_bytes()), NoRTM);
  1383   }
  1373   }
  1384   jmpb(L_done);
  1374   jmpb(L_done);
  1385   bind(L_check_always_rtm1);
  1375   bind(L_check_always_rtm1);
  1386   // Reload RTMLockingCounters* address
  1376   // Reload RTMLockingCounters* address
  1390   cmpptr(tmpReg, RTMLockingThreshold / RTMTotalCountIncrRate);
  1380   cmpptr(tmpReg, RTMLockingThreshold / RTMTotalCountIncrRate);
  1391   jccb(Assembler::below, L_done);
  1381   jccb(Assembler::below, L_done);
  1392   if (method_data != NULL) {
  1382   if (method_data != NULL) {
  1393     // set rtm_state to "always rtm" in MDO
  1383     // set rtm_state to "always rtm" in MDO
  1394     mov_metadata(tmpReg, method_data);
  1384     mov_metadata(tmpReg, method_data);
  1395     if (os::is_MP()) {
  1385     lock();
  1396       lock();
       
  1397     }
       
  1398     orl(Address(tmpReg, MethodData::rtm_state_offset_in_bytes()), UseRTM);
  1386     orl(Address(tmpReg, MethodData::rtm_state_offset_in_bytes()), UseRTM);
  1399   }
  1387   }
  1400   bind(L_done);
  1388   bind(L_done);
  1401 }
  1389 }
  1402 
  1390 
  1603   Register threadReg = r15_thread;
  1591   Register threadReg = r15_thread;
  1604 #else
  1592 #else
  1605   get_thread(scrReg);
  1593   get_thread(scrReg);
  1606   Register threadReg = scrReg;
  1594   Register threadReg = scrReg;
  1607 #endif
  1595 #endif
  1608   if (os::is_MP()) {
  1596   lock();
  1609     lock();
       
  1610   }
       
  1611   cmpxchgptr(threadReg, Address(boxReg, owner_offset)); // Updates tmpReg
  1597   cmpxchgptr(threadReg, Address(boxReg, owner_offset)); // Updates tmpReg
  1612 
  1598 
  1613   if (RTMRetryCount > 0) {
  1599   if (RTMRetryCount > 0) {
  1614     // success done else retry
  1600     // success done else retry
  1615     jccb(Assembler::equal, DONE_LABEL) ;
  1601     jccb(Assembler::equal, DONE_LABEL) ;
  1765   jccb(Assembler::notZero, IsInflated);
  1751   jccb(Assembler::notZero, IsInflated);
  1766 
  1752 
  1767   // Attempt stack-locking ...
  1753   // Attempt stack-locking ...
  1768   orptr (tmpReg, markOopDesc::unlocked_value);
  1754   orptr (tmpReg, markOopDesc::unlocked_value);
  1769   movptr(Address(boxReg, 0), tmpReg);          // Anticipate successful CAS
  1755   movptr(Address(boxReg, 0), tmpReg);          // Anticipate successful CAS
  1770   if (os::is_MP()) {
  1756   lock();
  1771     lock();
       
  1772   }
       
  1773   cmpxchgptr(boxReg, Address(objReg, oopDesc::mark_offset_in_bytes()));      // Updates tmpReg
  1757   cmpxchgptr(boxReg, Address(objReg, oopDesc::mark_offset_in_bytes()));      // Updates tmpReg
  1774   if (counters != NULL) {
  1758   if (counters != NULL) {
  1775     cond_inc32(Assembler::equal,
  1759     cond_inc32(Assembler::equal,
  1776                ExternalAddress((address)counters->fast_path_entry_count_addr()));
  1760                ExternalAddress((address)counters->fast_path_entry_count_addr()));
  1777   }
  1761   }
  1824   // But we don't have enough registers, so instead we can either try to CAS
  1808   // But we don't have enough registers, so instead we can either try to CAS
  1825   // rsp or the address of the box (in scr) into &m->owner.  If the CAS succeeds
  1809   // rsp or the address of the box (in scr) into &m->owner.  If the CAS succeeds
  1826   // we later store "Self" into m->Owner.  Transiently storing a stack address
  1810   // we later store "Self" into m->Owner.  Transiently storing a stack address
  1827   // (rsp or the address of the box) into  m->owner is harmless.
  1811   // (rsp or the address of the box) into  m->owner is harmless.
  1828   // Invariant: tmpReg == 0.  tmpReg is EAX which is the implicit cmpxchg comparand.
  1812   // Invariant: tmpReg == 0.  tmpReg is EAX which is the implicit cmpxchg comparand.
  1829   if (os::is_MP()) {
  1813   lock();
  1830     lock();
       
  1831   }
       
  1832   cmpxchgptr(scrReg, Address(boxReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  1814   cmpxchgptr(scrReg, Address(boxReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  1833   movptr(Address(scrReg, 0), 3);          // box->_displaced_header = 3
  1815   movptr(Address(scrReg, 0), 3);          // box->_displaced_header = 3
  1834   // If we weren't able to swing _owner from NULL to the BasicLock
  1816   // If we weren't able to swing _owner from NULL to the BasicLock
  1835   // then take the slow path.
  1817   // then take the slow path.
  1836   jccb  (Assembler::notZero, DONE_LABEL);
  1818   jccb  (Assembler::notZero, DONE_LABEL);
  1849 #else // _LP64
  1831 #else // _LP64
  1850   // It's inflated
  1832   // It's inflated
  1851   movq(scrReg, tmpReg);
  1833   movq(scrReg, tmpReg);
  1852   xorq(tmpReg, tmpReg);
  1834   xorq(tmpReg, tmpReg);
  1853 
  1835 
  1854   if (os::is_MP()) {
  1836   lock();
  1855     lock();
       
  1856   }
       
  1857   cmpxchgptr(r15_thread, Address(scrReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  1837   cmpxchgptr(r15_thread, Address(scrReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  1858   // Unconditionally set box->_displaced_header = markOopDesc::unused_mark().
  1838   // Unconditionally set box->_displaced_header = markOopDesc::unused_mark().
  1859   // Without cast to int32_t movptr will destroy r10 which is typically obj.
  1839   // Without cast to int32_t movptr will destroy r10 which is typically obj.
  1860   movptr(Address(boxReg, 0), (int32_t)intptr_t(markOopDesc::unused_mark()));
  1840   movptr(Address(boxReg, 0), (int32_t)intptr_t(markOopDesc::unused_mark()));
  1861   // Intentional fall-through into DONE_LABEL ...
  1841   // Intentional fall-through into DONE_LABEL ...
  1998   // It must be stack-locked.
  1978   // It must be stack-locked.
  1999   // Try to reset the header to displaced header.
  1979   // Try to reset the header to displaced header.
  2000   // The "box" value on the stack is stable, so we can reload
  1980   // The "box" value on the stack is stable, so we can reload
  2001   // and be assured we observe the same value as above.
  1981   // and be assured we observe the same value as above.
  2002   movptr(tmpReg, Address(boxReg, 0));
  1982   movptr(tmpReg, Address(boxReg, 0));
  2003   if (os::is_MP()) {
  1983   lock();
  2004     lock();
       
  2005   }
       
  2006   cmpxchgptr(tmpReg, Address(objReg, oopDesc::mark_offset_in_bytes())); // Uses RAX which is box
  1984   cmpxchgptr(tmpReg, Address(objReg, oopDesc::mark_offset_in_bytes())); // Uses RAX which is box
  2007   // Intention fall-thru into DONE_LABEL
  1985   // Intention fall-thru into DONE_LABEL
  2008 
  1986 
  2009   // DONE_LABEL is a hot target - we'd really like to place it at the
  1987   // DONE_LABEL is a hot target - we'd really like to place it at the
  2010   // start of cache line by padding with NOPs.
  1988   // start of cache line by padding with NOPs.
  2034   cmpptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(succ)), (int32_t)NULL_WORD);
  2012   cmpptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(succ)), (int32_t)NULL_WORD);
  2035   jccb  (Assembler::zero, LGoSlowPath);
  2013   jccb  (Assembler::zero, LGoSlowPath);
  2036 
  2014 
  2037   xorptr(boxReg, boxReg);
  2015   xorptr(boxReg, boxReg);
  2038   movptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)), (int32_t)NULL_WORD);
  2016   movptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)), (int32_t)NULL_WORD);
  2039   if (os::is_MP()) {
  2017 
  2040     // Memory barrier/fence
  2018   // Memory barrier/fence
  2041     // Dekker pivot point -- fulcrum : ST Owner; MEMBAR; LD Succ
  2019   // Dekker pivot point -- fulcrum : ST Owner; MEMBAR; LD Succ
  2042     // Instead of MFENCE we use a dummy locked add of 0 to the top-of-stack.
  2020   // Instead of MFENCE we use a dummy locked add of 0 to the top-of-stack.
  2043     // This is faster on Nehalem and AMD Shanghai/Barcelona.
  2021   // This is faster on Nehalem and AMD Shanghai/Barcelona.
  2044     // See https://blogs.oracle.com/dave/entry/instruction_selection_for_volatile_fences
  2022   // See https://blogs.oracle.com/dave/entry/instruction_selection_for_volatile_fences
  2045     // We might also restructure (ST Owner=0;barrier;LD _Succ) to
  2023   // We might also restructure (ST Owner=0;barrier;LD _Succ) to
  2046     // (mov box,0; xchgq box, &m->Owner; LD _succ) .
  2024   // (mov box,0; xchgq box, &m->Owner; LD _succ) .
  2047     lock(); addl(Address(rsp, 0), 0);
  2025   lock(); addl(Address(rsp, 0), 0);
  2048   }
  2026 
  2049   cmpptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(succ)), (int32_t)NULL_WORD);
  2027   cmpptr(Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(succ)), (int32_t)NULL_WORD);
  2050   jccb  (Assembler::notZero, LSuccess);
  2028   jccb  (Assembler::notZero, LSuccess);
  2051 
  2029 
  2052   // Rare inopportune interleaving - race.
  2030   // Rare inopportune interleaving - race.
  2053   // The successor vanished in the small window above.
  2031   // The successor vanished in the small window above.
  2061   // coherence traffic on the lock *and* artifically extended the critical section
  2039   // coherence traffic on the lock *and* artifically extended the critical section
  2062   // length while by virtue of passing control into the slow path.
  2040   // length while by virtue of passing control into the slow path.
  2063 
  2041 
  2064   // box is really RAX -- the following CMPXCHG depends on that binding
  2042   // box is really RAX -- the following CMPXCHG depends on that binding
  2065   // cmpxchg R,[M] is equivalent to rax = CAS(M,rax,R)
  2043   // cmpxchg R,[M] is equivalent to rax = CAS(M,rax,R)
  2066   if (os::is_MP()) { lock(); }
  2044   lock();
  2067   cmpxchgptr(r15_thread, Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  2045   cmpxchgptr(r15_thread, Address(tmpReg, OM_OFFSET_NO_MONITOR_VALUE_TAG(owner)));
  2068   // There's no successor so we tried to regrab the lock.
  2046   // There's no successor so we tried to regrab the lock.
  2069   // If that didn't work, then another thread grabbed the
  2047   // If that didn't work, then another thread grabbed the
  2070   // lock so we're done (and exit was a success).
  2048   // lock so we're done (and exit was a success).
  2071   jccb  (Assembler::notEqual, LSuccess);
  2049   jccb  (Assembler::notEqual, LSuccess);
  2079   testl (boxReg, 0);                      // set ICC.ZF=1 to indicate success
  2057   testl (boxReg, 0);                      // set ICC.ZF=1 to indicate success
  2080   jmpb  (DONE_LABEL);
  2058   jmpb  (DONE_LABEL);
  2081 
  2059 
  2082   bind  (Stacked);
  2060   bind  (Stacked);
  2083   movptr(tmpReg, Address (boxReg, 0));      // re-fetch
  2061   movptr(tmpReg, Address (boxReg, 0));      // re-fetch
  2084   if (os::is_MP()) { lock(); }
  2062   lock();
  2085   cmpxchgptr(tmpReg, Address(objReg, oopDesc::mark_offset_in_bytes())); // Uses RAX which is box
  2063   cmpxchgptr(tmpReg, Address(objReg, oopDesc::mark_offset_in_bytes())); // Uses RAX which is box
  2086 
  2064 
  2087 #endif
  2065 #endif
  2088   bind(DONE_LABEL);
  2066   bind(DONE_LABEL);
  2089 }
  2067 }
  2631 }
  2609 }
  2632 #endif
  2610 #endif
  2633 
  2611 
  2634 void MacroAssembler::locked_cmpxchgptr(Register reg, AddressLiteral adr) {
  2612 void MacroAssembler::locked_cmpxchgptr(Register reg, AddressLiteral adr) {
  2635   if (reachable(adr)) {
  2613   if (reachable(adr)) {
  2636     if (os::is_MP())
  2614     lock();
  2637       lock();
       
  2638     cmpxchgptr(reg, as_Address(adr));
  2615     cmpxchgptr(reg, as_Address(adr));
  2639   } else {
  2616   } else {
  2640     lea(rscratch1, adr);
  2617     lea(rscratch1, adr);
  2641     if (os::is_MP())
  2618     lock();
  2642       lock();
       
  2643     cmpxchgptr(reg, Address(rscratch1, 0));
  2619     cmpxchgptr(reg, Address(rscratch1, 0));
  2644   }
  2620   }
  2645 }
  2621 }
  2646 
  2622 
  2647 void MacroAssembler::cmpxchgptr(Register reg, Address adr) {
  2623 void MacroAssembler::cmpxchgptr(Register reg, Address adr) {