8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "ci/ciUtilities.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "gc/shared/barrierSet.hpp"
29 #include "gc/shared/c2/barrierSetC2.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "opto/addnode.hpp"
33 #include "opto/castnode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/graphKit.hpp"
36 #include "opto/idealKit.hpp"
37 #include "opto/intrinsicnode.hpp"
38 #include "opto/locknode.hpp"
39 #include "opto/machnode.hpp"
40 #include "opto/opaquenode.hpp"
41 #include "opto/parse.hpp"
42 #include "opto/rootnode.hpp"
43 #include "opto/runtime.hpp"
44 #include "runtime/deoptimization.hpp"
45 #include "runtime/sharedRuntime.hpp"
46
47 //----------------------------GraphKit-----------------------------------------
48 // Main utility constructor.
49 GraphKit::GraphKit(JVMState* jvms)
50 : Phase(Phase::Parser),
51 _env(C->env()),
52 _gvn(*C->initial_gvn()),
53 _barrier_set(BarrierSet::barrier_set()->barrier_set_c2())
54 {
55 _exceptions = jvms->map()->next_exception();
56 if (_exceptions != NULL) jvms->map()->set_next_exception(NULL);
57 set_jvms(jvms);
58 }
59
60 // Private constructor for parser.
61 GraphKit::GraphKit()
62 : Phase(Phase::Parser),
63 _env(C->env()),
64 _gvn(*C->initial_gvn()),
65 _barrier_set(BarrierSet::barrier_set()->barrier_set_c2())
66 {
67 _exceptions = NULL;
68 set_map(NULL);
69 debug_only(_sp = -99);
70 debug_only(set_bci(-99));
71 }
72
73
74
75 //---------------------------clean_stack---------------------------------------
76 // Clear away rubbish from the stack area of the JVM state.
77 // This destroys any arguments that may be waiting on the stack.
806 tty->print_cr("Zombie local %d: ", local);
807 jvms->dump();
808 }
809 return false;
810 }
811 }
812 }
813 return true;
814 }
815
816 #endif //ASSERT
817
818 // Helper function for enforcing certain bytecodes to reexecute if
819 // deoptimization happens
820 static bool should_reexecute_implied_by_bytecode(JVMState *jvms, bool is_anewarray) {
821 ciMethod* cur_method = jvms->method();
822 int cur_bci = jvms->bci();
823 if (cur_method != NULL && cur_bci != InvocationEntryBci) {
824 Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);
825 return Interpreter::bytecode_should_reexecute(code) ||
826 (is_anewarray && code == Bytecodes::_multianewarray);
827 // Reexecute _multianewarray bytecode which was replaced with
828 // sequence of [a]newarray. See Parse::do_multianewarray().
829 //
830 // Note: interpreter should not have it set since this optimization
831 // is limited by dimensions and guarded by flag so in some cases
832 // multianewarray() runtime calls will be generated and
833 // the bytecode should not be reexecutes (stack will not be reset).
834 } else
835 return false;
836 }
837
838 // Helper function for adding JVMState and debug information to node
839 void GraphKit::add_safepoint_edges(SafePointNode* call, bool must_throw) {
840 // Add the safepoint edges to the call (or other safepoint).
841
842 // Make sure dead locals are set to top. This
843 // should help register allocation time and cut down on the size
844 // of the deoptimization information.
845 assert(dead_locals_are_killed(), "garbage in debug info before safepoint");
846
847 // Walk the inline list to fill in the correct set of JVMState's
848 // Also fill in the associated edges for each JVMState.
849
850 // If the bytecode needs to be reexecuted we need to put
851 // the arguments back on the stack.
852 const bool should_reexecute = jvms()->should_reexecute();
853 JVMState* youngest_jvms = should_reexecute ? sync_jvms_for_reexecute() : sync_jvms();
854
855 // NOTE: set_bci (called from sync_jvms) might reset the reexecute bit to
1059 ciSignature* declared_signature = NULL;
1060 ciMethod* ignored_callee = method()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);
1061 assert(declared_signature != NULL, "cannot be null");
1062 inputs = declared_signature->arg_size_for_bc(code);
1063 int size = declared_signature->return_type()->size();
1064 depth = size - inputs;
1065 }
1066 break;
1067
1068 case Bytecodes::_multianewarray:
1069 {
1070 ciBytecodeStream iter(method());
1071 iter.reset_to_bci(bci());
1072 iter.next();
1073 inputs = iter.get_dimensions();
1074 assert(rsize == 1, "");
1075 depth = rsize - inputs;
1076 }
1077 break;
1078
1079 case Bytecodes::_ireturn:
1080 case Bytecodes::_lreturn:
1081 case Bytecodes::_freturn:
1082 case Bytecodes::_dreturn:
1083 case Bytecodes::_areturn:
1084 assert(rsize == -depth, "");
1085 inputs = rsize;
1086 break;
1087
1088 case Bytecodes::_jsr:
1089 case Bytecodes::_jsr_w:
1090 inputs = 0;
1091 depth = 1; // S.B. depth=1, not zero
1092 break;
1093
1094 default:
1095 // bytecode produces a typed result
1096 inputs = rsize - depth;
1097 assert(inputs >= 0, "");
1098 break;
1184 // the incoming address with NULL casted away. You are allowed to use the
1185 // not-null value only if you are control dependent on the test.
1186 #ifndef PRODUCT
1187 extern int explicit_null_checks_inserted,
1188 explicit_null_checks_elided;
1189 #endif
1190 Node* GraphKit::null_check_common(Node* value, BasicType type,
1191 // optional arguments for variations:
1192 bool assert_null,
1193 Node* *null_control,
1194 bool speculative) {
1195 assert(!assert_null || null_control == NULL, "not both at once");
1196 if (stopped()) return top();
1197 NOT_PRODUCT(explicit_null_checks_inserted++);
1198
1199 // Construct NULL check
1200 Node *chk = NULL;
1201 switch(type) {
1202 case T_LONG : chk = new CmpLNode(value, _gvn.zerocon(T_LONG)); break;
1203 case T_INT : chk = new CmpINode(value, _gvn.intcon(0)); break;
1204 case T_ARRAY : // fall through
1205 type = T_OBJECT; // simplify further tests
1206 case T_OBJECT : {
1207 const Type *t = _gvn.type( value );
1208
1209 const TypeOopPtr* tp = t->isa_oopptr();
1210 if (tp != NULL && tp->klass() != NULL && !tp->klass()->is_loaded()
1211 // Only for do_null_check, not any of its siblings:
1212 && !assert_null && null_control == NULL) {
1213 // Usually, any field access or invocation on an unloaded oop type
1214 // will simply fail to link, since the statically linked class is
1215 // likely also to be unloaded. However, in -Xcomp mode, sometimes
1216 // the static class is loaded but the sharper oop type is not.
1217 // Rather than checking for this obscure case in lots of places,
1218 // we simply observe that a null check on an unloaded class
1219 // will always be followed by a nonsense operation, so we
1220 // can just issue the uncommon trap here.
1221 // Our access to the unloaded class will only be correct
1222 // after it has been loaded and initialized, which requires
1223 // a trip through the interpreter.
1355 }
1356
1357 if (assert_null) {
1358 // Cast obj to null on this path.
1359 replace_in_map(value, zerocon(type));
1360 return zerocon(type);
1361 }
1362
1363 // Cast obj to not-null on this path, if there is no null_control.
1364 // (If there is a null_control, a non-null value may come back to haunt us.)
1365 if (type == T_OBJECT) {
1366 Node* cast = cast_not_null(value, false);
1367 if (null_control == NULL || (*null_control) == top())
1368 replace_in_map(value, cast);
1369 value = cast;
1370 }
1371
1372 return value;
1373 }
1374
1375
1376 //------------------------------cast_not_null----------------------------------
1377 // Cast obj to not-null on this path
1378 Node* GraphKit::cast_not_null(Node* obj, bool do_replace_in_map) {
1379 const Type *t = _gvn.type(obj);
1380 const Type *t_not_null = t->join_speculative(TypePtr::NOTNULL);
1381 // Object is already not-null?
1382 if( t == t_not_null ) return obj;
1383
1384 Node *cast = new CastPPNode(obj,t_not_null);
1385 cast->init_req(0, control());
1386 cast = _gvn.transform( cast );
1387
1388 // Scan for instances of 'obj' in the current JVM mapping.
1389 // These instances are known to be not-null after the test.
1390 if (do_replace_in_map)
1391 replace_in_map(obj, cast);
1392
1393 return cast; // Return casted value
1394 }
1395
1396 // Sometimes in intrinsics, we implicitly know an object is not null
1397 // (there's no actual null check) so we can cast it to not null. In
1398 // the course of optimizations, the input to the cast can become null.
1487 int adr_idx,
1488 MemNode::MemOrd mo,
1489 LoadNode::ControlDependency control_dependency,
1490 bool require_atomic_access,
1491 bool unaligned,
1492 bool mismatched,
1493 bool unsafe) {
1494 assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );
1495 const TypePtr* adr_type = NULL; // debug-mode-only argument
1496 debug_only(adr_type = C->get_adr_type(adr_idx));
1497 Node* mem = memory(adr_idx);
1498 Node* ld;
1499 if (require_atomic_access && bt == T_LONG) {
1500 ld = LoadLNode::make_atomic(ctl, mem, adr, adr_type, t, mo, control_dependency, unaligned, mismatched, unsafe);
1501 } else if (require_atomic_access && bt == T_DOUBLE) {
1502 ld = LoadDNode::make_atomic(ctl, mem, adr, adr_type, t, mo, control_dependency, unaligned, mismatched, unsafe);
1503 } else {
1504 ld = LoadNode::make(_gvn, ctl, mem, adr, adr_type, t, bt, mo, control_dependency, unaligned, mismatched, unsafe);
1505 }
1506 ld = _gvn.transform(ld);
1507 if (((bt == T_OBJECT) && C->do_escape_analysis()) || C->eliminate_boxing()) {
1508 // Improve graph before escape analysis and boxing elimination.
1509 record_for_igvn(ld);
1510 }
1511 return ld;
1512 }
1513
1514 Node* GraphKit::store_to_memory(Node* ctl, Node* adr, Node *val, BasicType bt,
1515 int adr_idx,
1516 MemNode::MemOrd mo,
1517 bool require_atomic_access,
1518 bool unaligned,
1519 bool mismatched,
1520 bool unsafe) {
1521 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
1522 const TypePtr* adr_type = NULL;
1523 debug_only(adr_type = C->get_adr_type(adr_idx));
1524 Node *mem = memory(adr_idx);
1525 Node* st;
1526 if (require_atomic_access && bt == T_LONG) {
1527 st = StoreLNode::make_atomic(ctl, mem, adr, adr_type, val, mo);
1538 }
1539 if (unsafe) {
1540 st->as_Store()->set_unsafe_access();
1541 }
1542 st = _gvn.transform(st);
1543 set_memory(st, adr_idx);
1544 // Back-to-back stores can only remove intermediate store with DU info
1545 // so push on worklist for optimizer.
1546 if (mem->req() > MemNode::Address && adr == mem->in(MemNode::Address))
1547 record_for_igvn(st);
1548
1549 return st;
1550 }
1551
1552 Node* GraphKit::access_store_at(Node* obj,
1553 Node* adr,
1554 const TypePtr* adr_type,
1555 Node* val,
1556 const Type* val_type,
1557 BasicType bt,
1558 DecoratorSet decorators) {
1559 // Transformation of a value which could be NULL pointer (CastPP #NULL)
1560 // could be delayed during Parse (for example, in adjust_map_after_if()).
1561 // Execute transformation here to avoid barrier generation in such case.
1562 if (_gvn.type(val) == TypePtr::NULL_PTR) {
1563 val = _gvn.makecon(TypePtr::NULL_PTR);
1564 }
1565
1566 if (stopped()) {
1567 return top(); // Dead path ?
1568 }
1569
1570 assert(val != NULL, "not dead path");
1571
1572 C2AccessValuePtr addr(adr, adr_type);
1573 C2AccessValue value(val, val_type);
1574 C2ParseAccess access(this, decorators | C2_WRITE_ACCESS, bt, obj, addr);
1575 if (access.is_raw()) {
1576 return _barrier_set->BarrierSetC2::store_at(access, value);
1577 } else {
1578 return _barrier_set->store_at(access, value);
1579 }
1580 }
1581
1582 Node* GraphKit::access_load_at(Node* obj, // containing obj
1583 Node* adr, // actual adress to store val at
1584 const TypePtr* adr_type,
1585 const Type* val_type,
1586 BasicType bt,
1587 DecoratorSet decorators) {
1588 if (stopped()) {
1589 return top(); // Dead path ?
1590 }
1671 }
1672 }
1673
1674 Node* GraphKit::access_atomic_add_at(Node* obj,
1675 Node* adr,
1676 const TypePtr* adr_type,
1677 int alias_idx,
1678 Node* new_val,
1679 const Type* value_type,
1680 BasicType bt,
1681 DecoratorSet decorators) {
1682 C2AccessValuePtr addr(adr, adr_type);
1683 C2AtomicParseAccess access(this, decorators | C2_READ_ACCESS | C2_WRITE_ACCESS, bt, obj, addr, alias_idx);
1684 if (access.is_raw()) {
1685 return _barrier_set->BarrierSetC2::atomic_add_at(access, new_val, value_type);
1686 } else {
1687 return _barrier_set->atomic_add_at(access, new_val, value_type);
1688 }
1689 }
1690
1691 void GraphKit::access_clone(Node* src, Node* dst, Node* size, bool is_array) {
1692 return _barrier_set->clone(this, src, dst, size, is_array);
1693 }
1694
1695 Node* GraphKit::access_resolve(Node* n, DecoratorSet decorators) {
1696 // Use stronger ACCESS_WRITE|ACCESS_READ by default.
1697 if ((decorators & (ACCESS_READ | ACCESS_WRITE)) == 0) {
1698 decorators |= ACCESS_READ | ACCESS_WRITE;
1699 }
1700 return _barrier_set->resolve(this, n, decorators);
1701 }
1702
1703 //-------------------------array_element_address-------------------------
1704 Node* GraphKit::array_element_address(Node* ary, Node* idx, BasicType elembt,
1705 const TypeInt* sizetype, Node* ctrl) {
1706 uint shift = exact_log2(type2aelembytes(elembt));
1707 uint header = arrayOopDesc::base_offset_in_bytes(elembt);
1708
1709 // short-circuit a common case (saves lots of confusing waste motion)
1710 jint idx_con = find_int_con(idx, -1);
1711 if (idx_con >= 0) {
1712 intptr_t offset = header + ((intptr_t)idx_con << shift);
1713 return basic_plus_adr(ary, offset);
1714 }
1715
1716 // must be correct type for alignment purposes
1717 Node* base = basic_plus_adr(ary, header);
1718 idx = Compile::conv_I2X_index(&_gvn, idx, sizetype, ctrl);
1719 Node* scale = _gvn.transform( new LShiftXNode(idx, intcon(shift)) );
1720 return basic_plus_adr(ary, base, scale);
1721 }
1722
1723 //-------------------------load_array_element-------------------------
1724 Node* GraphKit::load_array_element(Node* ctl, Node* ary, Node* idx, const TypeAryPtr* arytype) {
1725 const Type* elemtype = arytype->elem();
1726 BasicType elembt = elemtype->array_element_basic_type();
1727 Node* adr = array_element_address(ary, idx, elembt, arytype->size());
1728 if (elembt == T_NARROWOOP) {
1729 elembt = T_OBJECT; // To satisfy switch in LoadNode::make()
1730 }
1731 Node* ld = make_load(ctl, adr, elemtype, elembt, arytype, MemNode::unordered);
1732 return ld;
1733 }
1734
1735 //-------------------------set_arguments_for_java_call-------------------------
1736 // Arguments (pre-popped from the stack) are taken from the JVMS.
1737 void GraphKit::set_arguments_for_java_call(CallJavaNode* call) {
1738 // Add the call arguments:
1739 uint nargs = call->method()->arg_size();
1740 for (uint i = 0; i < nargs; i++) {
1741 Node* arg = argument(i);
1742 call->init_req(i + TypeFunc::Parms, arg);
1743 }
1744 }
1745
1746 //---------------------------set_edges_for_java_call---------------------------
1747 // Connect a newly created call into the current JVMS.
1748 // A return value node (if any) is returned from set_edges_for_java_call.
1749 void GraphKit::set_edges_for_java_call(CallJavaNode* call, bool must_throw, bool separate_io_proj) {
1750
1751 // Add the predefined inputs:
1752 call->init_req( TypeFunc::Control, control() );
1753 call->init_req( TypeFunc::I_O , i_o() );
1754 call->init_req( TypeFunc::Memory , reset_memory() );
1755 call->init_req( TypeFunc::FramePtr, frameptr() );
1756 call->init_req( TypeFunc::ReturnAdr, top() );
1757
1758 add_safepoint_edges(call, must_throw);
1759
1760 Node* xcall = _gvn.transform(call);
1761
1762 if (xcall == top()) {
1763 set_control(top());
1764 return;
1765 }
1766 assert(xcall == call, "call identity is stable");
1767
1768 // Re-use the current map to produce the result.
1769
1770 set_control(_gvn.transform(new ProjNode(call, TypeFunc::Control)));
1771 set_i_o( _gvn.transform(new ProjNode(call, TypeFunc::I_O , separate_io_proj)));
1772 set_all_memory_call(xcall, separate_io_proj);
1773
1774 //return xcall; // no need, caller already has it
1775 }
1776
1777 Node* GraphKit::set_results_for_java_call(CallJavaNode* call, bool separate_io_proj, bool deoptimize) {
1778 if (stopped()) return top(); // maybe the call folded up?
1779
1780 // Capture the return value, if any.
1781 Node* ret;
1782 if (call->method() == NULL ||
1783 call->method()->return_type()->basic_type() == T_VOID)
1784 ret = top();
1785 else ret = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
1786
1787 // Note: Since any out-of-line call can produce an exception,
1788 // we always insert an I_O projection from the call into the result.
1789
1790 make_slow_call_ex(call, env()->Throwable_klass(), separate_io_proj, deoptimize);
1791
1792 if (separate_io_proj) {
1793 // The caller requested separate projections be used by the fall
1794 // through and exceptional paths, so replace the projections for
1795 // the fall through path.
1796 set_i_o(_gvn.transform( new ProjNode(call, TypeFunc::I_O) ));
1797 set_all_memory(_gvn.transform( new ProjNode(call, TypeFunc::Memory) ));
1798 }
1799 return ret;
1800 }
1801
1802 //--------------------set_predefined_input_for_runtime_call--------------------
1803 // Reading and setting the memory state is way conservative here.
1804 // The real problem is that I am not doing real Type analysis on memory,
1805 // so I cannot distinguish card mark stores from other stores. Across a GC
1806 // point the Store Barrier and the card mark memory has to agree. I cannot
1807 // have a card mark store and its barrier split across the GC point from
1808 // either above or below. Here I get that to happen by reading ALL of memory.
1809 // A better answer would be to separate out card marks from other memory.
1810 // For now, return the input memory state, so that it can be reused
1811 // after the call, if this call has restricted memory effects.
1812 Node* GraphKit::set_predefined_input_for_runtime_call(SafePointNode* call, Node* narrow_mem) {
1813 // Set fixed predefined input arguments
1814 Node* memory = reset_memory();
1815 Node* m = narrow_mem == NULL ? memory : narrow_mem;
1816 call->init_req( TypeFunc::Control, control() );
1817 call->init_req( TypeFunc::I_O, top() ); // does no i/o
1818 call->init_req( TypeFunc::Memory, m ); // may gc ptrs
1857 // This is not a "slow path" call; all memory comes from the call.
1858 set_all_memory_call(call);
1859 }
1860 }
1861
1862
1863 // Replace the call with the current state of the kit.
1864 void GraphKit::replace_call(CallNode* call, Node* result, bool do_replaced_nodes) {
1865 JVMState* ejvms = NULL;
1866 if (has_exceptions()) {
1867 ejvms = transfer_exceptions_into_jvms();
1868 }
1869
1870 ReplacedNodes replaced_nodes = map()->replaced_nodes();
1871 ReplacedNodes replaced_nodes_exception;
1872 Node* ex_ctl = top();
1873
1874 SafePointNode* final_state = stop();
1875
1876 // Find all the needed outputs of this call
1877 CallProjections callprojs;
1878 call->extract_projections(&callprojs, true);
1879
1880 Node* init_mem = call->in(TypeFunc::Memory);
1881 Node* final_mem = final_state->in(TypeFunc::Memory);
1882 Node* final_ctl = final_state->in(TypeFunc::Control);
1883 Node* final_io = final_state->in(TypeFunc::I_O);
1884
1885 // Replace all the old call edges with the edges from the inlining result
1886 if (callprojs.fallthrough_catchproj != NULL) {
1887 C->gvn_replace_by(callprojs.fallthrough_catchproj, final_ctl);
1888 }
1889 if (callprojs.fallthrough_memproj != NULL) {
1890 if (final_mem->is_MergeMem()) {
1891 // Parser's exits MergeMem was not transformed but may be optimized
1892 final_mem = _gvn.transform(final_mem);
1893 }
1894 C->gvn_replace_by(callprojs.fallthrough_memproj, final_mem);
1895 }
1896 if (callprojs.fallthrough_ioproj != NULL) {
1897 C->gvn_replace_by(callprojs.fallthrough_ioproj, final_io);
1898 }
1899
1900 // Replace the result with the new result if it exists and is used
1901 if (callprojs.resproj != NULL && result != NULL) {
1902 C->gvn_replace_by(callprojs.resproj, result);
1903 }
1904
1905 if (ejvms == NULL) {
1906 // No exception edges to simply kill off those paths
1907 if (callprojs.catchall_catchproj != NULL) {
1908 C->gvn_replace_by(callprojs.catchall_catchproj, C->top());
1909 }
1910 if (callprojs.catchall_memproj != NULL) {
1911 C->gvn_replace_by(callprojs.catchall_memproj, C->top());
1912 }
1913 if (callprojs.catchall_ioproj != NULL) {
1914 C->gvn_replace_by(callprojs.catchall_ioproj, C->top());
1915 }
1916 // Replace the old exception object with top
1917 if (callprojs.exobj != NULL) {
1918 C->gvn_replace_by(callprojs.exobj, C->top());
1919 }
1920 } else {
1921 GraphKit ekit(ejvms);
1922
1923 // Load my combined exception state into the kit, with all phis transformed:
1924 SafePointNode* ex_map = ekit.combine_and_pop_all_exception_states();
1925 replaced_nodes_exception = ex_map->replaced_nodes();
1926
1927 Node* ex_oop = ekit.use_exception_state(ex_map);
1928
1929 if (callprojs.catchall_catchproj != NULL) {
1930 C->gvn_replace_by(callprojs.catchall_catchproj, ekit.control());
1931 ex_ctl = ekit.control();
1932 }
1933 if (callprojs.catchall_memproj != NULL) {
1934 C->gvn_replace_by(callprojs.catchall_memproj, ekit.reset_memory());
1935 }
1936 if (callprojs.catchall_ioproj != NULL) {
1937 C->gvn_replace_by(callprojs.catchall_ioproj, ekit.i_o());
1938 }
1939
1940 // Replace the old exception object with the newly created one
1941 if (callprojs.exobj != NULL) {
1942 C->gvn_replace_by(callprojs.exobj, ex_oop);
1943 }
1944 }
1945
1946 // Disconnect the call from the graph
1947 call->disconnect_inputs(NULL, C);
1948 C->gvn_replace_by(call, C->top());
1949
1950 // Clean up any MergeMems that feed other MergeMems since the
1951 // optimizer doesn't like that.
1952 if (final_mem->is_MergeMem()) {
1953 Node_List wl;
1954 for (SimpleDUIterator i(final_mem); i.has_next(); i.next()) {
1955 Node* m = i.get();
1956 if (m->is_MergeMem() && !wl.contains(m)) {
1957 wl.push(m);
1958 }
1959 }
1960 while (wl.size() > 0) {
1961 _gvn.transform(wl.pop());
1962 }
1963 }
1964
1965 if (callprojs.fallthrough_catchproj != NULL && !final_ctl->is_top() && do_replaced_nodes) {
1966 replaced_nodes.apply(C, final_ctl);
1967 }
1968 if (!ex_ctl->is_top() && do_replaced_nodes) {
1969 replaced_nodes_exception.apply(C, ex_ctl);
1970 }
1971 }
1972
1973
1974 //------------------------------increment_counter------------------------------
1975 // for statistics: increment a VM counter by 1
1976
1977 void GraphKit::increment_counter(address counter_addr) {
1978 Node* adr1 = makecon(TypeRawPtr::make(counter_addr));
1979 increment_counter(adr1);
1980 }
1981
1982 void GraphKit::increment_counter(Node* counter_addr) {
1983 int adr_type = Compile::AliasIdxRaw;
1984 Node* ctrl = control();
1985 Node* cnt = make_load(ctrl, counter_addr, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
2122 // it does not require card marks.
2123 Node* GraphKit::just_allocated_object(Node* current_control) {
2124 Node* ctrl = current_control;
2125 // Object::<init> is invoked after allocation, most of invoke nodes
2126 // will be reduced, but a region node is kept in parse time, we check
2127 // the pattern and skip the region node if it degraded to a copy.
2128 if (ctrl != NULL && ctrl->is_Region() && ctrl->req() == 2 &&
2129 ctrl->as_Region()->is_copy()) {
2130 ctrl = ctrl->as_Region()->is_copy();
2131 }
2132 if (C->recent_alloc_ctl() == ctrl) {
2133 return C->recent_alloc_obj();
2134 }
2135 return NULL;
2136 }
2137
2138
2139 void GraphKit::round_double_arguments(ciMethod* dest_method) {
2140 // (Note: TypeFunc::make has a cache that makes this fast.)
2141 const TypeFunc* tf = TypeFunc::make(dest_method);
2142 int nargs = tf->domain()->cnt() - TypeFunc::Parms;
2143 for (int j = 0; j < nargs; j++) {
2144 const Type *targ = tf->domain()->field_at(j + TypeFunc::Parms);
2145 if( targ->basic_type() == T_DOUBLE ) {
2146 // If any parameters are doubles, they must be rounded before
2147 // the call, dstore_rounding does gvn.transform
2148 Node *arg = argument(j);
2149 arg = dstore_rounding(arg);
2150 set_argument(j, arg);
2151 }
2152 }
2153 }
2154
2155 /**
2156 * Record profiling data exact_kls for Node n with the type system so
2157 * that it can propagate it (speculation)
2158 *
2159 * @param n node that the type applies to
2160 * @param exact_kls type from profiling
2161 * @param maybe_null did profiling see null?
2162 *
2163 * @return node with improved type
2164 */
2181 speculative = speculative->with_inline_depth(jvms()->depth());
2182 } else if (current_type->would_improve_ptr(ptr_kind)) {
2183 // Profiling report that null was never seen so we can change the
2184 // speculative type to non null ptr.
2185 if (ptr_kind == ProfileAlwaysNull) {
2186 speculative = TypePtr::NULL_PTR;
2187 } else {
2188 assert(ptr_kind == ProfileNeverNull, "nothing else is an improvement");
2189 const TypePtr* ptr = TypePtr::NOTNULL;
2190 if (speculative != NULL) {
2191 speculative = speculative->cast_to_ptr_type(ptr->ptr())->is_ptr();
2192 } else {
2193 speculative = ptr;
2194 }
2195 }
2196 }
2197
2198 if (speculative != current_type->speculative()) {
2199 // Build a type with a speculative type (what we think we know
2200 // about the type but will need a guard when we use it)
2201 const TypeOopPtr* spec_type = TypeOopPtr::make(TypePtr::BotPTR, Type::OffsetBot, TypeOopPtr::InstanceBot, speculative);
2202 // We're changing the type, we need a new CheckCast node to carry
2203 // the new type. The new type depends on the control: what
2204 // profiling tells us is only valid from here as far as we can
2205 // tell.
2206 Node* cast = new CheckCastPPNode(control(), n, current_type->remove_speculative()->join_speculative(spec_type));
2207 cast = _gvn.transform(cast);
2208 replace_in_map(n, cast);
2209 n = cast;
2210 }
2211
2212 return n;
2213 }
2214
2215 /**
2216 * Record profiling data from receiver profiling at an invoke with the
2217 * type system so that it can propagate it (speculation)
2218 *
2219 * @param n receiver node
2220 *
2221 * @return node with improved type
2246 }
2247 ptr_kind = (i == call->row_limit()) ? ProfileAlwaysNull : ProfileMaybeNull;
2248 }
2249 }
2250 }
2251 return record_profile_for_speculation(n, exact_kls, ptr_kind);
2252 }
2253
2254 /**
2255 * Record profiling data from argument profiling at an invoke with the
2256 * type system so that it can propagate it (speculation)
2257 *
2258 * @param dest_method target method for the call
2259 * @param bc what invoke bytecode is this?
2260 */
2261 void GraphKit::record_profiled_arguments_for_speculation(ciMethod* dest_method, Bytecodes::Code bc) {
2262 if (!UseTypeSpeculation) {
2263 return;
2264 }
2265 const TypeFunc* tf = TypeFunc::make(dest_method);
2266 int nargs = tf->domain()->cnt() - TypeFunc::Parms;
2267 int skip = Bytecodes::has_receiver(bc) ? 1 : 0;
2268 for (int j = skip, i = 0; j < nargs && i < TypeProfileArgsLimit; j++) {
2269 const Type *targ = tf->domain()->field_at(j + TypeFunc::Parms);
2270 if (targ->basic_type() == T_OBJECT || targ->basic_type() == T_ARRAY) {
2271 ProfilePtrKind ptr_kind = ProfileMaybeNull;
2272 ciKlass* better_type = NULL;
2273 if (method()->argument_profiled_type(bci(), i, better_type, ptr_kind)) {
2274 record_profile_for_speculation(argument(j), better_type, ptr_kind);
2275 }
2276 i++;
2277 }
2278 }
2279 }
2280
2281 /**
2282 * Record profiling data from parameter profiling at an invoke with
2283 * the type system so that it can propagate it (speculation)
2284 */
2285 void GraphKit::record_profiled_parameters_for_speculation() {
2286 if (!UseTypeSpeculation) {
2287 return;
2288 }
2289 for (int i = 0, j = 0; i < method()->arg_size() ; i++) {
2290 if (_gvn.type(local(i))->isa_oopptr()) {
2763 // The decision to inline or out-of-line this final check is platform
2764 // dependent, and is found in the AD file definition of PartialSubtypeCheck.
2765 Node* psc = gvn->transform(
2766 new PartialSubtypeCheckNode(*ctrl, subklass, superklass));
2767
2768 IfNode *iff4 = gen_subtype_check_compare(*ctrl, psc, gvn->zerocon(T_OBJECT), BoolTest::ne, PROB_FAIR, gvn, T_ADDRESS);
2769 r_not_subtype->init_req(2, gvn->transform(new IfTrueNode (iff4)));
2770 r_ok_subtype ->init_req(3, gvn->transform(new IfFalseNode(iff4)));
2771
2772 // Return false path; set default control to true path.
2773 *ctrl = gvn->transform(r_ok_subtype);
2774 return gvn->transform(r_not_subtype);
2775 }
2776
2777 // Profile-driven exact type check:
2778 Node* GraphKit::type_check_receiver(Node* receiver, ciKlass* klass,
2779 float prob,
2780 Node* *casted_receiver) {
2781 const TypeKlassPtr* tklass = TypeKlassPtr::make(klass);
2782 Node* recv_klass = load_object_klass(receiver);
2783 Node* want_klass = makecon(tklass);
2784 Node* cmp = _gvn.transform( new CmpPNode(recv_klass, want_klass) );
2785 Node* bol = _gvn.transform( new BoolNode(cmp, BoolTest::eq) );
2786 IfNode* iff = create_and_xform_if(control(), bol, prob, COUNT_UNKNOWN);
2787 set_control( _gvn.transform( new IfTrueNode (iff) ));
2788 Node* fail = _gvn.transform( new IfFalseNode(iff) );
2789
2790 const TypeOopPtr* recv_xtype = tklass->as_instance_type();
2791 assert(recv_xtype->klass_is_exact(), "");
2792
2793 // Subsume downstream occurrences of receiver with a cast to
2794 // recv_xtype, since now we know what the type will be.
2795 Node* cast = new CheckCastPPNode(control(), receiver, recv_xtype);
2796 (*casted_receiver) = _gvn.transform(cast);
2797 // (User must make the replace_in_map call.)
2798
2799 return fail;
2800 }
2801
2802 //------------------------------subtype_check_receiver-------------------------
2803 Node* GraphKit::subtype_check_receiver(Node* receiver, ciKlass* klass,
2804 Node** casted_receiver) {
2805 const TypeKlassPtr* tklass = TypeKlassPtr::make(klass);
2806 Node* recv_klass = load_object_klass(receiver);
2807 Node* want_klass = makecon(tklass);
2808
2809 Node* slow_ctl = gen_subtype_check(recv_klass, want_klass);
2810
2811 // Cast receiver after successful check
2812 const TypeOopPtr* recv_type = tklass->cast_to_exactness(false)->is_klassptr()->as_instance_type();
2813 Node* cast = new CheckCastPPNode(control(), receiver, recv_type);
2814 (*casted_receiver) = _gvn.transform(cast);
2815
2816 return slow_ctl;
2817 }
2818
2819 //------------------------------seems_never_null-------------------------------
2820 // Use null_seen information if it is available from the profile.
2821 // If we see an unexpected null at a type check we record it and force a
2952 // and the reflective instance-of call.
2953 Node* GraphKit::gen_instanceof(Node* obj, Node* superklass, bool safe_for_replace) {
2954 kill_dead_locals(); // Benefit all the uncommon traps
2955 assert( !stopped(), "dead parse path should be checked in callers" );
2956 assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()),
2957 "must check for not-null not-dead klass in callers");
2958
2959 // Make the merge point
2960 enum { _obj_path = 1, _fail_path, _null_path, PATH_LIMIT };
2961 RegionNode* region = new RegionNode(PATH_LIMIT);
2962 Node* phi = new PhiNode(region, TypeInt::BOOL);
2963 C->set_has_split_ifs(true); // Has chance for split-if optimization
2964
2965 ciProfileData* data = NULL;
2966 if (java_bc() == Bytecodes::_instanceof) { // Only for the bytecode
2967 data = method()->method_data()->bci_to_data(bci());
2968 }
2969 bool speculative_not_null = false;
2970 bool never_see_null = (ProfileDynamicTypes // aggressive use of profile
2971 && seems_never_null(obj, data, speculative_not_null));
2972
2973 // Null check; get casted pointer; set region slot 3
2974 Node* null_ctl = top();
2975 Node* not_null_obj = null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace, speculative_not_null);
2976
2977 // If not_null_obj is dead, only null-path is taken
2978 if (stopped()) { // Doing instance-of on a NULL?
2979 set_control(null_ctl);
2980 return intcon(0);
2981 }
2982 region->init_req(_null_path, null_ctl);
2983 phi ->init_req(_null_path, intcon(0)); // Set null path value
2984 if (null_ctl == top()) {
2985 // Do this eagerly, so that pattern matches like is_diamond_phi
2986 // will work even during parsing.
2987 assert(_null_path == PATH_LIMIT-1, "delete last");
2988 region->del_req(_null_path);
2989 phi ->del_req(_null_path);
2990 }
2991
2992 // Do we know the type check always succeed?
2993 bool known_statically = false;
2994 if (_gvn.type(superklass)->singleton()) {
2995 ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass();
2996 ciKlass* subk = _gvn.type(obj)->is_oopptr()->klass();
2997 if (subk != NULL && subk->is_loaded()) {
2998 int static_res = C->static_subtype_check(superk, subk);
2999 known_statically = (static_res == Compile::SSC_always_true || static_res == Compile::SSC_always_false);
3000 }
3001 }
3002
3003 if (!known_statically) {
3004 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
3005 // We may not have profiling here or it may not help us. If we
3006 // have a speculative type use it to perform an exact cast.
3007 ciKlass* spec_obj_type = obj_type->speculative_type();
3008 if (spec_obj_type != NULL || (ProfileDynamicTypes && data != NULL)) {
3009 Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, NULL, spec_obj_type, safe_for_replace);
3010 if (stopped()) { // Profile disagrees with this path.
3011 set_control(null_ctl); // Null is the only remaining possibility.
3012 return intcon(0);
3013 }
3014 if (cast_obj != NULL) {
3015 not_null_obj = cast_obj;
3016 }
3017 }
3018 }
3019
3020 // Load the object's klass
3021 Node* obj_klass = load_object_klass(not_null_obj);
3022
3023 // Generate the subtype check
3024 Node* not_subtype_ctrl = gen_subtype_check(obj_klass, superklass);
3025
3026 // Plug in the success path to the general merge in slot 1.
3027 region->init_req(_obj_path, control());
3028 phi ->init_req(_obj_path, intcon(1));
3029
3030 // Plug in the failing path to the general merge in slot 2.
3031 region->init_req(_fail_path, not_subtype_ctrl);
3032 phi ->init_req(_fail_path, intcon(0));
3033
3034 // Return final merged results
3035 set_control( _gvn.transform(region) );
3036 record_for_igvn(region);
3037
3038 // If we know the type check always succeeds then we don't use the
3039 // profiling data at this bytecode. Don't lose it, feed it to the
3040 // type system as a speculative type.
3041 if (safe_for_replace) {
3042 Node* casted_obj = record_profiled_receiver_for_speculation(obj);
3043 replace_in_map(obj, casted_obj);
3044 }
3045
3046 return _gvn.transform(phi);
3047 }
3048
3049 //-------------------------------gen_checkcast---------------------------------
3050 // Generate a checkcast idiom. Used by both the checkcast bytecode and the
3051 // array store bytecode. Stack must be as-if BEFORE doing the bytecode so the
3052 // uncommon-trap paths work. Adjust stack after this call.
3053 // If failure_control is supplied and not null, it is filled in with
3054 // the control edge for the cast failure. Otherwise, an appropriate
3055 // uncommon trap or exception is thrown.
3056 Node* GraphKit::gen_checkcast(Node *obj, Node* superklass,
3057 Node* *failure_control) {
3058 kill_dead_locals(); // Benefit all the uncommon traps
3059 const TypeKlassPtr *tk = _gvn.type(superklass)->is_klassptr();
3060 const Type *toop = TypeOopPtr::make_from_klass(tk->klass());
3061
3062 // Fast cutout: Check the case that the cast is vacuously true.
3063 // This detects the common cases where the test will short-circuit
3064 // away completely. We do this before we perform the null check,
3065 // because if the test is going to turn into zero code, we don't
3066 // want a residual null check left around. (Causes a slowdown,
3067 // for example, in some objArray manipulations, such as a[i]=a[j].)
3068 if (tk->singleton()) {
3069 const TypeOopPtr* objtp = _gvn.type(obj)->isa_oopptr();
3070 if (objtp != NULL && objtp->klass() != NULL) {
3071 switch (C->static_subtype_check(tk->klass(), objtp->klass())) {
3072 case Compile::SSC_always_true:
3073 // If we know the type check always succeed then we don't use
3074 // the profiling data at this bytecode. Don't lose it, feed it
3075 // to the type system as a speculative type.
3076 return record_profiled_receiver_for_speculation(obj);
3077 case Compile::SSC_always_false:
3078 // It needs a null check because a null will *pass* the cast check.
3079 // A non-null value will always produce an exception.
3080 return null_assert(obj);
3081 }
3082 }
3083 }
3084
3085 ciProfileData* data = NULL;
3086 bool safe_for_replace = false;
3087 if (failure_control == NULL) { // use MDO in regular case only
3088 assert(java_bc() == Bytecodes::_aastore ||
3089 java_bc() == Bytecodes::_checkcast,
3090 "interpreter profiles type checks only for these BCs");
3091 data = method()->method_data()->bci_to_data(bci());
3092 safe_for_replace = true;
3093 }
3094
3095 // Make the merge point
3096 enum { _obj_path = 1, _null_path, PATH_LIMIT };
3097 RegionNode* region = new RegionNode(PATH_LIMIT);
3098 Node* phi = new PhiNode(region, toop);
3099 C->set_has_split_ifs(true); // Has chance for split-if optimization
3100
3101 // Use null-cast information if it is available
3102 bool speculative_not_null = false;
3103 bool never_see_null = ((failure_control == NULL) // regular case only
3104 && seems_never_null(obj, data, speculative_not_null));
3105
3106 // Null check; get casted pointer; set region slot 3
3107 Node* null_ctl = top();
3108 Node* not_null_obj = null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace, speculative_not_null);
3109
3110 // If not_null_obj is dead, only null-path is taken
3111 if (stopped()) { // Doing instance-of on a NULL?
3112 set_control(null_ctl);
3113 return null();
3114 }
3115 region->init_req(_null_path, null_ctl);
3116 phi ->init_req(_null_path, null()); // Set null path value
3117 if (null_ctl == top()) {
3118 // Do this eagerly, so that pattern matches like is_diamond_phi
3119 // will work even during parsing.
3120 assert(_null_path == PATH_LIMIT-1, "delete last");
3121 region->del_req(_null_path);
3122 phi ->del_req(_null_path);
3123 }
3124
3125 Node* cast_obj = NULL;
3126 if (tk->klass_is_exact()) {
3127 // The following optimization tries to statically cast the speculative type of the object
3128 // (for example obtained during profiling) to the type of the superklass and then do a
3129 // dynamic check that the type of the object is what we expect. To work correctly
3130 // for checkcast and aastore the type of superklass should be exact.
3131 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
3132 // We may not have profiling here or it may not help us. If we have
3133 // a speculative type use it to perform an exact cast.
3134 ciKlass* spec_obj_type = obj_type->speculative_type();
3135 if (spec_obj_type != NULL || data != NULL) {
3136 cast_obj = maybe_cast_profiled_receiver(not_null_obj, tk->klass(), spec_obj_type, safe_for_replace);
3137 if (cast_obj != NULL) {
3138 if (failure_control != NULL) // failure is now impossible
3139 (*failure_control) = top();
3140 // adjust the type of the phi to the exact klass:
3141 phi->raise_bottom_type(_gvn.type(cast_obj)->meet_speculative(TypePtr::NULL_PTR));
3142 }
3143 }
3144 }
3145
3146 if (cast_obj == NULL) {
3147 // Load the object's klass
3148 Node* obj_klass = load_object_klass(not_null_obj);
3149
3150 // Generate the subtype check
3151 Node* not_subtype_ctrl = gen_subtype_check( obj_klass, superklass );
3152
3153 // Plug in success path into the merge
3154 cast_obj = _gvn.transform(new CheckCastPPNode(control(), not_null_obj, toop));
3155 // Failure path ends in uncommon trap (or may be dead - failure impossible)
3156 if (failure_control == NULL) {
3157 if (not_subtype_ctrl != top()) { // If failure is possible
3158 PreserveJVMState pjvms(this);
3159 set_control(not_subtype_ctrl);
3160 builtin_throw(Deoptimization::Reason_class_check, obj_klass);
3161 }
3162 } else {
3163 (*failure_control) = not_subtype_ctrl;
3164 }
3165 }
3166
3167 region->init_req(_obj_path, control());
3168 phi ->init_req(_obj_path, cast_obj);
3169
3170 // A merge of NULL or Casted-NotNull obj
3171 Node* res = _gvn.transform(phi);
3172
3173 // Note I do NOT always 'replace_in_map(obj,result)' here.
3174 // if( tk->klass()->can_be_primary_super() )
3175 // This means that if I successfully store an Object into an array-of-String
3176 // I 'forget' that the Object is really now known to be a String. I have to
3177 // do this because we don't have true union types for interfaces - if I store
3178 // a Baz into an array-of-Interface and then tell the optimizer it's an
3179 // Interface, I forget that it's also a Baz and cannot do Baz-like field
3180 // references to it. FIX THIS WHEN UNION TYPES APPEAR!
3181 // replace_in_map( obj, res );
3182
3183 // Return final merged results
3184 set_control( _gvn.transform(region) );
3185 record_for_igvn(region);
3186
3187 return record_profiled_receiver_for_speculation(res);
3188 }
3189
3190 //------------------------------next_monitor-----------------------------------
3191 // What number should be given to the next monitor?
3192 int GraphKit::next_monitor() {
3193 int current = jvms()->monitor_depth()* C->sync_stack_slots();
3194 int next = current + C->sync_stack_slots();
3195 // Keep the toplevel high water mark current:
3196 if (C->fixed_slots() < next) C->set_fixed_slots(next);
3197 return current;
3198 }
3199
3200 //------------------------------insert_mem_bar---------------------------------
3201 // Memory barrier to avoid floating things around
3202 // The membar serves as a pinch point between both control and all memory slices.
3203 Node* GraphKit::insert_mem_bar(int opcode, Node* precedent) {
3204 MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, precedent);
3205 mb->init_req(TypeFunc::Control, control());
3206 mb->init_req(TypeFunc::Memory, reset_memory());
3207 Node* membar = _gvn.transform(mb);
3208 set_control(_gvn.transform(new ProjNode(membar, TypeFunc::Control)));
3209 set_all_memory_call(membar);
3235 }
3236 Node* membar = _gvn.transform(mb);
3237 set_control(_gvn.transform(new ProjNode(membar, TypeFunc::Control)));
3238 if (alias_idx == Compile::AliasIdxBot) {
3239 merged_memory()->set_base_memory(_gvn.transform(new ProjNode(membar, TypeFunc::Memory)));
3240 } else {
3241 set_memory(_gvn.transform(new ProjNode(membar, TypeFunc::Memory)),alias_idx);
3242 }
3243 return membar;
3244 }
3245
3246 //------------------------------shared_lock------------------------------------
3247 // Emit locking code.
3248 FastLockNode* GraphKit::shared_lock(Node* obj) {
3249 // bci is either a monitorenter bc or InvocationEntryBci
3250 // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
3251 assert(SynchronizationEntryBCI == InvocationEntryBci, "");
3252
3253 if( !GenerateSynchronizationCode )
3254 return NULL; // Not locking things?
3255 if (stopped()) // Dead monitor?
3256 return NULL;
3257
3258 assert(dead_locals_are_killed(), "should kill locals before sync. point");
3259
3260 obj = access_resolve(obj, ACCESS_READ | ACCESS_WRITE);
3261
3262 // Box the stack location
3263 Node* box = _gvn.transform(new BoxLockNode(next_monitor()));
3264 Node* mem = reset_memory();
3265
3266 FastLockNode * flock = _gvn.transform(new FastLockNode(0, obj, box) )->as_FastLock();
3267 if (UseBiasedLocking && PrintPreciseBiasedLockingStatistics) {
3268 // Create the counters for this fast lock.
3269 flock->create_lock_counter(sync_jvms()); // sync_jvms used to get current bci
3270 }
3271
3272 // Create the rtm counters for this fast lock if needed.
3273 flock->create_rtm_lock_counter(sync_jvms()); // sync_jvms used to get current bci
3274
3309 }
3310 #endif
3311
3312 return flock;
3313 }
3314
3315
3316 //------------------------------shared_unlock----------------------------------
3317 // Emit unlocking code.
3318 void GraphKit::shared_unlock(Node* box, Node* obj) {
3319 // bci is either a monitorenter bc or InvocationEntryBci
3320 // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
3321 assert(SynchronizationEntryBCI == InvocationEntryBci, "");
3322
3323 if( !GenerateSynchronizationCode )
3324 return;
3325 if (stopped()) { // Dead monitor?
3326 map()->pop_monitor(); // Kill monitor from debug info
3327 return;
3328 }
3329
3330 // Memory barrier to avoid floating things down past the locked region
3331 insert_mem_bar(Op_MemBarReleaseLock);
3332
3333 const TypeFunc *tf = OptoRuntime::complete_monitor_exit_Type();
3334 UnlockNode *unlock = new UnlockNode(C, tf);
3335 #ifdef ASSERT
3336 unlock->set_dbg_jvms(sync_jvms());
3337 #endif
3338 uint raw_idx = Compile::AliasIdxRaw;
3339 unlock->init_req( TypeFunc::Control, control() );
3340 unlock->init_req( TypeFunc::Memory , memory(raw_idx) );
3341 unlock->init_req( TypeFunc::I_O , top() ) ; // does no i/o
3342 unlock->init_req( TypeFunc::FramePtr, frameptr() );
3343 unlock->init_req( TypeFunc::ReturnAdr, top() );
3344
3345 unlock->init_req(TypeFunc::Parms + 0, obj);
3346 unlock->init_req(TypeFunc::Parms + 1, box);
3347 unlock = _gvn.transform(unlock)->as_Unlock();
3348
3349 Node* mem = reset_memory();
3350
3351 // unlock has no side-effects, sets few values
3352 set_predefined_output_for_runtime_call(unlock, mem, TypeRawPtr::BOTTOM);
3353
3354 // Kill monitor from debug info
3355 map()->pop_monitor( );
3356 }
3357
3358 //-------------------------------get_layout_helper-----------------------------
3359 // If the given klass is a constant or known to be an array,
3360 // fetch the constant layout helper value into constant_value
3361 // and return (Node*)NULL. Otherwise, load the non-constant
3362 // layout helper value, and return the node which represents it.
3363 // This two-faced routine is useful because allocation sites
3364 // almost always feature constant types.
3365 Node* GraphKit::get_layout_helper(Node* klass_node, jint& constant_value) {
3366 const TypeKlassPtr* inst_klass = _gvn.type(klass_node)->isa_klassptr();
3367 if (!StressReflectiveCode && inst_klass != NULL) {
3368 ciKlass* klass = inst_klass->klass();
3369 bool xklass = inst_klass->klass_is_exact();
3370 if (xklass || klass->is_array_klass()) {
3371 jint lhelper = klass->layout_helper();
3372 if (lhelper != Klass::_lh_neutral_value) {
3373 constant_value = lhelper;
3374 return (Node*) NULL;
3375 }
3376 }
3377 }
3378 constant_value = Klass::_lh_neutral_value; // put in a known value
3379 Node* lhp = basic_plus_adr(klass_node, klass_node, in_bytes(Klass::layout_helper_offset()));
3380 return make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);
3381 }
3382
3383 // We just put in an allocate/initialize with a big raw-memory effect.
3384 // Hook selected additional alias categories on the initialization.
3385 static void hook_memory_on_init(GraphKit& kit, int alias_idx,
3386 MergeMemNode* init_in_merge,
3387 Node* init_out_raw) {
3388 DEBUG_ONLY(Node* init_in_raw = init_in_merge->base_memory());
3389 assert(init_in_merge->memory_at(alias_idx) == init_in_raw, "");
3390
3412
3413 // a normal slow-call doesn't change i_o, but an allocation does
3414 // we create a separate i_o projection for the normal control path
3415 set_i_o(_gvn.transform( new ProjNode(allocx, TypeFunc::I_O, false) ) );
3416 Node* rawoop = _gvn.transform( new ProjNode(allocx, TypeFunc::Parms) );
3417
3418 // put in an initialization barrier
3419 InitializeNode* init = insert_mem_bar_volatile(Op_Initialize, rawidx,
3420 rawoop)->as_Initialize();
3421 assert(alloc->initialization() == init, "2-way macro link must work");
3422 assert(init ->allocation() == alloc, "2-way macro link must work");
3423 {
3424 // Extract memory strands which may participate in the new object's
3425 // initialization, and source them from the new InitializeNode.
3426 // This will allow us to observe initializations when they occur,
3427 // and link them properly (as a group) to the InitializeNode.
3428 assert(init->in(InitializeNode::Memory) == malloc, "");
3429 MergeMemNode* minit_in = MergeMemNode::make(malloc);
3430 init->set_req(InitializeNode::Memory, minit_in);
3431 record_for_igvn(minit_in); // fold it up later, if possible
3432 Node* minit_out = memory(rawidx);
3433 assert(minit_out->is_Proj() && minit_out->in(0) == init, "");
3434 // Add an edge in the MergeMem for the header fields so an access
3435 // to one of those has correct memory state
3436 set_memory(minit_out, C->get_alias_index(oop_type->add_offset(oopDesc::mark_offset_in_bytes())));
3437 set_memory(minit_out, C->get_alias_index(oop_type->add_offset(oopDesc::klass_offset_in_bytes())));
3438 if (oop_type->isa_aryptr()) {
3439 const TypePtr* telemref = oop_type->add_offset(Type::OffsetBot);
3440 int elemidx = C->get_alias_index(telemref);
3441 hook_memory_on_init(*this, elemidx, minit_in, minit_out);
3442 } else if (oop_type->isa_instptr()) {
3443 ciInstanceKlass* ik = oop_type->klass()->as_instance_klass();
3444 for (int i = 0, len = ik->nof_nonstatic_fields(); i < len; i++) {
3445 ciField* field = ik->nonstatic_field_at(i);
3446 if (field->offset() >= TrackedInitializationLimit * HeapWordSize)
3447 continue; // do not bother to track really large numbers of fields
3448 // Find (or create) the alias category for this field:
3449 int fieldidx = C->alias_type(field)->index();
3450 hook_memory_on_init(*this, fieldidx, minit_in, minit_out);
3451 }
3452 }
3453 }
3454
3455 // Cast raw oop to the real thing...
3456 Node* javaoop = new CheckCastPPNode(control(), rawoop, oop_type);
3457 javaoop = _gvn.transform(javaoop);
3458 C->set_recent_alloc(control(), javaoop);
3459 assert(just_allocated_object(control()) == javaoop, "just allocated");
3460
3461 #ifdef ASSERT
3462 { // Verify that the AllocateNode::Ideal_allocation recognizers work:
3473 assert(alloc->in(AllocateNode::ALength)->is_top(), "no length, please");
3474 }
3475 }
3476 #endif //ASSERT
3477
3478 return javaoop;
3479 }
3480
3481 //---------------------------new_instance--------------------------------------
3482 // This routine takes a klass_node which may be constant (for a static type)
3483 // or may be non-constant (for reflective code). It will work equally well
3484 // for either, and the graph will fold nicely if the optimizer later reduces
3485 // the type to a constant.
3486 // The optional arguments are for specialized use by intrinsics:
3487 // - If 'extra_slow_test' if not null is an extra condition for the slow-path.
3488 // - If 'return_size_val', report the the total object size to the caller.
3489 // - deoptimize_on_exception controls how Java exceptions are handled (rethrow vs deoptimize)
3490 Node* GraphKit::new_instance(Node* klass_node,
3491 Node* extra_slow_test,
3492 Node* *return_size_val,
3493 bool deoptimize_on_exception) {
3494 // Compute size in doublewords
3495 // The size is always an integral number of doublewords, represented
3496 // as a positive bytewise size stored in the klass's layout_helper.
3497 // The layout_helper also encodes (in a low bit) the need for a slow path.
3498 jint layout_con = Klass::_lh_neutral_value;
3499 Node* layout_val = get_layout_helper(klass_node, layout_con);
3500 int layout_is_con = (layout_val == NULL);
3501
3502 if (extra_slow_test == NULL) extra_slow_test = intcon(0);
3503 // Generate the initial go-slow test. It's either ALWAYS (return a
3504 // Node for 1) or NEVER (return a NULL) or perhaps (in the reflective
3505 // case) a computed value derived from the layout_helper.
3506 Node* initial_slow_test = NULL;
3507 if (layout_is_con) {
3508 assert(!StressReflectiveCode, "stress mode does not use these paths");
3509 bool must_go_slow = Klass::layout_helper_needs_slow_path(layout_con);
3510 initial_slow_test = must_go_slow ? intcon(1) : extra_slow_test;
3511 } else { // reflective case
3512 // This reflective path is used by Unsafe.allocateInstance.
3513 // (It may be stress-tested by specifying StressReflectiveCode.)
3514 // Basically, we want to get into the VM is there's an illegal argument.
3515 Node* bit = intcon(Klass::_lh_instance_slow_path_bit);
3516 initial_slow_test = _gvn.transform( new AndINode(layout_val, bit) );
3517 if (extra_slow_test != intcon(0)) {
3518 initial_slow_test = _gvn.transform( new OrINode(initial_slow_test, extra_slow_test) );
3519 }
3520 // (Macro-expander will further convert this to a Bool, if necessary.)
3531
3532 // Clear the low bits to extract layout_helper_size_in_bytes:
3533 assert((int)Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit");
3534 Node* mask = MakeConX(~ (intptr_t)right_n_bits(LogBytesPerLong));
3535 size = _gvn.transform( new AndXNode(size, mask) );
3536 }
3537 if (return_size_val != NULL) {
3538 (*return_size_val) = size;
3539 }
3540
3541 // This is a precise notnull oop of the klass.
3542 // (Actually, it need not be precise if this is a reflective allocation.)
3543 // It's what we cast the result to.
3544 const TypeKlassPtr* tklass = _gvn.type(klass_node)->isa_klassptr();
3545 if (!tklass) tklass = TypeKlassPtr::OBJECT;
3546 const TypeOopPtr* oop_type = tklass->as_instance_type();
3547
3548 // Now generate allocation code
3549
3550 // The entire memory state is needed for slow path of the allocation
3551 // since GC and deoptimization can happened.
3552 Node *mem = reset_memory();
3553 set_all_memory(mem); // Create new memory state
3554
3555 AllocateNode* alloc = new AllocateNode(C, AllocateNode::alloc_type(Type::TOP),
3556 control(), mem, i_o(),
3557 size, klass_node,
3558 initial_slow_test);
3559
3560 return set_output_for_allocation(alloc, oop_type, deoptimize_on_exception);
3561 }
3562
3563 //-------------------------------new_array-------------------------------------
3564 // helper for both newarray and anewarray
3565 // The 'length' parameter is (obviously) the length of the array.
3566 // See comments on new_instance for the meaning of the other arguments.
3567 Node* GraphKit::new_array(Node* klass_node, // array klass (maybe variable)
3568 Node* length, // number of array elements
3569 int nargs, // number of arguments to push back for uncommon trap
3570 Node* *return_size_val,
3571 bool deoptimize_on_exception) {
3572 jint layout_con = Klass::_lh_neutral_value;
3573 Node* layout_val = get_layout_helper(klass_node, layout_con);
3574 int layout_is_con = (layout_val == NULL);
3575
3576 if (!layout_is_con && !StressReflectiveCode &&
3577 !too_many_traps(Deoptimization::Reason_class_check)) {
3578 // This is a reflective array creation site.
3579 // Optimistically assume that it is a subtype of Object[],
3580 // so that we can fold up all the address arithmetic.
3581 layout_con = Klass::array_layout_helper(T_OBJECT);
3582 Node* cmp_lh = _gvn.transform( new CmpINode(layout_val, intcon(layout_con)) );
3583 Node* bol_lh = _gvn.transform( new BoolNode(cmp_lh, BoolTest::eq) );
3584 { BuildCutout unless(this, bol_lh, PROB_MAX);
3585 inc_sp(nargs);
3586 uncommon_trap(Deoptimization::Reason_class_check,
3587 Deoptimization::Action_maybe_recompile);
3588 }
3589 layout_val = NULL;
3590 layout_is_con = true;
3591 }
3592
3593 // Generate the initial go-slow test. Make sure we do not overflow
3594 // if length is huge (near 2Gig) or negative! We do not need
3595 // exact double-words here, just a close approximation of needed
3596 // double-words. We can't add any offset or rounding bits, lest we
3597 // take a size -1 of bytes and make it positive. Use an unsigned
3598 // compare, so negative sizes look hugely positive.
3599 int fast_size_limit = FastAllocateSizeLimit;
3600 if (layout_is_con) {
3601 assert(!StressReflectiveCode, "stress mode does not use these paths");
3602 // Increase the size limit if we have exact knowledge of array type.
3603 int log2_esize = Klass::layout_helper_log2_element_size(layout_con);
3604 fast_size_limit <<= (LogBytesPerLong - log2_esize);
3605 }
3606
3607 Node* initial_slow_cmp = _gvn.transform( new CmpUNode( length, intcon( fast_size_limit ) ) );
3608 Node* initial_slow_test = _gvn.transform( new BoolNode( initial_slow_cmp, BoolTest::gt ) );
3609
3610 // --- Size Computation ---
3611 // array_size = round_to_heap(array_header + (length << elem_shift));
3612 // where round_to_heap(x) == align_to(x, MinObjAlignmentInBytes)
3613 // and align_to(x, y) == ((x + y-1) & ~(y-1))
3614 // The rounding mask is strength-reduced, if possible.
3615 int round_mask = MinObjAlignmentInBytes - 1;
3616 Node* header_size = NULL;
3617 int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
3618 // (T_BYTE has the weakest alignment and size restrictions...)
3619 if (layout_is_con) {
3620 int hsize = Klass::layout_helper_header_size(layout_con);
3621 int eshift = Klass::layout_helper_log2_element_size(layout_con);
3622 BasicType etype = Klass::layout_helper_element_type(layout_con);
3623 if ((round_mask & ~right_n_bits(eshift)) == 0)
3624 round_mask = 0; // strength-reduce it if it goes away completely
3625 assert((hsize & right_n_bits(eshift)) == 0, "hsize is pre-rounded");
3626 assert(header_size_min <= hsize, "generic minimum is smallest");
3627 header_size_min = hsize;
3628 header_size = intcon(hsize + round_mask);
3629 } else {
3630 Node* hss = intcon(Klass::_lh_header_size_shift);
3631 Node* hsm = intcon(Klass::_lh_header_size_mask);
3632 Node* hsize = _gvn.transform( new URShiftINode(layout_val, hss) );
3633 hsize = _gvn.transform( new AndINode(hsize, hsm) );
3634 Node* mask = intcon(round_mask);
3635 header_size = _gvn.transform( new AddINode(hsize, mask) );
3636 }
3637
3638 Node* elem_shift = NULL;
3639 if (layout_is_con) {
3640 int eshift = Klass::layout_helper_log2_element_size(layout_con);
3641 if (eshift != 0)
3642 elem_shift = intcon(eshift);
3643 } else {
3644 // There is no need to mask or shift this value.
3645 // The semantics of LShiftINode include an implicit mask to 0x1F.
3689 // places, one where the length is sharply limited, and the other
3690 // after a successful allocation.
3691 Node* abody = lengthx;
3692 if (elem_shift != NULL)
3693 abody = _gvn.transform( new LShiftXNode(lengthx, elem_shift) );
3694 Node* size = _gvn.transform( new AddXNode(headerx, abody) );
3695 if (round_mask != 0) {
3696 Node* mask = MakeConX(~round_mask);
3697 size = _gvn.transform( new AndXNode(size, mask) );
3698 }
3699 // else if round_mask == 0, the size computation is self-rounding
3700
3701 if (return_size_val != NULL) {
3702 // This is the size
3703 (*return_size_val) = size;
3704 }
3705
3706 // Now generate allocation code
3707
3708 // The entire memory state is needed for slow path of the allocation
3709 // since GC and deoptimization can happened.
3710 Node *mem = reset_memory();
3711 set_all_memory(mem); // Create new memory state
3712
3713 if (initial_slow_test->is_Bool()) {
3714 // Hide it behind a CMoveI, or else PhaseIdealLoop::split_up will get sick.
3715 initial_slow_test = initial_slow_test->as_Bool()->as_int_value(&_gvn);
3716 }
3717
3718 // Create the AllocateArrayNode and its result projections
3719 AllocateArrayNode* alloc
3720 = new AllocateArrayNode(C, AllocateArrayNode::alloc_type(TypeInt::INT),
3721 control(), mem, i_o(),
3722 size, klass_node,
3723 initial_slow_test,
3724 length);
3725
3726 // Cast to correct type. Note that the klass_node may be constant or not,
3727 // and in the latter case the actual array type will be inexact also.
3728 // (This happens via a non-constant argument to inline_native_newArray.)
3729 // In any case, the value of klass_node provides the desired array type.
3730 const TypeInt* length_type = _gvn.find_int_type(length);
3731 const TypeOopPtr* ary_type = _gvn.type(klass_node)->is_klassptr()->as_instance_type();
3732 if (ary_type->isa_aryptr() && length_type != NULL) {
3733 // Try to get a better type than POS for the size
3734 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
3735 }
3736
3737 Node* javaoop = set_output_for_allocation(alloc, ary_type, deoptimize_on_exception);
3738
3739 // Cast length on remaining path to be as narrow as possible
3740 if (map()->find_edge(length) >= 0) {
3741 Node* ccast = alloc->make_ideal_length(ary_type, &_gvn);
3742 if (ccast != length) {
3743 _gvn.set_type_bottom(ccast);
3744 record_for_igvn(ccast);
3745 replace_in_map(length, ccast);
3746 }
3747 }
3748
3749 return javaoop;
3750 }
3751
3866 set_all_memory(ideal.merged_memory());
3867 set_i_o(ideal.i_o());
3868 set_control(ideal.ctrl());
3869 }
3870
3871 void GraphKit::final_sync(IdealKit& ideal) {
3872 // Final sync IdealKit and graphKit.
3873 sync_kit(ideal);
3874 }
3875
3876 Node* GraphKit::load_String_length(Node* str, bool set_ctrl) {
3877 Node* len = load_array_length(load_String_value(str, set_ctrl));
3878 Node* coder = load_String_coder(str, set_ctrl);
3879 // Divide length by 2 if coder is UTF16
3880 return _gvn.transform(new RShiftINode(len, coder));
3881 }
3882
3883 Node* GraphKit::load_String_value(Node* str, bool set_ctrl) {
3884 int value_offset = java_lang_String::value_offset_in_bytes();
3885 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
3886 false, NULL, 0);
3887 const TypePtr* value_field_type = string_type->add_offset(value_offset);
3888 const TypeAryPtr* value_type = TypeAryPtr::make(TypePtr::NotNull,
3889 TypeAry::make(TypeInt::BYTE, TypeInt::POS),
3890 ciTypeArrayKlass::make(T_BYTE), true, 0);
3891 Node* p = basic_plus_adr(str, str, value_offset);
3892 Node* load = access_load_at(str, p, value_field_type, value_type, T_OBJECT,
3893 IN_HEAP | (set_ctrl ? C2_CONTROL_DEPENDENT_LOAD : 0) | MO_UNORDERED);
3894 return load;
3895 }
3896
3897 Node* GraphKit::load_String_coder(Node* str, bool set_ctrl) {
3898 if (!CompactStrings) {
3899 return intcon(java_lang_String::CODER_UTF16);
3900 }
3901 int coder_offset = java_lang_String::coder_offset_in_bytes();
3902 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
3903 false, NULL, 0);
3904 const TypePtr* coder_field_type = string_type->add_offset(coder_offset);
3905
3906 Node* p = basic_plus_adr(str, str, coder_offset);
3907 Node* load = access_load_at(str, p, coder_field_type, TypeInt::BYTE, T_BYTE,
3908 IN_HEAP | (set_ctrl ? C2_CONTROL_DEPENDENT_LOAD : 0) | MO_UNORDERED);
3909 return load;
3910 }
3911
3912 void GraphKit::store_String_value(Node* str, Node* value) {
3913 int value_offset = java_lang_String::value_offset_in_bytes();
3914 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
3915 false, NULL, 0);
3916 const TypePtr* value_field_type = string_type->add_offset(value_offset);
3917
3918 access_store_at(str, basic_plus_adr(str, value_offset), value_field_type,
3919 value, TypeAryPtr::BYTES, T_OBJECT, IN_HEAP | MO_UNORDERED);
3920 }
3921
3922 void GraphKit::store_String_coder(Node* str, Node* value) {
3923 int coder_offset = java_lang_String::coder_offset_in_bytes();
3924 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
3925 false, NULL, 0);
3926 const TypePtr* coder_field_type = string_type->add_offset(coder_offset);
3927
3928 access_store_at(str, basic_plus_adr(str, coder_offset), coder_field_type,
3929 value, TypeInt::BYTE, T_BYTE, IN_HEAP | MO_UNORDERED);
3930 }
3931
3932 // Capture src and dst memory state with a MergeMemNode
3933 Node* GraphKit::capture_memory(const TypePtr* src_type, const TypePtr* dst_type) {
3934 if (src_type == dst_type) {
3935 // Types are equal, we don't need a MergeMemNode
3936 return memory(src_type);
3937 }
3938 MergeMemNode* merge = MergeMemNode::make(map()->memory());
3939 record_for_igvn(merge); // fold it up later, if possible
3940 int src_idx = C->get_alias_index(src_type);
3941 int dst_idx = C->get_alias_index(dst_type);
3942 merge->set_memory_at(src_idx, memory(src_idx));
3943 merge->set_memory_at(dst_idx, memory(dst_idx));
3944 return merge;
3945 }
4018 i_char->init_req(2, AddI(i_char, intcon(2)));
4019
4020 set_control(IfFalse(iff));
4021 set_memory(st, TypeAryPtr::BYTES);
4022 }
4023
4024 Node* GraphKit::make_constant_from_field(ciField* field, Node* obj) {
4025 if (!field->is_constant()) {
4026 return NULL; // Field not marked as constant.
4027 }
4028 ciInstance* holder = NULL;
4029 if (!field->is_static()) {
4030 ciObject* const_oop = obj->bottom_type()->is_oopptr()->const_oop();
4031 if (const_oop != NULL && const_oop->is_instance()) {
4032 holder = const_oop->as_instance();
4033 }
4034 }
4035 const Type* con_type = Type::make_constant_from_field(field, holder, field->layout_type(),
4036 /*is_unsigned_load=*/false);
4037 if (con_type != NULL) {
4038 return makecon(con_type);
4039 }
4040 return NULL;
4041 }
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "ci/ciUtilities.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "ci/ciValueKlass.hpp"
29 #include "gc/shared/barrierSet.hpp"
30 #include "gc/shared/c2/barrierSetC2.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "opto/addnode.hpp"
34 #include "opto/castnode.hpp"
35 #include "opto/convertnode.hpp"
36 #include "opto/graphKit.hpp"
37 #include "opto/idealKit.hpp"
38 #include "opto/intrinsicnode.hpp"
39 #include "opto/locknode.hpp"
40 #include "opto/machnode.hpp"
41 #include "opto/narrowptrnode.hpp"
42 #include "opto/opaquenode.hpp"
43 #include "opto/parse.hpp"
44 #include "opto/rootnode.hpp"
45 #include "opto/runtime.hpp"
46 #include "opto/valuetypenode.hpp"
47 #include "runtime/deoptimization.hpp"
48 #include "runtime/sharedRuntime.hpp"
49
50 //----------------------------GraphKit-----------------------------------------
51 // Main utility constructor.
52 GraphKit::GraphKit(JVMState* jvms, PhaseGVN* gvn)
53 : Phase(Phase::Parser),
54 _env(C->env()),
55 _gvn((gvn != NULL) ? *gvn : *C->initial_gvn()),
56 _barrier_set(BarrierSet::barrier_set()->barrier_set_c2())
57 {
58 assert(gvn == NULL || !gvn->is_IterGVN() || gvn->is_IterGVN()->delay_transform(), "delay transform should be enabled");
59 _exceptions = jvms->map()->next_exception();
60 if (_exceptions != NULL) jvms->map()->set_next_exception(NULL);
61 set_jvms(jvms);
62 #ifdef ASSERT
63 if (_gvn.is_IterGVN() != NULL) {
64 assert(_gvn.is_IterGVN()->delay_transform(), "Transformation must be delayed if IterGVN is used");
65 // Save the initial size of _for_igvn worklist for verification (see ~GraphKit)
66 _worklist_size = _gvn.C->for_igvn()->size();
67 }
68 #endif
69 }
70
71 // Private constructor for parser.
72 GraphKit::GraphKit()
73 : Phase(Phase::Parser),
74 _env(C->env()),
75 _gvn(*C->initial_gvn()),
76 _barrier_set(BarrierSet::barrier_set()->barrier_set_c2())
77 {
78 _exceptions = NULL;
79 set_map(NULL);
80 debug_only(_sp = -99);
81 debug_only(set_bci(-99));
82 }
83
84
85
86 //---------------------------clean_stack---------------------------------------
87 // Clear away rubbish from the stack area of the JVM state.
88 // This destroys any arguments that may be waiting on the stack.
817 tty->print_cr("Zombie local %d: ", local);
818 jvms->dump();
819 }
820 return false;
821 }
822 }
823 }
824 return true;
825 }
826
827 #endif //ASSERT
828
829 // Helper function for enforcing certain bytecodes to reexecute if
830 // deoptimization happens
831 static bool should_reexecute_implied_by_bytecode(JVMState *jvms, bool is_anewarray) {
832 ciMethod* cur_method = jvms->method();
833 int cur_bci = jvms->bci();
834 if (cur_method != NULL && cur_bci != InvocationEntryBci) {
835 Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);
836 return Interpreter::bytecode_should_reexecute(code) ||
837 (is_anewarray && (code == Bytecodes::_multianewarray));
838 // Reexecute _multianewarray bytecode which was replaced with
839 // sequence of [a]newarray. See Parse::do_multianewarray().
840 //
841 // Note: interpreter should not have it set since this optimization
842 // is limited by dimensions and guarded by flag so in some cases
843 // multianewarray() runtime calls will be generated and
844 // the bytecode should not be reexecutes (stack will not be reset).
845 } else {
846 return false;
847 }
848 }
849
850 // Helper function for adding JVMState and debug information to node
851 void GraphKit::add_safepoint_edges(SafePointNode* call, bool must_throw) {
852 // Add the safepoint edges to the call (or other safepoint).
853
854 // Make sure dead locals are set to top. This
855 // should help register allocation time and cut down on the size
856 // of the deoptimization information.
857 assert(dead_locals_are_killed(), "garbage in debug info before safepoint");
858
859 // Walk the inline list to fill in the correct set of JVMState's
860 // Also fill in the associated edges for each JVMState.
861
862 // If the bytecode needs to be reexecuted we need to put
863 // the arguments back on the stack.
864 const bool should_reexecute = jvms()->should_reexecute();
865 JVMState* youngest_jvms = should_reexecute ? sync_jvms_for_reexecute() : sync_jvms();
866
867 // NOTE: set_bci (called from sync_jvms) might reset the reexecute bit to
1071 ciSignature* declared_signature = NULL;
1072 ciMethod* ignored_callee = method()->get_method_at_bci(bci(), ignored_will_link, &declared_signature);
1073 assert(declared_signature != NULL, "cannot be null");
1074 inputs = declared_signature->arg_size_for_bc(code);
1075 int size = declared_signature->return_type()->size();
1076 depth = size - inputs;
1077 }
1078 break;
1079
1080 case Bytecodes::_multianewarray:
1081 {
1082 ciBytecodeStream iter(method());
1083 iter.reset_to_bci(bci());
1084 iter.next();
1085 inputs = iter.get_dimensions();
1086 assert(rsize == 1, "");
1087 depth = rsize - inputs;
1088 }
1089 break;
1090
1091 case Bytecodes::_withfield: {
1092 bool ignored_will_link;
1093 ciField* field = method()->get_field_at_bci(bci(), ignored_will_link);
1094 int size = field->type()->size();
1095 inputs = size+1;
1096 depth = rsize - inputs;
1097 break;
1098 }
1099
1100 case Bytecodes::_ireturn:
1101 case Bytecodes::_lreturn:
1102 case Bytecodes::_freturn:
1103 case Bytecodes::_dreturn:
1104 case Bytecodes::_areturn:
1105 assert(rsize == -depth, "");
1106 inputs = rsize;
1107 break;
1108
1109 case Bytecodes::_jsr:
1110 case Bytecodes::_jsr_w:
1111 inputs = 0;
1112 depth = 1; // S.B. depth=1, not zero
1113 break;
1114
1115 default:
1116 // bytecode produces a typed result
1117 inputs = rsize - depth;
1118 assert(inputs >= 0, "");
1119 break;
1205 // the incoming address with NULL casted away. You are allowed to use the
1206 // not-null value only if you are control dependent on the test.
1207 #ifndef PRODUCT
1208 extern int explicit_null_checks_inserted,
1209 explicit_null_checks_elided;
1210 #endif
1211 Node* GraphKit::null_check_common(Node* value, BasicType type,
1212 // optional arguments for variations:
1213 bool assert_null,
1214 Node* *null_control,
1215 bool speculative) {
1216 assert(!assert_null || null_control == NULL, "not both at once");
1217 if (stopped()) return top();
1218 NOT_PRODUCT(explicit_null_checks_inserted++);
1219
1220 // Construct NULL check
1221 Node *chk = NULL;
1222 switch(type) {
1223 case T_LONG : chk = new CmpLNode(value, _gvn.zerocon(T_LONG)); break;
1224 case T_INT : chk = new CmpINode(value, _gvn.intcon(0)); break;
1225 case T_VALUETYPE : // fall through
1226 case T_ARRAY : // fall through
1227 type = T_OBJECT; // simplify further tests
1228 case T_OBJECT : {
1229 const Type *t = _gvn.type( value );
1230
1231 const TypeOopPtr* tp = t->isa_oopptr();
1232 if (tp != NULL && tp->klass() != NULL && !tp->klass()->is_loaded()
1233 // Only for do_null_check, not any of its siblings:
1234 && !assert_null && null_control == NULL) {
1235 // Usually, any field access or invocation on an unloaded oop type
1236 // will simply fail to link, since the statically linked class is
1237 // likely also to be unloaded. However, in -Xcomp mode, sometimes
1238 // the static class is loaded but the sharper oop type is not.
1239 // Rather than checking for this obscure case in lots of places,
1240 // we simply observe that a null check on an unloaded class
1241 // will always be followed by a nonsense operation, so we
1242 // can just issue the uncommon trap here.
1243 // Our access to the unloaded class will only be correct
1244 // after it has been loaded and initialized, which requires
1245 // a trip through the interpreter.
1377 }
1378
1379 if (assert_null) {
1380 // Cast obj to null on this path.
1381 replace_in_map(value, zerocon(type));
1382 return zerocon(type);
1383 }
1384
1385 // Cast obj to not-null on this path, if there is no null_control.
1386 // (If there is a null_control, a non-null value may come back to haunt us.)
1387 if (type == T_OBJECT) {
1388 Node* cast = cast_not_null(value, false);
1389 if (null_control == NULL || (*null_control) == top())
1390 replace_in_map(value, cast);
1391 value = cast;
1392 }
1393
1394 return value;
1395 }
1396
1397 Node* GraphKit::null2default(Node* value, ciValueKlass* vk) {
1398 Node* null_ctl = top();
1399 value = null_check_oop(value, &null_ctl);
1400 if (!null_ctl->is_top()) {
1401 // Return default value if oop is null
1402 Node* region = new RegionNode(3);
1403 region->init_req(1, control());
1404 region->init_req(2, null_ctl);
1405 value = PhiNode::make(region, value, TypeInstPtr::make(TypePtr::BotPTR, vk));
1406 value->set_req(2, ValueTypeNode::default_oop(gvn(), vk));
1407 set_control(gvn().transform(region));
1408 value = gvn().transform(value);
1409 }
1410 return value;
1411 }
1412
1413 //------------------------------cast_not_null----------------------------------
1414 // Cast obj to not-null on this path
1415 Node* GraphKit::cast_not_null(Node* obj, bool do_replace_in_map) {
1416 if (obj->is_ValueType()) {
1417 return obj;
1418 }
1419 const Type *t = _gvn.type(obj);
1420 const Type *t_not_null = t->join_speculative(TypePtr::NOTNULL);
1421 // Object is already not-null?
1422 if( t == t_not_null ) return obj;
1423
1424 Node *cast = new CastPPNode(obj,t_not_null);
1425 cast->init_req(0, control());
1426 cast = _gvn.transform( cast );
1427
1428 // Scan for instances of 'obj' in the current JVM mapping.
1429 // These instances are known to be not-null after the test.
1430 if (do_replace_in_map)
1431 replace_in_map(obj, cast);
1432
1433 return cast; // Return casted value
1434 }
1435
1436 // Sometimes in intrinsics, we implicitly know an object is not null
1437 // (there's no actual null check) so we can cast it to not null. In
1438 // the course of optimizations, the input to the cast can become null.
1527 int adr_idx,
1528 MemNode::MemOrd mo,
1529 LoadNode::ControlDependency control_dependency,
1530 bool require_atomic_access,
1531 bool unaligned,
1532 bool mismatched,
1533 bool unsafe) {
1534 assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );
1535 const TypePtr* adr_type = NULL; // debug-mode-only argument
1536 debug_only(adr_type = C->get_adr_type(adr_idx));
1537 Node* mem = memory(adr_idx);
1538 Node* ld;
1539 if (require_atomic_access && bt == T_LONG) {
1540 ld = LoadLNode::make_atomic(ctl, mem, adr, adr_type, t, mo, control_dependency, unaligned, mismatched, unsafe);
1541 } else if (require_atomic_access && bt == T_DOUBLE) {
1542 ld = LoadDNode::make_atomic(ctl, mem, adr, adr_type, t, mo, control_dependency, unaligned, mismatched, unsafe);
1543 } else {
1544 ld = LoadNode::make(_gvn, ctl, mem, adr, adr_type, t, bt, mo, control_dependency, unaligned, mismatched, unsafe);
1545 }
1546 ld = _gvn.transform(ld);
1547
1548 if (((bt == T_OBJECT || bt == T_VALUETYPE) && C->do_escape_analysis()) || C->eliminate_boxing()) {
1549 // Improve graph before escape analysis and boxing elimination.
1550 record_for_igvn(ld);
1551 }
1552 return ld;
1553 }
1554
1555 Node* GraphKit::store_to_memory(Node* ctl, Node* adr, Node *val, BasicType bt,
1556 int adr_idx,
1557 MemNode::MemOrd mo,
1558 bool require_atomic_access,
1559 bool unaligned,
1560 bool mismatched,
1561 bool unsafe) {
1562 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
1563 const TypePtr* adr_type = NULL;
1564 debug_only(adr_type = C->get_adr_type(adr_idx));
1565 Node *mem = memory(adr_idx);
1566 Node* st;
1567 if (require_atomic_access && bt == T_LONG) {
1568 st = StoreLNode::make_atomic(ctl, mem, adr, adr_type, val, mo);
1579 }
1580 if (unsafe) {
1581 st->as_Store()->set_unsafe_access();
1582 }
1583 st = _gvn.transform(st);
1584 set_memory(st, adr_idx);
1585 // Back-to-back stores can only remove intermediate store with DU info
1586 // so push on worklist for optimizer.
1587 if (mem->req() > MemNode::Address && adr == mem->in(MemNode::Address))
1588 record_for_igvn(st);
1589
1590 return st;
1591 }
1592
1593 Node* GraphKit::access_store_at(Node* obj,
1594 Node* adr,
1595 const TypePtr* adr_type,
1596 Node* val,
1597 const Type* val_type,
1598 BasicType bt,
1599 DecoratorSet decorators,
1600 bool deoptimize_on_exception) {
1601 // Transformation of a value which could be NULL pointer (CastPP #NULL)
1602 // could be delayed during Parse (for example, in adjust_map_after_if()).
1603 // Execute transformation here to avoid barrier generation in such case.
1604 if (_gvn.type(val) == TypePtr::NULL_PTR) {
1605 val = _gvn.makecon(TypePtr::NULL_PTR);
1606 }
1607
1608 if (stopped()) {
1609 return top(); // Dead path ?
1610 }
1611
1612 assert(val != NULL, "not dead path");
1613 if (val->is_ValueType()) {
1614 // Allocate value type and get oop
1615 val = val->as_ValueType()->allocate(this, deoptimize_on_exception)->get_oop();
1616 }
1617
1618 C2AccessValuePtr addr(adr, adr_type);
1619 C2AccessValue value(val, val_type);
1620 C2ParseAccess access(this, decorators | C2_WRITE_ACCESS, bt, obj, addr);
1621 if (access.is_raw()) {
1622 return _barrier_set->BarrierSetC2::store_at(access, value);
1623 } else {
1624 return _barrier_set->store_at(access, value);
1625 }
1626 }
1627
1628 Node* GraphKit::access_load_at(Node* obj, // containing obj
1629 Node* adr, // actual adress to store val at
1630 const TypePtr* adr_type,
1631 const Type* val_type,
1632 BasicType bt,
1633 DecoratorSet decorators) {
1634 if (stopped()) {
1635 return top(); // Dead path ?
1636 }
1717 }
1718 }
1719
1720 Node* GraphKit::access_atomic_add_at(Node* obj,
1721 Node* adr,
1722 const TypePtr* adr_type,
1723 int alias_idx,
1724 Node* new_val,
1725 const Type* value_type,
1726 BasicType bt,
1727 DecoratorSet decorators) {
1728 C2AccessValuePtr addr(adr, adr_type);
1729 C2AtomicParseAccess access(this, decorators | C2_READ_ACCESS | C2_WRITE_ACCESS, bt, obj, addr, alias_idx);
1730 if (access.is_raw()) {
1731 return _barrier_set->BarrierSetC2::atomic_add_at(access, new_val, value_type);
1732 } else {
1733 return _barrier_set->atomic_add_at(access, new_val, value_type);
1734 }
1735 }
1736
1737 void GraphKit::access_clone(Node* src_base, Node* dst_base, Node* countx, bool is_array) {
1738 return _barrier_set->clone(this, src_base, dst_base, countx, is_array);
1739 }
1740
1741 Node* GraphKit::access_resolve(Node* n, DecoratorSet decorators) {
1742 // Use stronger ACCESS_WRITE|ACCESS_READ by default.
1743 if ((decorators & (ACCESS_READ | ACCESS_WRITE)) == 0) {
1744 decorators |= ACCESS_READ | ACCESS_WRITE;
1745 }
1746 return _barrier_set->resolve(this, n, decorators);
1747 }
1748
1749 //-------------------------array_element_address-------------------------
1750 Node* GraphKit::array_element_address(Node* ary, Node* idx, BasicType elembt,
1751 const TypeInt* sizetype, Node* ctrl) {
1752 uint shift = exact_log2(type2aelembytes(elembt));
1753 ciKlass* arytype_klass = _gvn.type(ary)->is_aryptr()->klass();
1754 if (arytype_klass->is_value_array_klass()) {
1755 ciValueArrayKlass* vak = arytype_klass->as_value_array_klass();
1756 shift = vak->log2_element_size();
1757 }
1758 uint header = arrayOopDesc::base_offset_in_bytes(elembt);
1759
1760 // short-circuit a common case (saves lots of confusing waste motion)
1761 jint idx_con = find_int_con(idx, -1);
1762 if (idx_con >= 0) {
1763 intptr_t offset = header + ((intptr_t)idx_con << shift);
1764 return basic_plus_adr(ary, offset);
1765 }
1766
1767 // must be correct type for alignment purposes
1768 Node* base = basic_plus_adr(ary, header);
1769 idx = Compile::conv_I2X_index(&_gvn, idx, sizetype, ctrl);
1770 Node* scale = _gvn.transform( new LShiftXNode(idx, intcon(shift)) );
1771 return basic_plus_adr(ary, base, scale);
1772 }
1773
1774 //-------------------------load_array_element-------------------------
1775 Node* GraphKit::load_array_element(Node* ctl, Node* ary, Node* idx, const TypeAryPtr* arytype) {
1776 const Type* elemtype = arytype->elem();
1777 BasicType elembt = elemtype->array_element_basic_type();
1778 assert(elembt != T_VALUETYPE, "value types are not supported by this method");
1779 Node* adr = array_element_address(ary, idx, elembt, arytype->size());
1780 if (elembt == T_NARROWOOP) {
1781 elembt = T_OBJECT; // To satisfy switch in LoadNode::make()
1782 }
1783 Node* ld = make_load(ctl, adr, elemtype, elembt, arytype, MemNode::unordered);
1784 return ld;
1785 }
1786
1787 //-------------------------set_arguments_for_java_call-------------------------
1788 // Arguments (pre-popped from the stack) are taken from the JVMS.
1789 void GraphKit::set_arguments_for_java_call(CallJavaNode* call, bool incremental_inlining) {
1790 // Add the call arguments:
1791 const TypeTuple* domain = call->tf()->domain_sig();
1792 ExtendedSignature sig_cc = ExtendedSignature(call->method()->get_sig_cc(), SigEntryFilter());
1793 uint nargs = domain->cnt();
1794 for (uint i = TypeFunc::Parms, idx = TypeFunc::Parms; i < nargs; i++) {
1795 Node* arg = argument(i-TypeFunc::Parms);
1796 const Type* t = domain->field_at(i);
1797 if (call->method()->has_scalarized_args() && t->is_valuetypeptr() && !t->maybe_null()) {
1798 // We don't pass value type arguments by reference but instead
1799 // pass each field of the value type
1800 ValueTypeNode* vt = arg->isa_ValueType();
1801 if (vt == NULL) {
1802 // TODO why is that?? Shouldn't we always see a valuetype node here?
1803 vt = ValueTypeNode::make_from_oop(this, arg, t->value_klass());
1804 }
1805 vt->pass_fields(this, call, sig_cc, idx);
1806 // If a value type argument is passed as fields, attach the Method* to the call site
1807 // to be able to access the extended signature later via attached_method_before_pc().
1808 // For example, see CompiledMethod::preserve_callee_argument_oops().
1809 call->set_override_symbolic_info(true);
1810 continue;
1811 } else if (arg->is_ValueType()) {
1812 // Pass value type argument via oop to callee
1813 if (!incremental_inlining) {
1814 arg = arg->as_ValueType()->allocate(this)->get_oop();
1815 } else {
1816 arg = ValueTypePtrNode::make_from_value_type(this, arg->as_ValueType(), false);
1817 }
1818 }
1819 call->init_req(idx++, arg);
1820 // Skip reserved arguments
1821 BasicType bt = t->basic_type();
1822 while (SigEntry::next_is_reserved(sig_cc, bt, true)) {
1823 call->init_req(idx++, top());
1824 if (type2size[bt] == 2) {
1825 call->init_req(idx++, top());
1826 }
1827 }
1828 }
1829 }
1830
1831 //---------------------------set_edges_for_java_call---------------------------
1832 // Connect a newly created call into the current JVMS.
1833 // A return value node (if any) is returned from set_edges_for_java_call.
1834 void GraphKit::set_edges_for_java_call(CallJavaNode* call, bool must_throw, bool separate_io_proj) {
1835
1836 // Add the predefined inputs:
1837 call->init_req( TypeFunc::Control, control() );
1838 call->init_req( TypeFunc::I_O , i_o() );
1839 call->init_req( TypeFunc::Memory , reset_memory() );
1840 call->init_req( TypeFunc::FramePtr, frameptr() );
1841 call->init_req( TypeFunc::ReturnAdr, top() );
1842
1843 add_safepoint_edges(call, must_throw);
1844
1845 Node* xcall = _gvn.transform(call);
1846
1847 if (xcall == top()) {
1848 set_control(top());
1849 return;
1850 }
1851 assert(xcall == call, "call identity is stable");
1852
1853 // Re-use the current map to produce the result.
1854
1855 set_control(_gvn.transform(new ProjNode(call, TypeFunc::Control)));
1856 set_i_o( _gvn.transform(new ProjNode(call, TypeFunc::I_O , separate_io_proj)));
1857 set_all_memory_call(xcall, separate_io_proj);
1858
1859 //return xcall; // no need, caller already has it
1860 }
1861
1862 Node* GraphKit::set_results_for_java_call(CallJavaNode* call, bool separate_io_proj, bool deoptimize) {
1863 if (stopped()) return top(); // maybe the call folded up?
1864
1865 // Note: Since any out-of-line call can produce an exception,
1866 // we always insert an I_O projection from the call into the result.
1867
1868 make_slow_call_ex(call, env()->Throwable_klass(), separate_io_proj, deoptimize);
1869
1870 if (separate_io_proj) {
1871 // The caller requested separate projections be used by the fall
1872 // through and exceptional paths, so replace the projections for
1873 // the fall through path.
1874 set_i_o(_gvn.transform( new ProjNode(call, TypeFunc::I_O) ));
1875 set_all_memory(_gvn.transform( new ProjNode(call, TypeFunc::Memory) ));
1876 }
1877
1878 // Capture the return value, if any.
1879 Node* ret;
1880 if (call->method() == NULL || call->method()->return_type()->basic_type() == T_VOID) {
1881 ret = top();
1882 } else if (call->tf()->returns_value_type_as_fields()) {
1883 // Return of multiple values (value type fields): we create a
1884 // ValueType node, each field is a projection from the call.
1885 ciValueKlass* vk = call->method()->return_type()->as_value_klass();
1886 const Array<SigEntry>* sig_array = vk->extended_sig();
1887 GrowableArray<SigEntry> sig = GrowableArray<SigEntry>(sig_array->length());
1888 sig.appendAll(sig_array);
1889 ExtendedSignature sig_cc = ExtendedSignature(&sig, SigEntryFilter());
1890 uint base_input = TypeFunc::Parms + 1;
1891 ret = ValueTypeNode::make_from_multi(this, call, sig_cc, vk, base_input, false);
1892 } else {
1893 ret = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
1894 }
1895
1896 return ret;
1897 }
1898
1899 //--------------------set_predefined_input_for_runtime_call--------------------
1900 // Reading and setting the memory state is way conservative here.
1901 // The real problem is that I am not doing real Type analysis on memory,
1902 // so I cannot distinguish card mark stores from other stores. Across a GC
1903 // point the Store Barrier and the card mark memory has to agree. I cannot
1904 // have a card mark store and its barrier split across the GC point from
1905 // either above or below. Here I get that to happen by reading ALL of memory.
1906 // A better answer would be to separate out card marks from other memory.
1907 // For now, return the input memory state, so that it can be reused
1908 // after the call, if this call has restricted memory effects.
1909 Node* GraphKit::set_predefined_input_for_runtime_call(SafePointNode* call, Node* narrow_mem) {
1910 // Set fixed predefined input arguments
1911 Node* memory = reset_memory();
1912 Node* m = narrow_mem == NULL ? memory : narrow_mem;
1913 call->init_req( TypeFunc::Control, control() );
1914 call->init_req( TypeFunc::I_O, top() ); // does no i/o
1915 call->init_req( TypeFunc::Memory, m ); // may gc ptrs
1954 // This is not a "slow path" call; all memory comes from the call.
1955 set_all_memory_call(call);
1956 }
1957 }
1958
1959
1960 // Replace the call with the current state of the kit.
1961 void GraphKit::replace_call(CallNode* call, Node* result, bool do_replaced_nodes) {
1962 JVMState* ejvms = NULL;
1963 if (has_exceptions()) {
1964 ejvms = transfer_exceptions_into_jvms();
1965 }
1966
1967 ReplacedNodes replaced_nodes = map()->replaced_nodes();
1968 ReplacedNodes replaced_nodes_exception;
1969 Node* ex_ctl = top();
1970
1971 SafePointNode* final_state = stop();
1972
1973 // Find all the needed outputs of this call
1974 CallProjections* callprojs = call->extract_projections(true);
1975
1976 Node* init_mem = call->in(TypeFunc::Memory);
1977 Node* final_mem = final_state->in(TypeFunc::Memory);
1978 Node* final_ctl = final_state->in(TypeFunc::Control);
1979 Node* final_io = final_state->in(TypeFunc::I_O);
1980
1981 // Replace all the old call edges with the edges from the inlining result
1982 if (callprojs->fallthrough_catchproj != NULL) {
1983 C->gvn_replace_by(callprojs->fallthrough_catchproj, final_ctl);
1984 }
1985 if (callprojs->fallthrough_memproj != NULL) {
1986 if (final_mem->is_MergeMem()) {
1987 // Parser's exits MergeMem was not transformed but may be optimized
1988 final_mem = _gvn.transform(final_mem);
1989 }
1990 C->gvn_replace_by(callprojs->fallthrough_memproj, final_mem);
1991 }
1992 if (callprojs->fallthrough_ioproj != NULL) {
1993 C->gvn_replace_by(callprojs->fallthrough_ioproj, final_io);
1994 }
1995
1996 // Replace the result with the new result if it exists and is used
1997 if (callprojs->resproj[0] != NULL && result != NULL) {
1998 assert(callprojs->nb_resproj == 1, "unexpected number of results");
1999 C->gvn_replace_by(callprojs->resproj[0], result);
2000 }
2001
2002 if (ejvms == NULL) {
2003 // No exception edges to simply kill off those paths
2004 if (callprojs->catchall_catchproj != NULL) {
2005 C->gvn_replace_by(callprojs->catchall_catchproj, C->top());
2006 }
2007 if (callprojs->catchall_memproj != NULL) {
2008 C->gvn_replace_by(callprojs->catchall_memproj, C->top());
2009 }
2010 if (callprojs->catchall_ioproj != NULL) {
2011 C->gvn_replace_by(callprojs->catchall_ioproj, C->top());
2012 }
2013 // Replace the old exception object with top
2014 if (callprojs->exobj != NULL) {
2015 C->gvn_replace_by(callprojs->exobj, C->top());
2016 }
2017 } else {
2018 GraphKit ekit(ejvms);
2019
2020 // Load my combined exception state into the kit, with all phis transformed:
2021 SafePointNode* ex_map = ekit.combine_and_pop_all_exception_states();
2022 replaced_nodes_exception = ex_map->replaced_nodes();
2023
2024 Node* ex_oop = ekit.use_exception_state(ex_map);
2025
2026 if (callprojs->catchall_catchproj != NULL) {
2027 C->gvn_replace_by(callprojs->catchall_catchproj, ekit.control());
2028 ex_ctl = ekit.control();
2029 }
2030 if (callprojs->catchall_memproj != NULL) {
2031 C->gvn_replace_by(callprojs->catchall_memproj, ekit.reset_memory());
2032 }
2033 if (callprojs->catchall_ioproj != NULL) {
2034 C->gvn_replace_by(callprojs->catchall_ioproj, ekit.i_o());
2035 }
2036
2037 // Replace the old exception object with the newly created one
2038 if (callprojs->exobj != NULL) {
2039 C->gvn_replace_by(callprojs->exobj, ex_oop);
2040 }
2041 }
2042
2043 // Disconnect the call from the graph
2044 call->disconnect_inputs(NULL, C);
2045 C->gvn_replace_by(call, C->top());
2046
2047 // Clean up any MergeMems that feed other MergeMems since the
2048 // optimizer doesn't like that.
2049 if (final_mem->is_MergeMem()) {
2050 Node_List wl;
2051 for (SimpleDUIterator i(final_mem); i.has_next(); i.next()) {
2052 Node* m = i.get();
2053 if (m->is_MergeMem() && !wl.contains(m)) {
2054 wl.push(m);
2055 }
2056 }
2057 while (wl.size() > 0) {
2058 _gvn.transform(wl.pop());
2059 }
2060 }
2061
2062 if (callprojs->fallthrough_catchproj != NULL && !final_ctl->is_top() && do_replaced_nodes) {
2063 replaced_nodes.apply(C, final_ctl);
2064 }
2065 if (!ex_ctl->is_top() && do_replaced_nodes) {
2066 replaced_nodes_exception.apply(C, ex_ctl);
2067 }
2068 }
2069
2070
2071 //------------------------------increment_counter------------------------------
2072 // for statistics: increment a VM counter by 1
2073
2074 void GraphKit::increment_counter(address counter_addr) {
2075 Node* adr1 = makecon(TypeRawPtr::make(counter_addr));
2076 increment_counter(adr1);
2077 }
2078
2079 void GraphKit::increment_counter(Node* counter_addr) {
2080 int adr_type = Compile::AliasIdxRaw;
2081 Node* ctrl = control();
2082 Node* cnt = make_load(ctrl, counter_addr, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
2219 // it does not require card marks.
2220 Node* GraphKit::just_allocated_object(Node* current_control) {
2221 Node* ctrl = current_control;
2222 // Object::<init> is invoked after allocation, most of invoke nodes
2223 // will be reduced, but a region node is kept in parse time, we check
2224 // the pattern and skip the region node if it degraded to a copy.
2225 if (ctrl != NULL && ctrl->is_Region() && ctrl->req() == 2 &&
2226 ctrl->as_Region()->is_copy()) {
2227 ctrl = ctrl->as_Region()->is_copy();
2228 }
2229 if (C->recent_alloc_ctl() == ctrl) {
2230 return C->recent_alloc_obj();
2231 }
2232 return NULL;
2233 }
2234
2235
2236 void GraphKit::round_double_arguments(ciMethod* dest_method) {
2237 // (Note: TypeFunc::make has a cache that makes this fast.)
2238 const TypeFunc* tf = TypeFunc::make(dest_method);
2239 int nargs = tf->domain_sig()->cnt() - TypeFunc::Parms;
2240 for (int j = 0; j < nargs; j++) {
2241 const Type *targ = tf->domain_sig()->field_at(j + TypeFunc::Parms);
2242 if( targ->basic_type() == T_DOUBLE ) {
2243 // If any parameters are doubles, they must be rounded before
2244 // the call, dstore_rounding does gvn.transform
2245 Node *arg = argument(j);
2246 arg = dstore_rounding(arg);
2247 set_argument(j, arg);
2248 }
2249 }
2250 }
2251
2252 /**
2253 * Record profiling data exact_kls for Node n with the type system so
2254 * that it can propagate it (speculation)
2255 *
2256 * @param n node that the type applies to
2257 * @param exact_kls type from profiling
2258 * @param maybe_null did profiling see null?
2259 *
2260 * @return node with improved type
2261 */
2278 speculative = speculative->with_inline_depth(jvms()->depth());
2279 } else if (current_type->would_improve_ptr(ptr_kind)) {
2280 // Profiling report that null was never seen so we can change the
2281 // speculative type to non null ptr.
2282 if (ptr_kind == ProfileAlwaysNull) {
2283 speculative = TypePtr::NULL_PTR;
2284 } else {
2285 assert(ptr_kind == ProfileNeverNull, "nothing else is an improvement");
2286 const TypePtr* ptr = TypePtr::NOTNULL;
2287 if (speculative != NULL) {
2288 speculative = speculative->cast_to_ptr_type(ptr->ptr())->is_ptr();
2289 } else {
2290 speculative = ptr;
2291 }
2292 }
2293 }
2294
2295 if (speculative != current_type->speculative()) {
2296 // Build a type with a speculative type (what we think we know
2297 // about the type but will need a guard when we use it)
2298 const TypeOopPtr* spec_type = TypeOopPtr::make(TypePtr::BotPTR, Type::Offset::bottom, TypeOopPtr::InstanceBot, speculative);
2299 // We're changing the type, we need a new CheckCast node to carry
2300 // the new type. The new type depends on the control: what
2301 // profiling tells us is only valid from here as far as we can
2302 // tell.
2303 Node* cast = new CheckCastPPNode(control(), n, current_type->remove_speculative()->join_speculative(spec_type));
2304 cast = _gvn.transform(cast);
2305 replace_in_map(n, cast);
2306 n = cast;
2307 }
2308
2309 return n;
2310 }
2311
2312 /**
2313 * Record profiling data from receiver profiling at an invoke with the
2314 * type system so that it can propagate it (speculation)
2315 *
2316 * @param n receiver node
2317 *
2318 * @return node with improved type
2343 }
2344 ptr_kind = (i == call->row_limit()) ? ProfileAlwaysNull : ProfileMaybeNull;
2345 }
2346 }
2347 }
2348 return record_profile_for_speculation(n, exact_kls, ptr_kind);
2349 }
2350
2351 /**
2352 * Record profiling data from argument profiling at an invoke with the
2353 * type system so that it can propagate it (speculation)
2354 *
2355 * @param dest_method target method for the call
2356 * @param bc what invoke bytecode is this?
2357 */
2358 void GraphKit::record_profiled_arguments_for_speculation(ciMethod* dest_method, Bytecodes::Code bc) {
2359 if (!UseTypeSpeculation) {
2360 return;
2361 }
2362 const TypeFunc* tf = TypeFunc::make(dest_method);
2363 int nargs = tf->domain_sig()->cnt() - TypeFunc::Parms;
2364 int skip = Bytecodes::has_receiver(bc) ? 1 : 0;
2365 for (int j = skip, i = 0; j < nargs && i < TypeProfileArgsLimit; j++) {
2366 const Type *targ = tf->domain_sig()->field_at(j + TypeFunc::Parms);
2367 if (targ->isa_oopptr()) {
2368 ProfilePtrKind ptr_kind = ProfileMaybeNull;
2369 ciKlass* better_type = NULL;
2370 if (method()->argument_profiled_type(bci(), i, better_type, ptr_kind)) {
2371 record_profile_for_speculation(argument(j), better_type, ptr_kind);
2372 }
2373 i++;
2374 }
2375 }
2376 }
2377
2378 /**
2379 * Record profiling data from parameter profiling at an invoke with
2380 * the type system so that it can propagate it (speculation)
2381 */
2382 void GraphKit::record_profiled_parameters_for_speculation() {
2383 if (!UseTypeSpeculation) {
2384 return;
2385 }
2386 for (int i = 0, j = 0; i < method()->arg_size() ; i++) {
2387 if (_gvn.type(local(i))->isa_oopptr()) {
2860 // The decision to inline or out-of-line this final check is platform
2861 // dependent, and is found in the AD file definition of PartialSubtypeCheck.
2862 Node* psc = gvn->transform(
2863 new PartialSubtypeCheckNode(*ctrl, subklass, superklass));
2864
2865 IfNode *iff4 = gen_subtype_check_compare(*ctrl, psc, gvn->zerocon(T_OBJECT), BoolTest::ne, PROB_FAIR, gvn, T_ADDRESS);
2866 r_not_subtype->init_req(2, gvn->transform(new IfTrueNode (iff4)));
2867 r_ok_subtype ->init_req(3, gvn->transform(new IfFalseNode(iff4)));
2868
2869 // Return false path; set default control to true path.
2870 *ctrl = gvn->transform(r_ok_subtype);
2871 return gvn->transform(r_not_subtype);
2872 }
2873
2874 // Profile-driven exact type check:
2875 Node* GraphKit::type_check_receiver(Node* receiver, ciKlass* klass,
2876 float prob,
2877 Node* *casted_receiver) {
2878 const TypeKlassPtr* tklass = TypeKlassPtr::make(klass);
2879 Node* recv_klass = load_object_klass(receiver);
2880 Node* fail = type_check(recv_klass, tklass, prob);
2881 const TypeOopPtr* recv_xtype = tklass->as_instance_type();
2882 assert(recv_xtype->klass_is_exact(), "");
2883
2884 // Subsume downstream occurrences of receiver with a cast to
2885 // recv_xtype, since now we know what the type will be.
2886 Node* cast = new CheckCastPPNode(control(), receiver, recv_xtype);
2887 Node* res = _gvn.transform(cast);
2888 if (recv_xtype->is_valuetypeptr() && recv_xtype->value_klass()->is_scalarizable()) {
2889 assert(!gvn().type(res)->maybe_null(), "receiver should never be null");
2890 res = ValueTypeNode::make_from_oop(this, res, recv_xtype->value_klass());
2891 }
2892
2893 (*casted_receiver) = res;
2894 // (User must make the replace_in_map call.)
2895
2896 return fail;
2897 }
2898
2899 Node* GraphKit::type_check(Node* recv_klass, const TypeKlassPtr* tklass,
2900 float prob) {
2901 Node* want_klass = makecon(tklass);
2902 Node* cmp = _gvn.transform( new CmpPNode(recv_klass, want_klass));
2903 Node* bol = _gvn.transform( new BoolNode(cmp, BoolTest::eq) );
2904 IfNode* iff = create_and_xform_if(control(), bol, prob, COUNT_UNKNOWN);
2905 set_control( _gvn.transform( new IfTrueNode (iff)));
2906 Node* fail = _gvn.transform( new IfFalseNode(iff));
2907 return fail;
2908 }
2909
2910 //------------------------------subtype_check_receiver-------------------------
2911 Node* GraphKit::subtype_check_receiver(Node* receiver, ciKlass* klass,
2912 Node** casted_receiver) {
2913 const TypeKlassPtr* tklass = TypeKlassPtr::make(klass);
2914 Node* recv_klass = load_object_klass(receiver);
2915 Node* want_klass = makecon(tklass);
2916
2917 Node* slow_ctl = gen_subtype_check(recv_klass, want_klass);
2918
2919 // Cast receiver after successful check
2920 const TypeOopPtr* recv_type = tklass->cast_to_exactness(false)->is_klassptr()->as_instance_type();
2921 Node* cast = new CheckCastPPNode(control(), receiver, recv_type);
2922 (*casted_receiver) = _gvn.transform(cast);
2923
2924 return slow_ctl;
2925 }
2926
2927 //------------------------------seems_never_null-------------------------------
2928 // Use null_seen information if it is available from the profile.
2929 // If we see an unexpected null at a type check we record it and force a
3060 // and the reflective instance-of call.
3061 Node* GraphKit::gen_instanceof(Node* obj, Node* superklass, bool safe_for_replace) {
3062 kill_dead_locals(); // Benefit all the uncommon traps
3063 assert( !stopped(), "dead parse path should be checked in callers" );
3064 assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()),
3065 "must check for not-null not-dead klass in callers");
3066
3067 // Make the merge point
3068 enum { _obj_path = 1, _fail_path, _null_path, PATH_LIMIT };
3069 RegionNode* region = new RegionNode(PATH_LIMIT);
3070 Node* phi = new PhiNode(region, TypeInt::BOOL);
3071 C->set_has_split_ifs(true); // Has chance for split-if optimization
3072
3073 ciProfileData* data = NULL;
3074 if (java_bc() == Bytecodes::_instanceof) { // Only for the bytecode
3075 data = method()->method_data()->bci_to_data(bci());
3076 }
3077 bool speculative_not_null = false;
3078 bool never_see_null = (ProfileDynamicTypes // aggressive use of profile
3079 && seems_never_null(obj, data, speculative_not_null));
3080 bool is_value = obj->is_ValueType();
3081
3082 // Null check; get casted pointer; set region slot 3
3083 Node* null_ctl = top();
3084 Node* not_null_obj = is_value ? obj : null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace, speculative_not_null);
3085
3086 // If not_null_obj is dead, only null-path is taken
3087 if (stopped()) { // Doing instance-of on a NULL?
3088 set_control(null_ctl);
3089 return intcon(0);
3090 }
3091 region->init_req(_null_path, null_ctl);
3092 phi ->init_req(_null_path, intcon(0)); // Set null path value
3093 if (null_ctl == top()) {
3094 // Do this eagerly, so that pattern matches like is_diamond_phi
3095 // will work even during parsing.
3096 assert(_null_path == PATH_LIMIT-1, "delete last");
3097 region->del_req(_null_path);
3098 phi ->del_req(_null_path);
3099 }
3100
3101 // Do we know the type check always succeed?
3102 if (!is_value) {
3103 bool known_statically = false;
3104 if (_gvn.type(superklass)->singleton()) {
3105 ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass();
3106 ciKlass* subk = _gvn.type(obj)->is_oopptr()->klass();
3107 if (subk != NULL && subk->is_loaded()) {
3108 int static_res = C->static_subtype_check(superk, subk);
3109 known_statically = (static_res == Compile::SSC_always_true || static_res == Compile::SSC_always_false);
3110 }
3111 }
3112
3113 if (!known_statically) {
3114 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
3115 // We may not have profiling here or it may not help us. If we
3116 // have a speculative type use it to perform an exact cast.
3117 ciKlass* spec_obj_type = obj_type->speculative_type();
3118 if (spec_obj_type != NULL || (ProfileDynamicTypes && data != NULL)) {
3119 Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, NULL, spec_obj_type, safe_for_replace);
3120 if (stopped()) { // Profile disagrees with this path.
3121 set_control(null_ctl); // Null is the only remaining possibility.
3122 return intcon(0);
3123 }
3124 if (cast_obj != NULL &&
3125 // A value that's sometimes null is not something we can optimize well
3126 !(cast_obj->is_ValueType() && null_ctl != top())) {
3127 not_null_obj = cast_obj;
3128 is_value = not_null_obj->is_ValueType();
3129 }
3130 }
3131 }
3132 }
3133
3134 // Load the object's klass
3135 Node* obj_klass = NULL;
3136 if (is_value) {
3137 obj_klass = makecon(TypeKlassPtr::make(_gvn.type(not_null_obj)->is_valuetype()->value_klass()));
3138 } else {
3139 obj_klass = load_object_klass(not_null_obj);
3140 }
3141
3142 // Generate the subtype check
3143 Node* not_subtype_ctrl = gen_subtype_check(obj_klass, superklass);
3144
3145 // Plug in the success path to the general merge in slot 1.
3146 region->init_req(_obj_path, control());
3147 phi ->init_req(_obj_path, intcon(1));
3148
3149 // Plug in the failing path to the general merge in slot 2.
3150 region->init_req(_fail_path, not_subtype_ctrl);
3151 phi ->init_req(_fail_path, intcon(0));
3152
3153 // Return final merged results
3154 set_control( _gvn.transform(region) );
3155 record_for_igvn(region);
3156
3157 // If we know the type check always succeeds then we don't use the
3158 // profiling data at this bytecode. Don't lose it, feed it to the
3159 // type system as a speculative type.
3160 if (safe_for_replace && !is_value) {
3161 Node* casted_obj = record_profiled_receiver_for_speculation(obj);
3162 replace_in_map(obj, casted_obj);
3163 }
3164
3165 return _gvn.transform(phi);
3166 }
3167
3168 //-------------------------------gen_checkcast---------------------------------
3169 // Generate a checkcast idiom. Used by both the checkcast bytecode and the
3170 // array store bytecode. Stack must be as-if BEFORE doing the bytecode so the
3171 // uncommon-trap paths work. Adjust stack after this call.
3172 // If failure_control is supplied and not null, it is filled in with
3173 // the control edge for the cast failure. Otherwise, an appropriate
3174 // uncommon trap or exception is thrown.
3175 Node* GraphKit::gen_checkcast(Node *obj, Node* superklass, Node* *failure_control, bool never_null) {
3176 kill_dead_locals(); // Benefit all the uncommon traps
3177 const TypeKlassPtr* tk = _gvn.type(superklass)->is_klassptr();
3178 const TypeOopPtr* toop = TypeOopPtr::make_from_klass(tk->klass());
3179 assert(!never_null || toop->is_valuetypeptr(), "must be a value type pointer");
3180 bool is_value = obj->is_ValueType();
3181
3182 // Fast cutout: Check the case that the cast is vacuously true.
3183 // This detects the common cases where the test will short-circuit
3184 // away completely. We do this before we perform the null check,
3185 // because if the test is going to turn into zero code, we don't
3186 // want a residual null check left around. (Causes a slowdown,
3187 // for example, in some objArray manipulations, such as a[i]=a[j].)
3188 if (tk->singleton()) {
3189 ciKlass* klass = NULL;
3190 if (is_value) {
3191 klass = _gvn.type(obj)->is_valuetype()->value_klass();
3192 } else {
3193 const TypeOopPtr* objtp = _gvn.type(obj)->isa_oopptr();
3194 if (objtp != NULL) {
3195 klass = objtp->klass();
3196 }
3197 }
3198 if (klass != NULL) {
3199 switch (C->static_subtype_check(tk->klass(), klass)) {
3200 case Compile::SSC_always_true:
3201 // If we know the type check always succeed then we don't use
3202 // the profiling data at this bytecode. Don't lose it, feed it
3203 // to the type system as a speculative type.
3204 if (!is_value) {
3205 obj = record_profiled_receiver_for_speculation(obj);
3206 if (never_null) {
3207 obj = null_check(obj);
3208 }
3209 if (toop->is_valuetypeptr() && toop->value_klass()->is_scalarizable() && !gvn().type(obj)->maybe_null()) {
3210 obj = ValueTypeNode::make_from_oop(this, obj, toop->value_klass());
3211 }
3212 }
3213 return obj;
3214 case Compile::SSC_always_false:
3215 if (is_value || never_null) {
3216 if (!is_value) {
3217 null_check(obj);
3218 }
3219 // Value type is never null. Always throw an exception.
3220 builtin_throw(Deoptimization::Reason_class_check, makecon(TypeKlassPtr::make(klass)));
3221 return top();
3222 } else {
3223 // It needs a null check because a null will *pass* the cast check.
3224 return null_assert(obj);
3225 }
3226 }
3227 }
3228 }
3229
3230 ciProfileData* data = NULL;
3231 bool safe_for_replace = false;
3232 if (failure_control == NULL) { // use MDO in regular case only
3233 assert(java_bc() == Bytecodes::_aastore ||
3234 java_bc() == Bytecodes::_checkcast,
3235 "interpreter profiles type checks only for these BCs");
3236 data = method()->method_data()->bci_to_data(bci());
3237 safe_for_replace = true;
3238 }
3239
3240 // Make the merge point
3241 enum { _obj_path = 1, _null_path, PATH_LIMIT };
3242 RegionNode* region = new RegionNode(PATH_LIMIT);
3243 Node* phi = new PhiNode(region, toop);
3244 C->set_has_split_ifs(true); // Has chance for split-if optimization
3245
3246 // Use null-cast information if it is available
3247 bool speculative_not_null = false;
3248 bool never_see_null = ((failure_control == NULL) // regular case only
3249 && seems_never_null(obj, data, speculative_not_null));
3250
3251 // Null check; get casted pointer; set region slot 3
3252 Node* null_ctl = top();
3253 Node* not_null_obj = NULL;
3254 if (is_value) {
3255 not_null_obj = obj;
3256 } else if (never_null) {
3257 not_null_obj = null_check(obj);
3258 } else {
3259 not_null_obj = null_check_oop(obj, &null_ctl, never_see_null, safe_for_replace, speculative_not_null);
3260 }
3261
3262 // If not_null_obj is dead, only null-path is taken
3263 if (stopped()) { // Doing instance-of on a NULL?
3264 set_control(null_ctl);
3265 return null();
3266 }
3267 region->init_req(_null_path, null_ctl);
3268 phi ->init_req(_null_path, null()); // Set null path value
3269 if (null_ctl == top()) {
3270 // Do this eagerly, so that pattern matches like is_diamond_phi
3271 // will work even during parsing.
3272 assert(_null_path == PATH_LIMIT-1, "delete last");
3273 region->del_req(_null_path);
3274 phi ->del_req(_null_path);
3275 }
3276
3277 Node* cast_obj = NULL;
3278 if (!is_value && tk->klass_is_exact()) {
3279 // The following optimization tries to statically cast the speculative type of the object
3280 // (for example obtained during profiling) to the type of the superklass and then do a
3281 // dynamic check that the type of the object is what we expect. To work correctly
3282 // for checkcast and aastore the type of superklass should be exact.
3283 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
3284 // We may not have profiling here or it may not help us. If we have
3285 // a speculative type use it to perform an exact cast.
3286 ciKlass* spec_obj_type = obj_type->speculative_type();
3287 if (spec_obj_type != NULL || data != NULL) {
3288 cast_obj = maybe_cast_profiled_receiver(not_null_obj, tk->klass(), spec_obj_type, safe_for_replace);
3289 if (cast_obj != NULL && cast_obj->is_ValueType()) {
3290 if (null_ctl != top()) {
3291 cast_obj = NULL; // A value that's sometimes null is not something we can optimize well
3292 } else {
3293 return cast_obj;
3294 }
3295 }
3296 if (cast_obj != NULL) {
3297 if (failure_control != NULL) // failure is now impossible
3298 (*failure_control) = top();
3299 // adjust the type of the phi to the exact klass:
3300 phi->raise_bottom_type(_gvn.type(cast_obj)->meet_speculative(TypePtr::NULL_PTR));
3301 }
3302 }
3303 }
3304
3305 if (cast_obj == NULL) {
3306 // Load the object's klass
3307 Node* obj_klass = NULL;
3308 if (is_value) {
3309 obj_klass = makecon(TypeKlassPtr::make(_gvn.type(not_null_obj)->is_valuetype()->value_klass()));
3310 } else {
3311 obj_klass = load_object_klass(not_null_obj);
3312 }
3313
3314 // Generate the subtype check
3315 Node* not_subtype_ctrl = gen_subtype_check( obj_klass, superklass );
3316
3317 // Plug in success path into the merge
3318 cast_obj = is_value ? not_null_obj : _gvn.transform(new CheckCastPPNode(control(), not_null_obj, toop));
3319 // Failure path ends in uncommon trap (or may be dead - failure impossible)
3320 if (failure_control == NULL) {
3321 if (not_subtype_ctrl != top()) { // If failure is possible
3322 PreserveJVMState pjvms(this);
3323 set_control(not_subtype_ctrl);
3324 builtin_throw(Deoptimization::Reason_class_check, obj_klass);
3325 }
3326 } else {
3327 (*failure_control) = not_subtype_ctrl;
3328 }
3329 }
3330
3331 region->init_req(_obj_path, control());
3332 phi ->init_req(_obj_path, cast_obj);
3333
3334 // A merge of NULL or Casted-NotNull obj
3335 Node* res = _gvn.transform(phi);
3336
3337 // Note I do NOT always 'replace_in_map(obj,result)' here.
3338 // if( tk->klass()->can_be_primary_super() )
3339 // This means that if I successfully store an Object into an array-of-String
3340 // I 'forget' that the Object is really now known to be a String. I have to
3341 // do this because we don't have true union types for interfaces - if I store
3342 // a Baz into an array-of-Interface and then tell the optimizer it's an
3343 // Interface, I forget that it's also a Baz and cannot do Baz-like field
3344 // references to it. FIX THIS WHEN UNION TYPES APPEAR!
3345 // replace_in_map( obj, res );
3346
3347 // Return final merged results
3348 set_control( _gvn.transform(region) );
3349 record_for_igvn(region);
3350
3351 if (!is_value) {
3352 res = record_profiled_receiver_for_speculation(res);
3353 if (toop->is_valuetypeptr() && toop->value_klass()->is_scalarizable() && !gvn().type(res)->maybe_null()) {
3354 res = ValueTypeNode::make_from_oop(this, res, toop->value_klass());
3355 }
3356 }
3357 return res;
3358 }
3359
3360 Node* GraphKit::is_always_locked(Node* obj) {
3361 Node* mark_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
3362 Node* mark = make_load(NULL, mark_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
3363 Node* value_mask = _gvn.MakeConX(markOopDesc::always_locked_pattern);
3364 return _gvn.transform(new AndXNode(mark, value_mask));
3365 }
3366
3367 Node* GraphKit::gen_value_type_test(Node* kls) {
3368 Node* flags_addr = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
3369 Node* flags = make_load(NULL, flags_addr, TypeInt::INT, T_INT, MemNode::unordered);
3370 Node* is_value = _gvn.transform(new AndINode(flags, intcon(JVM_ACC_VALUE)));
3371 Node* cmp = _gvn.transform(new CmpINode(is_value, intcon(0)));
3372 return cmp;
3373 }
3374
3375 // Deoptimize if 'obj' is a value type
3376 void GraphKit::gen_value_type_guard(Node* obj, int nargs) {
3377 assert(EnableValhalla, "should only be used if value types are enabled");
3378 Node* bol = NULL;
3379 if (obj->is_ValueTypeBase()) {
3380 bol = intcon(0);
3381 } else {
3382 Node* is_value = is_always_locked(obj);
3383 Node* value_mask = _gvn.MakeConX(markOopDesc::always_locked_pattern);
3384 Node* cmp = _gvn.transform(new CmpXNode(is_value, value_mask));
3385 bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3386 }
3387 { BuildCutout unless(this, bol, PROB_MAX);
3388 inc_sp(nargs);
3389 uncommon_trap(Deoptimization::Reason_class_check,
3390 Deoptimization::Action_none);
3391 }
3392 }
3393
3394 // Deoptimize if 'ary' is flattened or if 'obj' is null and 'ary' is a value type array
3395 void GraphKit::gen_value_type_array_guard(Node* ary, Node* obj, int nargs) {
3396 assert(EnableValhalla, "should only be used if value types are enabled");
3397 // Load array element klass
3398 Node* kls = load_object_klass(ary);
3399 Node* k_adr = basic_plus_adr(kls, in_bytes(ArrayKlass::element_klass_offset()));
3400 Node* elem_klass = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), k_adr, TypeInstPtr::KLASS));
3401 // Check if element is a value type
3402 Node* flags_addr = basic_plus_adr(elem_klass, in_bytes(Klass::access_flags_offset()));
3403 Node* flags = make_load(NULL, flags_addr, TypeInt::INT, T_INT, MemNode::unordered);
3404 Node* is_value_elem = _gvn.transform(new AndINode(flags, intcon(JVM_ACC_VALUE)));
3405
3406 const Type* objtype = _gvn.type(obj);
3407 if (objtype == TypePtr::NULL_PTR) {
3408 // Object is always null, check if array is a value type array
3409 Node* cmp = _gvn.transform(new CmpINode(is_value_elem, intcon(0)));
3410 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
3411 { BuildCutout unless(this, bol, PROB_MAX);
3412 // TODO just deoptimize for now if we store null to a value type array
3413 inc_sp(nargs);
3414 uncommon_trap(Deoptimization::Reason_array_check,
3415 Deoptimization::Action_none);
3416 }
3417 } else {
3418 // Check if (is_value_elem && obj_is_null) <=> (!is_value_elem | !obj_is_null == 0)
3419 // TODO what if we later figure out that obj is never null?
3420 Node* not_value = _gvn.transform(new XorINode(is_value_elem, intcon(JVM_ACC_VALUE)));
3421 not_value = _gvn.transform(new ConvI2LNode(not_value));
3422 Node* not_null = _gvn.transform(new CastP2XNode(NULL, obj));
3423 Node* both = _gvn.transform(new OrLNode(not_null, not_value));
3424 Node* cmp = _gvn.transform(new CmpLNode(both, longcon(0)));
3425 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3426 { BuildCutout unless(this, bol, PROB_MAX);
3427 // TODO just deoptimize for now if we store null to a value type array
3428 inc_sp(nargs);
3429 uncommon_trap(Deoptimization::Reason_array_check,
3430 Deoptimization::Action_none);
3431 }
3432 }
3433 }
3434
3435 Node* GraphKit::load_lh_array_tag(Node* kls) {
3436 Node* lhp = basic_plus_adr(kls, in_bytes(Klass::layout_helper_offset()));
3437 Node* layout_val = make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);
3438 return _gvn.transform(new RShiftINode(layout_val, intcon(Klass::_lh_array_tag_shift)));
3439 }
3440
3441
3442 Node* GraphKit::gen_lh_array_test(Node* kls, unsigned int lh_value) {
3443 Node* layout_val = load_lh_array_tag(kls);
3444 Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(lh_value)));
3445 return cmp;
3446 }
3447
3448
3449 //------------------------------next_monitor-----------------------------------
3450 // What number should be given to the next monitor?
3451 int GraphKit::next_monitor() {
3452 int current = jvms()->monitor_depth()* C->sync_stack_slots();
3453 int next = current + C->sync_stack_slots();
3454 // Keep the toplevel high water mark current:
3455 if (C->fixed_slots() < next) C->set_fixed_slots(next);
3456 return current;
3457 }
3458
3459 //------------------------------insert_mem_bar---------------------------------
3460 // Memory barrier to avoid floating things around
3461 // The membar serves as a pinch point between both control and all memory slices.
3462 Node* GraphKit::insert_mem_bar(int opcode, Node* precedent) {
3463 MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, precedent);
3464 mb->init_req(TypeFunc::Control, control());
3465 mb->init_req(TypeFunc::Memory, reset_memory());
3466 Node* membar = _gvn.transform(mb);
3467 set_control(_gvn.transform(new ProjNode(membar, TypeFunc::Control)));
3468 set_all_memory_call(membar);
3494 }
3495 Node* membar = _gvn.transform(mb);
3496 set_control(_gvn.transform(new ProjNode(membar, TypeFunc::Control)));
3497 if (alias_idx == Compile::AliasIdxBot) {
3498 merged_memory()->set_base_memory(_gvn.transform(new ProjNode(membar, TypeFunc::Memory)));
3499 } else {
3500 set_memory(_gvn.transform(new ProjNode(membar, TypeFunc::Memory)),alias_idx);
3501 }
3502 return membar;
3503 }
3504
3505 //------------------------------shared_lock------------------------------------
3506 // Emit locking code.
3507 FastLockNode* GraphKit::shared_lock(Node* obj) {
3508 // bci is either a monitorenter bc or InvocationEntryBci
3509 // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
3510 assert(SynchronizationEntryBCI == InvocationEntryBci, "");
3511
3512 if( !GenerateSynchronizationCode )
3513 return NULL; // Not locking things?
3514
3515 // We cannot lock on a value type
3516 const TypeOopPtr* objptr = _gvn.type(obj)->make_oopptr();
3517 if (objptr->can_be_value_type()) {
3518 gen_value_type_guard(obj, 1);
3519 }
3520
3521 if (stopped()) // Dead monitor?
3522 return NULL;
3523
3524 assert(dead_locals_are_killed(), "should kill locals before sync. point");
3525
3526 obj = access_resolve(obj, ACCESS_READ | ACCESS_WRITE);
3527
3528 // Box the stack location
3529 Node* box = _gvn.transform(new BoxLockNode(next_monitor()));
3530 Node* mem = reset_memory();
3531
3532 FastLockNode * flock = _gvn.transform(new FastLockNode(0, obj, box) )->as_FastLock();
3533 if (UseBiasedLocking && PrintPreciseBiasedLockingStatistics) {
3534 // Create the counters for this fast lock.
3535 flock->create_lock_counter(sync_jvms()); // sync_jvms used to get current bci
3536 }
3537
3538 // Create the rtm counters for this fast lock if needed.
3539 flock->create_rtm_lock_counter(sync_jvms()); // sync_jvms used to get current bci
3540
3575 }
3576 #endif
3577
3578 return flock;
3579 }
3580
3581
3582 //------------------------------shared_unlock----------------------------------
3583 // Emit unlocking code.
3584 void GraphKit::shared_unlock(Node* box, Node* obj) {
3585 // bci is either a monitorenter bc or InvocationEntryBci
3586 // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
3587 assert(SynchronizationEntryBCI == InvocationEntryBci, "");
3588
3589 if( !GenerateSynchronizationCode )
3590 return;
3591 if (stopped()) { // Dead monitor?
3592 map()->pop_monitor(); // Kill monitor from debug info
3593 return;
3594 }
3595 assert(!obj->is_ValueTypeBase(), "should not unlock on value type");
3596
3597 // Memory barrier to avoid floating things down past the locked region
3598 insert_mem_bar(Op_MemBarReleaseLock);
3599
3600 const TypeFunc *tf = OptoRuntime::complete_monitor_exit_Type();
3601 UnlockNode *unlock = new UnlockNode(C, tf);
3602 #ifdef ASSERT
3603 unlock->set_dbg_jvms(sync_jvms());
3604 #endif
3605 uint raw_idx = Compile::AliasIdxRaw;
3606 unlock->init_req( TypeFunc::Control, control() );
3607 unlock->init_req( TypeFunc::Memory , memory(raw_idx) );
3608 unlock->init_req( TypeFunc::I_O , top() ) ; // does no i/o
3609 unlock->init_req( TypeFunc::FramePtr, frameptr() );
3610 unlock->init_req( TypeFunc::ReturnAdr, top() );
3611
3612 unlock->init_req(TypeFunc::Parms + 0, obj);
3613 unlock->init_req(TypeFunc::Parms + 1, box);
3614 unlock = _gvn.transform(unlock)->as_Unlock();
3615
3616 Node* mem = reset_memory();
3617
3618 // unlock has no side-effects, sets few values
3619 set_predefined_output_for_runtime_call(unlock, mem, TypeRawPtr::BOTTOM);
3620
3621 // Kill monitor from debug info
3622 map()->pop_monitor( );
3623 }
3624
3625 //-------------------------------get_layout_helper-----------------------------
3626 // If the given klass is a constant or known to be an array,
3627 // fetch the constant layout helper value into constant_value
3628 // and return (Node*)NULL. Otherwise, load the non-constant
3629 // layout helper value, and return the node which represents it.
3630 // This two-faced routine is useful because allocation sites
3631 // almost always feature constant types.
3632 Node* GraphKit::get_layout_helper(Node* klass_node, jint& constant_value) {
3633 const TypeKlassPtr* inst_klass = _gvn.type(klass_node)->isa_klassptr();
3634 if (!StressReflectiveCode && inst_klass != NULL) {
3635 ciKlass* klass = inst_klass->klass();
3636 assert(klass != NULL, "klass should not be NULL");
3637 bool xklass = inst_klass->klass_is_exact();
3638 bool can_be_value_array = false;
3639 if (klass->is_array_klass() && EnableValhalla && ValueArrayFlatten) {
3640 ciKlass* elem = klass->as_array_klass()->element_klass();
3641 can_be_value_array = elem != NULL && (elem->is_java_lang_Object() || elem->is_interface());
3642 }
3643 if (xklass || (klass->is_array_klass() && !can_be_value_array)) {
3644 jint lhelper = klass->layout_helper();
3645 if (lhelper != Klass::_lh_neutral_value) {
3646 constant_value = lhelper;
3647 return (Node*) NULL;
3648 }
3649 }
3650 }
3651 constant_value = Klass::_lh_neutral_value; // put in a known value
3652 Node* lhp = basic_plus_adr(klass_node, klass_node, in_bytes(Klass::layout_helper_offset()));
3653 return make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);
3654 }
3655
3656 // We just put in an allocate/initialize with a big raw-memory effect.
3657 // Hook selected additional alias categories on the initialization.
3658 static void hook_memory_on_init(GraphKit& kit, int alias_idx,
3659 MergeMemNode* init_in_merge,
3660 Node* init_out_raw) {
3661 DEBUG_ONLY(Node* init_in_raw = init_in_merge->base_memory());
3662 assert(init_in_merge->memory_at(alias_idx) == init_in_raw, "");
3663
3685
3686 // a normal slow-call doesn't change i_o, but an allocation does
3687 // we create a separate i_o projection for the normal control path
3688 set_i_o(_gvn.transform( new ProjNode(allocx, TypeFunc::I_O, false) ) );
3689 Node* rawoop = _gvn.transform( new ProjNode(allocx, TypeFunc::Parms) );
3690
3691 // put in an initialization barrier
3692 InitializeNode* init = insert_mem_bar_volatile(Op_Initialize, rawidx,
3693 rawoop)->as_Initialize();
3694 assert(alloc->initialization() == init, "2-way macro link must work");
3695 assert(init ->allocation() == alloc, "2-way macro link must work");
3696 {
3697 // Extract memory strands which may participate in the new object's
3698 // initialization, and source them from the new InitializeNode.
3699 // This will allow us to observe initializations when they occur,
3700 // and link them properly (as a group) to the InitializeNode.
3701 assert(init->in(InitializeNode::Memory) == malloc, "");
3702 MergeMemNode* minit_in = MergeMemNode::make(malloc);
3703 init->set_req(InitializeNode::Memory, minit_in);
3704 record_for_igvn(minit_in); // fold it up later, if possible
3705 _gvn.set_type(minit_in, Type::MEMORY);
3706 Node* minit_out = memory(rawidx);
3707 assert(minit_out->is_Proj() && minit_out->in(0) == init, "");
3708 // Add an edge in the MergeMem for the header fields so an access
3709 // to one of those has correct memory state
3710 set_memory(minit_out, C->get_alias_index(oop_type->add_offset(oopDesc::mark_offset_in_bytes())));
3711 set_memory(minit_out, C->get_alias_index(oop_type->add_offset(oopDesc::klass_offset_in_bytes())));
3712 if (oop_type->isa_aryptr()) {
3713 const TypeAryPtr* arytype = oop_type->is_aryptr();
3714 if (arytype->klass()->is_value_array_klass()) {
3715 ciValueArrayKlass* vak = arytype->klass()->as_value_array_klass();
3716 ciValueKlass* vk = vak->element_klass()->as_value_klass();
3717 for (int i = 0, len = vk->nof_nonstatic_fields(); i < len; i++) {
3718 ciField* field = vk->nonstatic_field_at(i);
3719 if (field->offset() >= TrackedInitializationLimit * HeapWordSize)
3720 continue; // do not bother to track really large numbers of fields
3721 int off_in_vt = field->offset() - vk->first_field_offset();
3722 const TypePtr* adr_type = arytype->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
3723 int fieldidx = C->get_alias_index(adr_type);
3724 hook_memory_on_init(*this, fieldidx, minit_in, minit_out);
3725 }
3726 } else {
3727 const TypePtr* telemref = oop_type->add_offset(Type::OffsetBot);
3728 int elemidx = C->get_alias_index(telemref);
3729 hook_memory_on_init(*this, elemidx, minit_in, minit_out);
3730 }
3731 } else if (oop_type->isa_instptr()) {
3732 set_memory(minit_out, C->get_alias_index(oop_type)); // mark word
3733 ciInstanceKlass* ik = oop_type->klass()->as_instance_klass();
3734 for (int i = 0, len = ik->nof_nonstatic_fields(); i < len; i++) {
3735 ciField* field = ik->nonstatic_field_at(i);
3736 if (field->offset() >= TrackedInitializationLimit * HeapWordSize)
3737 continue; // do not bother to track really large numbers of fields
3738 // Find (or create) the alias category for this field:
3739 int fieldidx = C->alias_type(field)->index();
3740 hook_memory_on_init(*this, fieldidx, minit_in, minit_out);
3741 }
3742 }
3743 }
3744
3745 // Cast raw oop to the real thing...
3746 Node* javaoop = new CheckCastPPNode(control(), rawoop, oop_type);
3747 javaoop = _gvn.transform(javaoop);
3748 C->set_recent_alloc(control(), javaoop);
3749 assert(just_allocated_object(control()) == javaoop, "just allocated");
3750
3751 #ifdef ASSERT
3752 { // Verify that the AllocateNode::Ideal_allocation recognizers work:
3763 assert(alloc->in(AllocateNode::ALength)->is_top(), "no length, please");
3764 }
3765 }
3766 #endif //ASSERT
3767
3768 return javaoop;
3769 }
3770
3771 //---------------------------new_instance--------------------------------------
3772 // This routine takes a klass_node which may be constant (for a static type)
3773 // or may be non-constant (for reflective code). It will work equally well
3774 // for either, and the graph will fold nicely if the optimizer later reduces
3775 // the type to a constant.
3776 // The optional arguments are for specialized use by intrinsics:
3777 // - If 'extra_slow_test' if not null is an extra condition for the slow-path.
3778 // - If 'return_size_val', report the the total object size to the caller.
3779 // - deoptimize_on_exception controls how Java exceptions are handled (rethrow vs deoptimize)
3780 Node* GraphKit::new_instance(Node* klass_node,
3781 Node* extra_slow_test,
3782 Node* *return_size_val,
3783 bool deoptimize_on_exception,
3784 ValueTypeBaseNode* value_node) {
3785 // Compute size in doublewords
3786 // The size is always an integral number of doublewords, represented
3787 // as a positive bytewise size stored in the klass's layout_helper.
3788 // The layout_helper also encodes (in a low bit) the need for a slow path.
3789 jint layout_con = Klass::_lh_neutral_value;
3790 Node* layout_val = get_layout_helper(klass_node, layout_con);
3791 bool layout_is_con = (layout_val == NULL);
3792
3793 if (extra_slow_test == NULL) extra_slow_test = intcon(0);
3794 // Generate the initial go-slow test. It's either ALWAYS (return a
3795 // Node for 1) or NEVER (return a NULL) or perhaps (in the reflective
3796 // case) a computed value derived from the layout_helper.
3797 Node* initial_slow_test = NULL;
3798 if (layout_is_con) {
3799 assert(!StressReflectiveCode, "stress mode does not use these paths");
3800 bool must_go_slow = Klass::layout_helper_needs_slow_path(layout_con);
3801 initial_slow_test = must_go_slow ? intcon(1) : extra_slow_test;
3802 } else { // reflective case
3803 // This reflective path is used by Unsafe.allocateInstance.
3804 // (It may be stress-tested by specifying StressReflectiveCode.)
3805 // Basically, we want to get into the VM is there's an illegal argument.
3806 Node* bit = intcon(Klass::_lh_instance_slow_path_bit);
3807 initial_slow_test = _gvn.transform( new AndINode(layout_val, bit) );
3808 if (extra_slow_test != intcon(0)) {
3809 initial_slow_test = _gvn.transform( new OrINode(initial_slow_test, extra_slow_test) );
3810 }
3811 // (Macro-expander will further convert this to a Bool, if necessary.)
3822
3823 // Clear the low bits to extract layout_helper_size_in_bytes:
3824 assert((int)Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit");
3825 Node* mask = MakeConX(~ (intptr_t)right_n_bits(LogBytesPerLong));
3826 size = _gvn.transform( new AndXNode(size, mask) );
3827 }
3828 if (return_size_val != NULL) {
3829 (*return_size_val) = size;
3830 }
3831
3832 // This is a precise notnull oop of the klass.
3833 // (Actually, it need not be precise if this is a reflective allocation.)
3834 // It's what we cast the result to.
3835 const TypeKlassPtr* tklass = _gvn.type(klass_node)->isa_klassptr();
3836 if (!tklass) tklass = TypeKlassPtr::OBJECT;
3837 const TypeOopPtr* oop_type = tklass->as_instance_type();
3838
3839 // Now generate allocation code
3840
3841 // The entire memory state is needed for slow path of the allocation
3842 // since GC and deoptimization can happen.
3843 Node *mem = reset_memory();
3844 set_all_memory(mem); // Create new memory state
3845
3846 AllocateNode* alloc = new AllocateNode(C, AllocateNode::alloc_type(Type::TOP),
3847 control(), mem, i_o(),
3848 size, klass_node,
3849 initial_slow_test, value_node);
3850
3851 return set_output_for_allocation(alloc, oop_type, deoptimize_on_exception);
3852 }
3853
3854 // With compressed oops, the 64 bit init value for non flattened value
3855 // arrays is built from 2 32 bit compressed oops
3856 static Node* raw_default_for_coops(Node* default_value, GraphKit& kit) {
3857 Node* lower = kit.gvn().transform(new CastP2XNode(kit.control(), default_value));
3858 Node* upper = kit.gvn().transform(new LShiftLNode(lower, kit.intcon(32)));
3859 return kit.gvn().transform(new OrLNode(lower, upper));
3860 }
3861
3862 //-------------------------------new_array-------------------------------------
3863 // helper for newarray and anewarray
3864 // The 'length' parameter is (obviously) the length of the array.
3865 // See comments on new_instance for the meaning of the other arguments.
3866 Node* GraphKit::new_array(Node* klass_node, // array klass (maybe variable)
3867 Node* length, // number of array elements
3868 int nargs, // number of arguments to push back for uncommon trap
3869 Node* *return_size_val,
3870 bool deoptimize_on_exception) {
3871 jint layout_con = Klass::_lh_neutral_value;
3872 Node* layout_val = get_layout_helper(klass_node, layout_con);
3873 bool layout_is_con = (layout_val == NULL);
3874
3875 if (!layout_is_con && !StressReflectiveCode &&
3876 !too_many_traps(Deoptimization::Reason_class_check)) {
3877 // This is a reflective array creation site.
3878 // Optimistically assume that it is a subtype of Object[],
3879 // so that we can fold up all the address arithmetic.
3880 layout_con = Klass::array_layout_helper(T_OBJECT);
3881 Node* cmp_lh = _gvn.transform( new CmpINode(layout_val, intcon(layout_con)) );
3882 Node* bol_lh = _gvn.transform( new BoolNode(cmp_lh, BoolTest::eq) );
3883 { BuildCutout unless(this, bol_lh, PROB_MAX);
3884 inc_sp(nargs);
3885 uncommon_trap(Deoptimization::Reason_class_check,
3886 Deoptimization::Action_maybe_recompile);
3887 }
3888 layout_val = NULL;
3889 layout_is_con = true;
3890 }
3891
3892 // Generate the initial go-slow test. Make sure we do not overflow
3893 // if length is huge (near 2Gig) or negative! We do not need
3894 // exact double-words here, just a close approximation of needed
3895 // double-words. We can't add any offset or rounding bits, lest we
3896 // take a size -1 of bytes and make it positive. Use an unsigned
3897 // compare, so negative sizes look hugely positive.
3898 int fast_size_limit = FastAllocateSizeLimit;
3899 if (layout_is_con) {
3900 assert(!StressReflectiveCode, "stress mode does not use these paths");
3901 // Increase the size limit if we have exact knowledge of array type.
3902 int log2_esize = Klass::layout_helper_log2_element_size(layout_con);
3903 fast_size_limit <<= MAX2(LogBytesPerLong - log2_esize, 0);
3904 }
3905
3906 Node* initial_slow_cmp = _gvn.transform( new CmpUNode( length, intcon( fast_size_limit ) ) );
3907 Node* initial_slow_test = _gvn.transform( new BoolNode( initial_slow_cmp, BoolTest::gt ) );
3908
3909 // --- Size Computation ---
3910 // array_size = round_to_heap(array_header + (length << elem_shift));
3911 // where round_to_heap(x) == align_to(x, MinObjAlignmentInBytes)
3912 // and align_to(x, y) == ((x + y-1) & ~(y-1))
3913 // The rounding mask is strength-reduced, if possible.
3914 int round_mask = MinObjAlignmentInBytes - 1;
3915 Node* header_size = NULL;
3916 int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
3917 // (T_BYTE has the weakest alignment and size restrictions...)
3918 if (layout_is_con) {
3919 int hsize = Klass::layout_helper_header_size(layout_con);
3920 int eshift = Klass::layout_helper_log2_element_size(layout_con);
3921 BasicType etype = Klass::layout_helper_element_type(layout_con);
3922 bool is_value_array = Klass::layout_helper_is_valueArray(layout_con);
3923 if ((round_mask & ~right_n_bits(eshift)) == 0)
3924 round_mask = 0; // strength-reduce it if it goes away completely
3925 assert(is_value_array || (hsize & right_n_bits(eshift)) == 0, "hsize is pre-rounded");
3926 assert(header_size_min <= hsize, "generic minimum is smallest");
3927 header_size_min = hsize;
3928 header_size = intcon(hsize + round_mask);
3929 } else {
3930 Node* hss = intcon(Klass::_lh_header_size_shift);
3931 Node* hsm = intcon(Klass::_lh_header_size_mask);
3932 Node* hsize = _gvn.transform( new URShiftINode(layout_val, hss) );
3933 hsize = _gvn.transform( new AndINode(hsize, hsm) );
3934 Node* mask = intcon(round_mask);
3935 header_size = _gvn.transform( new AddINode(hsize, mask) );
3936 }
3937
3938 Node* elem_shift = NULL;
3939 if (layout_is_con) {
3940 int eshift = Klass::layout_helper_log2_element_size(layout_con);
3941 if (eshift != 0)
3942 elem_shift = intcon(eshift);
3943 } else {
3944 // There is no need to mask or shift this value.
3945 // The semantics of LShiftINode include an implicit mask to 0x1F.
3989 // places, one where the length is sharply limited, and the other
3990 // after a successful allocation.
3991 Node* abody = lengthx;
3992 if (elem_shift != NULL)
3993 abody = _gvn.transform( new LShiftXNode(lengthx, elem_shift) );
3994 Node* size = _gvn.transform( new AddXNode(headerx, abody) );
3995 if (round_mask != 0) {
3996 Node* mask = MakeConX(~round_mask);
3997 size = _gvn.transform( new AndXNode(size, mask) );
3998 }
3999 // else if round_mask == 0, the size computation is self-rounding
4000
4001 if (return_size_val != NULL) {
4002 // This is the size
4003 (*return_size_val) = size;
4004 }
4005
4006 // Now generate allocation code
4007
4008 // The entire memory state is needed for slow path of the allocation
4009 // since GC and deoptimization can happen.
4010 Node *mem = reset_memory();
4011 set_all_memory(mem); // Create new memory state
4012
4013 if (initial_slow_test->is_Bool()) {
4014 // Hide it behind a CMoveI, or else PhaseIdealLoop::split_up will get sick.
4015 initial_slow_test = initial_slow_test->as_Bool()->as_int_value(&_gvn);
4016 }
4017
4018 const TypeOopPtr* ary_type = _gvn.type(klass_node)->is_klassptr()->as_instance_type();
4019 const TypeAryPtr* ary_ptr = ary_type->isa_aryptr();
4020 const Type* elem = NULL;
4021 ciKlass* elem_klass = NULL;
4022 if (ary_ptr != NULL) {
4023 elem = ary_ptr->elem();
4024 elem_klass = ary_ptr->klass()->as_array_klass()->element_klass();
4025 }
4026 Node* default_value = NULL;
4027 Node* raw_default_value = NULL;
4028 if (elem != NULL && elem->make_ptr()) {
4029 if (elem_klass != NULL && elem_klass->is_valuetype()) {
4030 ciValueKlass* vk = elem_klass->as_value_klass();
4031 if (!vk->flatten_array()) {
4032 default_value = ValueTypeNode::default_oop(gvn(), vk);
4033 if (elem->isa_narrowoop()) {
4034 default_value = _gvn.transform(new EncodePNode(default_value, elem));
4035 raw_default_value = raw_default_for_coops(default_value, *this);
4036 } else {
4037 raw_default_value = _gvn.transform(new CastP2XNode(control(), default_value));
4038 }
4039 }
4040 }
4041 }
4042
4043 if (EnableValhalla && (elem == NULL || (elem_klass != NULL && elem_klass->is_java_lang_Object() && !ary_type->klass_is_exact()))) {
4044 assert(raw_default_value == NULL, "shouldn't be set yet");
4045
4046 // unkown array type, could be a non flattened value array that's
4047 // initialize to a non zero default value
4048
4049 Node* r = new RegionNode(4);
4050 Node* phi = new PhiNode(r, TypeX_X);
4051
4052 Node* cmp = gen_lh_array_test(klass_node, Klass::_lh_array_tag_obj_value);
4053 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
4054 IfNode* iff = create_and_map_if(control(), bol, PROB_FAIR, COUNT_UNKNOWN);
4055 r->init_req(1, _gvn.transform(new IfFalseNode(iff)));
4056 phi->init_req(1, MakeConX(0));
4057 set_control(_gvn.transform(new IfTrueNode(iff)));
4058 Node* k_adr = basic_plus_adr(klass_node, in_bytes(ArrayKlass::element_klass_offset()));
4059 Node* elem_klass = _gvn.transform(LoadKlassNode::make(_gvn, control(), immutable_memory(), k_adr, TypeInstPtr::KLASS));
4060 cmp = gen_value_type_test(elem_klass);
4061 bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
4062 iff = create_and_map_if(control(), bol, PROB_FAIR, COUNT_UNKNOWN);
4063 r->init_req(2, _gvn.transform(new IfTrueNode(iff)));
4064 phi->init_req(2, MakeConX(0));
4065 set_control(_gvn.transform(new IfFalseNode(iff)));
4066
4067 Node* adr_fixed_block_addr = basic_plus_adr(elem_klass, in_bytes(InstanceKlass::adr_valueklass_fixed_block_offset()));
4068 Node* adr_fixed_block = make_load(control(), adr_fixed_block_addr, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
4069
4070 Node* default_value_offset_addr = basic_plus_adr(adr_fixed_block, in_bytes(ValueKlass::default_value_offset_offset()));
4071 Node* default_value_offset = make_load(control(), default_value_offset_addr, TypeInt::INT, T_INT, MemNode::unordered);
4072
4073 Node* elem_mirror = load_mirror_from_klass(elem_klass);
4074
4075 Node* default_value_addr = basic_plus_adr(elem_mirror, ConvI2X(default_value_offset));
4076 const TypePtr* adr_type = _gvn.type(default_value_addr)->is_ptr();
4077 Node* val = access_load_at(elem_mirror, default_value_addr, adr_type, TypeInstPtr::BOTTOM, T_OBJECT, IN_HEAP);
4078
4079 if (UseCompressedOops) {
4080 val = _gvn.transform(new EncodePNode(val, elem));
4081 val = raw_default_for_coops(val, *this);
4082 } else {
4083 val = _gvn.transform(new CastP2XNode(control(), val));
4084 }
4085 r->init_req(3, control());
4086 phi->init_req(3, val);
4087 set_control(_gvn.transform(r));
4088 raw_default_value = _gvn.transform(phi);
4089 }
4090
4091 // Create the AllocateArrayNode and its result projections
4092 AllocateArrayNode* alloc
4093 = new AllocateArrayNode(C, AllocateArrayNode::alloc_type(TypeInt::INT),
4094 control(), mem, i_o(),
4095 size, klass_node,
4096 initial_slow_test,
4097 length, default_value,
4098 raw_default_value);
4099
4100 // Cast to correct type. Note that the klass_node may be constant or not,
4101 // and in the latter case the actual array type will be inexact also.
4102 // (This happens via a non-constant argument to inline_native_newArray.)
4103 // In any case, the value of klass_node provides the desired array type.
4104 const TypeInt* length_type = _gvn.find_int_type(length);
4105 if (ary_type->isa_aryptr() && length_type != NULL) {
4106 // Try to get a better type than POS for the size
4107 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
4108 }
4109
4110 Node* javaoop = set_output_for_allocation(alloc, ary_type, deoptimize_on_exception);
4111
4112 // Cast length on remaining path to be as narrow as possible
4113 if (map()->find_edge(length) >= 0) {
4114 Node* ccast = alloc->make_ideal_length(ary_type, &_gvn);
4115 if (ccast != length) {
4116 _gvn.set_type_bottom(ccast);
4117 record_for_igvn(ccast);
4118 replace_in_map(length, ccast);
4119 }
4120 }
4121
4122 return javaoop;
4123 }
4124
4239 set_all_memory(ideal.merged_memory());
4240 set_i_o(ideal.i_o());
4241 set_control(ideal.ctrl());
4242 }
4243
4244 void GraphKit::final_sync(IdealKit& ideal) {
4245 // Final sync IdealKit and graphKit.
4246 sync_kit(ideal);
4247 }
4248
4249 Node* GraphKit::load_String_length(Node* str, bool set_ctrl) {
4250 Node* len = load_array_length(load_String_value(str, set_ctrl));
4251 Node* coder = load_String_coder(str, set_ctrl);
4252 // Divide length by 2 if coder is UTF16
4253 return _gvn.transform(new RShiftINode(len, coder));
4254 }
4255
4256 Node* GraphKit::load_String_value(Node* str, bool set_ctrl) {
4257 int value_offset = java_lang_String::value_offset_in_bytes();
4258 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
4259 false, NULL, Type::Offset(0));
4260 const TypePtr* value_field_type = string_type->add_offset(value_offset);
4261 const TypeAryPtr* value_type = TypeAryPtr::make(TypePtr::NotNull,
4262 TypeAry::make(TypeInt::BYTE, TypeInt::POS),
4263 ciTypeArrayKlass::make(T_BYTE), true, Type::Offset(0));
4264 Node* p = basic_plus_adr(str, str, value_offset);
4265 Node* load = access_load_at(str, p, value_field_type, value_type, T_OBJECT,
4266 IN_HEAP | (set_ctrl ? C2_CONTROL_DEPENDENT_LOAD : 0) | MO_UNORDERED);
4267 return load;
4268 }
4269
4270 Node* GraphKit::load_String_coder(Node* str, bool set_ctrl) {
4271 if (!CompactStrings) {
4272 return intcon(java_lang_String::CODER_UTF16);
4273 }
4274 int coder_offset = java_lang_String::coder_offset_in_bytes();
4275 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
4276 false, NULL, Type::Offset(0));
4277 const TypePtr* coder_field_type = string_type->add_offset(coder_offset);
4278
4279 Node* p = basic_plus_adr(str, str, coder_offset);
4280 Node* load = access_load_at(str, p, coder_field_type, TypeInt::BYTE, T_BYTE,
4281 IN_HEAP | (set_ctrl ? C2_CONTROL_DEPENDENT_LOAD : 0) | MO_UNORDERED);
4282 return load;
4283 }
4284
4285 void GraphKit::store_String_value(Node* str, Node* value) {
4286 int value_offset = java_lang_String::value_offset_in_bytes();
4287 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
4288 false, NULL, Type::Offset(0));
4289 const TypePtr* value_field_type = string_type->add_offset(value_offset);
4290
4291 access_store_at(str, basic_plus_adr(str, value_offset), value_field_type,
4292 value, TypeAryPtr::BYTES, T_OBJECT, IN_HEAP | MO_UNORDERED);
4293 }
4294
4295 void GraphKit::store_String_coder(Node* str, Node* value) {
4296 int coder_offset = java_lang_String::coder_offset_in_bytes();
4297 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
4298 false, NULL, Type::Offset(0));
4299 const TypePtr* coder_field_type = string_type->add_offset(coder_offset);
4300
4301 access_store_at(str, basic_plus_adr(str, coder_offset), coder_field_type,
4302 value, TypeInt::BYTE, T_BYTE, IN_HEAP | MO_UNORDERED);
4303 }
4304
4305 // Capture src and dst memory state with a MergeMemNode
4306 Node* GraphKit::capture_memory(const TypePtr* src_type, const TypePtr* dst_type) {
4307 if (src_type == dst_type) {
4308 // Types are equal, we don't need a MergeMemNode
4309 return memory(src_type);
4310 }
4311 MergeMemNode* merge = MergeMemNode::make(map()->memory());
4312 record_for_igvn(merge); // fold it up later, if possible
4313 int src_idx = C->get_alias_index(src_type);
4314 int dst_idx = C->get_alias_index(dst_type);
4315 merge->set_memory_at(src_idx, memory(src_idx));
4316 merge->set_memory_at(dst_idx, memory(dst_idx));
4317 return merge;
4318 }
4391 i_char->init_req(2, AddI(i_char, intcon(2)));
4392
4393 set_control(IfFalse(iff));
4394 set_memory(st, TypeAryPtr::BYTES);
4395 }
4396
4397 Node* GraphKit::make_constant_from_field(ciField* field, Node* obj) {
4398 if (!field->is_constant()) {
4399 return NULL; // Field not marked as constant.
4400 }
4401 ciInstance* holder = NULL;
4402 if (!field->is_static()) {
4403 ciObject* const_oop = obj->bottom_type()->is_oopptr()->const_oop();
4404 if (const_oop != NULL && const_oop->is_instance()) {
4405 holder = const_oop->as_instance();
4406 }
4407 }
4408 const Type* con_type = Type::make_constant_from_field(field, holder, field->layout_type(),
4409 /*is_unsigned_load=*/false);
4410 if (con_type != NULL) {
4411 Node* con = makecon(con_type);
4412 if (field->layout_type() == T_VALUETYPE && field->type()->as_value_klass()->is_scalarizable()) {
4413 // Load value type from constant oop
4414 assert(!con_type->maybe_null(), "should never be null");
4415 con = ValueTypeNode::make_from_oop(this, con, field->type()->as_value_klass());
4416 }
4417 return con;
4418 }
4419 return NULL;
4420 }
4421
4422 //---------------------------load_mirror_from_klass----------------------------
4423 // Given a klass oop, load its java mirror (a java.lang.Class oop).
4424 Node* GraphKit::load_mirror_from_klass(Node* klass) {
4425 Node* p = basic_plus_adr(klass, in_bytes(Klass::java_mirror_offset()));
4426 Node* load = make_load(NULL, p, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
4427 // mirror = ((OopHandle)mirror)->resolve();
4428 return access_load(load, TypeInstPtr::MIRROR, T_OBJECT, IN_NATIVE);
4429 }
|