src/share/vm/opto/macro.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
child 9041
95a08233f46c
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

aoqi@0 1 /*
drchase@7605 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "compiler/compileLog.hpp"
aoqi@0 27 #include "libadt/vectset.hpp"
aoqi@0 28 #include "opto/addnode.hpp"
aoqi@0 29 #include "opto/callnode.hpp"
aoqi@0 30 #include "opto/cfgnode.hpp"
aoqi@0 31 #include "opto/compile.hpp"
aoqi@0 32 #include "opto/connode.hpp"
aoqi@0 33 #include "opto/locknode.hpp"
aoqi@0 34 #include "opto/loopnode.hpp"
aoqi@0 35 #include "opto/macro.hpp"
aoqi@0 36 #include "opto/memnode.hpp"
aoqi@0 37 #include "opto/node.hpp"
aoqi@0 38 #include "opto/phaseX.hpp"
aoqi@0 39 #include "opto/rootnode.hpp"
aoqi@0 40 #include "opto/runtime.hpp"
aoqi@0 41 #include "opto/subnode.hpp"
aoqi@0 42 #include "opto/type.hpp"
aoqi@0 43 #include "runtime/sharedRuntime.hpp"
aoqi@0 44
aoqi@0 45
aoqi@0 46 //
aoqi@0 47 // Replace any references to "oldref" in inputs to "use" with "newref".
aoqi@0 48 // Returns the number of replacements made.
aoqi@0 49 //
aoqi@0 50 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
aoqi@0 51 int nreplacements = 0;
aoqi@0 52 uint req = use->req();
aoqi@0 53 for (uint j = 0; j < use->len(); j++) {
aoqi@0 54 Node *uin = use->in(j);
aoqi@0 55 if (uin == oldref) {
aoqi@0 56 if (j < req)
aoqi@0 57 use->set_req(j, newref);
aoqi@0 58 else
aoqi@0 59 use->set_prec(j, newref);
aoqi@0 60 nreplacements++;
aoqi@0 61 } else if (j >= req && uin == NULL) {
aoqi@0 62 break;
aoqi@0 63 }
aoqi@0 64 }
aoqi@0 65 return nreplacements;
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 void PhaseMacroExpand::copy_call_debug_info(CallNode *oldcall, CallNode * newcall) {
aoqi@0 69 // Copy debug information and adjust JVMState information
aoqi@0 70 uint old_dbg_start = oldcall->tf()->domain()->cnt();
aoqi@0 71 uint new_dbg_start = newcall->tf()->domain()->cnt();
aoqi@0 72 int jvms_adj = new_dbg_start - old_dbg_start;
aoqi@0 73 assert (new_dbg_start == newcall->req(), "argument count mismatch");
aoqi@0 74
aoqi@0 75 // SafePointScalarObject node could be referenced several times in debug info.
aoqi@0 76 // Use Dict to record cloned nodes.
aoqi@0 77 Dict* sosn_map = new Dict(cmpkey,hashkey);
aoqi@0 78 for (uint i = old_dbg_start; i < oldcall->req(); i++) {
aoqi@0 79 Node* old_in = oldcall->in(i);
aoqi@0 80 // Clone old SafePointScalarObjectNodes, adjusting their field contents.
aoqi@0 81 if (old_in != NULL && old_in->is_SafePointScalarObject()) {
aoqi@0 82 SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject();
aoqi@0 83 uint old_unique = C->unique();
aoqi@0 84 Node* new_in = old_sosn->clone(sosn_map);
aoqi@0 85 if (old_unique != C->unique()) { // New node?
aoqi@0 86 new_in->set_req(0, C->root()); // reset control edge
aoqi@0 87 new_in = transform_later(new_in); // Register new node.
aoqi@0 88 }
aoqi@0 89 old_in = new_in;
aoqi@0 90 }
aoqi@0 91 newcall->add_req(old_in);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 newcall->set_jvms(oldcall->jvms());
aoqi@0 95 for (JVMState *jvms = newcall->jvms(); jvms != NULL; jvms = jvms->caller()) {
aoqi@0 96 jvms->set_map(newcall);
aoqi@0 97 jvms->set_locoff(jvms->locoff()+jvms_adj);
aoqi@0 98 jvms->set_stkoff(jvms->stkoff()+jvms_adj);
aoqi@0 99 jvms->set_monoff(jvms->monoff()+jvms_adj);
aoqi@0 100 jvms->set_scloff(jvms->scloff()+jvms_adj);
aoqi@0 101 jvms->set_endoff(jvms->endoff()+jvms_adj);
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) {
aoqi@0 106 Node* cmp;
aoqi@0 107 if (mask != 0) {
aoqi@0 108 Node* and_node = transform_later(new (C) AndXNode(word, MakeConX(mask)));
aoqi@0 109 cmp = transform_later(new (C) CmpXNode(and_node, MakeConX(bits)));
aoqi@0 110 } else {
aoqi@0 111 cmp = word;
aoqi@0 112 }
aoqi@0 113 Node* bol = transform_later(new (C) BoolNode(cmp, BoolTest::ne));
aoqi@0 114 IfNode* iff = new (C) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
aoqi@0 115 transform_later(iff);
aoqi@0 116
aoqi@0 117 // Fast path taken.
aoqi@0 118 Node *fast_taken = transform_later( new (C) IfFalseNode(iff) );
aoqi@0 119
aoqi@0 120 // Fast path not-taken, i.e. slow path
aoqi@0 121 Node *slow_taken = transform_later( new (C) IfTrueNode(iff) );
aoqi@0 122
aoqi@0 123 if (return_fast_path) {
aoqi@0 124 region->init_req(edge, slow_taken); // Capture slow-control
aoqi@0 125 return fast_taken;
aoqi@0 126 } else {
aoqi@0 127 region->init_req(edge, fast_taken); // Capture fast-control
aoqi@0 128 return slow_taken;
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 //--------------------copy_predefined_input_for_runtime_call--------------------
aoqi@0 133 void PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) {
aoqi@0 134 // Set fixed predefined input arguments
aoqi@0 135 call->init_req( TypeFunc::Control, ctrl );
aoqi@0 136 call->init_req( TypeFunc::I_O , oldcall->in( TypeFunc::I_O) );
aoqi@0 137 call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ?????
aoqi@0 138 call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) );
aoqi@0 139 call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) );
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 //------------------------------make_slow_call---------------------------------
aoqi@0 143 CallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, address slow_call, const char* leaf_name, Node* slow_path, Node* parm0, Node* parm1) {
aoqi@0 144
aoqi@0 145 // Slow-path call
aoqi@0 146 CallNode *call = leaf_name
aoqi@0 147 ? (CallNode*)new (C) CallLeafNode ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
aoqi@0 148 : (CallNode*)new (C) CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), oldcall->jvms()->bci(), TypeRawPtr::BOTTOM );
aoqi@0 149
aoqi@0 150 // Slow path call has no side-effects, uses few values
aoqi@0 151 copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
aoqi@0 152 if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0);
aoqi@0 153 if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1);
aoqi@0 154 copy_call_debug_info(oldcall, call);
aoqi@0 155 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
aoqi@0 156 _igvn.replace_node(oldcall, call);
aoqi@0 157 transform_later(call);
aoqi@0 158
aoqi@0 159 return call;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 void PhaseMacroExpand::extract_call_projections(CallNode *call) {
aoqi@0 163 _fallthroughproj = NULL;
aoqi@0 164 _fallthroughcatchproj = NULL;
aoqi@0 165 _ioproj_fallthrough = NULL;
aoqi@0 166 _ioproj_catchall = NULL;
aoqi@0 167 _catchallcatchproj = NULL;
aoqi@0 168 _memproj_fallthrough = NULL;
aoqi@0 169 _memproj_catchall = NULL;
aoqi@0 170 _resproj = NULL;
aoqi@0 171 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
aoqi@0 172 ProjNode *pn = call->fast_out(i)->as_Proj();
aoqi@0 173 switch (pn->_con) {
aoqi@0 174 case TypeFunc::Control:
aoqi@0 175 {
aoqi@0 176 // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj
aoqi@0 177 _fallthroughproj = pn;
aoqi@0 178 DUIterator_Fast jmax, j = pn->fast_outs(jmax);
aoqi@0 179 const Node *cn = pn->fast_out(j);
aoqi@0 180 if (cn->is_Catch()) {
aoqi@0 181 ProjNode *cpn = NULL;
aoqi@0 182 for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) {
aoqi@0 183 cpn = cn->fast_out(k)->as_Proj();
aoqi@0 184 assert(cpn->is_CatchProj(), "must be a CatchProjNode");
aoqi@0 185 if (cpn->_con == CatchProjNode::fall_through_index)
aoqi@0 186 _fallthroughcatchproj = cpn;
aoqi@0 187 else {
aoqi@0 188 assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index.");
aoqi@0 189 _catchallcatchproj = cpn;
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193 break;
aoqi@0 194 }
aoqi@0 195 case TypeFunc::I_O:
aoqi@0 196 if (pn->_is_io_use)
aoqi@0 197 _ioproj_catchall = pn;
aoqi@0 198 else
aoqi@0 199 _ioproj_fallthrough = pn;
aoqi@0 200 break;
aoqi@0 201 case TypeFunc::Memory:
aoqi@0 202 if (pn->_is_io_use)
aoqi@0 203 _memproj_catchall = pn;
aoqi@0 204 else
aoqi@0 205 _memproj_fallthrough = pn;
aoqi@0 206 break;
aoqi@0 207 case TypeFunc::Parms:
aoqi@0 208 _resproj = pn;
aoqi@0 209 break;
aoqi@0 210 default:
aoqi@0 211 assert(false, "unexpected projection from allocation node.");
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 // Eliminate a card mark sequence. p2x is a ConvP2XNode
aoqi@0 218 void PhaseMacroExpand::eliminate_card_mark(Node* p2x) {
aoqi@0 219 assert(p2x->Opcode() == Op_CastP2X, "ConvP2XNode required");
aoqi@0 220 if (!UseG1GC) {
aoqi@0 221 // vanilla/CMS post barrier
aoqi@0 222 Node *shift = p2x->unique_out();
aoqi@0 223 Node *addp = shift->unique_out();
aoqi@0 224 for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
aoqi@0 225 Node *mem = addp->last_out(j);
aoqi@0 226 if (UseCondCardMark && mem->is_Load()) {
aoqi@0 227 assert(mem->Opcode() == Op_LoadB, "unexpected code shape");
aoqi@0 228 // The load is checking if the card has been written so
aoqi@0 229 // replace it with zero to fold the test.
aoqi@0 230 _igvn.replace_node(mem, intcon(0));
aoqi@0 231 continue;
aoqi@0 232 }
aoqi@0 233 assert(mem->is_Store(), "store required");
aoqi@0 234 _igvn.replace_node(mem, mem->in(MemNode::Memory));
aoqi@0 235 }
aoqi@0 236 } else {
aoqi@0 237 // G1 pre/post barriers
aoqi@0 238 assert(p2x->outcnt() <= 2, "expects 1 or 2 users: Xor and URShift nodes");
aoqi@0 239 // It could be only one user, URShift node, in Object.clone() instrinsic
aoqi@0 240 // but the new allocation is passed to arraycopy stub and it could not
aoqi@0 241 // be scalar replaced. So we don't check the case.
aoqi@0 242
aoqi@0 243 // An other case of only one user (Xor) is when the value check for NULL
aoqi@0 244 // in G1 post barrier is folded after CCP so the code which used URShift
aoqi@0 245 // is removed.
aoqi@0 246
aoqi@0 247 // Take Region node before eliminating post barrier since it also
aoqi@0 248 // eliminates CastP2X node when it has only one user.
aoqi@0 249 Node* this_region = p2x->in(0);
aoqi@0 250 assert(this_region != NULL, "");
aoqi@0 251
aoqi@0 252 // Remove G1 post barrier.
aoqi@0 253
aoqi@0 254 // Search for CastP2X->Xor->URShift->Cmp path which
aoqi@0 255 // checks if the store done to a different from the value's region.
aoqi@0 256 // And replace Cmp with #0 (false) to collapse G1 post barrier.
aoqi@0 257 Node* xorx = NULL;
aoqi@0 258 for (DUIterator_Fast imax, i = p2x->fast_outs(imax); i < imax; i++) {
aoqi@0 259 Node* u = p2x->fast_out(i);
aoqi@0 260 if (u->Opcode() == Op_XorX) {
aoqi@0 261 xorx = u;
aoqi@0 262 break;
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265 assert(xorx != NULL, "missing G1 post barrier");
aoqi@0 266 Node* shift = xorx->unique_out();
aoqi@0 267 Node* cmpx = shift->unique_out();
aoqi@0 268 assert(cmpx->is_Cmp() && cmpx->unique_out()->is_Bool() &&
aoqi@0 269 cmpx->unique_out()->as_Bool()->_test._test == BoolTest::ne,
aoqi@0 270 "missing region check in G1 post barrier");
aoqi@0 271 _igvn.replace_node(cmpx, makecon(TypeInt::CC_EQ));
aoqi@0 272
aoqi@0 273 // Remove G1 pre barrier.
aoqi@0 274
aoqi@0 275 // Search "if (marking != 0)" check and set it to "false".
aoqi@0 276 // There is no G1 pre barrier if previous stored value is NULL
aoqi@0 277 // (for example, after initialization).
aoqi@0 278 if (this_region->is_Region() && this_region->req() == 3) {
aoqi@0 279 int ind = 1;
aoqi@0 280 if (!this_region->in(ind)->is_IfFalse()) {
aoqi@0 281 ind = 2;
aoqi@0 282 }
aoqi@0 283 if (this_region->in(ind)->is_IfFalse()) {
aoqi@0 284 Node* bol = this_region->in(ind)->in(0)->in(1);
aoqi@0 285 assert(bol->is_Bool(), "");
aoqi@0 286 cmpx = bol->in(1);
aoqi@0 287 if (bol->as_Bool()->_test._test == BoolTest::ne &&
aoqi@0 288 cmpx->is_Cmp() && cmpx->in(2) == intcon(0) &&
aoqi@0 289 cmpx->in(1)->is_Load()) {
aoqi@0 290 Node* adr = cmpx->in(1)->as_Load()->in(MemNode::Address);
aoqi@0 291 const int marking_offset = in_bytes(JavaThread::satb_mark_queue_offset() +
aoqi@0 292 PtrQueue::byte_offset_of_active());
aoqi@0 293 if (adr->is_AddP() && adr->in(AddPNode::Base) == top() &&
aoqi@0 294 adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
aoqi@0 295 adr->in(AddPNode::Offset) == MakeConX(marking_offset)) {
aoqi@0 296 _igvn.replace_node(cmpx, makecon(TypeInt::CC_EQ));
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301 // Now CastP2X can be removed since it is used only on dead path
aoqi@0 302 // which currently still alive until igvn optimize it.
aoqi@0 303 assert(p2x->outcnt() == 0 || p2x->unique_out()->Opcode() == Op_URShiftX, "");
aoqi@0 304 _igvn.replace_node(p2x, top());
aoqi@0 305 }
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 // Search for a memory operation for the specified memory slice.
aoqi@0 309 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) {
aoqi@0 310 Node *orig_mem = mem;
aoqi@0 311 Node *alloc_mem = alloc->in(TypeFunc::Memory);
aoqi@0 312 const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr();
aoqi@0 313 while (true) {
aoqi@0 314 if (mem == alloc_mem || mem == start_mem ) {
aoqi@0 315 return mem; // hit one of our sentinels
aoqi@0 316 } else if (mem->is_MergeMem()) {
aoqi@0 317 mem = mem->as_MergeMem()->memory_at(alias_idx);
aoqi@0 318 } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
aoqi@0 319 Node *in = mem->in(0);
aoqi@0 320 // we can safely skip over safepoints, calls, locks and membars because we
aoqi@0 321 // already know that the object is safe to eliminate.
aoqi@0 322 if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) {
aoqi@0 323 return in;
aoqi@0 324 } else if (in->is_Call()) {
aoqi@0 325 CallNode *call = in->as_Call();
aoqi@0 326 if (!call->may_modify(tinst, phase)) {
aoqi@0 327 mem = call->in(TypeFunc::Memory);
aoqi@0 328 }
aoqi@0 329 mem = in->in(TypeFunc::Memory);
aoqi@0 330 } else if (in->is_MemBar()) {
aoqi@0 331 mem = in->in(TypeFunc::Memory);
aoqi@0 332 } else {
aoqi@0 333 assert(false, "unexpected projection");
aoqi@0 334 }
aoqi@0 335 } else if (mem->is_Store()) {
aoqi@0 336 const TypePtr* atype = mem->as_Store()->adr_type();
aoqi@0 337 int adr_idx = Compile::current()->get_alias_index(atype);
aoqi@0 338 if (adr_idx == alias_idx) {
aoqi@0 339 assert(atype->isa_oopptr(), "address type must be oopptr");
aoqi@0 340 int adr_offset = atype->offset();
aoqi@0 341 uint adr_iid = atype->is_oopptr()->instance_id();
aoqi@0 342 // Array elements references have the same alias_idx
aoqi@0 343 // but different offset and different instance_id.
aoqi@0 344 if (adr_offset == offset && adr_iid == alloc->_idx)
aoqi@0 345 return mem;
aoqi@0 346 } else {
aoqi@0 347 assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
aoqi@0 348 }
aoqi@0 349 mem = mem->in(MemNode::Memory);
aoqi@0 350 } else if (mem->is_ClearArray()) {
aoqi@0 351 if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) {
aoqi@0 352 // Can not bypass initialization of the instance
aoqi@0 353 // we are looking.
aoqi@0 354 debug_only(intptr_t offset;)
aoqi@0 355 assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity");
aoqi@0 356 InitializeNode* init = alloc->as_Allocate()->initialization();
aoqi@0 357 // We are looking for stored value, return Initialize node
aoqi@0 358 // or memory edge from Allocate node.
aoqi@0 359 if (init != NULL)
aoqi@0 360 return init;
aoqi@0 361 else
aoqi@0 362 return alloc->in(TypeFunc::Memory); // It will produce zero value (see callers).
aoqi@0 363 }
aoqi@0 364 // Otherwise skip it (the call updated 'mem' value).
aoqi@0 365 } else if (mem->Opcode() == Op_SCMemProj) {
aoqi@0 366 mem = mem->in(0);
aoqi@0 367 Node* adr = NULL;
aoqi@0 368 if (mem->is_LoadStore()) {
aoqi@0 369 adr = mem->in(MemNode::Address);
aoqi@0 370 } else {
aoqi@0 371 assert(mem->Opcode() == Op_EncodeISOArray, "sanity");
aoqi@0 372 adr = mem->in(3); // Destination array
aoqi@0 373 }
aoqi@0 374 const TypePtr* atype = adr->bottom_type()->is_ptr();
aoqi@0 375 int adr_idx = Compile::current()->get_alias_index(atype);
aoqi@0 376 if (adr_idx == alias_idx) {
aoqi@0 377 assert(false, "Object is not scalar replaceable if a LoadStore node access its field");
aoqi@0 378 return NULL;
aoqi@0 379 }
aoqi@0 380 mem = mem->in(MemNode::Memory);
aoqi@0 381 } else {
aoqi@0 382 return mem;
aoqi@0 383 }
aoqi@0 384 assert(mem != orig_mem, "dead memory loop");
aoqi@0 385 }
aoqi@0 386 }
aoqi@0 387
aoqi@0 388 //
aoqi@0 389 // Given a Memory Phi, compute a value Phi containing the values from stores
aoqi@0 390 // on the input paths.
aoqi@0 391 // Note: this function is recursive, its depth is limied by the "level" argument
aoqi@0 392 // Returns the computed Phi, or NULL if it cannot compute it.
aoqi@0 393 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, Node *alloc, Node_Stack *value_phis, int level) {
aoqi@0 394 assert(mem->is_Phi(), "sanity");
aoqi@0 395 int alias_idx = C->get_alias_index(adr_t);
aoqi@0 396 int offset = adr_t->offset();
aoqi@0 397 int instance_id = adr_t->instance_id();
aoqi@0 398
aoqi@0 399 // Check if an appropriate value phi already exists.
aoqi@0 400 Node* region = mem->in(0);
aoqi@0 401 for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
aoqi@0 402 Node* phi = region->fast_out(k);
aoqi@0 403 if (phi->is_Phi() && phi != mem &&
poonam@8646 404 phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) {
aoqi@0 405 return phi;
aoqi@0 406 }
aoqi@0 407 }
aoqi@0 408 // Check if an appropriate new value phi already exists.
aoqi@0 409 Node* new_phi = value_phis->find(mem->_idx);
aoqi@0 410 if (new_phi != NULL)
aoqi@0 411 return new_phi;
aoqi@0 412
aoqi@0 413 if (level <= 0) {
aoqi@0 414 return NULL; // Give up: phi tree too deep
aoqi@0 415 }
aoqi@0 416 Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
aoqi@0 417 Node *alloc_mem = alloc->in(TypeFunc::Memory);
aoqi@0 418
aoqi@0 419 uint length = mem->req();
aoqi@0 420 GrowableArray <Node *> values(length, length, NULL, false);
aoqi@0 421
aoqi@0 422 // create a new Phi for the value
poonam@8646 423 PhiNode *phi = new (C) PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset);
aoqi@0 424 transform_later(phi);
aoqi@0 425 value_phis->push(phi, mem->_idx);
aoqi@0 426
aoqi@0 427 for (uint j = 1; j < length; j++) {
aoqi@0 428 Node *in = mem->in(j);
aoqi@0 429 if (in == NULL || in->is_top()) {
aoqi@0 430 values.at_put(j, in);
aoqi@0 431 } else {
aoqi@0 432 Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn);
aoqi@0 433 if (val == start_mem || val == alloc_mem) {
aoqi@0 434 // hit a sentinel, return appropriate 0 value
aoqi@0 435 values.at_put(j, _igvn.zerocon(ft));
aoqi@0 436 continue;
aoqi@0 437 }
aoqi@0 438 if (val->is_Initialize()) {
aoqi@0 439 val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
aoqi@0 440 }
aoqi@0 441 if (val == NULL) {
aoqi@0 442 return NULL; // can't find a value on this path
aoqi@0 443 }
aoqi@0 444 if (val == mem) {
aoqi@0 445 values.at_put(j, mem);
aoqi@0 446 } else if (val->is_Store()) {
aoqi@0 447 values.at_put(j, val->in(MemNode::ValueIn));
aoqi@0 448 } else if(val->is_Proj() && val->in(0) == alloc) {
aoqi@0 449 values.at_put(j, _igvn.zerocon(ft));
aoqi@0 450 } else if (val->is_Phi()) {
aoqi@0 451 val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1);
aoqi@0 452 if (val == NULL) {
aoqi@0 453 return NULL;
aoqi@0 454 }
aoqi@0 455 values.at_put(j, val);
aoqi@0 456 } else if (val->Opcode() == Op_SCMemProj) {
aoqi@0 457 assert(val->in(0)->is_LoadStore() || val->in(0)->Opcode() == Op_EncodeISOArray, "sanity");
aoqi@0 458 assert(false, "Object is not scalar replaceable if a LoadStore node access its field");
aoqi@0 459 return NULL;
aoqi@0 460 } else {
aoqi@0 461 #ifdef ASSERT
aoqi@0 462 val->dump();
aoqi@0 463 assert(false, "unknown node on this path");
aoqi@0 464 #endif
aoqi@0 465 return NULL; // unknown node on this path
aoqi@0 466 }
aoqi@0 467 }
aoqi@0 468 }
aoqi@0 469 // Set Phi's inputs
aoqi@0 470 for (uint j = 1; j < length; j++) {
aoqi@0 471 if (values.at(j) == mem) {
aoqi@0 472 phi->init_req(j, phi);
aoqi@0 473 } else {
aoqi@0 474 phi->init_req(j, values.at(j));
aoqi@0 475 }
aoqi@0 476 }
aoqi@0 477 return phi;
aoqi@0 478 }
aoqi@0 479
aoqi@0 480 // Search the last value stored into the object's field.
aoqi@0 481 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, Node *alloc) {
aoqi@0 482 assert(adr_t->is_known_instance_field(), "instance required");
aoqi@0 483 int instance_id = adr_t->instance_id();
aoqi@0 484 assert((uint)instance_id == alloc->_idx, "wrong allocation");
aoqi@0 485
aoqi@0 486 int alias_idx = C->get_alias_index(adr_t);
aoqi@0 487 int offset = adr_t->offset();
aoqi@0 488 Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
aoqi@0 489 Node *alloc_ctrl = alloc->in(TypeFunc::Control);
aoqi@0 490 Node *alloc_mem = alloc->in(TypeFunc::Memory);
aoqi@0 491 Arena *a = Thread::current()->resource_area();
aoqi@0 492 VectorSet visited(a);
aoqi@0 493
aoqi@0 494
aoqi@0 495 bool done = sfpt_mem == alloc_mem;
aoqi@0 496 Node *mem = sfpt_mem;
aoqi@0 497 while (!done) {
aoqi@0 498 if (visited.test_set(mem->_idx)) {
aoqi@0 499 return NULL; // found a loop, give up
aoqi@0 500 }
aoqi@0 501 mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn);
aoqi@0 502 if (mem == start_mem || mem == alloc_mem) {
aoqi@0 503 done = true; // hit a sentinel, return appropriate 0 value
aoqi@0 504 } else if (mem->is_Initialize()) {
aoqi@0 505 mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
aoqi@0 506 if (mem == NULL) {
aoqi@0 507 done = true; // Something go wrong.
aoqi@0 508 } else if (mem->is_Store()) {
aoqi@0 509 const TypePtr* atype = mem->as_Store()->adr_type();
aoqi@0 510 assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
aoqi@0 511 done = true;
aoqi@0 512 }
aoqi@0 513 } else if (mem->is_Store()) {
aoqi@0 514 const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
aoqi@0 515 assert(atype != NULL, "address type must be oopptr");
aoqi@0 516 assert(C->get_alias_index(atype) == alias_idx &&
aoqi@0 517 atype->is_known_instance_field() && atype->offset() == offset &&
aoqi@0 518 atype->instance_id() == instance_id, "store is correct memory slice");
aoqi@0 519 done = true;
aoqi@0 520 } else if (mem->is_Phi()) {
aoqi@0 521 // try to find a phi's unique input
aoqi@0 522 Node *unique_input = NULL;
aoqi@0 523 Node *top = C->top();
aoqi@0 524 for (uint i = 1; i < mem->req(); i++) {
aoqi@0 525 Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn);
aoqi@0 526 if (n == NULL || n == top || n == mem) {
aoqi@0 527 continue;
aoqi@0 528 } else if (unique_input == NULL) {
aoqi@0 529 unique_input = n;
aoqi@0 530 } else if (unique_input != n) {
aoqi@0 531 unique_input = top;
aoqi@0 532 break;
aoqi@0 533 }
aoqi@0 534 }
aoqi@0 535 if (unique_input != NULL && unique_input != top) {
aoqi@0 536 mem = unique_input;
aoqi@0 537 } else {
aoqi@0 538 done = true;
aoqi@0 539 }
aoqi@0 540 } else {
aoqi@0 541 assert(false, "unexpected node");
aoqi@0 542 }
aoqi@0 543 }
aoqi@0 544 if (mem != NULL) {
aoqi@0 545 if (mem == start_mem || mem == alloc_mem) {
aoqi@0 546 // hit a sentinel, return appropriate 0 value
aoqi@0 547 return _igvn.zerocon(ft);
aoqi@0 548 } else if (mem->is_Store()) {
aoqi@0 549 return mem->in(MemNode::ValueIn);
aoqi@0 550 } else if (mem->is_Phi()) {
aoqi@0 551 // attempt to produce a Phi reflecting the values on the input paths of the Phi
aoqi@0 552 Node_Stack value_phis(a, 8);
aoqi@0 553 Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit);
aoqi@0 554 if (phi != NULL) {
aoqi@0 555 return phi;
aoqi@0 556 } else {
aoqi@0 557 // Kill all new Phis
aoqi@0 558 while(value_phis.is_nonempty()) {
aoqi@0 559 Node* n = value_phis.node();
aoqi@0 560 _igvn.replace_node(n, C->top());
aoqi@0 561 value_phis.pop();
aoqi@0 562 }
aoqi@0 563 }
aoqi@0 564 }
aoqi@0 565 }
aoqi@0 566 // Something go wrong.
aoqi@0 567 return NULL;
aoqi@0 568 }
aoqi@0 569
aoqi@0 570 // Check the possibility of scalar replacement.
aoqi@0 571 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
aoqi@0 572 // Scan the uses of the allocation to check for anything that would
aoqi@0 573 // prevent us from eliminating it.
aoqi@0 574 NOT_PRODUCT( const char* fail_eliminate = NULL; )
aoqi@0 575 DEBUG_ONLY( Node* disq_node = NULL; )
aoqi@0 576 bool can_eliminate = true;
aoqi@0 577
aoqi@0 578 Node* res = alloc->result_cast();
aoqi@0 579 const TypeOopPtr* res_type = NULL;
aoqi@0 580 if (res == NULL) {
aoqi@0 581 // All users were eliminated.
aoqi@0 582 } else if (!res->is_CheckCastPP()) {
aoqi@0 583 NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
aoqi@0 584 can_eliminate = false;
aoqi@0 585 } else {
aoqi@0 586 res_type = _igvn.type(res)->isa_oopptr();
aoqi@0 587 if (res_type == NULL) {
aoqi@0 588 NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
aoqi@0 589 can_eliminate = false;
aoqi@0 590 } else if (res_type->isa_aryptr()) {
aoqi@0 591 int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
aoqi@0 592 if (length < 0) {
aoqi@0 593 NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
aoqi@0 594 can_eliminate = false;
aoqi@0 595 }
aoqi@0 596 }
aoqi@0 597 }
aoqi@0 598
aoqi@0 599 if (can_eliminate && res != NULL) {
aoqi@0 600 for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
aoqi@0 601 j < jmax && can_eliminate; j++) {
aoqi@0 602 Node* use = res->fast_out(j);
aoqi@0 603
aoqi@0 604 if (use->is_AddP()) {
aoqi@0 605 const TypePtr* addp_type = _igvn.type(use)->is_ptr();
aoqi@0 606 int offset = addp_type->offset();
aoqi@0 607
aoqi@0 608 if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
aoqi@0 609 NOT_PRODUCT(fail_eliminate = "Undefined field referrence";)
aoqi@0 610 can_eliminate = false;
aoqi@0 611 break;
aoqi@0 612 }
aoqi@0 613 for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
aoqi@0 614 k < kmax && can_eliminate; k++) {
aoqi@0 615 Node* n = use->fast_out(k);
aoqi@0 616 if (!n->is_Store() && n->Opcode() != Op_CastP2X) {
aoqi@0 617 DEBUG_ONLY(disq_node = n;)
aoqi@0 618 if (n->is_Load() || n->is_LoadStore()) {
aoqi@0 619 NOT_PRODUCT(fail_eliminate = "Field load";)
aoqi@0 620 } else {
aoqi@0 621 NOT_PRODUCT(fail_eliminate = "Not store field referrence";)
aoqi@0 622 }
aoqi@0 623 can_eliminate = false;
aoqi@0 624 }
aoqi@0 625 }
aoqi@0 626 } else if (use->is_SafePoint()) {
aoqi@0 627 SafePointNode* sfpt = use->as_SafePoint();
aoqi@0 628 if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
aoqi@0 629 // Object is passed as argument.
aoqi@0 630 DEBUG_ONLY(disq_node = use;)
aoqi@0 631 NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
aoqi@0 632 can_eliminate = false;
aoqi@0 633 }
aoqi@0 634 Node* sfptMem = sfpt->memory();
aoqi@0 635 if (sfptMem == NULL || sfptMem->is_top()) {
aoqi@0 636 DEBUG_ONLY(disq_node = use;)
aoqi@0 637 NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
aoqi@0 638 can_eliminate = false;
aoqi@0 639 } else {
aoqi@0 640 safepoints.append_if_missing(sfpt);
aoqi@0 641 }
aoqi@0 642 } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
aoqi@0 643 if (use->is_Phi()) {
aoqi@0 644 if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
aoqi@0 645 NOT_PRODUCT(fail_eliminate = "Object is return value";)
aoqi@0 646 } else {
aoqi@0 647 NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
aoqi@0 648 }
aoqi@0 649 DEBUG_ONLY(disq_node = use;)
aoqi@0 650 } else {
aoqi@0 651 if (use->Opcode() == Op_Return) {
aoqi@0 652 NOT_PRODUCT(fail_eliminate = "Object is return value";)
aoqi@0 653 }else {
aoqi@0 654 NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
aoqi@0 655 }
aoqi@0 656 DEBUG_ONLY(disq_node = use;)
aoqi@0 657 }
aoqi@0 658 can_eliminate = false;
aoqi@0 659 }
aoqi@0 660 }
aoqi@0 661 }
aoqi@0 662
aoqi@0 663 #ifndef PRODUCT
aoqi@0 664 if (PrintEliminateAllocations) {
aoqi@0 665 if (can_eliminate) {
aoqi@0 666 tty->print("Scalar ");
aoqi@0 667 if (res == NULL)
aoqi@0 668 alloc->dump();
aoqi@0 669 else
aoqi@0 670 res->dump();
aoqi@0 671 } else if (alloc->_is_scalar_replaceable) {
aoqi@0 672 tty->print("NotScalar (%s)", fail_eliminate);
aoqi@0 673 if (res == NULL)
aoqi@0 674 alloc->dump();
aoqi@0 675 else
aoqi@0 676 res->dump();
aoqi@0 677 #ifdef ASSERT
aoqi@0 678 if (disq_node != NULL) {
aoqi@0 679 tty->print(" >>>> ");
aoqi@0 680 disq_node->dump();
aoqi@0 681 }
aoqi@0 682 #endif /*ASSERT*/
aoqi@0 683 }
aoqi@0 684 }
aoqi@0 685 #endif
aoqi@0 686 return can_eliminate;
aoqi@0 687 }
aoqi@0 688
aoqi@0 689 // Do scalar replacement.
aoqi@0 690 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
aoqi@0 691 GrowableArray <SafePointNode *> safepoints_done;
aoqi@0 692
aoqi@0 693 ciKlass* klass = NULL;
aoqi@0 694 ciInstanceKlass* iklass = NULL;
aoqi@0 695 int nfields = 0;
csahu@8316 696 int array_base = 0;
csahu@8316 697 int element_size = 0;
csahu@8316 698 BasicType basic_elem_type = T_ILLEGAL;
csahu@8316 699 ciType* elem_type = NULL;
aoqi@0 700
aoqi@0 701 Node* res = alloc->result_cast();
roland@7166 702 assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result");
aoqi@0 703 const TypeOopPtr* res_type = NULL;
aoqi@0 704 if (res != NULL) { // Could be NULL when there are no users
aoqi@0 705 res_type = _igvn.type(res)->isa_oopptr();
aoqi@0 706 }
aoqi@0 707
aoqi@0 708 if (res != NULL) {
aoqi@0 709 klass = res_type->klass();
aoqi@0 710 if (res_type->isa_instptr()) {
aoqi@0 711 // find the fields of the class which will be needed for safepoint debug information
aoqi@0 712 assert(klass->is_instance_klass(), "must be an instance klass.");
aoqi@0 713 iklass = klass->as_instance_klass();
aoqi@0 714 nfields = iklass->nof_nonstatic_fields();
aoqi@0 715 } else {
aoqi@0 716 // find the array's elements which will be needed for safepoint debug information
aoqi@0 717 nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
aoqi@0 718 assert(klass->is_array_klass() && nfields >= 0, "must be an array klass.");
aoqi@0 719 elem_type = klass->as_array_klass()->element_type();
aoqi@0 720 basic_elem_type = elem_type->basic_type();
aoqi@0 721 array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
aoqi@0 722 element_size = type2aelembytes(basic_elem_type);
aoqi@0 723 }
aoqi@0 724 }
aoqi@0 725 //
aoqi@0 726 // Process the safepoint uses
aoqi@0 727 //
aoqi@0 728 while (safepoints.length() > 0) {
aoqi@0 729 SafePointNode* sfpt = safepoints.pop();
aoqi@0 730 Node* mem = sfpt->memory();
aoqi@0 731 assert(sfpt->jvms() != NULL, "missed JVMS");
aoqi@0 732 // Fields of scalar objs are referenced only at the end
aoqi@0 733 // of regular debuginfo at the last (youngest) JVMS.
aoqi@0 734 // Record relative start index.
aoqi@0 735 uint first_ind = (sfpt->req() - sfpt->jvms()->scloff());
aoqi@0 736 SafePointScalarObjectNode* sobj = new (C) SafePointScalarObjectNode(res_type,
aoqi@0 737 #ifdef ASSERT
aoqi@0 738 alloc,
aoqi@0 739 #endif
aoqi@0 740 first_ind, nfields);
aoqi@0 741 sobj->init_req(0, C->root());
aoqi@0 742 transform_later(sobj);
aoqi@0 743
aoqi@0 744 // Scan object's fields adding an input to the safepoint for each field.
aoqi@0 745 for (int j = 0; j < nfields; j++) {
aoqi@0 746 intptr_t offset;
aoqi@0 747 ciField* field = NULL;
aoqi@0 748 if (iklass != NULL) {
aoqi@0 749 field = iklass->nonstatic_field_at(j);
aoqi@0 750 offset = field->offset();
aoqi@0 751 elem_type = field->type();
aoqi@0 752 basic_elem_type = field->layout_type();
aoqi@0 753 } else {
aoqi@0 754 offset = array_base + j * (intptr_t)element_size;
aoqi@0 755 }
aoqi@0 756
aoqi@0 757 const Type *field_type;
aoqi@0 758 // The next code is taken from Parse::do_get_xxx().
aoqi@0 759 if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) {
aoqi@0 760 if (!elem_type->is_loaded()) {
aoqi@0 761 field_type = TypeInstPtr::BOTTOM;
aoqi@0 762 } else if (field != NULL && field->is_constant() && field->is_static()) {
aoqi@0 763 // This can happen if the constant oop is non-perm.
aoqi@0 764 ciObject* con = field->constant_value().as_object();
aoqi@0 765 // Do not "join" in the previous type; it doesn't add value,
aoqi@0 766 // and may yield a vacuous result if the field is of interface type.
aoqi@0 767 field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
aoqi@0 768 assert(field_type != NULL, "field singleton type must be consistent");
aoqi@0 769 } else {
aoqi@0 770 field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
aoqi@0 771 }
aoqi@0 772 if (UseCompressedOops) {
aoqi@0 773 field_type = field_type->make_narrowoop();
aoqi@0 774 basic_elem_type = T_NARROWOOP;
aoqi@0 775 }
aoqi@0 776 } else {
aoqi@0 777 field_type = Type::get_const_basic_type(basic_elem_type);
aoqi@0 778 }
aoqi@0 779
aoqi@0 780 const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
aoqi@0 781
aoqi@0 782 Node *field_val = value_from_mem(mem, basic_elem_type, field_type, field_addr_type, alloc);
aoqi@0 783 if (field_val == NULL) {
aoqi@0 784 // We weren't able to find a value for this field,
aoqi@0 785 // give up on eliminating this allocation.
aoqi@0 786
aoqi@0 787 // Remove any extra entries we added to the safepoint.
aoqi@0 788 uint last = sfpt->req() - 1;
aoqi@0 789 for (int k = 0; k < j; k++) {
aoqi@0 790 sfpt->del_req(last--);
aoqi@0 791 }
aoqi@0 792 // rollback processed safepoints
aoqi@0 793 while (safepoints_done.length() > 0) {
aoqi@0 794 SafePointNode* sfpt_done = safepoints_done.pop();
aoqi@0 795 // remove any extra entries we added to the safepoint
aoqi@0 796 last = sfpt_done->req() - 1;
aoqi@0 797 for (int k = 0; k < nfields; k++) {
aoqi@0 798 sfpt_done->del_req(last--);
aoqi@0 799 }
aoqi@0 800 JVMState *jvms = sfpt_done->jvms();
aoqi@0 801 jvms->set_endoff(sfpt_done->req());
aoqi@0 802 // Now make a pass over the debug information replacing any references
aoqi@0 803 // to SafePointScalarObjectNode with the allocated object.
aoqi@0 804 int start = jvms->debug_start();
aoqi@0 805 int end = jvms->debug_end();
aoqi@0 806 for (int i = start; i < end; i++) {
aoqi@0 807 if (sfpt_done->in(i)->is_SafePointScalarObject()) {
aoqi@0 808 SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject();
aoqi@0 809 if (scobj->first_index(jvms) == sfpt_done->req() &&
aoqi@0 810 scobj->n_fields() == (uint)nfields) {
aoqi@0 811 assert(scobj->alloc() == alloc, "sanity");
aoqi@0 812 sfpt_done->set_req(i, res);
aoqi@0 813 }
aoqi@0 814 }
aoqi@0 815 }
aoqi@0 816 }
aoqi@0 817 #ifndef PRODUCT
aoqi@0 818 if (PrintEliminateAllocations) {
aoqi@0 819 if (field != NULL) {
aoqi@0 820 tty->print("=== At SafePoint node %d can't find value of Field: ",
aoqi@0 821 sfpt->_idx);
aoqi@0 822 field->print();
aoqi@0 823 int field_idx = C->get_alias_index(field_addr_type);
aoqi@0 824 tty->print(" (alias_idx=%d)", field_idx);
aoqi@0 825 } else { // Array's element
aoqi@0 826 tty->print("=== At SafePoint node %d can't find value of array element [%d]",
aoqi@0 827 sfpt->_idx, j);
aoqi@0 828 }
aoqi@0 829 tty->print(", which prevents elimination of: ");
aoqi@0 830 if (res == NULL)
aoqi@0 831 alloc->dump();
aoqi@0 832 else
aoqi@0 833 res->dump();
aoqi@0 834 }
aoqi@0 835 #endif
aoqi@0 836 return false;
aoqi@0 837 }
aoqi@0 838 if (UseCompressedOops && field_type->isa_narrowoop()) {
aoqi@0 839 // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
aoqi@0 840 // to be able scalar replace the allocation.
aoqi@0 841 if (field_val->is_EncodeP()) {
aoqi@0 842 field_val = field_val->in(1);
aoqi@0 843 } else {
aoqi@0 844 field_val = transform_later(new (C) DecodeNNode(field_val, field_val->get_ptr_type()));
aoqi@0 845 }
aoqi@0 846 }
aoqi@0 847 sfpt->add_req(field_val);
aoqi@0 848 }
aoqi@0 849 JVMState *jvms = sfpt->jvms();
aoqi@0 850 jvms->set_endoff(sfpt->req());
aoqi@0 851 // Now make a pass over the debug information replacing any references
aoqi@0 852 // to the allocated object with "sobj"
aoqi@0 853 int start = jvms->debug_start();
aoqi@0 854 int end = jvms->debug_end();
aoqi@0 855 sfpt->replace_edges_in_range(res, sobj, start, end);
aoqi@0 856 safepoints_done.append_if_missing(sfpt); // keep it for rollback
aoqi@0 857 }
aoqi@0 858 return true;
aoqi@0 859 }
aoqi@0 860
aoqi@0 861 // Process users of eliminated allocation.
aoqi@0 862 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) {
aoqi@0 863 Node* res = alloc->result_cast();
aoqi@0 864 if (res != NULL) {
aoqi@0 865 for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
aoqi@0 866 Node *use = res->last_out(j);
aoqi@0 867 uint oc1 = res->outcnt();
aoqi@0 868
aoqi@0 869 if (use->is_AddP()) {
aoqi@0 870 for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
aoqi@0 871 Node *n = use->last_out(k);
aoqi@0 872 uint oc2 = use->outcnt();
aoqi@0 873 if (n->is_Store()) {
aoqi@0 874 #ifdef ASSERT
aoqi@0 875 // Verify that there is no dependent MemBarVolatile nodes,
aoqi@0 876 // they should be removed during IGVN, see MemBarNode::Ideal().
aoqi@0 877 for (DUIterator_Fast pmax, p = n->fast_outs(pmax);
aoqi@0 878 p < pmax; p++) {
aoqi@0 879 Node* mb = n->fast_out(p);
aoqi@0 880 assert(mb->is_Initialize() || !mb->is_MemBar() ||
aoqi@0 881 mb->req() <= MemBarNode::Precedent ||
aoqi@0 882 mb->in(MemBarNode::Precedent) != n,
aoqi@0 883 "MemBarVolatile should be eliminated for non-escaping object");
aoqi@0 884 }
aoqi@0 885 #endif
aoqi@0 886 _igvn.replace_node(n, n->in(MemNode::Memory));
aoqi@0 887 } else {
aoqi@0 888 eliminate_card_mark(n);
aoqi@0 889 }
aoqi@0 890 k -= (oc2 - use->outcnt());
aoqi@0 891 }
aoqi@0 892 } else {
aoqi@0 893 eliminate_card_mark(use);
aoqi@0 894 }
aoqi@0 895 j -= (oc1 - res->outcnt());
aoqi@0 896 }
aoqi@0 897 assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
aoqi@0 898 _igvn.remove_dead_node(res);
aoqi@0 899 }
aoqi@0 900
aoqi@0 901 //
aoqi@0 902 // Process other users of allocation's projections
aoqi@0 903 //
aoqi@0 904 if (_resproj != NULL && _resproj->outcnt() != 0) {
aoqi@0 905 // First disconnect stores captured by Initialize node.
aoqi@0 906 // If Initialize node is eliminated first in the following code,
aoqi@0 907 // it will kill such stores and DUIterator_Last will assert.
aoqi@0 908 for (DUIterator_Fast jmax, j = _resproj->fast_outs(jmax); j < jmax; j++) {
aoqi@0 909 Node *use = _resproj->fast_out(j);
aoqi@0 910 if (use->is_AddP()) {
aoqi@0 911 // raw memory addresses used only by the initialization
aoqi@0 912 _igvn.replace_node(use, C->top());
aoqi@0 913 --j; --jmax;
aoqi@0 914 }
aoqi@0 915 }
aoqi@0 916 for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) {
aoqi@0 917 Node *use = _resproj->last_out(j);
aoqi@0 918 uint oc1 = _resproj->outcnt();
aoqi@0 919 if (use->is_Initialize()) {
aoqi@0 920 // Eliminate Initialize node.
aoqi@0 921 InitializeNode *init = use->as_Initialize();
aoqi@0 922 assert(init->outcnt() <= 2, "only a control and memory projection expected");
aoqi@0 923 Node *ctrl_proj = init->proj_out(TypeFunc::Control);
aoqi@0 924 if (ctrl_proj != NULL) {
aoqi@0 925 assert(init->in(TypeFunc::Control) == _fallthroughcatchproj, "allocation control projection");
aoqi@0 926 _igvn.replace_node(ctrl_proj, _fallthroughcatchproj);
aoqi@0 927 }
aoqi@0 928 Node *mem_proj = init->proj_out(TypeFunc::Memory);
aoqi@0 929 if (mem_proj != NULL) {
aoqi@0 930 Node *mem = init->in(TypeFunc::Memory);
aoqi@0 931 #ifdef ASSERT
aoqi@0 932 if (mem->is_MergeMem()) {
aoqi@0 933 assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection");
aoqi@0 934 } else {
aoqi@0 935 assert(mem == _memproj_fallthrough, "allocation memory projection");
aoqi@0 936 }
aoqi@0 937 #endif
aoqi@0 938 _igvn.replace_node(mem_proj, mem);
aoqi@0 939 }
aoqi@0 940 } else {
aoqi@0 941 assert(false, "only Initialize or AddP expected");
aoqi@0 942 }
aoqi@0 943 j -= (oc1 - _resproj->outcnt());
aoqi@0 944 }
aoqi@0 945 }
aoqi@0 946 if (_fallthroughcatchproj != NULL) {
aoqi@0 947 _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control));
aoqi@0 948 }
aoqi@0 949 if (_memproj_fallthrough != NULL) {
aoqi@0 950 _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory));
aoqi@0 951 }
aoqi@0 952 if (_memproj_catchall != NULL) {
aoqi@0 953 _igvn.replace_node(_memproj_catchall, C->top());
aoqi@0 954 }
aoqi@0 955 if (_ioproj_fallthrough != NULL) {
aoqi@0 956 _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O));
aoqi@0 957 }
aoqi@0 958 if (_ioproj_catchall != NULL) {
aoqi@0 959 _igvn.replace_node(_ioproj_catchall, C->top());
aoqi@0 960 }
aoqi@0 961 if (_catchallcatchproj != NULL) {
aoqi@0 962 _igvn.replace_node(_catchallcatchproj, C->top());
aoqi@0 963 }
aoqi@0 964 }
aoqi@0 965
aoqi@0 966 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
roland@7419 967 // Don't do scalar replacement if the frame can be popped by JVMTI:
roland@7419 968 // if reallocation fails during deoptimization we'll pop all
roland@7419 969 // interpreter frames for this compiled frame and that won't play
roland@7419 970 // nice with JVMTI popframe.
roland@7419 971 if (!EliminateAllocations || JvmtiExport::can_pop_frame() || !alloc->_is_non_escaping) {
aoqi@0 972 return false;
aoqi@0 973 }
aoqi@0 974 Node* klass = alloc->in(AllocateNode::KlassNode);
aoqi@0 975 const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr();
aoqi@0 976 Node* res = alloc->result_cast();
aoqi@0 977 // Eliminate boxing allocations which are not used
aoqi@0 978 // regardless scalar replacable status.
aoqi@0 979 bool boxing_alloc = C->eliminate_boxing() &&
aoqi@0 980 tklass->klass()->is_instance_klass() &&
aoqi@0 981 tklass->klass()->as_instance_klass()->is_box_klass();
aoqi@0 982 if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) {
aoqi@0 983 return false;
aoqi@0 984 }
aoqi@0 985
aoqi@0 986 extract_call_projections(alloc);
aoqi@0 987
aoqi@0 988 GrowableArray <SafePointNode *> safepoints;
aoqi@0 989 if (!can_eliminate_allocation(alloc, safepoints)) {
aoqi@0 990 return false;
aoqi@0 991 }
aoqi@0 992
aoqi@0 993 if (!alloc->_is_scalar_replaceable) {
aoqi@0 994 assert(res == NULL, "sanity");
aoqi@0 995 // We can only eliminate allocation if all debug info references
aoqi@0 996 // are already replaced with SafePointScalarObject because
aoqi@0 997 // we can't search for a fields value without instance_id.
aoqi@0 998 if (safepoints.length() > 0) {
aoqi@0 999 return false;
aoqi@0 1000 }
aoqi@0 1001 }
aoqi@0 1002
aoqi@0 1003 if (!scalar_replacement(alloc, safepoints)) {
aoqi@0 1004 return false;
aoqi@0 1005 }
aoqi@0 1006
aoqi@0 1007 CompileLog* log = C->log();
aoqi@0 1008 if (log != NULL) {
aoqi@0 1009 log->head("eliminate_allocation type='%d'",
aoqi@0 1010 log->identify(tklass->klass()));
aoqi@0 1011 JVMState* p = alloc->jvms();
aoqi@0 1012 while (p != NULL) {
aoqi@0 1013 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
aoqi@0 1014 p = p->caller();
aoqi@0 1015 }
aoqi@0 1016 log->tail("eliminate_allocation");
aoqi@0 1017 }
aoqi@0 1018
aoqi@0 1019 process_users_of_allocation(alloc);
aoqi@0 1020
aoqi@0 1021 #ifndef PRODUCT
aoqi@0 1022 if (PrintEliminateAllocations) {
aoqi@0 1023 if (alloc->is_AllocateArray())
aoqi@0 1024 tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
aoqi@0 1025 else
aoqi@0 1026 tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
aoqi@0 1027 }
aoqi@0 1028 #endif
aoqi@0 1029
aoqi@0 1030 return true;
aoqi@0 1031 }
aoqi@0 1032
aoqi@0 1033 bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) {
aoqi@0 1034 // EA should remove all uses of non-escaping boxing node.
aoqi@0 1035 if (!C->eliminate_boxing() || boxing->proj_out(TypeFunc::Parms) != NULL) {
aoqi@0 1036 return false;
aoqi@0 1037 }
aoqi@0 1038
roland@7166 1039 assert(boxing->result_cast() == NULL, "unexpected boxing node result");
roland@7166 1040
aoqi@0 1041 extract_call_projections(boxing);
aoqi@0 1042
aoqi@0 1043 const TypeTuple* r = boxing->tf()->range();
aoqi@0 1044 assert(r->cnt() > TypeFunc::Parms, "sanity");
aoqi@0 1045 const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr();
aoqi@0 1046 assert(t != NULL, "sanity");
aoqi@0 1047
aoqi@0 1048 CompileLog* log = C->log();
aoqi@0 1049 if (log != NULL) {
aoqi@0 1050 log->head("eliminate_boxing type='%d'",
aoqi@0 1051 log->identify(t->klass()));
aoqi@0 1052 JVMState* p = boxing->jvms();
aoqi@0 1053 while (p != NULL) {
aoqi@0 1054 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
aoqi@0 1055 p = p->caller();
aoqi@0 1056 }
aoqi@0 1057 log->tail("eliminate_boxing");
aoqi@0 1058 }
aoqi@0 1059
aoqi@0 1060 process_users_of_allocation(boxing);
aoqi@0 1061
aoqi@0 1062 #ifndef PRODUCT
aoqi@0 1063 if (PrintEliminateAllocations) {
aoqi@0 1064 tty->print("++++ Eliminated: %d ", boxing->_idx);
aoqi@0 1065 boxing->method()->print_short_name(tty);
aoqi@0 1066 tty->cr();
aoqi@0 1067 }
aoqi@0 1068 #endif
aoqi@0 1069
aoqi@0 1070 return true;
aoqi@0 1071 }
aoqi@0 1072
aoqi@0 1073 //---------------------------set_eden_pointers-------------------------
aoqi@0 1074 void PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) {
aoqi@0 1075 if (UseTLAB) { // Private allocation: load from TLS
aoqi@0 1076 Node* thread = transform_later(new (C) ThreadLocalNode());
aoqi@0 1077 int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset());
aoqi@0 1078 int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset());
aoqi@0 1079 eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset);
aoqi@0 1080 eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset);
aoqi@0 1081 } else { // Shared allocation: load from globals
aoqi@0 1082 CollectedHeap* ch = Universe::heap();
aoqi@0 1083 address top_adr = (address)ch->top_addr();
aoqi@0 1084 address end_adr = (address)ch->end_addr();
aoqi@0 1085 eden_top_adr = makecon(TypeRawPtr::make(top_adr));
aoqi@0 1086 eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr);
aoqi@0 1087 }
aoqi@0 1088 }
aoqi@0 1089
aoqi@0 1090
aoqi@0 1091 Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) {
aoqi@0 1092 Node* adr = basic_plus_adr(base, offset);
aoqi@0 1093 const TypePtr* adr_type = adr->bottom_type()->is_ptr();
aoqi@0 1094 Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt, MemNode::unordered);
aoqi@0 1095 transform_later(value);
aoqi@0 1096 return value;
aoqi@0 1097 }
aoqi@0 1098
aoqi@0 1099
aoqi@0 1100 Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) {
aoqi@0 1101 Node* adr = basic_plus_adr(base, offset);
aoqi@0 1102 mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt, MemNode::unordered);
aoqi@0 1103 transform_later(mem);
aoqi@0 1104 return mem;
aoqi@0 1105 }
aoqi@0 1106
aoqi@0 1107 //=============================================================================
aoqi@0 1108 //
aoqi@0 1109 // A L L O C A T I O N
aoqi@0 1110 //
aoqi@0 1111 // Allocation attempts to be fast in the case of frequent small objects.
aoqi@0 1112 // It breaks down like this:
aoqi@0 1113 //
aoqi@0 1114 // 1) Size in doublewords is computed. This is a constant for objects and
aoqi@0 1115 // variable for most arrays. Doubleword units are used to avoid size
aoqi@0 1116 // overflow of huge doubleword arrays. We need doublewords in the end for
aoqi@0 1117 // rounding.
aoqi@0 1118 //
aoqi@0 1119 // 2) Size is checked for being 'too large'. Too-large allocations will go
aoqi@0 1120 // the slow path into the VM. The slow path can throw any required
aoqi@0 1121 // exceptions, and does all the special checks for very large arrays. The
aoqi@0 1122 // size test can constant-fold away for objects. For objects with
aoqi@0 1123 // finalizers it constant-folds the otherway: you always go slow with
aoqi@0 1124 // finalizers.
aoqi@0 1125 //
aoqi@0 1126 // 3) If NOT using TLABs, this is the contended loop-back point.
aoqi@0 1127 // Load-Locked the heap top. If using TLABs normal-load the heap top.
aoqi@0 1128 //
aoqi@0 1129 // 4) Check that heap top + size*8 < max. If we fail go the slow ` route.
aoqi@0 1130 // NOTE: "top+size*8" cannot wrap the 4Gig line! Here's why: for largish
aoqi@0 1131 // "size*8" we always enter the VM, where "largish" is a constant picked small
aoqi@0 1132 // enough that there's always space between the eden max and 4Gig (old space is
aoqi@0 1133 // there so it's quite large) and large enough that the cost of entering the VM
aoqi@0 1134 // is dwarfed by the cost to initialize the space.
aoqi@0 1135 //
aoqi@0 1136 // 5) If NOT using TLABs, Store-Conditional the adjusted heap top back
aoqi@0 1137 // down. If contended, repeat at step 3. If using TLABs normal-store
aoqi@0 1138 // adjusted heap top back down; there is no contention.
aoqi@0 1139 //
aoqi@0 1140 // 6) If !ZeroTLAB then Bulk-clear the object/array. Fill in klass & mark
aoqi@0 1141 // fields.
aoqi@0 1142 //
aoqi@0 1143 // 7) Merge with the slow-path; cast the raw memory pointer to the correct
aoqi@0 1144 // oop flavor.
aoqi@0 1145 //
aoqi@0 1146 //=============================================================================
aoqi@0 1147 // FastAllocateSizeLimit value is in DOUBLEWORDS.
aoqi@0 1148 // Allocations bigger than this always go the slow route.
aoqi@0 1149 // This value must be small enough that allocation attempts that need to
aoqi@0 1150 // trigger exceptions go the slow route. Also, it must be small enough so
aoqi@0 1151 // that heap_top + size_in_bytes does not wrap around the 4Gig limit.
aoqi@0 1152 //=============================================================================j//
aoqi@0 1153 // %%% Here is an old comment from parseHelper.cpp; is it outdated?
aoqi@0 1154 // The allocator will coalesce int->oop copies away. See comment in
aoqi@0 1155 // coalesce.cpp about how this works. It depends critically on the exact
aoqi@0 1156 // code shape produced here, so if you are changing this code shape
aoqi@0 1157 // make sure the GC info for the heap-top is correct in and around the
aoqi@0 1158 // slow-path call.
aoqi@0 1159 //
aoqi@0 1160
aoqi@0 1161 void PhaseMacroExpand::expand_allocate_common(
aoqi@0 1162 AllocateNode* alloc, // allocation node to be expanded
aoqi@0 1163 Node* length, // array length for an array allocation
aoqi@0 1164 const TypeFunc* slow_call_type, // Type of slow call
aoqi@0 1165 address slow_call_address // Address of slow call
aoqi@0 1166 )
aoqi@0 1167 {
aoqi@0 1168
aoqi@0 1169 Node* ctrl = alloc->in(TypeFunc::Control);
aoqi@0 1170 Node* mem = alloc->in(TypeFunc::Memory);
aoqi@0 1171 Node* i_o = alloc->in(TypeFunc::I_O);
aoqi@0 1172 Node* size_in_bytes = alloc->in(AllocateNode::AllocSize);
aoqi@0 1173 Node* klass_node = alloc->in(AllocateNode::KlassNode);
aoqi@0 1174 Node* initial_slow_test = alloc->in(AllocateNode::InitialTest);
aoqi@0 1175
aoqi@0 1176 assert(ctrl != NULL, "must have control");
aoqi@0 1177 // We need a Region and corresponding Phi's to merge the slow-path and fast-path results.
aoqi@0 1178 // they will not be used if "always_slow" is set
aoqi@0 1179 enum { slow_result_path = 1, fast_result_path = 2 };
csahu@8316 1180 Node *result_region = NULL;
csahu@8316 1181 Node *result_phi_rawmem = NULL;
csahu@8316 1182 Node *result_phi_rawoop = NULL;
csahu@8316 1183 Node *result_phi_i_o = NULL;
aoqi@0 1184
aoqi@0 1185 // The initial slow comparison is a size check, the comparison
aoqi@0 1186 // we want to do is a BoolTest::gt
aoqi@0 1187 bool always_slow = false;
aoqi@0 1188 int tv = _igvn.find_int_con(initial_slow_test, -1);
aoqi@0 1189 if (tv >= 0) {
aoqi@0 1190 always_slow = (tv == 1);
aoqi@0 1191 initial_slow_test = NULL;
aoqi@0 1192 } else {
aoqi@0 1193 initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn);
aoqi@0 1194 }
aoqi@0 1195
aoqi@0 1196 if (C->env()->dtrace_alloc_probes() ||
aoqi@0 1197 !UseTLAB && (!Universe::heap()->supports_inline_contig_alloc() ||
aoqi@0 1198 (UseConcMarkSweepGC && CMSIncrementalMode))) {
aoqi@0 1199 // Force slow-path allocation
aoqi@0 1200 always_slow = true;
aoqi@0 1201 initial_slow_test = NULL;
aoqi@0 1202 }
aoqi@0 1203
aoqi@0 1204
aoqi@0 1205 enum { too_big_or_final_path = 1, need_gc_path = 2 };
aoqi@0 1206 Node *slow_region = NULL;
aoqi@0 1207 Node *toobig_false = ctrl;
aoqi@0 1208
aoqi@0 1209 assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent");
aoqi@0 1210 // generate the initial test if necessary
aoqi@0 1211 if (initial_slow_test != NULL ) {
aoqi@0 1212 slow_region = new (C) RegionNode(3);
aoqi@0 1213
aoqi@0 1214 // Now make the initial failure test. Usually a too-big test but
aoqi@0 1215 // might be a TRUE for finalizers or a fancy class check for
aoqi@0 1216 // newInstance0.
aoqi@0 1217 IfNode *toobig_iff = new (C) IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1218 transform_later(toobig_iff);
aoqi@0 1219 // Plug the failing-too-big test into the slow-path region
aoqi@0 1220 Node *toobig_true = new (C) IfTrueNode( toobig_iff );
aoqi@0 1221 transform_later(toobig_true);
aoqi@0 1222 slow_region ->init_req( too_big_or_final_path, toobig_true );
aoqi@0 1223 toobig_false = new (C) IfFalseNode( toobig_iff );
aoqi@0 1224 transform_later(toobig_false);
aoqi@0 1225 } else { // No initial test, just fall into next case
aoqi@0 1226 toobig_false = ctrl;
aoqi@0 1227 debug_only(slow_region = NodeSentinel);
aoqi@0 1228 }
aoqi@0 1229
aoqi@0 1230 Node *slow_mem = mem; // save the current memory state for slow path
aoqi@0 1231 // generate the fast allocation code unless we know that the initial test will always go slow
aoqi@0 1232 if (!always_slow) {
aoqi@0 1233 // Fast path modifies only raw memory.
aoqi@0 1234 if (mem->is_MergeMem()) {
aoqi@0 1235 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
aoqi@0 1236 }
aoqi@0 1237
aoqi@0 1238 Node* eden_top_adr;
aoqi@0 1239 Node* eden_end_adr;
aoqi@0 1240
aoqi@0 1241 set_eden_pointers(eden_top_adr, eden_end_adr);
aoqi@0 1242
aoqi@0 1243 // Load Eden::end. Loop invariant and hoisted.
aoqi@0 1244 //
aoqi@0 1245 // Note: We set the control input on "eden_end" and "old_eden_top" when using
aoqi@0 1246 // a TLAB to work around a bug where these values were being moved across
aoqi@0 1247 // a safepoint. These are not oops, so they cannot be include in the oop
aoqi@0 1248 // map, but they can be changed by a GC. The proper way to fix this would
aoqi@0 1249 // be to set the raw memory state when generating a SafepointNode. However
aoqi@0 1250 // this will require extensive changes to the loop optimization in order to
aoqi@0 1251 // prevent a degradation of the optimization.
aoqi@0 1252 // See comment in memnode.hpp, around line 227 in class LoadPNode.
aoqi@0 1253 Node *eden_end = make_load(ctrl, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS);
aoqi@0 1254
aoqi@0 1255 // allocate the Region and Phi nodes for the result
aoqi@0 1256 result_region = new (C) RegionNode(3);
aoqi@0 1257 result_phi_rawmem = new (C) PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 1258 result_phi_rawoop = new (C) PhiNode(result_region, TypeRawPtr::BOTTOM);
aoqi@0 1259 result_phi_i_o = new (C) PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch
aoqi@0 1260
aoqi@0 1261 // We need a Region for the loop-back contended case.
aoqi@0 1262 enum { fall_in_path = 1, contended_loopback_path = 2 };
aoqi@0 1263 Node *contended_region;
aoqi@0 1264 Node *contended_phi_rawmem;
aoqi@0 1265 if (UseTLAB) {
aoqi@0 1266 contended_region = toobig_false;
aoqi@0 1267 contended_phi_rawmem = mem;
aoqi@0 1268 } else {
aoqi@0 1269 contended_region = new (C) RegionNode(3);
aoqi@0 1270 contended_phi_rawmem = new (C) PhiNode(contended_region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 1271 // Now handle the passing-too-big test. We fall into the contended
aoqi@0 1272 // loop-back merge point.
aoqi@0 1273 contended_region ->init_req(fall_in_path, toobig_false);
aoqi@0 1274 contended_phi_rawmem->init_req(fall_in_path, mem);
aoqi@0 1275 transform_later(contended_region);
aoqi@0 1276 transform_later(contended_phi_rawmem);
aoqi@0 1277 }
aoqi@0 1278
aoqi@0 1279 // Load(-locked) the heap top.
aoqi@0 1280 // See note above concerning the control input when using a TLAB
aoqi@0 1281 Node *old_eden_top = UseTLAB
aoqi@0 1282 ? new (C) LoadPNode (ctrl, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, MemNode::unordered)
aoqi@0 1283 : new (C) LoadPLockedNode(contended_region, contended_phi_rawmem, eden_top_adr, MemNode::acquire);
aoqi@0 1284
aoqi@0 1285 transform_later(old_eden_top);
aoqi@0 1286 // Add to heap top to get a new heap top
aoqi@0 1287 Node *new_eden_top = new (C) AddPNode(top(), old_eden_top, size_in_bytes);
aoqi@0 1288 transform_later(new_eden_top);
aoqi@0 1289 // Check for needing a GC; compare against heap end
aoqi@0 1290 Node *needgc_cmp = new (C) CmpPNode(new_eden_top, eden_end);
aoqi@0 1291 transform_later(needgc_cmp);
aoqi@0 1292 Node *needgc_bol = new (C) BoolNode(needgc_cmp, BoolTest::ge);
aoqi@0 1293 transform_later(needgc_bol);
aoqi@0 1294 IfNode *needgc_iff = new (C) IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN);
aoqi@0 1295 transform_later(needgc_iff);
aoqi@0 1296
aoqi@0 1297 // Plug the failing-heap-space-need-gc test into the slow-path region
aoqi@0 1298 Node *needgc_true = new (C) IfTrueNode(needgc_iff);
aoqi@0 1299 transform_later(needgc_true);
aoqi@0 1300 if (initial_slow_test) {
aoqi@0 1301 slow_region->init_req(need_gc_path, needgc_true);
aoqi@0 1302 // This completes all paths into the slow merge point
aoqi@0 1303 transform_later(slow_region);
aoqi@0 1304 } else { // No initial slow path needed!
aoqi@0 1305 // Just fall from the need-GC path straight into the VM call.
aoqi@0 1306 slow_region = needgc_true;
aoqi@0 1307 }
aoqi@0 1308 // No need for a GC. Setup for the Store-Conditional
aoqi@0 1309 Node *needgc_false = new (C) IfFalseNode(needgc_iff);
aoqi@0 1310 transform_later(needgc_false);
aoqi@0 1311
aoqi@0 1312 // Grab regular I/O before optional prefetch may change it.
aoqi@0 1313 // Slow-path does no I/O so just set it to the original I/O.
aoqi@0 1314 result_phi_i_o->init_req(slow_result_path, i_o);
aoqi@0 1315
aoqi@0 1316 i_o = prefetch_allocation(i_o, needgc_false, contended_phi_rawmem,
aoqi@0 1317 old_eden_top, new_eden_top, length);
aoqi@0 1318
aoqi@0 1319 // Name successful fast-path variables
aoqi@0 1320 Node* fast_oop = old_eden_top;
aoqi@0 1321 Node* fast_oop_ctrl;
aoqi@0 1322 Node* fast_oop_rawmem;
aoqi@0 1323
aoqi@0 1324 // Store (-conditional) the modified eden top back down.
aoqi@0 1325 // StorePConditional produces flags for a test PLUS a modified raw
aoqi@0 1326 // memory state.
aoqi@0 1327 if (UseTLAB) {
aoqi@0 1328 Node* store_eden_top =
aoqi@0 1329 new (C) StorePNode(needgc_false, contended_phi_rawmem, eden_top_adr,
aoqi@0 1330 TypeRawPtr::BOTTOM, new_eden_top, MemNode::unordered);
aoqi@0 1331 transform_later(store_eden_top);
aoqi@0 1332 fast_oop_ctrl = needgc_false; // No contention, so this is the fast path
aoqi@0 1333 fast_oop_rawmem = store_eden_top;
aoqi@0 1334 } else {
aoqi@0 1335 Node* store_eden_top =
aoqi@0 1336 new (C) StorePConditionalNode(needgc_false, contended_phi_rawmem, eden_top_adr,
aoqi@0 1337 new_eden_top, fast_oop/*old_eden_top*/);
aoqi@0 1338 transform_later(store_eden_top);
aoqi@0 1339 Node *contention_check = new (C) BoolNode(store_eden_top, BoolTest::ne);
aoqi@0 1340 transform_later(contention_check);
aoqi@0 1341 store_eden_top = new (C) SCMemProjNode(store_eden_top);
aoqi@0 1342 transform_later(store_eden_top);
aoqi@0 1343
aoqi@0 1344 // If not using TLABs, check to see if there was contention.
aoqi@0 1345 IfNode *contention_iff = new (C) IfNode (needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1346 transform_later(contention_iff);
aoqi@0 1347 Node *contention_true = new (C) IfTrueNode(contention_iff);
aoqi@0 1348 transform_later(contention_true);
aoqi@0 1349 // If contention, loopback and try again.
aoqi@0 1350 contended_region->init_req(contended_loopback_path, contention_true);
aoqi@0 1351 contended_phi_rawmem->init_req(contended_loopback_path, store_eden_top);
aoqi@0 1352
aoqi@0 1353 // Fast-path succeeded with no contention!
aoqi@0 1354 Node *contention_false = new (C) IfFalseNode(contention_iff);
aoqi@0 1355 transform_later(contention_false);
aoqi@0 1356 fast_oop_ctrl = contention_false;
aoqi@0 1357
aoqi@0 1358 // Bump total allocated bytes for this thread
aoqi@0 1359 Node* thread = new (C) ThreadLocalNode();
aoqi@0 1360 transform_later(thread);
aoqi@0 1361 Node* alloc_bytes_adr = basic_plus_adr(top()/*not oop*/, thread,
aoqi@0 1362 in_bytes(JavaThread::allocated_bytes_offset()));
aoqi@0 1363 Node* alloc_bytes = make_load(fast_oop_ctrl, store_eden_top, alloc_bytes_adr,
aoqi@0 1364 0, TypeLong::LONG, T_LONG);
aoqi@0 1365 #ifdef _LP64
aoqi@0 1366 Node* alloc_size = size_in_bytes;
aoqi@0 1367 #else
aoqi@0 1368 Node* alloc_size = new (C) ConvI2LNode(size_in_bytes);
aoqi@0 1369 transform_later(alloc_size);
aoqi@0 1370 #endif
aoqi@0 1371 Node* new_alloc_bytes = new (C) AddLNode(alloc_bytes, alloc_size);
aoqi@0 1372 transform_later(new_alloc_bytes);
aoqi@0 1373 fast_oop_rawmem = make_store(fast_oop_ctrl, store_eden_top, alloc_bytes_adr,
aoqi@0 1374 0, new_alloc_bytes, T_LONG);
aoqi@0 1375 }
aoqi@0 1376
aoqi@0 1377 InitializeNode* init = alloc->initialization();
aoqi@0 1378 fast_oop_rawmem = initialize_object(alloc,
aoqi@0 1379 fast_oop_ctrl, fast_oop_rawmem, fast_oop,
aoqi@0 1380 klass_node, length, size_in_bytes);
aoqi@0 1381
aoqi@0 1382 // If initialization is performed by an array copy, any required
aoqi@0 1383 // MemBarStoreStore was already added. If the object does not
aoqi@0 1384 // escape no need for a MemBarStoreStore. Otherwise we need a
aoqi@0 1385 // MemBarStoreStore so that stores that initialize this object
aoqi@0 1386 // can't be reordered with a subsequent store that makes this
aoqi@0 1387 // object accessible by other threads.
aoqi@0 1388 if (init == NULL || (!init->is_complete_with_arraycopy() && !init->does_not_escape())) {
aoqi@0 1389 if (init == NULL || init->req() < InitializeNode::RawStores) {
aoqi@0 1390 // No InitializeNode or no stores captured by zeroing
aoqi@0 1391 // elimination. Simply add the MemBarStoreStore after object
aoqi@0 1392 // initialization.
aoqi@0 1393 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot);
aoqi@0 1394 transform_later(mb);
aoqi@0 1395
aoqi@0 1396 mb->init_req(TypeFunc::Memory, fast_oop_rawmem);
aoqi@0 1397 mb->init_req(TypeFunc::Control, fast_oop_ctrl);
aoqi@0 1398 fast_oop_ctrl = new (C) ProjNode(mb,TypeFunc::Control);
aoqi@0 1399 transform_later(fast_oop_ctrl);
aoqi@0 1400 fast_oop_rawmem = new (C) ProjNode(mb,TypeFunc::Memory);
aoqi@0 1401 transform_later(fast_oop_rawmem);
aoqi@0 1402 } else {
aoqi@0 1403 // Add the MemBarStoreStore after the InitializeNode so that
aoqi@0 1404 // all stores performing the initialization that were moved
aoqi@0 1405 // before the InitializeNode happen before the storestore
aoqi@0 1406 // barrier.
aoqi@0 1407
aoqi@0 1408 Node* init_ctrl = init->proj_out(TypeFunc::Control);
aoqi@0 1409 Node* init_mem = init->proj_out(TypeFunc::Memory);
aoqi@0 1410
aoqi@0 1411 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot);
aoqi@0 1412 transform_later(mb);
aoqi@0 1413
aoqi@0 1414 Node* ctrl = new (C) ProjNode(init,TypeFunc::Control);
aoqi@0 1415 transform_later(ctrl);
aoqi@0 1416 Node* mem = new (C) ProjNode(init,TypeFunc::Memory);
aoqi@0 1417 transform_later(mem);
aoqi@0 1418
aoqi@0 1419 // The MemBarStoreStore depends on control and memory coming
aoqi@0 1420 // from the InitializeNode
aoqi@0 1421 mb->init_req(TypeFunc::Memory, mem);
aoqi@0 1422 mb->init_req(TypeFunc::Control, ctrl);
aoqi@0 1423
aoqi@0 1424 ctrl = new (C) ProjNode(mb,TypeFunc::Control);
aoqi@0 1425 transform_later(ctrl);
aoqi@0 1426 mem = new (C) ProjNode(mb,TypeFunc::Memory);
aoqi@0 1427 transform_later(mem);
aoqi@0 1428
aoqi@0 1429 // All nodes that depended on the InitializeNode for control
aoqi@0 1430 // and memory must now depend on the MemBarNode that itself
aoqi@0 1431 // depends on the InitializeNode
aoqi@0 1432 _igvn.replace_node(init_ctrl, ctrl);
aoqi@0 1433 _igvn.replace_node(init_mem, mem);
aoqi@0 1434 }
aoqi@0 1435 }
aoqi@0 1436
aoqi@0 1437 if (C->env()->dtrace_extended_probes()) {
aoqi@0 1438 // Slow-path call
aoqi@0 1439 int size = TypeFunc::Parms + 2;
aoqi@0 1440 CallLeafNode *call = new (C) CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(),
aoqi@0 1441 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base),
aoqi@0 1442 "dtrace_object_alloc",
aoqi@0 1443 TypeRawPtr::BOTTOM);
aoqi@0 1444
aoqi@0 1445 // Get base of thread-local storage area
aoqi@0 1446 Node* thread = new (C) ThreadLocalNode();
aoqi@0 1447 transform_later(thread);
aoqi@0 1448
aoqi@0 1449 call->init_req(TypeFunc::Parms+0, thread);
aoqi@0 1450 call->init_req(TypeFunc::Parms+1, fast_oop);
aoqi@0 1451 call->init_req(TypeFunc::Control, fast_oop_ctrl);
aoqi@0 1452 call->init_req(TypeFunc::I_O , top()); // does no i/o
aoqi@0 1453 call->init_req(TypeFunc::Memory , fast_oop_rawmem);
aoqi@0 1454 call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
aoqi@0 1455 call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr));
aoqi@0 1456 transform_later(call);
aoqi@0 1457 fast_oop_ctrl = new (C) ProjNode(call,TypeFunc::Control);
aoqi@0 1458 transform_later(fast_oop_ctrl);
aoqi@0 1459 fast_oop_rawmem = new (C) ProjNode(call,TypeFunc::Memory);
aoqi@0 1460 transform_later(fast_oop_rawmem);
aoqi@0 1461 }
aoqi@0 1462
aoqi@0 1463 // Plug in the successful fast-path into the result merge point
aoqi@0 1464 result_region ->init_req(fast_result_path, fast_oop_ctrl);
aoqi@0 1465 result_phi_rawoop->init_req(fast_result_path, fast_oop);
aoqi@0 1466 result_phi_i_o ->init_req(fast_result_path, i_o);
aoqi@0 1467 result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem);
aoqi@0 1468 } else {
aoqi@0 1469 slow_region = ctrl;
aoqi@0 1470 result_phi_i_o = i_o; // Rename it to use in the following code.
aoqi@0 1471 }
aoqi@0 1472
aoqi@0 1473 // Generate slow-path call
aoqi@0 1474 CallNode *call = new (C) CallStaticJavaNode(slow_call_type, slow_call_address,
aoqi@0 1475 OptoRuntime::stub_name(slow_call_address),
aoqi@0 1476 alloc->jvms()->bci(),
aoqi@0 1477 TypePtr::BOTTOM);
aoqi@0 1478 call->init_req( TypeFunc::Control, slow_region );
aoqi@0 1479 call->init_req( TypeFunc::I_O , top() ) ; // does no i/o
aoqi@0 1480 call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs
aoqi@0 1481 call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
aoqi@0 1482 call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
aoqi@0 1483
aoqi@0 1484 call->init_req(TypeFunc::Parms+0, klass_node);
aoqi@0 1485 if (length != NULL) {
aoqi@0 1486 call->init_req(TypeFunc::Parms+1, length);
aoqi@0 1487 }
aoqi@0 1488
aoqi@0 1489 // Copy debug information and adjust JVMState information, then replace
aoqi@0 1490 // allocate node with the call
aoqi@0 1491 copy_call_debug_info((CallNode *) alloc, call);
aoqi@0 1492 if (!always_slow) {
aoqi@0 1493 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
aoqi@0 1494 } else {
aoqi@0 1495 // Hook i_o projection to avoid its elimination during allocation
aoqi@0 1496 // replacement (when only a slow call is generated).
aoqi@0 1497 call->set_req(TypeFunc::I_O, result_phi_i_o);
aoqi@0 1498 }
aoqi@0 1499 _igvn.replace_node(alloc, call);
aoqi@0 1500 transform_later(call);
aoqi@0 1501
aoqi@0 1502 // Identify the output projections from the allocate node and
aoqi@0 1503 // adjust any references to them.
aoqi@0 1504 // The control and io projections look like:
aoqi@0 1505 //
aoqi@0 1506 // v---Proj(ctrl) <-----+ v---CatchProj(ctrl)
aoqi@0 1507 // Allocate Catch
aoqi@0 1508 // ^---Proj(io) <-------+ ^---CatchProj(io)
aoqi@0 1509 //
aoqi@0 1510 // We are interested in the CatchProj nodes.
aoqi@0 1511 //
aoqi@0 1512 extract_call_projections(call);
aoqi@0 1513
aoqi@0 1514 // An allocate node has separate memory projections for the uses on
aoqi@0 1515 // the control and i_o paths. Replace the control memory projection with
aoqi@0 1516 // result_phi_rawmem (unless we are only generating a slow call when
aoqi@0 1517 // both memory projections are combined)
aoqi@0 1518 if (!always_slow && _memproj_fallthrough != NULL) {
aoqi@0 1519 for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) {
aoqi@0 1520 Node *use = _memproj_fallthrough->fast_out(i);
aoqi@0 1521 _igvn.rehash_node_delayed(use);
aoqi@0 1522 imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem);
aoqi@0 1523 // back up iterator
aoqi@0 1524 --i;
aoqi@0 1525 }
aoqi@0 1526 }
aoqi@0 1527 // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete
aoqi@0 1528 // _memproj_catchall so we end up with a call that has only 1 memory projection.
aoqi@0 1529 if (_memproj_catchall != NULL ) {
aoqi@0 1530 if (_memproj_fallthrough == NULL) {
aoqi@0 1531 _memproj_fallthrough = new (C) ProjNode(call, TypeFunc::Memory);
aoqi@0 1532 transform_later(_memproj_fallthrough);
aoqi@0 1533 }
aoqi@0 1534 for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) {
aoqi@0 1535 Node *use = _memproj_catchall->fast_out(i);
aoqi@0 1536 _igvn.rehash_node_delayed(use);
aoqi@0 1537 imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough);
aoqi@0 1538 // back up iterator
aoqi@0 1539 --i;
aoqi@0 1540 }
aoqi@0 1541 assert(_memproj_catchall->outcnt() == 0, "all uses must be deleted");
aoqi@0 1542 _igvn.remove_dead_node(_memproj_catchall);
aoqi@0 1543 }
aoqi@0 1544
aoqi@0 1545 // An allocate node has separate i_o projections for the uses on the control
aoqi@0 1546 // and i_o paths. Always replace the control i_o projection with result i_o
aoqi@0 1547 // otherwise incoming i_o become dead when only a slow call is generated
aoqi@0 1548 // (it is different from memory projections where both projections are
aoqi@0 1549 // combined in such case).
aoqi@0 1550 if (_ioproj_fallthrough != NULL) {
aoqi@0 1551 for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) {
aoqi@0 1552 Node *use = _ioproj_fallthrough->fast_out(i);
aoqi@0 1553 _igvn.rehash_node_delayed(use);
aoqi@0 1554 imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o);
aoqi@0 1555 // back up iterator
aoqi@0 1556 --i;
aoqi@0 1557 }
aoqi@0 1558 }
aoqi@0 1559 // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete
aoqi@0 1560 // _ioproj_catchall so we end up with a call that has only 1 i_o projection.
aoqi@0 1561 if (_ioproj_catchall != NULL ) {
aoqi@0 1562 if (_ioproj_fallthrough == NULL) {
aoqi@0 1563 _ioproj_fallthrough = new (C) ProjNode(call, TypeFunc::I_O);
aoqi@0 1564 transform_later(_ioproj_fallthrough);
aoqi@0 1565 }
aoqi@0 1566 for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) {
aoqi@0 1567 Node *use = _ioproj_catchall->fast_out(i);
aoqi@0 1568 _igvn.rehash_node_delayed(use);
aoqi@0 1569 imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough);
aoqi@0 1570 // back up iterator
aoqi@0 1571 --i;
aoqi@0 1572 }
aoqi@0 1573 assert(_ioproj_catchall->outcnt() == 0, "all uses must be deleted");
aoqi@0 1574 _igvn.remove_dead_node(_ioproj_catchall);
aoqi@0 1575 }
aoqi@0 1576
aoqi@0 1577 // if we generated only a slow call, we are done
aoqi@0 1578 if (always_slow) {
aoqi@0 1579 // Now we can unhook i_o.
aoqi@0 1580 if (result_phi_i_o->outcnt() > 1) {
aoqi@0 1581 call->set_req(TypeFunc::I_O, top());
aoqi@0 1582 } else {
aoqi@0 1583 assert(result_phi_i_o->unique_ctrl_out() == call, "");
aoqi@0 1584 // Case of new array with negative size known during compilation.
aoqi@0 1585 // AllocateArrayNode::Ideal() optimization disconnect unreachable
aoqi@0 1586 // following code since call to runtime will throw exception.
aoqi@0 1587 // As result there will be no users of i_o after the call.
aoqi@0 1588 // Leave i_o attached to this call to avoid problems in preceding graph.
aoqi@0 1589 }
aoqi@0 1590 return;
aoqi@0 1591 }
aoqi@0 1592
aoqi@0 1593
aoqi@0 1594 if (_fallthroughcatchproj != NULL) {
aoqi@0 1595 ctrl = _fallthroughcatchproj->clone();
aoqi@0 1596 transform_later(ctrl);
aoqi@0 1597 _igvn.replace_node(_fallthroughcatchproj, result_region);
aoqi@0 1598 } else {
aoqi@0 1599 ctrl = top();
aoqi@0 1600 }
aoqi@0 1601 Node *slow_result;
aoqi@0 1602 if (_resproj == NULL) {
aoqi@0 1603 // no uses of the allocation result
aoqi@0 1604 slow_result = top();
aoqi@0 1605 } else {
aoqi@0 1606 slow_result = _resproj->clone();
aoqi@0 1607 transform_later(slow_result);
aoqi@0 1608 _igvn.replace_node(_resproj, result_phi_rawoop);
aoqi@0 1609 }
aoqi@0 1610
aoqi@0 1611 // Plug slow-path into result merge point
aoqi@0 1612 result_region ->init_req( slow_result_path, ctrl );
aoqi@0 1613 result_phi_rawoop->init_req( slow_result_path, slow_result);
aoqi@0 1614 result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough );
aoqi@0 1615 transform_later(result_region);
aoqi@0 1616 transform_later(result_phi_rawoop);
aoqi@0 1617 transform_later(result_phi_rawmem);
aoqi@0 1618 transform_later(result_phi_i_o);
aoqi@0 1619 // This completes all paths into the result merge point
aoqi@0 1620 }
aoqi@0 1621
aoqi@0 1622
aoqi@0 1623 // Helper for PhaseMacroExpand::expand_allocate_common.
aoqi@0 1624 // Initializes the newly-allocated storage.
aoqi@0 1625 Node*
aoqi@0 1626 PhaseMacroExpand::initialize_object(AllocateNode* alloc,
aoqi@0 1627 Node* control, Node* rawmem, Node* object,
aoqi@0 1628 Node* klass_node, Node* length,
aoqi@0 1629 Node* size_in_bytes) {
aoqi@0 1630 InitializeNode* init = alloc->initialization();
aoqi@0 1631 // Store the klass & mark bits
aoqi@0 1632 Node* mark_node = NULL;
aoqi@0 1633 // For now only enable fast locking for non-array types
aoqi@0 1634 if (UseBiasedLocking && (length == NULL)) {
aoqi@0 1635 mark_node = make_load(control, rawmem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeRawPtr::BOTTOM, T_ADDRESS);
aoqi@0 1636 } else {
aoqi@0 1637 mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype()));
aoqi@0 1638 }
aoqi@0 1639 rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
aoqi@0 1640
aoqi@0 1641 rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA);
aoqi@0 1642 int header_size = alloc->minimum_header_size(); // conservatively small
aoqi@0 1643
aoqi@0 1644 // Array length
aoqi@0 1645 if (length != NULL) { // Arrays need length field
aoqi@0 1646 rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
aoqi@0 1647 // conservatively small header size:
aoqi@0 1648 header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
aoqi@0 1649 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
aoqi@0 1650 if (k->is_array_klass()) // we know the exact header size in most cases:
aoqi@0 1651 header_size = Klass::layout_helper_header_size(k->layout_helper());
aoqi@0 1652 }
aoqi@0 1653
aoqi@0 1654 // Clear the object body, if necessary.
aoqi@0 1655 if (init == NULL) {
aoqi@0 1656 // The init has somehow disappeared; be cautious and clear everything.
aoqi@0 1657 //
aoqi@0 1658 // This can happen if a node is allocated but an uncommon trap occurs
aoqi@0 1659 // immediately. In this case, the Initialize gets associated with the
aoqi@0 1660 // trap, and may be placed in a different (outer) loop, if the Allocate
aoqi@0 1661 // is in a loop. If (this is rare) the inner loop gets unrolled, then
aoqi@0 1662 // there can be two Allocates to one Initialize. The answer in all these
aoqi@0 1663 // edge cases is safety first. It is always safe to clear immediately
aoqi@0 1664 // within an Allocate, and then (maybe or maybe not) clear some more later.
aoqi@0 1665 if (!ZeroTLAB)
aoqi@0 1666 rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
aoqi@0 1667 header_size, size_in_bytes,
aoqi@0 1668 &_igvn);
aoqi@0 1669 } else {
aoqi@0 1670 if (!init->is_complete()) {
aoqi@0 1671 // Try to win by zeroing only what the init does not store.
aoqi@0 1672 // We can also try to do some peephole optimizations,
aoqi@0 1673 // such as combining some adjacent subword stores.
aoqi@0 1674 rawmem = init->complete_stores(control, rawmem, object,
aoqi@0 1675 header_size, size_in_bytes, &_igvn);
aoqi@0 1676 }
aoqi@0 1677 // We have no more use for this link, since the AllocateNode goes away:
aoqi@0 1678 init->set_req(InitializeNode::RawAddress, top());
aoqi@0 1679 // (If we keep the link, it just confuses the register allocator,
aoqi@0 1680 // who thinks he sees a real use of the address by the membar.)
aoqi@0 1681 }
aoqi@0 1682
aoqi@0 1683 return rawmem;
aoqi@0 1684 }
aoqi@0 1685
aoqi@0 1686 // Generate prefetch instructions for next allocations.
aoqi@0 1687 Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false,
aoqi@0 1688 Node*& contended_phi_rawmem,
aoqi@0 1689 Node* old_eden_top, Node* new_eden_top,
aoqi@0 1690 Node* length) {
aoqi@0 1691 enum { fall_in_path = 1, pf_path = 2 };
aoqi@0 1692 if( UseTLAB && AllocatePrefetchStyle == 2 ) {
aoqi@0 1693 // Generate prefetch allocation with watermark check.
aoqi@0 1694 // As an allocation hits the watermark, we will prefetch starting
aoqi@0 1695 // at a "distance" away from watermark.
aoqi@0 1696
aoqi@0 1697 Node *pf_region = new (C) RegionNode(3);
aoqi@0 1698 Node *pf_phi_rawmem = new (C) PhiNode( pf_region, Type::MEMORY,
aoqi@0 1699 TypeRawPtr::BOTTOM );
aoqi@0 1700 // I/O is used for Prefetch
aoqi@0 1701 Node *pf_phi_abio = new (C) PhiNode( pf_region, Type::ABIO );
aoqi@0 1702
aoqi@0 1703 Node *thread = new (C) ThreadLocalNode();
aoqi@0 1704 transform_later(thread);
aoqi@0 1705
aoqi@0 1706 Node *eden_pf_adr = new (C) AddPNode( top()/*not oop*/, thread,
aoqi@0 1707 _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) );
aoqi@0 1708 transform_later(eden_pf_adr);
aoqi@0 1709
aoqi@0 1710 Node *old_pf_wm = new (C) LoadPNode(needgc_false,
aoqi@0 1711 contended_phi_rawmem, eden_pf_adr,
aoqi@0 1712 TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM,
aoqi@0 1713 MemNode::unordered);
aoqi@0 1714 transform_later(old_pf_wm);
aoqi@0 1715
aoqi@0 1716 // check against new_eden_top
aoqi@0 1717 Node *need_pf_cmp = new (C) CmpPNode( new_eden_top, old_pf_wm );
aoqi@0 1718 transform_later(need_pf_cmp);
aoqi@0 1719 Node *need_pf_bol = new (C) BoolNode( need_pf_cmp, BoolTest::ge );
aoqi@0 1720 transform_later(need_pf_bol);
aoqi@0 1721 IfNode *need_pf_iff = new (C) IfNode( needgc_false, need_pf_bol,
aoqi@0 1722 PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
aoqi@0 1723 transform_later(need_pf_iff);
aoqi@0 1724
aoqi@0 1725 // true node, add prefetchdistance
aoqi@0 1726 Node *need_pf_true = new (C) IfTrueNode( need_pf_iff );
aoqi@0 1727 transform_later(need_pf_true);
aoqi@0 1728
aoqi@0 1729 Node *need_pf_false = new (C) IfFalseNode( need_pf_iff );
aoqi@0 1730 transform_later(need_pf_false);
aoqi@0 1731
aoqi@0 1732 Node *new_pf_wmt = new (C) AddPNode( top(), old_pf_wm,
aoqi@0 1733 _igvn.MakeConX(AllocatePrefetchDistance) );
aoqi@0 1734 transform_later(new_pf_wmt );
aoqi@0 1735 new_pf_wmt->set_req(0, need_pf_true);
aoqi@0 1736
aoqi@0 1737 Node *store_new_wmt = new (C) StorePNode(need_pf_true,
aoqi@0 1738 contended_phi_rawmem, eden_pf_adr,
aoqi@0 1739 TypeRawPtr::BOTTOM, new_pf_wmt,
aoqi@0 1740 MemNode::unordered);
aoqi@0 1741 transform_later(store_new_wmt);
aoqi@0 1742
aoqi@0 1743 // adding prefetches
aoqi@0 1744 pf_phi_abio->init_req( fall_in_path, i_o );
aoqi@0 1745
aoqi@0 1746 Node *prefetch_adr;
aoqi@0 1747 Node *prefetch;
aoqi@0 1748 uint lines = AllocatePrefetchDistance / AllocatePrefetchStepSize;
aoqi@0 1749 uint step_size = AllocatePrefetchStepSize;
aoqi@0 1750 uint distance = 0;
aoqi@0 1751
aoqi@0 1752 for ( uint i = 0; i < lines; i++ ) {
aoqi@0 1753 prefetch_adr = new (C) AddPNode( old_pf_wm, new_pf_wmt,
aoqi@0 1754 _igvn.MakeConX(distance) );
aoqi@0 1755 transform_later(prefetch_adr);
aoqi@0 1756 prefetch = new (C) PrefetchAllocationNode( i_o, prefetch_adr );
aoqi@0 1757 transform_later(prefetch);
aoqi@0 1758 distance += step_size;
aoqi@0 1759 i_o = prefetch;
aoqi@0 1760 }
aoqi@0 1761 pf_phi_abio->set_req( pf_path, i_o );
aoqi@0 1762
aoqi@0 1763 pf_region->init_req( fall_in_path, need_pf_false );
aoqi@0 1764 pf_region->init_req( pf_path, need_pf_true );
aoqi@0 1765
aoqi@0 1766 pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem );
aoqi@0 1767 pf_phi_rawmem->init_req( pf_path, store_new_wmt );
aoqi@0 1768
aoqi@0 1769 transform_later(pf_region);
aoqi@0 1770 transform_later(pf_phi_rawmem);
aoqi@0 1771 transform_later(pf_phi_abio);
aoqi@0 1772
aoqi@0 1773 needgc_false = pf_region;
aoqi@0 1774 contended_phi_rawmem = pf_phi_rawmem;
aoqi@0 1775 i_o = pf_phi_abio;
aoqi@0 1776 } else if( UseTLAB && AllocatePrefetchStyle == 3 ) {
aoqi@0 1777 // Insert a prefetch for each allocation.
aoqi@0 1778 // This code is used for Sparc with BIS.
aoqi@0 1779 Node *pf_region = new (C) RegionNode(3);
aoqi@0 1780 Node *pf_phi_rawmem = new (C) PhiNode( pf_region, Type::MEMORY,
aoqi@0 1781 TypeRawPtr::BOTTOM );
aoqi@0 1782
aoqi@0 1783 // Generate several prefetch instructions.
aoqi@0 1784 uint lines = (length != NULL) ? AllocatePrefetchLines : AllocateInstancePrefetchLines;
aoqi@0 1785 uint step_size = AllocatePrefetchStepSize;
aoqi@0 1786 uint distance = AllocatePrefetchDistance;
aoqi@0 1787
aoqi@0 1788 // Next cache address.
aoqi@0 1789 Node *cache_adr = new (C) AddPNode(old_eden_top, old_eden_top,
aoqi@0 1790 _igvn.MakeConX(distance));
aoqi@0 1791 transform_later(cache_adr);
aoqi@0 1792 cache_adr = new (C) CastP2XNode(needgc_false, cache_adr);
aoqi@0 1793 transform_later(cache_adr);
aoqi@0 1794 Node* mask = _igvn.MakeConX(~(intptr_t)(step_size-1));
aoqi@0 1795 cache_adr = new (C) AndXNode(cache_adr, mask);
aoqi@0 1796 transform_later(cache_adr);
aoqi@0 1797 cache_adr = new (C) CastX2PNode(cache_adr);
aoqi@0 1798 transform_later(cache_adr);
aoqi@0 1799
aoqi@0 1800 // Prefetch
aoqi@0 1801 Node *prefetch = new (C) PrefetchAllocationNode( contended_phi_rawmem, cache_adr );
aoqi@0 1802 prefetch->set_req(0, needgc_false);
aoqi@0 1803 transform_later(prefetch);
aoqi@0 1804 contended_phi_rawmem = prefetch;
aoqi@0 1805 Node *prefetch_adr;
aoqi@0 1806 distance = step_size;
aoqi@0 1807 for ( uint i = 1; i < lines; i++ ) {
aoqi@0 1808 prefetch_adr = new (C) AddPNode( cache_adr, cache_adr,
aoqi@0 1809 _igvn.MakeConX(distance) );
aoqi@0 1810 transform_later(prefetch_adr);
aoqi@0 1811 prefetch = new (C) PrefetchAllocationNode( contended_phi_rawmem, prefetch_adr );
aoqi@0 1812 transform_later(prefetch);
aoqi@0 1813 distance += step_size;
aoqi@0 1814 contended_phi_rawmem = prefetch;
aoqi@0 1815 }
aoqi@0 1816 } else if( AllocatePrefetchStyle > 0 ) {
aoqi@0 1817 // Insert a prefetch for each allocation only on the fast-path
aoqi@0 1818 Node *prefetch_adr;
aoqi@0 1819 Node *prefetch;
aoqi@0 1820 // Generate several prefetch instructions.
aoqi@0 1821 uint lines = (length != NULL) ? AllocatePrefetchLines : AllocateInstancePrefetchLines;
aoqi@0 1822 uint step_size = AllocatePrefetchStepSize;
aoqi@0 1823 uint distance = AllocatePrefetchDistance;
aoqi@0 1824 for ( uint i = 0; i < lines; i++ ) {
aoqi@0 1825 prefetch_adr = new (C) AddPNode( old_eden_top, new_eden_top,
aoqi@0 1826 _igvn.MakeConX(distance) );
aoqi@0 1827 transform_later(prefetch_adr);
aoqi@0 1828 prefetch = new (C) PrefetchAllocationNode( i_o, prefetch_adr );
aoqi@0 1829 // Do not let it float too high, since if eden_top == eden_end,
aoqi@0 1830 // both might be null.
aoqi@0 1831 if( i == 0 ) { // Set control for first prefetch, next follows it
aoqi@0 1832 prefetch->init_req(0, needgc_false);
aoqi@0 1833 }
aoqi@0 1834 transform_later(prefetch);
aoqi@0 1835 distance += step_size;
aoqi@0 1836 i_o = prefetch;
aoqi@0 1837 }
aoqi@0 1838 }
aoqi@0 1839 return i_o;
aoqi@0 1840 }
aoqi@0 1841
aoqi@0 1842
aoqi@0 1843 void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) {
aoqi@0 1844 expand_allocate_common(alloc, NULL,
aoqi@0 1845 OptoRuntime::new_instance_Type(),
aoqi@0 1846 OptoRuntime::new_instance_Java());
aoqi@0 1847 }
aoqi@0 1848
aoqi@0 1849 void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) {
aoqi@0 1850 Node* length = alloc->in(AllocateNode::ALength);
aoqi@0 1851 InitializeNode* init = alloc->initialization();
aoqi@0 1852 Node* klass_node = alloc->in(AllocateNode::KlassNode);
aoqi@0 1853 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
aoqi@0 1854 address slow_call_address; // Address of slow call
aoqi@0 1855 if (init != NULL && init->is_complete_with_arraycopy() &&
aoqi@0 1856 k->is_type_array_klass()) {
aoqi@0 1857 // Don't zero type array during slow allocation in VM since
aoqi@0 1858 // it will be initialized later by arraycopy in compiled code.
aoqi@0 1859 slow_call_address = OptoRuntime::new_array_nozero_Java();
aoqi@0 1860 } else {
aoqi@0 1861 slow_call_address = OptoRuntime::new_array_Java();
aoqi@0 1862 }
aoqi@0 1863 expand_allocate_common(alloc, length,
aoqi@0 1864 OptoRuntime::new_array_Type(),
aoqi@0 1865 slow_call_address);
aoqi@0 1866 }
aoqi@0 1867
aoqi@0 1868 //-------------------mark_eliminated_box----------------------------------
aoqi@0 1869 //
aoqi@0 1870 // During EA obj may point to several objects but after few ideal graph
aoqi@0 1871 // transformations (CCP) it may point to only one non escaping object
aoqi@0 1872 // (but still using phi), corresponding locks and unlocks will be marked
aoqi@0 1873 // for elimination. Later obj could be replaced with a new node (new phi)
aoqi@0 1874 // and which does not have escape information. And later after some graph
aoqi@0 1875 // reshape other locks and unlocks (which were not marked for elimination
aoqi@0 1876 // before) are connected to this new obj (phi) but they still will not be
aoqi@0 1877 // marked for elimination since new obj has no escape information.
aoqi@0 1878 // Mark all associated (same box and obj) lock and unlock nodes for
aoqi@0 1879 // elimination if some of them marked already.
aoqi@0 1880 void PhaseMacroExpand::mark_eliminated_box(Node* oldbox, Node* obj) {
aoqi@0 1881 if (oldbox->as_BoxLock()->is_eliminated())
aoqi@0 1882 return; // This BoxLock node was processed already.
aoqi@0 1883
aoqi@0 1884 // New implementation (EliminateNestedLocks) has separate BoxLock
aoqi@0 1885 // node for each locked region so mark all associated locks/unlocks as
aoqi@0 1886 // eliminated even if different objects are referenced in one locked region
aoqi@0 1887 // (for example, OSR compilation of nested loop inside locked scope).
aoqi@0 1888 if (EliminateNestedLocks ||
aoqi@0 1889 oldbox->as_BoxLock()->is_simple_lock_region(NULL, obj)) {
aoqi@0 1890 // Box is used only in one lock region. Mark this box as eliminated.
aoqi@0 1891 _igvn.hash_delete(oldbox);
aoqi@0 1892 oldbox->as_BoxLock()->set_eliminated(); // This changes box's hash value
drchase@7605 1893 _igvn.hash_insert(oldbox);
aoqi@0 1894
aoqi@0 1895 for (uint i = 0; i < oldbox->outcnt(); i++) {
aoqi@0 1896 Node* u = oldbox->raw_out(i);
aoqi@0 1897 if (u->is_AbstractLock() && !u->as_AbstractLock()->is_non_esc_obj()) {
aoqi@0 1898 AbstractLockNode* alock = u->as_AbstractLock();
aoqi@0 1899 // Check lock's box since box could be referenced by Lock's debug info.
aoqi@0 1900 if (alock->box_node() == oldbox) {
aoqi@0 1901 // Mark eliminated all related locks and unlocks.
drchase@7605 1902 #ifdef ASSERT
drchase@7605 1903 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc4");
drchase@7605 1904 #endif
aoqi@0 1905 alock->set_non_esc_obj();
aoqi@0 1906 }
aoqi@0 1907 }
aoqi@0 1908 }
aoqi@0 1909 return;
aoqi@0 1910 }
aoqi@0 1911
aoqi@0 1912 // Create new "eliminated" BoxLock node and use it in monitor debug info
aoqi@0 1913 // instead of oldbox for the same object.
aoqi@0 1914 BoxLockNode* newbox = oldbox->clone()->as_BoxLock();
aoqi@0 1915
aoqi@0 1916 // Note: BoxLock node is marked eliminated only here and it is used
aoqi@0 1917 // to indicate that all associated lock and unlock nodes are marked
aoqi@0 1918 // for elimination.
aoqi@0 1919 newbox->set_eliminated();
aoqi@0 1920 transform_later(newbox);
aoqi@0 1921
aoqi@0 1922 // Replace old box node with new box for all users of the same object.
aoqi@0 1923 for (uint i = 0; i < oldbox->outcnt();) {
aoqi@0 1924 bool next_edge = true;
aoqi@0 1925
aoqi@0 1926 Node* u = oldbox->raw_out(i);
aoqi@0 1927 if (u->is_AbstractLock()) {
aoqi@0 1928 AbstractLockNode* alock = u->as_AbstractLock();
aoqi@0 1929 if (alock->box_node() == oldbox && alock->obj_node()->eqv_uncast(obj)) {
aoqi@0 1930 // Replace Box and mark eliminated all related locks and unlocks.
drchase@7605 1931 #ifdef ASSERT
drchase@7605 1932 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc5");
drchase@7605 1933 #endif
aoqi@0 1934 alock->set_non_esc_obj();
aoqi@0 1935 _igvn.rehash_node_delayed(alock);
aoqi@0 1936 alock->set_box_node(newbox);
aoqi@0 1937 next_edge = false;
aoqi@0 1938 }
aoqi@0 1939 }
aoqi@0 1940 if (u->is_FastLock() && u->as_FastLock()->obj_node()->eqv_uncast(obj)) {
aoqi@0 1941 FastLockNode* flock = u->as_FastLock();
aoqi@0 1942 assert(flock->box_node() == oldbox, "sanity");
aoqi@0 1943 _igvn.rehash_node_delayed(flock);
aoqi@0 1944 flock->set_box_node(newbox);
aoqi@0 1945 next_edge = false;
aoqi@0 1946 }
aoqi@0 1947
aoqi@0 1948 // Replace old box in monitor debug info.
aoqi@0 1949 if (u->is_SafePoint() && u->as_SafePoint()->jvms()) {
aoqi@0 1950 SafePointNode* sfn = u->as_SafePoint();
aoqi@0 1951 JVMState* youngest_jvms = sfn->jvms();
aoqi@0 1952 int max_depth = youngest_jvms->depth();
aoqi@0 1953 for (int depth = 1; depth <= max_depth; depth++) {
aoqi@0 1954 JVMState* jvms = youngest_jvms->of_depth(depth);
aoqi@0 1955 int num_mon = jvms->nof_monitors();
aoqi@0 1956 // Loop over monitors
aoqi@0 1957 for (int idx = 0; idx < num_mon; idx++) {
aoqi@0 1958 Node* obj_node = sfn->monitor_obj(jvms, idx);
aoqi@0 1959 Node* box_node = sfn->monitor_box(jvms, idx);
aoqi@0 1960 if (box_node == oldbox && obj_node->eqv_uncast(obj)) {
aoqi@0 1961 int j = jvms->monitor_box_offset(idx);
aoqi@0 1962 _igvn.replace_input_of(u, j, newbox);
aoqi@0 1963 next_edge = false;
aoqi@0 1964 }
aoqi@0 1965 }
aoqi@0 1966 }
aoqi@0 1967 }
aoqi@0 1968 if (next_edge) i++;
aoqi@0 1969 }
aoqi@0 1970 }
aoqi@0 1971
aoqi@0 1972 //-----------------------mark_eliminated_locking_nodes-----------------------
aoqi@0 1973 void PhaseMacroExpand::mark_eliminated_locking_nodes(AbstractLockNode *alock) {
aoqi@0 1974 if (EliminateNestedLocks) {
aoqi@0 1975 if (alock->is_nested()) {
aoqi@0 1976 assert(alock->box_node()->as_BoxLock()->is_eliminated(), "sanity");
aoqi@0 1977 return;
aoqi@0 1978 } else if (!alock->is_non_esc_obj()) { // Not eliminated or coarsened
aoqi@0 1979 // Only Lock node has JVMState needed here.
drchase@7605 1980 // Not that preceding claim is documented anywhere else.
drchase@7605 1981 if (alock->jvms() != NULL) {
drchase@7605 1982 if (alock->as_Lock()->is_nested_lock_region()) {
drchase@7605 1983 // Mark eliminated related nested locks and unlocks.
drchase@7605 1984 Node* obj = alock->obj_node();
drchase@7605 1985 BoxLockNode* box_node = alock->box_node()->as_BoxLock();
drchase@7605 1986 assert(!box_node->is_eliminated(), "should not be marked yet");
drchase@7605 1987 // Note: BoxLock node is marked eliminated only here
drchase@7605 1988 // and it is used to indicate that all associated lock
drchase@7605 1989 // and unlock nodes are marked for elimination.
drchase@7605 1990 box_node->set_eliminated(); // Box's hash is always NO_HASH here
drchase@7605 1991 for (uint i = 0; i < box_node->outcnt(); i++) {
drchase@7605 1992 Node* u = box_node->raw_out(i);
drchase@7605 1993 if (u->is_AbstractLock()) {
drchase@7605 1994 alock = u->as_AbstractLock();
drchase@7605 1995 if (alock->box_node() == box_node) {
drchase@7605 1996 // Verify that this Box is referenced only by related locks.
drchase@7605 1997 assert(alock->obj_node()->eqv_uncast(obj), "");
drchase@7605 1998 // Mark all related locks and unlocks.
drchase@7605 1999 #ifdef ASSERT
drchase@7605 2000 alock->log_lock_optimization(C, "eliminate_lock_set_nested");
drchase@7605 2001 #endif
drchase@7605 2002 alock->set_nested();
drchase@7605 2003 }
aoqi@0 2004 }
aoqi@0 2005 }
drchase@7605 2006 } else {
drchase@7605 2007 #ifdef ASSERT
drchase@7605 2008 alock->log_lock_optimization(C, "eliminate_lock_NOT_nested_lock_region");
drchase@7605 2009 if (C->log() != NULL)
drchase@7605 2010 alock->as_Lock()->is_nested_lock_region(C); // rerun for debugging output
drchase@7605 2011 #endif
aoqi@0 2012 }
aoqi@0 2013 }
aoqi@0 2014 return;
aoqi@0 2015 }
aoqi@0 2016 // Process locks for non escaping object
aoqi@0 2017 assert(alock->is_non_esc_obj(), "");
aoqi@0 2018 } // EliminateNestedLocks
aoqi@0 2019
aoqi@0 2020 if (alock->is_non_esc_obj()) { // Lock is used for non escaping object
aoqi@0 2021 // Look for all locks of this object and mark them and
aoqi@0 2022 // corresponding BoxLock nodes as eliminated.
aoqi@0 2023 Node* obj = alock->obj_node();
aoqi@0 2024 for (uint j = 0; j < obj->outcnt(); j++) {
aoqi@0 2025 Node* o = obj->raw_out(j);
aoqi@0 2026 if (o->is_AbstractLock() &&
aoqi@0 2027 o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) {
aoqi@0 2028 alock = o->as_AbstractLock();
aoqi@0 2029 Node* box = alock->box_node();
aoqi@0 2030 // Replace old box node with new eliminated box for all users
aoqi@0 2031 // of the same object and mark related locks as eliminated.
aoqi@0 2032 mark_eliminated_box(box, obj);
aoqi@0 2033 }
aoqi@0 2034 }
aoqi@0 2035 }
aoqi@0 2036 }
aoqi@0 2037
aoqi@0 2038 // we have determined that this lock/unlock can be eliminated, we simply
aoqi@0 2039 // eliminate the node without expanding it.
aoqi@0 2040 //
aoqi@0 2041 // Note: The membar's associated with the lock/unlock are currently not
aoqi@0 2042 // eliminated. This should be investigated as a future enhancement.
aoqi@0 2043 //
aoqi@0 2044 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
aoqi@0 2045
aoqi@0 2046 if (!alock->is_eliminated()) {
aoqi@0 2047 return false;
aoqi@0 2048 }
aoqi@0 2049 #ifdef ASSERT
aoqi@0 2050 if (!alock->is_coarsened()) {
aoqi@0 2051 // Check that new "eliminated" BoxLock node is created.
aoqi@0 2052 BoxLockNode* oldbox = alock->box_node()->as_BoxLock();
aoqi@0 2053 assert(oldbox->is_eliminated(), "should be done already");
aoqi@0 2054 }
aoqi@0 2055 #endif
aoqi@0 2056
drchase@7605 2057 alock->log_lock_optimization(C, "eliminate_lock");
drchase@7605 2058
drchase@7605 2059 #ifndef PRODUCT
aoqi@0 2060 if (PrintEliminateLocks) {
aoqi@0 2061 if (alock->is_Lock()) {
aoqi@0 2062 tty->print_cr("++++ Eliminated: %d Lock", alock->_idx);
aoqi@0 2063 } else {
aoqi@0 2064 tty->print_cr("++++ Eliminated: %d Unlock", alock->_idx);
aoqi@0 2065 }
aoqi@0 2066 }
drchase@7605 2067 #endif
aoqi@0 2068
aoqi@0 2069 Node* mem = alock->in(TypeFunc::Memory);
aoqi@0 2070 Node* ctrl = alock->in(TypeFunc::Control);
aoqi@0 2071
aoqi@0 2072 extract_call_projections(alock);
aoqi@0 2073 // There are 2 projections from the lock. The lock node will
aoqi@0 2074 // be deleted when its last use is subsumed below.
aoqi@0 2075 assert(alock->outcnt() == 2 &&
aoqi@0 2076 _fallthroughproj != NULL &&
aoqi@0 2077 _memproj_fallthrough != NULL,
aoqi@0 2078 "Unexpected projections from Lock/Unlock");
aoqi@0 2079
aoqi@0 2080 Node* fallthroughproj = _fallthroughproj;
aoqi@0 2081 Node* memproj_fallthrough = _memproj_fallthrough;
aoqi@0 2082
aoqi@0 2083 // The memory projection from a lock/unlock is RawMem
aoqi@0 2084 // The input to a Lock is merged memory, so extract its RawMem input
aoqi@0 2085 // (unless the MergeMem has been optimized away.)
aoqi@0 2086 if (alock->is_Lock()) {
aoqi@0 2087 // Seach for MemBarAcquireLock node and delete it also.
aoqi@0 2088 MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
aoqi@0 2089 assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, "");
aoqi@0 2090 Node* ctrlproj = membar->proj_out(TypeFunc::Control);
aoqi@0 2091 Node* memproj = membar->proj_out(TypeFunc::Memory);
aoqi@0 2092 _igvn.replace_node(ctrlproj, fallthroughproj);
aoqi@0 2093 _igvn.replace_node(memproj, memproj_fallthrough);
aoqi@0 2094
aoqi@0 2095 // Delete FastLock node also if this Lock node is unique user
aoqi@0 2096 // (a loop peeling may clone a Lock node).
aoqi@0 2097 Node* flock = alock->as_Lock()->fastlock_node();
aoqi@0 2098 if (flock->outcnt() == 1) {
aoqi@0 2099 assert(flock->unique_out() == alock, "sanity");
aoqi@0 2100 _igvn.replace_node(flock, top());
aoqi@0 2101 }
aoqi@0 2102 }
aoqi@0 2103
aoqi@0 2104 // Seach for MemBarReleaseLock node and delete it also.
aoqi@0 2105 if (alock->is_Unlock() && ctrl != NULL && ctrl->is_Proj() &&
aoqi@0 2106 ctrl->in(0)->is_MemBar()) {
aoqi@0 2107 MemBarNode* membar = ctrl->in(0)->as_MemBar();
aoqi@0 2108 assert(membar->Opcode() == Op_MemBarReleaseLock &&
aoqi@0 2109 mem->is_Proj() && membar == mem->in(0), "");
aoqi@0 2110 _igvn.replace_node(fallthroughproj, ctrl);
aoqi@0 2111 _igvn.replace_node(memproj_fallthrough, mem);
aoqi@0 2112 fallthroughproj = ctrl;
aoqi@0 2113 memproj_fallthrough = mem;
aoqi@0 2114 ctrl = membar->in(TypeFunc::Control);
aoqi@0 2115 mem = membar->in(TypeFunc::Memory);
aoqi@0 2116 }
aoqi@0 2117
aoqi@0 2118 _igvn.replace_node(fallthroughproj, ctrl);
aoqi@0 2119 _igvn.replace_node(memproj_fallthrough, mem);
aoqi@0 2120 return true;
aoqi@0 2121 }
aoqi@0 2122
aoqi@0 2123
aoqi@0 2124 //------------------------------expand_lock_node----------------------
aoqi@0 2125 void PhaseMacroExpand::expand_lock_node(LockNode *lock) {
aoqi@0 2126
aoqi@0 2127 Node* ctrl = lock->in(TypeFunc::Control);
aoqi@0 2128 Node* mem = lock->in(TypeFunc::Memory);
aoqi@0 2129 Node* obj = lock->obj_node();
aoqi@0 2130 Node* box = lock->box_node();
aoqi@0 2131 Node* flock = lock->fastlock_node();
aoqi@0 2132
aoqi@0 2133 assert(!box->as_BoxLock()->is_eliminated(), "sanity");
aoqi@0 2134
aoqi@0 2135 // Make the merge point
aoqi@0 2136 Node *region;
aoqi@0 2137 Node *mem_phi;
aoqi@0 2138 Node *slow_path;
aoqi@0 2139
aoqi@0 2140 if (UseOptoBiasInlining) {
aoqi@0 2141 /*
aoqi@0 2142 * See the full description in MacroAssembler::biased_locking_enter().
aoqi@0 2143 *
aoqi@0 2144 * if( (mark_word & biased_lock_mask) == biased_lock_pattern ) {
aoqi@0 2145 * // The object is biased.
aoqi@0 2146 * proto_node = klass->prototype_header;
aoqi@0 2147 * o_node = thread | proto_node;
aoqi@0 2148 * x_node = o_node ^ mark_word;
aoqi@0 2149 * if( (x_node & ~age_mask) == 0 ) { // Biased to the current thread ?
aoqi@0 2150 * // Done.
aoqi@0 2151 * } else {
aoqi@0 2152 * if( (x_node & biased_lock_mask) != 0 ) {
aoqi@0 2153 * // The klass's prototype header is no longer biased.
aoqi@0 2154 * cas(&mark_word, mark_word, proto_node)
aoqi@0 2155 * goto cas_lock;
aoqi@0 2156 * } else {
aoqi@0 2157 * // The klass's prototype header is still biased.
aoqi@0 2158 * if( (x_node & epoch_mask) != 0 ) { // Expired epoch?
aoqi@0 2159 * old = mark_word;
aoqi@0 2160 * new = o_node;
aoqi@0 2161 * } else {
aoqi@0 2162 * // Different thread or anonymous biased.
aoqi@0 2163 * old = mark_word & (epoch_mask | age_mask | biased_lock_mask);
aoqi@0 2164 * new = thread | old;
aoqi@0 2165 * }
aoqi@0 2166 * // Try to rebias.
aoqi@0 2167 * if( cas(&mark_word, old, new) == 0 ) {
aoqi@0 2168 * // Done.
aoqi@0 2169 * } else {
aoqi@0 2170 * goto slow_path; // Failed.
aoqi@0 2171 * }
aoqi@0 2172 * }
aoqi@0 2173 * }
aoqi@0 2174 * } else {
aoqi@0 2175 * // The object is not biased.
aoqi@0 2176 * cas_lock:
aoqi@0 2177 * if( FastLock(obj) == 0 ) {
aoqi@0 2178 * // Done.
aoqi@0 2179 * } else {
aoqi@0 2180 * slow_path:
aoqi@0 2181 * OptoRuntime::complete_monitor_locking_Java(obj);
aoqi@0 2182 * }
aoqi@0 2183 * }
aoqi@0 2184 */
aoqi@0 2185
aoqi@0 2186 region = new (C) RegionNode(5);
aoqi@0 2187 // create a Phi for the memory state
aoqi@0 2188 mem_phi = new (C) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2189
aoqi@0 2190 Node* fast_lock_region = new (C) RegionNode(3);
aoqi@0 2191 Node* fast_lock_mem_phi = new (C) PhiNode( fast_lock_region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2192
aoqi@0 2193 // First, check mark word for the biased lock pattern.
aoqi@0 2194 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
aoqi@0 2195
aoqi@0 2196 // Get fast path - mark word has the biased lock pattern.
aoqi@0 2197 ctrl = opt_bits_test(ctrl, fast_lock_region, 1, mark_node,
aoqi@0 2198 markOopDesc::biased_lock_mask_in_place,
aoqi@0 2199 markOopDesc::biased_lock_pattern, true);
aoqi@0 2200 // fast_lock_region->in(1) is set to slow path.
aoqi@0 2201 fast_lock_mem_phi->init_req(1, mem);
aoqi@0 2202
aoqi@0 2203 // Now check that the lock is biased to the current thread and has
aoqi@0 2204 // the same epoch and bias as Klass::_prototype_header.
aoqi@0 2205
aoqi@0 2206 // Special-case a fresh allocation to avoid building nodes:
aoqi@0 2207 Node* klass_node = AllocateNode::Ideal_klass(obj, &_igvn);
aoqi@0 2208 if (klass_node == NULL) {
aoqi@0 2209 Node* k_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());
zmajo@7341 2210 klass_node = transform_later(LoadKlassNode::make(_igvn, NULL, mem, k_adr, _igvn.type(k_adr)->is_ptr()));
aoqi@0 2211 #ifdef _LP64
aoqi@0 2212 if (UseCompressedClassPointers && klass_node->is_DecodeNKlass()) {
aoqi@0 2213 assert(klass_node->in(1)->Opcode() == Op_LoadNKlass, "sanity");
aoqi@0 2214 klass_node->in(1)->init_req(0, ctrl);
aoqi@0 2215 } else
aoqi@0 2216 #endif
aoqi@0 2217 klass_node->init_req(0, ctrl);
aoqi@0 2218 }
aoqi@0 2219 Node *proto_node = make_load(ctrl, mem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeX_X, TypeX_X->basic_type());
aoqi@0 2220
aoqi@0 2221 Node* thread = transform_later(new (C) ThreadLocalNode());
aoqi@0 2222 Node* cast_thread = transform_later(new (C) CastP2XNode(ctrl, thread));
aoqi@0 2223 Node* o_node = transform_later(new (C) OrXNode(cast_thread, proto_node));
aoqi@0 2224 Node* x_node = transform_later(new (C) XorXNode(o_node, mark_node));
aoqi@0 2225
aoqi@0 2226 // Get slow path - mark word does NOT match the value.
aoqi@0 2227 Node* not_biased_ctrl = opt_bits_test(ctrl, region, 3, x_node,
aoqi@0 2228 (~markOopDesc::age_mask_in_place), 0);
aoqi@0 2229 // region->in(3) is set to fast path - the object is biased to the current thread.
aoqi@0 2230 mem_phi->init_req(3, mem);
aoqi@0 2231
aoqi@0 2232
aoqi@0 2233 // Mark word does NOT match the value (thread | Klass::_prototype_header).
aoqi@0 2234
aoqi@0 2235
aoqi@0 2236 // First, check biased pattern.
aoqi@0 2237 // Get fast path - _prototype_header has the same biased lock pattern.
aoqi@0 2238 ctrl = opt_bits_test(not_biased_ctrl, fast_lock_region, 2, x_node,
aoqi@0 2239 markOopDesc::biased_lock_mask_in_place, 0, true);
aoqi@0 2240
aoqi@0 2241 not_biased_ctrl = fast_lock_region->in(2); // Slow path
aoqi@0 2242 // fast_lock_region->in(2) - the prototype header is no longer biased
aoqi@0 2243 // and we have to revoke the bias on this object.
aoqi@0 2244 // We are going to try to reset the mark of this object to the prototype
aoqi@0 2245 // value and fall through to the CAS-based locking scheme.
aoqi@0 2246 Node* adr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
aoqi@0 2247 Node* cas = new (C) StoreXConditionalNode(not_biased_ctrl, mem, adr,
aoqi@0 2248 proto_node, mark_node);
aoqi@0 2249 transform_later(cas);
aoqi@0 2250 Node* proj = transform_later( new (C) SCMemProjNode(cas));
aoqi@0 2251 fast_lock_mem_phi->init_req(2, proj);
aoqi@0 2252
aoqi@0 2253
aoqi@0 2254 // Second, check epoch bits.
aoqi@0 2255 Node* rebiased_region = new (C) RegionNode(3);
aoqi@0 2256 Node* old_phi = new (C) PhiNode( rebiased_region, TypeX_X);
aoqi@0 2257 Node* new_phi = new (C) PhiNode( rebiased_region, TypeX_X);
aoqi@0 2258
aoqi@0 2259 // Get slow path - mark word does NOT match epoch bits.
aoqi@0 2260 Node* epoch_ctrl = opt_bits_test(ctrl, rebiased_region, 1, x_node,
aoqi@0 2261 markOopDesc::epoch_mask_in_place, 0);
aoqi@0 2262 // The epoch of the current bias is not valid, attempt to rebias the object
aoqi@0 2263 // toward the current thread.
aoqi@0 2264 rebiased_region->init_req(2, epoch_ctrl);
aoqi@0 2265 old_phi->init_req(2, mark_node);
aoqi@0 2266 new_phi->init_req(2, o_node);
aoqi@0 2267
aoqi@0 2268 // rebiased_region->in(1) is set to fast path.
aoqi@0 2269 // The epoch of the current bias is still valid but we know
aoqi@0 2270 // nothing about the owner; it might be set or it might be clear.
aoqi@0 2271 Node* cmask = MakeConX(markOopDesc::biased_lock_mask_in_place |
aoqi@0 2272 markOopDesc::age_mask_in_place |
aoqi@0 2273 markOopDesc::epoch_mask_in_place);
aoqi@0 2274 Node* old = transform_later(new (C) AndXNode(mark_node, cmask));
aoqi@0 2275 cast_thread = transform_later(new (C) CastP2XNode(ctrl, thread));
aoqi@0 2276 Node* new_mark = transform_later(new (C) OrXNode(cast_thread, old));
aoqi@0 2277 old_phi->init_req(1, old);
aoqi@0 2278 new_phi->init_req(1, new_mark);
aoqi@0 2279
aoqi@0 2280 transform_later(rebiased_region);
aoqi@0 2281 transform_later(old_phi);
aoqi@0 2282 transform_later(new_phi);
aoqi@0 2283
aoqi@0 2284 // Try to acquire the bias of the object using an atomic operation.
aoqi@0 2285 // If this fails we will go in to the runtime to revoke the object's bias.
aoqi@0 2286 cas = new (C) StoreXConditionalNode(rebiased_region, mem, adr,
aoqi@0 2287 new_phi, old_phi);
aoqi@0 2288 transform_later(cas);
aoqi@0 2289 proj = transform_later( new (C) SCMemProjNode(cas));
aoqi@0 2290
aoqi@0 2291 // Get slow path - Failed to CAS.
aoqi@0 2292 not_biased_ctrl = opt_bits_test(rebiased_region, region, 4, cas, 0, 0);
aoqi@0 2293 mem_phi->init_req(4, proj);
aoqi@0 2294 // region->in(4) is set to fast path - the object is rebiased to the current thread.
aoqi@0 2295
aoqi@0 2296 // Failed to CAS.
aoqi@0 2297 slow_path = new (C) RegionNode(3);
aoqi@0 2298 Node *slow_mem = new (C) PhiNode( slow_path, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2299
aoqi@0 2300 slow_path->init_req(1, not_biased_ctrl); // Capture slow-control
aoqi@0 2301 slow_mem->init_req(1, proj);
aoqi@0 2302
aoqi@0 2303 // Call CAS-based locking scheme (FastLock node).
aoqi@0 2304
aoqi@0 2305 transform_later(fast_lock_region);
aoqi@0 2306 transform_later(fast_lock_mem_phi);
aoqi@0 2307
aoqi@0 2308 // Get slow path - FastLock failed to lock the object.
aoqi@0 2309 ctrl = opt_bits_test(fast_lock_region, region, 2, flock, 0, 0);
aoqi@0 2310 mem_phi->init_req(2, fast_lock_mem_phi);
aoqi@0 2311 // region->in(2) is set to fast path - the object is locked to the current thread.
aoqi@0 2312
aoqi@0 2313 slow_path->init_req(2, ctrl); // Capture slow-control
aoqi@0 2314 slow_mem->init_req(2, fast_lock_mem_phi);
aoqi@0 2315
aoqi@0 2316 transform_later(slow_path);
aoqi@0 2317 transform_later(slow_mem);
aoqi@0 2318 // Reset lock's memory edge.
aoqi@0 2319 lock->set_req(TypeFunc::Memory, slow_mem);
aoqi@0 2320
aoqi@0 2321 } else {
aoqi@0 2322 region = new (C) RegionNode(3);
aoqi@0 2323 // create a Phi for the memory state
aoqi@0 2324 mem_phi = new (C) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2325
aoqi@0 2326 // Optimize test; set region slot 2
aoqi@0 2327 slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0);
aoqi@0 2328 mem_phi->init_req(2, mem);
aoqi@0 2329 }
aoqi@0 2330
aoqi@0 2331 // Make slow path call
aoqi@0 2332 CallNode *call = make_slow_call( (CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, obj, box );
aoqi@0 2333
aoqi@0 2334 extract_call_projections(call);
aoqi@0 2335
aoqi@0 2336 // Slow path can only throw asynchronous exceptions, which are always
aoqi@0 2337 // de-opted. So the compiler thinks the slow-call can never throw an
aoqi@0 2338 // exception. If it DOES throw an exception we would need the debug
aoqi@0 2339 // info removed first (since if it throws there is no monitor).
aoqi@0 2340 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
aoqi@0 2341 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
aoqi@0 2342
aoqi@0 2343 // Capture slow path
aoqi@0 2344 // disconnect fall-through projection from call and create a new one
aoqi@0 2345 // hook up users of fall-through projection to region
aoqi@0 2346 Node *slow_ctrl = _fallthroughproj->clone();
aoqi@0 2347 transform_later(slow_ctrl);
aoqi@0 2348 _igvn.hash_delete(_fallthroughproj);
aoqi@0 2349 _fallthroughproj->disconnect_inputs(NULL, C);
aoqi@0 2350 region->init_req(1, slow_ctrl);
aoqi@0 2351 // region inputs are now complete
aoqi@0 2352 transform_later(region);
aoqi@0 2353 _igvn.replace_node(_fallthroughproj, region);
aoqi@0 2354
aoqi@0 2355 Node *memproj = transform_later( new(C) ProjNode(call, TypeFunc::Memory) );
aoqi@0 2356 mem_phi->init_req(1, memproj );
aoqi@0 2357 transform_later(mem_phi);
aoqi@0 2358 _igvn.replace_node(_memproj_fallthrough, mem_phi);
aoqi@0 2359 }
aoqi@0 2360
aoqi@0 2361 //------------------------------expand_unlock_node----------------------
aoqi@0 2362 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
aoqi@0 2363
aoqi@0 2364 Node* ctrl = unlock->in(TypeFunc::Control);
aoqi@0 2365 Node* mem = unlock->in(TypeFunc::Memory);
aoqi@0 2366 Node* obj = unlock->obj_node();
aoqi@0 2367 Node* box = unlock->box_node();
aoqi@0 2368
aoqi@0 2369 assert(!box->as_BoxLock()->is_eliminated(), "sanity");
aoqi@0 2370
aoqi@0 2371 // No need for a null check on unlock
aoqi@0 2372
aoqi@0 2373 // Make the merge point
aoqi@0 2374 Node *region;
aoqi@0 2375 Node *mem_phi;
aoqi@0 2376
aoqi@0 2377 if (UseOptoBiasInlining) {
aoqi@0 2378 // Check for biased locking unlock case, which is a no-op.
aoqi@0 2379 // See the full description in MacroAssembler::biased_locking_exit().
aoqi@0 2380 region = new (C) RegionNode(4);
aoqi@0 2381 // create a Phi for the memory state
aoqi@0 2382 mem_phi = new (C) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2383 mem_phi->init_req(3, mem);
aoqi@0 2384
aoqi@0 2385 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
aoqi@0 2386 ctrl = opt_bits_test(ctrl, region, 3, mark_node,
aoqi@0 2387 markOopDesc::biased_lock_mask_in_place,
aoqi@0 2388 markOopDesc::biased_lock_pattern);
aoqi@0 2389 } else {
aoqi@0 2390 region = new (C) RegionNode(3);
aoqi@0 2391 // create a Phi for the memory state
aoqi@0 2392 mem_phi = new (C) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
aoqi@0 2393 }
aoqi@0 2394
aoqi@0 2395 FastUnlockNode *funlock = new (C) FastUnlockNode( ctrl, obj, box );
aoqi@0 2396 funlock = transform_later( funlock )->as_FastUnlock();
aoqi@0 2397 // Optimize test; set region slot 2
aoqi@0 2398 Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0);
aoqi@0 2399
aoqi@0 2400 CallNode *call = make_slow_call( (CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), "complete_monitor_unlocking_C", slow_path, obj, box );
aoqi@0 2401
aoqi@0 2402 extract_call_projections(call);
aoqi@0 2403
aoqi@0 2404 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
aoqi@0 2405 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
aoqi@0 2406
aoqi@0 2407 // No exceptions for unlocking
aoqi@0 2408 // Capture slow path
aoqi@0 2409 // disconnect fall-through projection from call and create a new one
aoqi@0 2410 // hook up users of fall-through projection to region
aoqi@0 2411 Node *slow_ctrl = _fallthroughproj->clone();
aoqi@0 2412 transform_later(slow_ctrl);
aoqi@0 2413 _igvn.hash_delete(_fallthroughproj);
aoqi@0 2414 _fallthroughproj->disconnect_inputs(NULL, C);
aoqi@0 2415 region->init_req(1, slow_ctrl);
aoqi@0 2416 // region inputs are now complete
aoqi@0 2417 transform_later(region);
aoqi@0 2418 _igvn.replace_node(_fallthroughproj, region);
aoqi@0 2419
aoqi@0 2420 Node *memproj = transform_later( new(C) ProjNode(call, TypeFunc::Memory) );
aoqi@0 2421 mem_phi->init_req(1, memproj );
aoqi@0 2422 mem_phi->init_req(2, mem);
aoqi@0 2423 transform_later(mem_phi);
aoqi@0 2424 _igvn.replace_node(_memproj_fallthrough, mem_phi);
aoqi@0 2425 }
aoqi@0 2426
aoqi@0 2427 //---------------------------eliminate_macro_nodes----------------------
aoqi@0 2428 // Eliminate scalar replaced allocations and associated locks.
aoqi@0 2429 void PhaseMacroExpand::eliminate_macro_nodes() {
aoqi@0 2430 if (C->macro_count() == 0)
aoqi@0 2431 return;
aoqi@0 2432
aoqi@0 2433 // First, attempt to eliminate locks
aoqi@0 2434 int cnt = C->macro_count();
aoqi@0 2435 for (int i=0; i < cnt; i++) {
aoqi@0 2436 Node *n = C->macro_node(i);
aoqi@0 2437 if (n->is_AbstractLock()) { // Lock and Unlock nodes
aoqi@0 2438 // Before elimination mark all associated (same box and obj)
aoqi@0 2439 // lock and unlock nodes.
aoqi@0 2440 mark_eliminated_locking_nodes(n->as_AbstractLock());
aoqi@0 2441 }
aoqi@0 2442 }
aoqi@0 2443 bool progress = true;
aoqi@0 2444 while (progress) {
aoqi@0 2445 progress = false;
aoqi@0 2446 for (int i = C->macro_count(); i > 0; i--) {
aoqi@0 2447 Node * n = C->macro_node(i-1);
aoqi@0 2448 bool success = false;
aoqi@0 2449 debug_only(int old_macro_count = C->macro_count(););
aoqi@0 2450 if (n->is_AbstractLock()) {
aoqi@0 2451 success = eliminate_locking_node(n->as_AbstractLock());
aoqi@0 2452 }
aoqi@0 2453 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
aoqi@0 2454 progress = progress || success;
aoqi@0 2455 }
aoqi@0 2456 }
aoqi@0 2457 // Next, attempt to eliminate allocations
aoqi@0 2458 _has_locks = false;
aoqi@0 2459 progress = true;
aoqi@0 2460 while (progress) {
aoqi@0 2461 progress = false;
aoqi@0 2462 for (int i = C->macro_count(); i > 0; i--) {
aoqi@0 2463 Node * n = C->macro_node(i-1);
aoqi@0 2464 bool success = false;
aoqi@0 2465 debug_only(int old_macro_count = C->macro_count(););
aoqi@0 2466 switch (n->class_id()) {
aoqi@0 2467 case Node::Class_Allocate:
aoqi@0 2468 case Node::Class_AllocateArray:
aoqi@0 2469 success = eliminate_allocate_node(n->as_Allocate());
aoqi@0 2470 break;
aoqi@0 2471 case Node::Class_CallStaticJava:
aoqi@0 2472 success = eliminate_boxing_node(n->as_CallStaticJava());
aoqi@0 2473 break;
aoqi@0 2474 case Node::Class_Lock:
aoqi@0 2475 case Node::Class_Unlock:
aoqi@0 2476 assert(!n->as_AbstractLock()->is_eliminated(), "sanity");
aoqi@0 2477 _has_locks = true;
aoqi@0 2478 break;
aoqi@0 2479 default:
aoqi@0 2480 assert(n->Opcode() == Op_LoopLimit ||
aoqi@0 2481 n->Opcode() == Op_Opaque1 ||
aoqi@0 2482 n->Opcode() == Op_Opaque2 ||
aoqi@0 2483 n->Opcode() == Op_Opaque3, "unknown node type in macro list");
aoqi@0 2484 }
aoqi@0 2485 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
aoqi@0 2486 progress = progress || success;
aoqi@0 2487 }
aoqi@0 2488 }
aoqi@0 2489 }
aoqi@0 2490
aoqi@0 2491 //------------------------------expand_macro_nodes----------------------
aoqi@0 2492 // Returns true if a failure occurred.
aoqi@0 2493 bool PhaseMacroExpand::expand_macro_nodes() {
aoqi@0 2494 // Last attempt to eliminate macro nodes.
aoqi@0 2495 eliminate_macro_nodes();
aoqi@0 2496
aoqi@0 2497 // Make sure expansion will not cause node limit to be exceeded.
aoqi@0 2498 // Worst case is a macro node gets expanded into about 50 nodes.
aoqi@0 2499 // Allow 50% more for optimization.
aoqi@0 2500 if (C->check_node_count(C->macro_count() * 75, "out of nodes before macro expansion" ) )
aoqi@0 2501 return true;
aoqi@0 2502
aoqi@0 2503 // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations.
aoqi@0 2504 bool progress = true;
aoqi@0 2505 while (progress) {
aoqi@0 2506 progress = false;
aoqi@0 2507 for (int i = C->macro_count(); i > 0; i--) {
aoqi@0 2508 Node * n = C->macro_node(i-1);
aoqi@0 2509 bool success = false;
aoqi@0 2510 debug_only(int old_macro_count = C->macro_count(););
aoqi@0 2511 if (n->Opcode() == Op_LoopLimit) {
aoqi@0 2512 // Remove it from macro list and put on IGVN worklist to optimize.
aoqi@0 2513 C->remove_macro_node(n);
aoqi@0 2514 _igvn._worklist.push(n);
aoqi@0 2515 success = true;
aoqi@0 2516 } else if (n->Opcode() == Op_CallStaticJava) {
aoqi@0 2517 // Remove it from macro list and put on IGVN worklist to optimize.
aoqi@0 2518 C->remove_macro_node(n);
aoqi@0 2519 _igvn._worklist.push(n);
aoqi@0 2520 success = true;
aoqi@0 2521 } else if (n->Opcode() == Op_Opaque1 || n->Opcode() == Op_Opaque2) {
aoqi@0 2522 _igvn.replace_node(n, n->in(1));
aoqi@0 2523 success = true;
aoqi@0 2524 #if INCLUDE_RTM_OPT
aoqi@0 2525 } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) {
aoqi@0 2526 assert(C->profile_rtm(), "should be used only in rtm deoptimization code");
aoqi@0 2527 assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), "");
aoqi@0 2528 Node* cmp = n->unique_out();
aoqi@0 2529 #ifdef ASSERT
aoqi@0 2530 // Validate graph.
aoqi@0 2531 assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), "");
aoqi@0 2532 BoolNode* bol = cmp->unique_out()->as_Bool();
aoqi@0 2533 assert((bol->outcnt() == 1) && bol->unique_out()->is_If() &&
aoqi@0 2534 (bol->_test._test == BoolTest::ne), "");
aoqi@0 2535 IfNode* ifn = bol->unique_out()->as_If();
aoqi@0 2536 assert((ifn->outcnt() == 2) &&
aoqi@0 2537 ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change), "");
aoqi@0 2538 #endif
aoqi@0 2539 Node* repl = n->in(1);
aoqi@0 2540 if (!_has_locks) {
aoqi@0 2541 // Remove RTM state check if there are no locks in the code.
aoqi@0 2542 // Replace input to compare the same value.
aoqi@0 2543 repl = (cmp->in(1) == n) ? cmp->in(2) : cmp->in(1);
aoqi@0 2544 }
aoqi@0 2545 _igvn.replace_node(n, repl);
aoqi@0 2546 success = true;
aoqi@0 2547 #endif
aoqi@0 2548 }
aoqi@0 2549 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
aoqi@0 2550 progress = progress || success;
aoqi@0 2551 }
aoqi@0 2552 }
aoqi@0 2553
aoqi@0 2554 // expand "macro" nodes
aoqi@0 2555 // nodes are removed from the macro list as they are processed
aoqi@0 2556 while (C->macro_count() > 0) {
aoqi@0 2557 int macro_count = C->macro_count();
aoqi@0 2558 Node * n = C->macro_node(macro_count-1);
aoqi@0 2559 assert(n->is_macro(), "only macro nodes expected here");
aoqi@0 2560 if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) {
aoqi@0 2561 // node is unreachable, so don't try to expand it
aoqi@0 2562 C->remove_macro_node(n);
aoqi@0 2563 continue;
aoqi@0 2564 }
aoqi@0 2565 switch (n->class_id()) {
aoqi@0 2566 case Node::Class_Allocate:
aoqi@0 2567 expand_allocate(n->as_Allocate());
aoqi@0 2568 break;
aoqi@0 2569 case Node::Class_AllocateArray:
aoqi@0 2570 expand_allocate_array(n->as_AllocateArray());
aoqi@0 2571 break;
aoqi@0 2572 case Node::Class_Lock:
aoqi@0 2573 expand_lock_node(n->as_Lock());
aoqi@0 2574 break;
aoqi@0 2575 case Node::Class_Unlock:
aoqi@0 2576 expand_unlock_node(n->as_Unlock());
aoqi@0 2577 break;
aoqi@0 2578 default:
aoqi@0 2579 assert(false, "unknown node type in macro list");
aoqi@0 2580 }
aoqi@0 2581 assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
aoqi@0 2582 if (C->failing()) return true;
aoqi@0 2583 }
aoqi@0 2584
aoqi@0 2585 _igvn.set_delay_transform(false);
aoqi@0 2586 _igvn.optimize();
aoqi@0 2587 if (C->failing()) return true;
aoqi@0 2588 return false;
aoqi@0 2589 }

mercurial