src/share/vm/opto/memnode.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, 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 "classfile/systemDictionary.hpp"
aoqi@0 27 #include "compiler/compileLog.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "oops/objArrayKlass.hpp"
aoqi@0 30 #include "opto/addnode.hpp"
aoqi@0 31 #include "opto/cfgnode.hpp"
aoqi@0 32 #include "opto/compile.hpp"
aoqi@0 33 #include "opto/connode.hpp"
aoqi@0 34 #include "opto/loopnode.hpp"
aoqi@0 35 #include "opto/machnode.hpp"
aoqi@0 36 #include "opto/matcher.hpp"
aoqi@0 37 #include "opto/memnode.hpp"
aoqi@0 38 #include "opto/mulnode.hpp"
aoqi@0 39 #include "opto/phaseX.hpp"
aoqi@0 40 #include "opto/regmask.hpp"
aoqi@0 41
aoqi@0 42 // Portions of code courtesy of Clifford Click
aoqi@0 43
aoqi@0 44 // Optimization - Graph Style
aoqi@0 45
aoqi@0 46 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
aoqi@0 47
aoqi@0 48 //=============================================================================
aoqi@0 49 uint MemNode::size_of() const { return sizeof(*this); }
aoqi@0 50
aoqi@0 51 const TypePtr *MemNode::adr_type() const {
aoqi@0 52 Node* adr = in(Address);
aoqi@0 53 const TypePtr* cross_check = NULL;
aoqi@0 54 DEBUG_ONLY(cross_check = _adr_type);
aoqi@0 55 return calculate_adr_type(adr->bottom_type(), cross_check);
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 #ifndef PRODUCT
aoqi@0 59 void MemNode::dump_spec(outputStream *st) const {
aoqi@0 60 if (in(Address) == NULL) return; // node is dead
aoqi@0 61 #ifndef ASSERT
aoqi@0 62 // fake the missing field
aoqi@0 63 const TypePtr* _adr_type = NULL;
aoqi@0 64 if (in(Address) != NULL)
aoqi@0 65 _adr_type = in(Address)->bottom_type()->isa_ptr();
aoqi@0 66 #endif
aoqi@0 67 dump_adr_type(this, _adr_type, st);
aoqi@0 68
aoqi@0 69 Compile* C = Compile::current();
aoqi@0 70 if( C->alias_type(_adr_type)->is_volatile() )
aoqi@0 71 st->print(" Volatile!");
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 void MemNode::dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st) {
aoqi@0 75 st->print(" @");
aoqi@0 76 if (adr_type == NULL) {
aoqi@0 77 st->print("NULL");
aoqi@0 78 } else {
aoqi@0 79 adr_type->dump_on(st);
aoqi@0 80 Compile* C = Compile::current();
aoqi@0 81 Compile::AliasType* atp = NULL;
aoqi@0 82 if (C->have_alias_type(adr_type)) atp = C->alias_type(adr_type);
aoqi@0 83 if (atp == NULL)
aoqi@0 84 st->print(", idx=?\?;");
aoqi@0 85 else if (atp->index() == Compile::AliasIdxBot)
aoqi@0 86 st->print(", idx=Bot;");
aoqi@0 87 else if (atp->index() == Compile::AliasIdxTop)
aoqi@0 88 st->print(", idx=Top;");
aoqi@0 89 else if (atp->index() == Compile::AliasIdxRaw)
aoqi@0 90 st->print(", idx=Raw;");
aoqi@0 91 else {
aoqi@0 92 ciField* field = atp->field();
aoqi@0 93 if (field) {
aoqi@0 94 st->print(", name=");
aoqi@0 95 field->print_name_on(st);
aoqi@0 96 }
aoqi@0 97 st->print(", idx=%d;", atp->index());
aoqi@0 98 }
aoqi@0 99 }
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 extern void print_alias_types();
aoqi@0 103
aoqi@0 104 #endif
aoqi@0 105
aoqi@0 106 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) {
aoqi@0 107 assert((t_oop != NULL), "sanity");
aoqi@0 108 bool is_instance = t_oop->is_known_instance_field();
aoqi@0 109 bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() &&
aoqi@0 110 (load != NULL) && load->is_Load() &&
aoqi@0 111 (phase->is_IterGVN() != NULL);
aoqi@0 112 if (!(is_instance || is_boxed_value_load))
aoqi@0 113 return mchain; // don't try to optimize non-instance types
aoqi@0 114 uint instance_id = t_oop->instance_id();
aoqi@0 115 Node *start_mem = phase->C->start()->proj_out(TypeFunc::Memory);
aoqi@0 116 Node *prev = NULL;
aoqi@0 117 Node *result = mchain;
aoqi@0 118 while (prev != result) {
aoqi@0 119 prev = result;
aoqi@0 120 if (result == start_mem)
aoqi@0 121 break; // hit one of our sentinels
aoqi@0 122 // skip over a call which does not affect this memory slice
aoqi@0 123 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
aoqi@0 124 Node *proj_in = result->in(0);
aoqi@0 125 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
aoqi@0 126 break; // hit one of our sentinels
aoqi@0 127 } else if (proj_in->is_Call()) {
aoqi@0 128 CallNode *call = proj_in->as_Call();
aoqi@0 129 if (!call->may_modify(t_oop, phase)) { // returns false for instances
aoqi@0 130 result = call->in(TypeFunc::Memory);
aoqi@0 131 }
aoqi@0 132 } else if (proj_in->is_Initialize()) {
aoqi@0 133 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
aoqi@0 134 // Stop if this is the initialization for the object instance which
aoqi@0 135 // which contains this memory slice, otherwise skip over it.
aoqi@0 136 if ((alloc == NULL) || (alloc->_idx == instance_id)) {
aoqi@0 137 break;
aoqi@0 138 }
aoqi@0 139 if (is_instance) {
aoqi@0 140 result = proj_in->in(TypeFunc::Memory);
aoqi@0 141 } else if (is_boxed_value_load) {
aoqi@0 142 Node* klass = alloc->in(AllocateNode::KlassNode);
aoqi@0 143 const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr();
aoqi@0 144 if (tklass->klass_is_exact() && !tklass->klass()->equals(t_oop->klass())) {
aoqi@0 145 result = proj_in->in(TypeFunc::Memory); // not related allocation
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148 } else if (proj_in->is_MemBar()) {
aoqi@0 149 result = proj_in->in(TypeFunc::Memory);
aoqi@0 150 } else {
aoqi@0 151 assert(false, "unexpected projection");
aoqi@0 152 }
aoqi@0 153 } else if (result->is_ClearArray()) {
aoqi@0 154 if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) {
aoqi@0 155 // Can not bypass initialization of the instance
aoqi@0 156 // we are looking for.
aoqi@0 157 break;
aoqi@0 158 }
aoqi@0 159 // Otherwise skip it (the call updated 'result' value).
aoqi@0 160 } else if (result->is_MergeMem()) {
aoqi@0 161 result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, NULL, tty);
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164 return result;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) {
aoqi@0 168 const TypeOopPtr* t_oop = t_adr->isa_oopptr();
aoqi@0 169 if (t_oop == NULL)
aoqi@0 170 return mchain; // don't try to optimize non-oop types
aoqi@0 171 Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase);
aoqi@0 172 bool is_instance = t_oop->is_known_instance_field();
aoqi@0 173 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 174 if (is_instance && igvn != NULL && result->is_Phi()) {
aoqi@0 175 PhiNode *mphi = result->as_Phi();
aoqi@0 176 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
aoqi@0 177 const TypePtr *t = mphi->adr_type();
aoqi@0 178 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||
aoqi@0 179 t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&
aoqi@0 180 t->is_oopptr()->cast_to_exactness(true)
aoqi@0 181 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
aoqi@0 182 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop) {
aoqi@0 183 // clone the Phi with our address type
aoqi@0 184 result = mphi->split_out_instance(t_adr, igvn);
aoqi@0 185 } else {
aoqi@0 186 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189 return result;
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
aoqi@0 193 uint alias_idx = phase->C->get_alias_index(tp);
aoqi@0 194 Node *mem = mmem;
aoqi@0 195 #ifdef ASSERT
aoqi@0 196 {
aoqi@0 197 // Check that current type is consistent with the alias index used during graph construction
aoqi@0 198 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
aoqi@0 199 bool consistent = adr_check == NULL || adr_check->empty() ||
aoqi@0 200 phase->C->must_alias(adr_check, alias_idx );
aoqi@0 201 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
aoqi@0 202 if( !consistent && adr_check != NULL && !adr_check->empty() &&
aoqi@0 203 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
aoqi@0 204 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
aoqi@0 205 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
aoqi@0 206 adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
aoqi@0 207 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
aoqi@0 208 // don't assert if it is dead code.
aoqi@0 209 consistent = true;
aoqi@0 210 }
aoqi@0 211 if( !consistent ) {
aoqi@0 212 st->print("alias_idx==%d, adr_check==", alias_idx);
aoqi@0 213 if( adr_check == NULL ) {
aoqi@0 214 st->print("NULL");
aoqi@0 215 } else {
aoqi@0 216 adr_check->dump();
aoqi@0 217 }
aoqi@0 218 st->cr();
aoqi@0 219 print_alias_types();
aoqi@0 220 assert(consistent, "adr_check must match alias idx");
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223 #endif
aoqi@0 224 // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
aoqi@0 225 // means an array I have not precisely typed yet. Do not do any
aoqi@0 226 // alias stuff with it any time soon.
aoqi@0 227 const TypeOopPtr *toop = tp->isa_oopptr();
aoqi@0 228 if( tp->base() != Type::AnyPtr &&
aoqi@0 229 !(toop &&
aoqi@0 230 toop->klass() != NULL &&
aoqi@0 231 toop->klass()->is_java_lang_Object() &&
aoqi@0 232 toop->offset() == Type::OffsetBot) ) {
aoqi@0 233 // compress paths and change unreachable cycles to TOP
aoqi@0 234 // If not, we can update the input infinitely along a MergeMem cycle
aoqi@0 235 // Equivalent code in PhiNode::Ideal
aoqi@0 236 Node* m = phase->transform(mmem);
aoqi@0 237 // If transformed to a MergeMem, get the desired slice
aoqi@0 238 // Otherwise the returned node represents memory for every slice
aoqi@0 239 mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m;
aoqi@0 240 // Update input if it is progress over what we have now
aoqi@0 241 }
aoqi@0 242 return mem;
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 //--------------------------Ideal_common---------------------------------------
aoqi@0 246 // Look for degenerate control and memory inputs. Bypass MergeMem inputs.
aoqi@0 247 // Unhook non-raw memories from complete (macro-expanded) initializations.
aoqi@0 248 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
aoqi@0 249 // If our control input is a dead region, kill all below the region
aoqi@0 250 Node *ctl = in(MemNode::Control);
aoqi@0 251 if (ctl && remove_dead_region(phase, can_reshape))
aoqi@0 252 return this;
aoqi@0 253 ctl = in(MemNode::Control);
aoqi@0 254 // Don't bother trying to transform a dead node
aoqi@0 255 if (ctl && ctl->is_top()) return NodeSentinel;
aoqi@0 256
aoqi@0 257 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 258 // Wait if control on the worklist.
aoqi@0 259 if (ctl && can_reshape && igvn != NULL) {
aoqi@0 260 Node* bol = NULL;
aoqi@0 261 Node* cmp = NULL;
aoqi@0 262 if (ctl->in(0)->is_If()) {
aoqi@0 263 assert(ctl->is_IfTrue() || ctl->is_IfFalse(), "sanity");
aoqi@0 264 bol = ctl->in(0)->in(1);
aoqi@0 265 if (bol->is_Bool())
aoqi@0 266 cmp = ctl->in(0)->in(1)->in(1);
aoqi@0 267 }
aoqi@0 268 if (igvn->_worklist.member(ctl) ||
aoqi@0 269 (bol != NULL && igvn->_worklist.member(bol)) ||
aoqi@0 270 (cmp != NULL && igvn->_worklist.member(cmp)) ) {
aoqi@0 271 // This control path may be dead.
aoqi@0 272 // Delay this memory node transformation until the control is processed.
aoqi@0 273 phase->is_IterGVN()->_worklist.push(this);
aoqi@0 274 return NodeSentinel; // caller will return NULL
aoqi@0 275 }
aoqi@0 276 }
aoqi@0 277 // Ignore if memory is dead, or self-loop
aoqi@0 278 Node *mem = in(MemNode::Memory);
aoqi@0 279 if (phase->type( mem ) == Type::TOP) return NodeSentinel; // caller will return NULL
aoqi@0 280 assert(mem != this, "dead loop in MemNode::Ideal");
aoqi@0 281
aoqi@0 282 if (can_reshape && igvn != NULL && igvn->_worklist.member(mem)) {
aoqi@0 283 // This memory slice may be dead.
aoqi@0 284 // Delay this mem node transformation until the memory is processed.
aoqi@0 285 phase->is_IterGVN()->_worklist.push(this);
aoqi@0 286 return NodeSentinel; // caller will return NULL
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 Node *address = in(MemNode::Address);
aoqi@0 290 const Type *t_adr = phase->type(address);
aoqi@0 291 if (t_adr == Type::TOP) return NodeSentinel; // caller will return NULL
aoqi@0 292
aoqi@0 293 if (can_reshape && igvn != NULL &&
aoqi@0 294 (igvn->_worklist.member(address) ||
aoqi@0 295 igvn->_worklist.size() > 0 && (t_adr != adr_type())) ) {
aoqi@0 296 // The address's base and type may change when the address is processed.
aoqi@0 297 // Delay this mem node transformation until the address is processed.
aoqi@0 298 phase->is_IterGVN()->_worklist.push(this);
aoqi@0 299 return NodeSentinel; // caller will return NULL
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 // Do NOT remove or optimize the next lines: ensure a new alias index
aoqi@0 303 // is allocated for an oop pointer type before Escape Analysis.
aoqi@0 304 // Note: C++ will not remove it since the call has side effect.
aoqi@0 305 if (t_adr->isa_oopptr()) {
aoqi@0 306 int alias_idx = phase->C->get_alias_index(t_adr->is_ptr());
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 Node* base = NULL;
aoqi@0 310 if (address->is_AddP()) {
aoqi@0 311 base = address->in(AddPNode::Base);
aoqi@0 312 }
aoqi@0 313 if (base != NULL && phase->type(base)->higher_equal(TypePtr::NULL_PTR) &&
aoqi@0 314 !t_adr->isa_rawptr()) {
aoqi@0 315 // Note: raw address has TOP base and top->higher_equal(TypePtr::NULL_PTR) is true.
aoqi@0 316 // Skip this node optimization if its address has TOP base.
aoqi@0 317 return NodeSentinel; // caller will return NULL
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 // Avoid independent memory operations
aoqi@0 321 Node* old_mem = mem;
aoqi@0 322
aoqi@0 323 // The code which unhooks non-raw memories from complete (macro-expanded)
aoqi@0 324 // initializations was removed. After macro-expansion all stores catched
aoqi@0 325 // by Initialize node became raw stores and there is no information
aoqi@0 326 // which memory slices they modify. So it is unsafe to move any memory
aoqi@0 327 // operation above these stores. Also in most cases hooked non-raw memories
aoqi@0 328 // were already unhooked by using information from detect_ptr_independence()
aoqi@0 329 // and find_previous_store().
aoqi@0 330
aoqi@0 331 if (mem->is_MergeMem()) {
aoqi@0 332 MergeMemNode* mmem = mem->as_MergeMem();
aoqi@0 333 const TypePtr *tp = t_adr->is_ptr();
aoqi@0 334
aoqi@0 335 mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty);
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 if (mem != old_mem) {
aoqi@0 339 set_req(MemNode::Memory, mem);
aoqi@0 340 if (can_reshape && old_mem->outcnt() == 0) {
aoqi@0 341 igvn->_worklist.push(old_mem);
aoqi@0 342 }
aoqi@0 343 if (phase->type( mem ) == Type::TOP) return NodeSentinel;
aoqi@0 344 return this;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 // let the subclass continue analyzing...
aoqi@0 348 return NULL;
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 // Helper function for proving some simple control dominations.
aoqi@0 352 // Attempt to prove that all control inputs of 'dom' dominate 'sub'.
aoqi@0 353 // Already assumes that 'dom' is available at 'sub', and that 'sub'
aoqi@0 354 // is not a constant (dominated by the method's StartNode).
aoqi@0 355 // Used by MemNode::find_previous_store to prove that the
aoqi@0 356 // control input of a memory operation predates (dominates)
aoqi@0 357 // an allocation it wants to look past.
aoqi@0 358 bool MemNode::all_controls_dominate(Node* dom, Node* sub) {
aoqi@0 359 if (dom == NULL || dom->is_top() || sub == NULL || sub->is_top())
aoqi@0 360 return false; // Conservative answer for dead code
aoqi@0 361
aoqi@0 362 // Check 'dom'. Skip Proj and CatchProj nodes.
aoqi@0 363 dom = dom->find_exact_control(dom);
aoqi@0 364 if (dom == NULL || dom->is_top())
aoqi@0 365 return false; // Conservative answer for dead code
aoqi@0 366
aoqi@0 367 if (dom == sub) {
aoqi@0 368 // For the case when, for example, 'sub' is Initialize and the original
aoqi@0 369 // 'dom' is Proj node of the 'sub'.
aoqi@0 370 return false;
aoqi@0 371 }
aoqi@0 372
aoqi@0 373 if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub)
aoqi@0 374 return true;
aoqi@0 375
aoqi@0 376 // 'dom' dominates 'sub' if its control edge and control edges
aoqi@0 377 // of all its inputs dominate or equal to sub's control edge.
aoqi@0 378
aoqi@0 379 // Currently 'sub' is either Allocate, Initialize or Start nodes.
aoqi@0 380 // Or Region for the check in LoadNode::Ideal();
aoqi@0 381 // 'sub' should have sub->in(0) != NULL.
aoqi@0 382 assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() ||
aoqi@0 383 sub->is_Region() || sub->is_Call(), "expecting only these nodes");
aoqi@0 384
aoqi@0 385 // Get control edge of 'sub'.
aoqi@0 386 Node* orig_sub = sub;
aoqi@0 387 sub = sub->find_exact_control(sub->in(0));
aoqi@0 388 if (sub == NULL || sub->is_top())
aoqi@0 389 return false; // Conservative answer for dead code
aoqi@0 390
aoqi@0 391 assert(sub->is_CFG(), "expecting control");
aoqi@0 392
aoqi@0 393 if (sub == dom)
aoqi@0 394 return true;
aoqi@0 395
aoqi@0 396 if (sub->is_Start() || sub->is_Root())
aoqi@0 397 return false;
aoqi@0 398
aoqi@0 399 {
aoqi@0 400 // Check all control edges of 'dom'.
aoqi@0 401
aoqi@0 402 ResourceMark rm;
aoqi@0 403 Arena* arena = Thread::current()->resource_area();
aoqi@0 404 Node_List nlist(arena);
aoqi@0 405 Unique_Node_List dom_list(arena);
aoqi@0 406
aoqi@0 407 dom_list.push(dom);
aoqi@0 408 bool only_dominating_controls = false;
aoqi@0 409
aoqi@0 410 for (uint next = 0; next < dom_list.size(); next++) {
aoqi@0 411 Node* n = dom_list.at(next);
aoqi@0 412 if (n == orig_sub)
aoqi@0 413 return false; // One of dom's inputs dominated by sub.
aoqi@0 414 if (!n->is_CFG() && n->pinned()) {
aoqi@0 415 // Check only own control edge for pinned non-control nodes.
aoqi@0 416 n = n->find_exact_control(n->in(0));
aoqi@0 417 if (n == NULL || n->is_top())
aoqi@0 418 return false; // Conservative answer for dead code
aoqi@0 419 assert(n->is_CFG(), "expecting control");
aoqi@0 420 dom_list.push(n);
aoqi@0 421 } else if (n->is_Con() || n->is_Start() || n->is_Root()) {
aoqi@0 422 only_dominating_controls = true;
aoqi@0 423 } else if (n->is_CFG()) {
aoqi@0 424 if (n->dominates(sub, nlist))
aoqi@0 425 only_dominating_controls = true;
aoqi@0 426 else
aoqi@0 427 return false;
aoqi@0 428 } else {
aoqi@0 429 // First, own control edge.
aoqi@0 430 Node* m = n->find_exact_control(n->in(0));
aoqi@0 431 if (m != NULL) {
aoqi@0 432 if (m->is_top())
aoqi@0 433 return false; // Conservative answer for dead code
aoqi@0 434 dom_list.push(m);
aoqi@0 435 }
aoqi@0 436 // Now, the rest of edges.
aoqi@0 437 uint cnt = n->req();
aoqi@0 438 for (uint i = 1; i < cnt; i++) {
aoqi@0 439 m = n->find_exact_control(n->in(i));
aoqi@0 440 if (m == NULL || m->is_top())
aoqi@0 441 continue;
aoqi@0 442 dom_list.push(m);
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445 }
aoqi@0 446 return only_dominating_controls;
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 //---------------------detect_ptr_independence---------------------------------
aoqi@0 451 // Used by MemNode::find_previous_store to prove that two base
aoqi@0 452 // pointers are never equal.
aoqi@0 453 // The pointers are accompanied by their associated allocations,
aoqi@0 454 // if any, which have been previously discovered by the caller.
aoqi@0 455 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1,
aoqi@0 456 Node* p2, AllocateNode* a2,
aoqi@0 457 PhaseTransform* phase) {
aoqi@0 458 // Attempt to prove that these two pointers cannot be aliased.
aoqi@0 459 // They may both manifestly be allocations, and they should differ.
aoqi@0 460 // Or, if they are not both allocations, they can be distinct constants.
aoqi@0 461 // Otherwise, one is an allocation and the other a pre-existing value.
aoqi@0 462 if (a1 == NULL && a2 == NULL) { // neither an allocation
aoqi@0 463 return (p1 != p2) && p1->is_Con() && p2->is_Con();
aoqi@0 464 } else if (a1 != NULL && a2 != NULL) { // both allocations
aoqi@0 465 return (a1 != a2);
aoqi@0 466 } else if (a1 != NULL) { // one allocation a1
aoqi@0 467 // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.)
aoqi@0 468 return all_controls_dominate(p2, a1);
aoqi@0 469 } else { //(a2 != NULL) // one allocation a2
aoqi@0 470 return all_controls_dominate(p1, a2);
aoqi@0 471 }
aoqi@0 472 return false;
aoqi@0 473 }
aoqi@0 474
aoqi@0 475
aoqi@0 476 // The logic for reordering loads and stores uses four steps:
aoqi@0 477 // (a) Walk carefully past stores and initializations which we
aoqi@0 478 // can prove are independent of this load.
aoqi@0 479 // (b) Observe that the next memory state makes an exact match
aoqi@0 480 // with self (load or store), and locate the relevant store.
aoqi@0 481 // (c) Ensure that, if we were to wire self directly to the store,
aoqi@0 482 // the optimizer would fold it up somehow.
aoqi@0 483 // (d) Do the rewiring, and return, depending on some other part of
aoqi@0 484 // the optimizer to fold up the load.
aoqi@0 485 // This routine handles steps (a) and (b). Steps (c) and (d) are
aoqi@0 486 // specific to loads and stores, so they are handled by the callers.
aoqi@0 487 // (Currently, only LoadNode::Ideal has steps (c), (d). More later.)
aoqi@0 488 //
aoqi@0 489 Node* MemNode::find_previous_store(PhaseTransform* phase) {
aoqi@0 490 Node* ctrl = in(MemNode::Control);
aoqi@0 491 Node* adr = in(MemNode::Address);
aoqi@0 492 intptr_t offset = 0;
aoqi@0 493 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
aoqi@0 494 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
aoqi@0 495
aoqi@0 496 if (offset == Type::OffsetBot)
aoqi@0 497 return NULL; // cannot unalias unless there are precise offsets
aoqi@0 498
aoqi@0 499 const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr();
aoqi@0 500
aoqi@0 501 intptr_t size_in_bytes = memory_size();
aoqi@0 502
aoqi@0 503 Node* mem = in(MemNode::Memory); // start searching here...
aoqi@0 504
aoqi@0 505 int cnt = 50; // Cycle limiter
aoqi@0 506 for (;;) { // While we can dance past unrelated stores...
aoqi@0 507 if (--cnt < 0) break; // Caught in cycle or a complicated dance?
aoqi@0 508
aoqi@0 509 if (mem->is_Store()) {
aoqi@0 510 Node* st_adr = mem->in(MemNode::Address);
aoqi@0 511 intptr_t st_offset = 0;
aoqi@0 512 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
aoqi@0 513 if (st_base == NULL)
aoqi@0 514 break; // inscrutable pointer
aoqi@0 515 if (st_offset != offset && st_offset != Type::OffsetBot) {
aoqi@0 516 const int MAX_STORE = BytesPerLong;
aoqi@0 517 if (st_offset >= offset + size_in_bytes ||
aoqi@0 518 st_offset <= offset - MAX_STORE ||
aoqi@0 519 st_offset <= offset - mem->as_Store()->memory_size()) {
aoqi@0 520 // Success: The offsets are provably independent.
aoqi@0 521 // (You may ask, why not just test st_offset != offset and be done?
aoqi@0 522 // The answer is that stores of different sizes can co-exist
aoqi@0 523 // in the same sequence of RawMem effects. We sometimes initialize
aoqi@0 524 // a whole 'tile' of array elements with a single jint or jlong.)
aoqi@0 525 mem = mem->in(MemNode::Memory);
aoqi@0 526 continue; // (a) advance through independent store memory
aoqi@0 527 }
aoqi@0 528 }
aoqi@0 529 if (st_base != base &&
aoqi@0 530 detect_ptr_independence(base, alloc,
aoqi@0 531 st_base,
aoqi@0 532 AllocateNode::Ideal_allocation(st_base, phase),
aoqi@0 533 phase)) {
aoqi@0 534 // Success: The bases are provably independent.
aoqi@0 535 mem = mem->in(MemNode::Memory);
aoqi@0 536 continue; // (a) advance through independent store memory
aoqi@0 537 }
aoqi@0 538
aoqi@0 539 // (b) At this point, if the bases or offsets do not agree, we lose,
aoqi@0 540 // since we have not managed to prove 'this' and 'mem' independent.
aoqi@0 541 if (st_base == base && st_offset == offset) {
aoqi@0 542 return mem; // let caller handle steps (c), (d)
aoqi@0 543 }
aoqi@0 544
aoqi@0 545 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
aoqi@0 546 InitializeNode* st_init = mem->in(0)->as_Initialize();
aoqi@0 547 AllocateNode* st_alloc = st_init->allocation();
aoqi@0 548 if (st_alloc == NULL)
aoqi@0 549 break; // something degenerated
aoqi@0 550 bool known_identical = false;
aoqi@0 551 bool known_independent = false;
aoqi@0 552 if (alloc == st_alloc)
aoqi@0 553 known_identical = true;
aoqi@0 554 else if (alloc != NULL)
aoqi@0 555 known_independent = true;
aoqi@0 556 else if (all_controls_dominate(this, st_alloc))
aoqi@0 557 known_independent = true;
aoqi@0 558
aoqi@0 559 if (known_independent) {
aoqi@0 560 // The bases are provably independent: Either they are
aoqi@0 561 // manifestly distinct allocations, or else the control
aoqi@0 562 // of this load dominates the store's allocation.
aoqi@0 563 int alias_idx = phase->C->get_alias_index(adr_type());
aoqi@0 564 if (alias_idx == Compile::AliasIdxRaw) {
aoqi@0 565 mem = st_alloc->in(TypeFunc::Memory);
aoqi@0 566 } else {
aoqi@0 567 mem = st_init->memory(alias_idx);
aoqi@0 568 }
aoqi@0 569 continue; // (a) advance through independent store memory
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 // (b) at this point, if we are not looking at a store initializing
aoqi@0 573 // the same allocation we are loading from, we lose.
aoqi@0 574 if (known_identical) {
aoqi@0 575 // From caller, can_see_stored_value will consult find_captured_store.
aoqi@0 576 return mem; // let caller handle steps (c), (d)
aoqi@0 577 }
aoqi@0 578
aoqi@0 579 } else if (addr_t != NULL && addr_t->is_known_instance_field()) {
aoqi@0 580 // Can't use optimize_simple_memory_chain() since it needs PhaseGVN.
aoqi@0 581 if (mem->is_Proj() && mem->in(0)->is_Call()) {
aoqi@0 582 CallNode *call = mem->in(0)->as_Call();
aoqi@0 583 if (!call->may_modify(addr_t, phase)) {
aoqi@0 584 mem = call->in(TypeFunc::Memory);
aoqi@0 585 continue; // (a) advance through independent call memory
aoqi@0 586 }
aoqi@0 587 } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) {
aoqi@0 588 mem = mem->in(0)->in(TypeFunc::Memory);
aoqi@0 589 continue; // (a) advance through independent MemBar memory
aoqi@0 590 } else if (mem->is_ClearArray()) {
aoqi@0 591 if (ClearArrayNode::step_through(&mem, (uint)addr_t->instance_id(), phase)) {
aoqi@0 592 // (the call updated 'mem' value)
aoqi@0 593 continue; // (a) advance through independent allocation memory
aoqi@0 594 } else {
aoqi@0 595 // Can not bypass initialization of the instance
aoqi@0 596 // we are looking for.
aoqi@0 597 return mem;
aoqi@0 598 }
aoqi@0 599 } else if (mem->is_MergeMem()) {
aoqi@0 600 int alias_idx = phase->C->get_alias_index(adr_type());
aoqi@0 601 mem = mem->as_MergeMem()->memory_at(alias_idx);
aoqi@0 602 continue; // (a) advance through independent MergeMem memory
aoqi@0 603 }
aoqi@0 604 }
aoqi@0 605
aoqi@0 606 // Unless there is an explicit 'continue', we must bail out here,
aoqi@0 607 // because 'mem' is an inscrutable memory state (e.g., a call).
aoqi@0 608 break;
aoqi@0 609 }
aoqi@0 610
aoqi@0 611 return NULL; // bail out
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 //----------------------calculate_adr_type-------------------------------------
aoqi@0 615 // Helper function. Notices when the given type of address hits top or bottom.
aoqi@0 616 // Also, asserts a cross-check of the type against the expected address type.
aoqi@0 617 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) {
aoqi@0 618 if (t == Type::TOP) return NULL; // does not touch memory any more?
aoqi@0 619 #ifdef PRODUCT
aoqi@0 620 cross_check = NULL;
aoqi@0 621 #else
aoqi@0 622 if (!VerifyAliases || is_error_reported() || Node::in_dump()) cross_check = NULL;
aoqi@0 623 #endif
aoqi@0 624 const TypePtr* tp = t->isa_ptr();
aoqi@0 625 if (tp == NULL) {
aoqi@0 626 assert(cross_check == NULL || cross_check == TypePtr::BOTTOM, "expected memory type must be wide");
aoqi@0 627 return TypePtr::BOTTOM; // touches lots of memory
aoqi@0 628 } else {
aoqi@0 629 #ifdef ASSERT
aoqi@0 630 // %%%% [phh] We don't check the alias index if cross_check is
aoqi@0 631 // TypeRawPtr::BOTTOM. Needs to be investigated.
aoqi@0 632 if (cross_check != NULL &&
aoqi@0 633 cross_check != TypePtr::BOTTOM &&
aoqi@0 634 cross_check != TypeRawPtr::BOTTOM) {
aoqi@0 635 // Recheck the alias index, to see if it has changed (due to a bug).
aoqi@0 636 Compile* C = Compile::current();
aoqi@0 637 assert(C->get_alias_index(cross_check) == C->get_alias_index(tp),
aoqi@0 638 "must stay in the original alias category");
aoqi@0 639 // The type of the address must be contained in the adr_type,
aoqi@0 640 // disregarding "null"-ness.
aoqi@0 641 // (We make an exception for TypeRawPtr::BOTTOM, which is a bit bucket.)
aoqi@0 642 const TypePtr* tp_notnull = tp->join(TypePtr::NOTNULL)->is_ptr();
aoqi@0 643 assert(cross_check->meet(tp_notnull) == cross_check->remove_speculative(),
aoqi@0 644 "real address must not escape from expected memory type");
aoqi@0 645 }
aoqi@0 646 #endif
aoqi@0 647 return tp;
aoqi@0 648 }
aoqi@0 649 }
aoqi@0 650
aoqi@0 651 //------------------------adr_phi_is_loop_invariant----------------------------
aoqi@0 652 // A helper function for Ideal_DU_postCCP to check if a Phi in a counted
aoqi@0 653 // loop is loop invariant. Make a quick traversal of Phi and associated
aoqi@0 654 // CastPP nodes, looking to see if they are a closed group within the loop.
aoqi@0 655 bool MemNode::adr_phi_is_loop_invariant(Node* adr_phi, Node* cast) {
aoqi@0 656 // The idea is that the phi-nest must boil down to only CastPP nodes
aoqi@0 657 // with the same data. This implies that any path into the loop already
aoqi@0 658 // includes such a CastPP, and so the original cast, whatever its input,
aoqi@0 659 // must be covered by an equivalent cast, with an earlier control input.
aoqi@0 660 ResourceMark rm;
aoqi@0 661
aoqi@0 662 // The loop entry input of the phi should be the unique dominating
aoqi@0 663 // node for every Phi/CastPP in the loop.
aoqi@0 664 Unique_Node_List closure;
aoqi@0 665 closure.push(adr_phi->in(LoopNode::EntryControl));
aoqi@0 666
aoqi@0 667 // Add the phi node and the cast to the worklist.
aoqi@0 668 Unique_Node_List worklist;
aoqi@0 669 worklist.push(adr_phi);
aoqi@0 670 if( cast != NULL ){
aoqi@0 671 if( !cast->is_ConstraintCast() ) return false;
aoqi@0 672 worklist.push(cast);
aoqi@0 673 }
aoqi@0 674
aoqi@0 675 // Begin recursive walk of phi nodes.
aoqi@0 676 while( worklist.size() ){
aoqi@0 677 // Take a node off the worklist
aoqi@0 678 Node *n = worklist.pop();
aoqi@0 679 if( !closure.member(n) ){
aoqi@0 680 // Add it to the closure.
aoqi@0 681 closure.push(n);
aoqi@0 682 // Make a sanity check to ensure we don't waste too much time here.
aoqi@0 683 if( closure.size() > 20) return false;
aoqi@0 684 // This node is OK if:
aoqi@0 685 // - it is a cast of an identical value
aoqi@0 686 // - or it is a phi node (then we add its inputs to the worklist)
aoqi@0 687 // Otherwise, the node is not OK, and we presume the cast is not invariant
aoqi@0 688 if( n->is_ConstraintCast() ){
aoqi@0 689 worklist.push(n->in(1));
aoqi@0 690 } else if( n->is_Phi() ) {
aoqi@0 691 for( uint i = 1; i < n->req(); i++ ) {
aoqi@0 692 worklist.push(n->in(i));
aoqi@0 693 }
aoqi@0 694 } else {
aoqi@0 695 return false;
aoqi@0 696 }
aoqi@0 697 }
aoqi@0 698 }
aoqi@0 699
aoqi@0 700 // Quit when the worklist is empty, and we've found no offending nodes.
aoqi@0 701 return true;
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 //------------------------------Ideal_DU_postCCP-------------------------------
aoqi@0 705 // Find any cast-away of null-ness and keep its control. Null cast-aways are
aoqi@0 706 // going away in this pass and we need to make this memory op depend on the
aoqi@0 707 // gating null check.
aoqi@0 708 Node *MemNode::Ideal_DU_postCCP( PhaseCCP *ccp ) {
aoqi@0 709 return Ideal_common_DU_postCCP(ccp, this, in(MemNode::Address));
aoqi@0 710 }
aoqi@0 711
aoqi@0 712 // I tried to leave the CastPP's in. This makes the graph more accurate in
aoqi@0 713 // some sense; we get to keep around the knowledge that an oop is not-null
aoqi@0 714 // after some test. Alas, the CastPP's interfere with GVN (some values are
aoqi@0 715 // the regular oop, some are the CastPP of the oop, all merge at Phi's which
aoqi@0 716 // cannot collapse, etc). This cost us 10% on SpecJVM, even when I removed
aoqi@0 717 // some of the more trivial cases in the optimizer. Removing more useless
aoqi@0 718 // Phi's started allowing Loads to illegally float above null checks. I gave
aoqi@0 719 // up on this approach. CNC 10/20/2000
aoqi@0 720 // This static method may be called not from MemNode (EncodePNode calls it).
aoqi@0 721 // Only the control edge of the node 'n' might be updated.
aoqi@0 722 Node *MemNode::Ideal_common_DU_postCCP( PhaseCCP *ccp, Node* n, Node* adr ) {
aoqi@0 723 Node *skipped_cast = NULL;
aoqi@0 724 // Need a null check? Regular static accesses do not because they are
aoqi@0 725 // from constant addresses. Array ops are gated by the range check (which
aoqi@0 726 // always includes a NULL check). Just check field ops.
aoqi@0 727 if( n->in(MemNode::Control) == NULL ) {
aoqi@0 728 // Scan upwards for the highest location we can place this memory op.
aoqi@0 729 while( true ) {
aoqi@0 730 switch( adr->Opcode() ) {
aoqi@0 731
aoqi@0 732 case Op_AddP: // No change to NULL-ness, so peek thru AddP's
aoqi@0 733 adr = adr->in(AddPNode::Base);
aoqi@0 734 continue;
aoqi@0 735
aoqi@0 736 case Op_DecodeN: // No change to NULL-ness, so peek thru
aoqi@0 737 case Op_DecodeNKlass:
aoqi@0 738 adr = adr->in(1);
aoqi@0 739 continue;
aoqi@0 740
aoqi@0 741 case Op_EncodeP:
aoqi@0 742 case Op_EncodePKlass:
aoqi@0 743 // EncodeP node's control edge could be set by this method
aoqi@0 744 // when EncodeP node depends on CastPP node.
aoqi@0 745 //
aoqi@0 746 // Use its control edge for memory op because EncodeP may go away
aoqi@0 747 // later when it is folded with following or preceding DecodeN node.
aoqi@0 748 if (adr->in(0) == NULL) {
aoqi@0 749 // Keep looking for cast nodes.
aoqi@0 750 adr = adr->in(1);
aoqi@0 751 continue;
aoqi@0 752 }
aoqi@0 753 ccp->hash_delete(n);
aoqi@0 754 n->set_req(MemNode::Control, adr->in(0));
aoqi@0 755 ccp->hash_insert(n);
aoqi@0 756 return n;
aoqi@0 757
aoqi@0 758 case Op_CastPP:
aoqi@0 759 // If the CastPP is useless, just peek on through it.
aoqi@0 760 if( ccp->type(adr) == ccp->type(adr->in(1)) ) {
aoqi@0 761 // Remember the cast that we've peeked though. If we peek
aoqi@0 762 // through more than one, then we end up remembering the highest
aoqi@0 763 // one, that is, if in a loop, the one closest to the top.
aoqi@0 764 skipped_cast = adr;
aoqi@0 765 adr = adr->in(1);
aoqi@0 766 continue;
aoqi@0 767 }
aoqi@0 768 // CastPP is going away in this pass! We need this memory op to be
aoqi@0 769 // control-dependent on the test that is guarding the CastPP.
aoqi@0 770 ccp->hash_delete(n);
aoqi@0 771 n->set_req(MemNode::Control, adr->in(0));
aoqi@0 772 ccp->hash_insert(n);
aoqi@0 773 return n;
aoqi@0 774
aoqi@0 775 case Op_Phi:
aoqi@0 776 // Attempt to float above a Phi to some dominating point.
aoqi@0 777 if (adr->in(0) != NULL && adr->in(0)->is_CountedLoop()) {
aoqi@0 778 // If we've already peeked through a Cast (which could have set the
aoqi@0 779 // control), we can't float above a Phi, because the skipped Cast
aoqi@0 780 // may not be loop invariant.
aoqi@0 781 if (adr_phi_is_loop_invariant(adr, skipped_cast)) {
aoqi@0 782 adr = adr->in(1);
aoqi@0 783 continue;
aoqi@0 784 }
aoqi@0 785 }
aoqi@0 786
aoqi@0 787 // Intentional fallthrough!
aoqi@0 788
aoqi@0 789 // No obvious dominating point. The mem op is pinned below the Phi
aoqi@0 790 // by the Phi itself. If the Phi goes away (no true value is merged)
aoqi@0 791 // then the mem op can float, but not indefinitely. It must be pinned
aoqi@0 792 // behind the controls leading to the Phi.
aoqi@0 793 case Op_CheckCastPP:
aoqi@0 794 // These usually stick around to change address type, however a
aoqi@0 795 // useless one can be elided and we still need to pick up a control edge
aoqi@0 796 if (adr->in(0) == NULL) {
aoqi@0 797 // This CheckCastPP node has NO control and is likely useless. But we
aoqi@0 798 // need check further up the ancestor chain for a control input to keep
aoqi@0 799 // the node in place. 4959717.
aoqi@0 800 skipped_cast = adr;
aoqi@0 801 adr = adr->in(1);
aoqi@0 802 continue;
aoqi@0 803 }
aoqi@0 804 ccp->hash_delete(n);
aoqi@0 805 n->set_req(MemNode::Control, adr->in(0));
aoqi@0 806 ccp->hash_insert(n);
aoqi@0 807 return n;
aoqi@0 808
aoqi@0 809 // List of "safe" opcodes; those that implicitly block the memory
aoqi@0 810 // op below any null check.
aoqi@0 811 case Op_CastX2P: // no null checks on native pointers
aoqi@0 812 case Op_Parm: // 'this' pointer is not null
aoqi@0 813 case Op_LoadP: // Loading from within a klass
aoqi@0 814 case Op_LoadN: // Loading from within a klass
aoqi@0 815 case Op_LoadKlass: // Loading from within a klass
aoqi@0 816 case Op_LoadNKlass: // Loading from within a klass
aoqi@0 817 case Op_ConP: // Loading from a klass
aoqi@0 818 case Op_ConN: // Loading from a klass
aoqi@0 819 case Op_ConNKlass: // Loading from a klass
aoqi@0 820 case Op_CreateEx: // Sucking up the guts of an exception oop
aoqi@0 821 case Op_Con: // Reading from TLS
aoqi@0 822 case Op_CMoveP: // CMoveP is pinned
aoqi@0 823 case Op_CMoveN: // CMoveN is pinned
aoqi@0 824 break; // No progress
aoqi@0 825
aoqi@0 826 case Op_Proj: // Direct call to an allocation routine
aoqi@0 827 case Op_SCMemProj: // Memory state from store conditional ops
aoqi@0 828 #ifdef ASSERT
aoqi@0 829 {
aoqi@0 830 assert(adr->as_Proj()->_con == TypeFunc::Parms, "must be return value");
aoqi@0 831 const Node* call = adr->in(0);
aoqi@0 832 if (call->is_CallJava()) {
aoqi@0 833 const CallJavaNode* call_java = call->as_CallJava();
aoqi@0 834 const TypeTuple *r = call_java->tf()->range();
aoqi@0 835 assert(r->cnt() > TypeFunc::Parms, "must return value");
aoqi@0 836 const Type* ret_type = r->field_at(TypeFunc::Parms);
aoqi@0 837 assert(ret_type && ret_type->isa_ptr(), "must return pointer");
aoqi@0 838 // We further presume that this is one of
aoqi@0 839 // new_instance_Java, new_array_Java, or
aoqi@0 840 // the like, but do not assert for this.
aoqi@0 841 } else if (call->is_Allocate()) {
aoqi@0 842 // similar case to new_instance_Java, etc.
aoqi@0 843 } else if (!call->is_CallLeaf()) {
aoqi@0 844 // Projections from fetch_oop (OSR) are allowed as well.
aoqi@0 845 ShouldNotReachHere();
aoqi@0 846 }
aoqi@0 847 }
aoqi@0 848 #endif
aoqi@0 849 break;
aoqi@0 850 default:
aoqi@0 851 ShouldNotReachHere();
aoqi@0 852 }
aoqi@0 853 break;
aoqi@0 854 }
aoqi@0 855 }
aoqi@0 856
aoqi@0 857 return NULL; // No progress
aoqi@0 858 }
aoqi@0 859
aoqi@0 860
aoqi@0 861 //=============================================================================
aoqi@0 862 uint LoadNode::size_of() const { return sizeof(*this); }
aoqi@0 863 uint LoadNode::cmp( const Node &n ) const
aoqi@0 864 { return !Type::cmp( _type, ((LoadNode&)n)._type ); }
aoqi@0 865 const Type *LoadNode::bottom_type() const { return _type; }
aoqi@0 866 uint LoadNode::ideal_reg() const {
aoqi@0 867 return _type->ideal_reg();
aoqi@0 868 }
aoqi@0 869
aoqi@0 870 #ifndef PRODUCT
aoqi@0 871 void LoadNode::dump_spec(outputStream *st) const {
aoqi@0 872 MemNode::dump_spec(st);
aoqi@0 873 if( !Verbose && !WizardMode ) {
aoqi@0 874 // standard dump does this in Verbose and WizardMode
aoqi@0 875 st->print(" #"); _type->dump_on(st);
aoqi@0 876 }
aoqi@0 877 }
aoqi@0 878 #endif
aoqi@0 879
aoqi@0 880 #ifdef ASSERT
aoqi@0 881 //----------------------------is_immutable_value-------------------------------
aoqi@0 882 // Helper function to allow a raw load without control edge for some cases
aoqi@0 883 bool LoadNode::is_immutable_value(Node* adr) {
aoqi@0 884 return (adr->is_AddP() && adr->in(AddPNode::Base)->is_top() &&
aoqi@0 885 adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
aoqi@0 886 (adr->in(AddPNode::Offset)->find_intptr_t_con(-1) ==
aoqi@0 887 in_bytes(JavaThread::osthread_offset())));
aoqi@0 888 }
aoqi@0 889 #endif
aoqi@0 890
aoqi@0 891 //----------------------------LoadNode::make-----------------------------------
aoqi@0 892 // Polymorphic factory method:
aoqi@0 893 Node *LoadNode::make(PhaseGVN& gvn, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt, MemOrd mo) {
aoqi@0 894 Compile* C = gvn.C;
aoqi@0 895
aoqi@0 896 // sanity check the alias category against the created node type
aoqi@0 897 assert(!(adr_type->isa_oopptr() &&
aoqi@0 898 adr_type->offset() == oopDesc::klass_offset_in_bytes()),
aoqi@0 899 "use LoadKlassNode instead");
aoqi@0 900 assert(!(adr_type->isa_aryptr() &&
aoqi@0 901 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
aoqi@0 902 "use LoadRangeNode instead");
aoqi@0 903 // Check control edge of raw loads
aoqi@0 904 assert( ctl != NULL || C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
aoqi@0 905 // oop will be recorded in oop map if load crosses safepoint
aoqi@0 906 rt->isa_oopptr() || is_immutable_value(adr),
aoqi@0 907 "raw memory operations should have control edge");
aoqi@0 908 switch (bt) {
aoqi@0 909 case T_BOOLEAN: return new (C) LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(), mo);
aoqi@0 910 case T_BYTE: return new (C) LoadBNode (ctl, mem, adr, adr_type, rt->is_int(), mo);
aoqi@0 911 case T_INT: return new (C) LoadINode (ctl, mem, adr, adr_type, rt->is_int(), mo);
aoqi@0 912 case T_CHAR: return new (C) LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(), mo);
aoqi@0 913 case T_SHORT: return new (C) LoadSNode (ctl, mem, adr, adr_type, rt->is_int(), mo);
aoqi@0 914 case T_LONG: return new (C) LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo);
aoqi@0 915 case T_FLOAT: return new (C) LoadFNode (ctl, mem, adr, adr_type, rt, mo);
aoqi@0 916 case T_DOUBLE: return new (C) LoadDNode (ctl, mem, adr, adr_type, rt, mo);
aoqi@0 917 case T_ADDRESS: return new (C) LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(), mo);
aoqi@0 918 case T_OBJECT:
aoqi@0 919 #ifdef _LP64
aoqi@0 920 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
aoqi@0 921 Node* load = gvn.transform(new (C) LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo));
aoqi@0 922 return new (C) DecodeNNode(load, load->bottom_type()->make_ptr());
aoqi@0 923 } else
aoqi@0 924 #endif
aoqi@0 925 {
aoqi@0 926 assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop");
aoqi@0 927 return new (C) LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr(), mo);
aoqi@0 928 }
aoqi@0 929 }
aoqi@0 930 ShouldNotReachHere();
aoqi@0 931 return (LoadNode*)NULL;
aoqi@0 932 }
aoqi@0 933
aoqi@0 934 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt, MemOrd mo) {
aoqi@0 935 bool require_atomic = true;
aoqi@0 936 return new (C) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), mo, require_atomic);
aoqi@0 937 }
aoqi@0 938
aoqi@0 939
aoqi@0 940
aoqi@0 941
aoqi@0 942 //------------------------------hash-------------------------------------------
aoqi@0 943 uint LoadNode::hash() const {
aoqi@0 944 // unroll addition of interesting fields
aoqi@0 945 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
aoqi@0 946 }
aoqi@0 947
aoqi@0 948 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
aoqi@0 949 if ((atp != NULL) && (atp->index() >= Compile::AliasIdxRaw)) {
aoqi@0 950 bool non_volatile = (atp->field() != NULL) && !atp->field()->is_volatile();
aoqi@0 951 bool is_stable_ary = FoldStableValues &&
aoqi@0 952 (tp != NULL) && (tp->isa_aryptr() != NULL) &&
aoqi@0 953 tp->isa_aryptr()->is_stable();
aoqi@0 954
aoqi@0 955 return (eliminate_boxing && non_volatile) || is_stable_ary;
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 return false;
aoqi@0 959 }
aoqi@0 960
aoqi@0 961 //---------------------------can_see_stored_value------------------------------
aoqi@0 962 // This routine exists to make sure this set of tests is done the same
aoqi@0 963 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
aoqi@0 964 // will change the graph shape in a way which makes memory alive twice at the
aoqi@0 965 // same time (uses the Oracle model of aliasing), then some
aoqi@0 966 // LoadXNode::Identity will fold things back to the equivalence-class model
aoqi@0 967 // of aliasing.
aoqi@0 968 Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const {
aoqi@0 969 Node* ld_adr = in(MemNode::Address);
aoqi@0 970 intptr_t ld_off = 0;
aoqi@0 971 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
aoqi@0 972 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
aoqi@0 973 Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL;
aoqi@0 974 // This is more general than load from boxing objects.
aoqi@0 975 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
aoqi@0 976 uint alias_idx = atp->index();
aoqi@0 977 bool final = !atp->is_rewritable();
aoqi@0 978 Node* result = NULL;
aoqi@0 979 Node* current = st;
aoqi@0 980 // Skip through chains of MemBarNodes checking the MergeMems for
aoqi@0 981 // new states for the slice of this load. Stop once any other
aoqi@0 982 // kind of node is encountered. Loads from final memory can skip
aoqi@0 983 // through any kind of MemBar but normal loads shouldn't skip
aoqi@0 984 // through MemBarAcquire since the could allow them to move out of
aoqi@0 985 // a synchronized region.
aoqi@0 986 while (current->is_Proj()) {
aoqi@0 987 int opc = current->in(0)->Opcode();
aoqi@0 988 if ((final && (opc == Op_MemBarAcquire ||
aoqi@0 989 opc == Op_MemBarAcquireLock ||
aoqi@0 990 opc == Op_LoadFence)) ||
aoqi@0 991 opc == Op_MemBarRelease ||
aoqi@0 992 opc == Op_StoreFence ||
aoqi@0 993 opc == Op_MemBarReleaseLock ||
aoqi@0 994 opc == Op_MemBarCPUOrder) {
aoqi@0 995 Node* mem = current->in(0)->in(TypeFunc::Memory);
aoqi@0 996 if (mem->is_MergeMem()) {
aoqi@0 997 MergeMemNode* merge = mem->as_MergeMem();
aoqi@0 998 Node* new_st = merge->memory_at(alias_idx);
aoqi@0 999 if (new_st == merge->base_memory()) {
aoqi@0 1000 // Keep searching
aoqi@0 1001 current = new_st;
aoqi@0 1002 continue;
aoqi@0 1003 }
aoqi@0 1004 // Save the new memory state for the slice and fall through
aoqi@0 1005 // to exit.
aoqi@0 1006 result = new_st;
aoqi@0 1007 }
aoqi@0 1008 }
aoqi@0 1009 break;
aoqi@0 1010 }
aoqi@0 1011 if (result != NULL) {
aoqi@0 1012 st = result;
aoqi@0 1013 }
aoqi@0 1014 }
aoqi@0 1015
aoqi@0 1016 // Loop around twice in the case Load -> Initialize -> Store.
aoqi@0 1017 // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.)
aoqi@0 1018 for (int trip = 0; trip <= 1; trip++) {
aoqi@0 1019
aoqi@0 1020 if (st->is_Store()) {
aoqi@0 1021 Node* st_adr = st->in(MemNode::Address);
aoqi@0 1022 if (!phase->eqv(st_adr, ld_adr)) {
aoqi@0 1023 // Try harder before giving up... Match raw and non-raw pointers.
aoqi@0 1024 intptr_t st_off = 0;
aoqi@0 1025 AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off);
aoqi@0 1026 if (alloc == NULL) return NULL;
aoqi@0 1027 if (alloc != ld_alloc) return NULL;
aoqi@0 1028 if (ld_off != st_off) return NULL;
aoqi@0 1029 // At this point we have proven something like this setup:
aoqi@0 1030 // A = Allocate(...)
aoqi@0 1031 // L = LoadQ(, AddP(CastPP(, A.Parm),, #Off))
aoqi@0 1032 // S = StoreQ(, AddP(, A.Parm , #Off), V)
aoqi@0 1033 // (Actually, we haven't yet proven the Q's are the same.)
aoqi@0 1034 // In other words, we are loading from a casted version of
aoqi@0 1035 // the same pointer-and-offset that we stored to.
aoqi@0 1036 // Thus, we are able to replace L by V.
aoqi@0 1037 }
aoqi@0 1038 // Now prove that we have a LoadQ matched to a StoreQ, for some Q.
aoqi@0 1039 if (store_Opcode() != st->Opcode())
aoqi@0 1040 return NULL;
aoqi@0 1041 return st->in(MemNode::ValueIn);
aoqi@0 1042 }
aoqi@0 1043
aoqi@0 1044 // A load from a freshly-created object always returns zero.
aoqi@0 1045 // (This can happen after LoadNode::Ideal resets the load's memory input
aoqi@0 1046 // to find_captured_store, which returned InitializeNode::zero_memory.)
aoqi@0 1047 if (st->is_Proj() && st->in(0)->is_Allocate() &&
aoqi@0 1048 (st->in(0) == ld_alloc) &&
aoqi@0 1049 (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) {
aoqi@0 1050 // return a zero value for the load's basic type
aoqi@0 1051 // (This is one of the few places where a generic PhaseTransform
aoqi@0 1052 // can create new nodes. Think of it as lazily manifesting
aoqi@0 1053 // virtually pre-existing constants.)
aoqi@0 1054 return phase->zerocon(memory_type());
aoqi@0 1055 }
aoqi@0 1056
aoqi@0 1057 // A load from an initialization barrier can match a captured store.
aoqi@0 1058 if (st->is_Proj() && st->in(0)->is_Initialize()) {
aoqi@0 1059 InitializeNode* init = st->in(0)->as_Initialize();
aoqi@0 1060 AllocateNode* alloc = init->allocation();
aoqi@0 1061 if ((alloc != NULL) && (alloc == ld_alloc)) {
aoqi@0 1062 // examine a captured store value
aoqi@0 1063 st = init->find_captured_store(ld_off, memory_size(), phase);
aoqi@0 1064 if (st != NULL)
aoqi@0 1065 continue; // take one more trip around
aoqi@0 1066 }
aoqi@0 1067 }
aoqi@0 1068
aoqi@0 1069 // Load boxed value from result of valueOf() call is input parameter.
aoqi@0 1070 if (this->is_Load() && ld_adr->is_AddP() &&
aoqi@0 1071 (tp != NULL) && tp->is_ptr_to_boxed_value()) {
aoqi@0 1072 intptr_t ignore = 0;
aoqi@0 1073 Node* base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ignore);
aoqi@0 1074 if (base != NULL && base->is_Proj() &&
aoqi@0 1075 base->as_Proj()->_con == TypeFunc::Parms &&
aoqi@0 1076 base->in(0)->is_CallStaticJava() &&
aoqi@0 1077 base->in(0)->as_CallStaticJava()->is_boxing_method()) {
aoqi@0 1078 return base->in(0)->in(TypeFunc::Parms);
aoqi@0 1079 }
aoqi@0 1080 }
aoqi@0 1081
aoqi@0 1082 break;
aoqi@0 1083 }
aoqi@0 1084
aoqi@0 1085 return NULL;
aoqi@0 1086 }
aoqi@0 1087
aoqi@0 1088 //----------------------is_instance_field_load_with_local_phi------------------
aoqi@0 1089 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
aoqi@0 1090 if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl &&
aoqi@0 1091 in(Address)->is_AddP() ) {
aoqi@0 1092 const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr();
aoqi@0 1093 // Only instances and boxed values.
aoqi@0 1094 if( t_oop != NULL &&
aoqi@0 1095 (t_oop->is_ptr_to_boxed_value() ||
aoqi@0 1096 t_oop->is_known_instance_field()) &&
aoqi@0 1097 t_oop->offset() != Type::OffsetBot &&
aoqi@0 1098 t_oop->offset() != Type::OffsetTop) {
aoqi@0 1099 return true;
aoqi@0 1100 }
aoqi@0 1101 }
aoqi@0 1102 return false;
aoqi@0 1103 }
aoqi@0 1104
aoqi@0 1105 //------------------------------Identity---------------------------------------
aoqi@0 1106 // Loads are identity if previous store is to same address
aoqi@0 1107 Node *LoadNode::Identity( PhaseTransform *phase ) {
aoqi@0 1108 // If the previous store-maker is the right kind of Store, and the store is
aoqi@0 1109 // to the same address, then we are equal to the value stored.
aoqi@0 1110 Node* mem = in(Memory);
aoqi@0 1111 Node* value = can_see_stored_value(mem, phase);
aoqi@0 1112 if( value ) {
aoqi@0 1113 // byte, short & char stores truncate naturally.
aoqi@0 1114 // A load has to load the truncated value which requires
aoqi@0 1115 // some sort of masking operation and that requires an
aoqi@0 1116 // Ideal call instead of an Identity call.
aoqi@0 1117 if (memory_size() < BytesPerInt) {
aoqi@0 1118 // If the input to the store does not fit with the load's result type,
aoqi@0 1119 // it must be truncated via an Ideal call.
aoqi@0 1120 if (!phase->type(value)->higher_equal(phase->type(this)))
aoqi@0 1121 return this;
aoqi@0 1122 }
aoqi@0 1123 // (This works even when value is a Con, but LoadNode::Value
aoqi@0 1124 // usually runs first, producing the singleton type of the Con.)
aoqi@0 1125 return value;
aoqi@0 1126 }
aoqi@0 1127
aoqi@0 1128 // Search for an existing data phi which was generated before for the same
aoqi@0 1129 // instance's field to avoid infinite generation of phis in a loop.
aoqi@0 1130 Node *region = mem->in(0);
aoqi@0 1131 if (is_instance_field_load_with_local_phi(region)) {
aoqi@0 1132 const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr();
aoqi@0 1133 int this_index = phase->C->get_alias_index(addr_t);
aoqi@0 1134 int this_offset = addr_t->offset();
aoqi@0 1135 int this_iid = addr_t->instance_id();
aoqi@0 1136 if (!addr_t->is_known_instance() &&
aoqi@0 1137 addr_t->is_ptr_to_boxed_value()) {
aoqi@0 1138 // Use _idx of address base (could be Phi node) for boxed values.
aoqi@0 1139 intptr_t ignore = 0;
aoqi@0 1140 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
aoqi@0 1141 this_iid = base->_idx;
aoqi@0 1142 }
aoqi@0 1143 const Type* this_type = bottom_type();
aoqi@0 1144 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
aoqi@0 1145 Node* phi = region->fast_out(i);
aoqi@0 1146 if (phi->is_Phi() && phi != mem &&
aoqi@0 1147 phi->as_Phi()->is_same_inst_field(this_type, this_iid, this_index, this_offset)) {
aoqi@0 1148 return phi;
aoqi@0 1149 }
aoqi@0 1150 }
aoqi@0 1151 }
aoqi@0 1152
aoqi@0 1153 return this;
aoqi@0 1154 }
aoqi@0 1155
aoqi@0 1156 // We're loading from an object which has autobox behaviour.
aoqi@0 1157 // If this object is result of a valueOf call we'll have a phi
aoqi@0 1158 // merging a newly allocated object and a load from the cache.
aoqi@0 1159 // We want to replace this load with the original incoming
aoqi@0 1160 // argument to the valueOf call.
aoqi@0 1161 Node* LoadNode::eliminate_autobox(PhaseGVN* phase) {
aoqi@0 1162 assert(phase->C->eliminate_boxing(), "sanity");
aoqi@0 1163 intptr_t ignore = 0;
aoqi@0 1164 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
aoqi@0 1165 if ((base == NULL) || base->is_Phi()) {
aoqi@0 1166 // Push the loads from the phi that comes from valueOf up
aoqi@0 1167 // through it to allow elimination of the loads and the recovery
aoqi@0 1168 // of the original value. It is done in split_through_phi().
aoqi@0 1169 return NULL;
aoqi@0 1170 } else if (base->is_Load() ||
aoqi@0 1171 base->is_DecodeN() && base->in(1)->is_Load()) {
aoqi@0 1172 // Eliminate the load of boxed value for integer types from the cache
aoqi@0 1173 // array by deriving the value from the index into the array.
aoqi@0 1174 // Capture the offset of the load and then reverse the computation.
aoqi@0 1175
aoqi@0 1176 // Get LoadN node which loads a boxing object from 'cache' array.
aoqi@0 1177 if (base->is_DecodeN()) {
aoqi@0 1178 base = base->in(1);
aoqi@0 1179 }
aoqi@0 1180 if (!base->in(Address)->is_AddP()) {
aoqi@0 1181 return NULL; // Complex address
aoqi@0 1182 }
aoqi@0 1183 AddPNode* address = base->in(Address)->as_AddP();
aoqi@0 1184 Node* cache_base = address->in(AddPNode::Base);
aoqi@0 1185 if ((cache_base != NULL) && cache_base->is_DecodeN()) {
aoqi@0 1186 // Get ConP node which is static 'cache' field.
aoqi@0 1187 cache_base = cache_base->in(1);
aoqi@0 1188 }
aoqi@0 1189 if ((cache_base != NULL) && cache_base->is_Con()) {
aoqi@0 1190 const TypeAryPtr* base_type = cache_base->bottom_type()->isa_aryptr();
aoqi@0 1191 if ((base_type != NULL) && base_type->is_autobox_cache()) {
aoqi@0 1192 Node* elements[4];
aoqi@0 1193 int shift = exact_log2(type2aelembytes(T_OBJECT));
aoqi@0 1194 int count = address->unpack_offsets(elements, ARRAY_SIZE(elements));
aoqi@0 1195 if ((count > 0) && elements[0]->is_Con() &&
aoqi@0 1196 ((count == 1) ||
aoqi@0 1197 (count == 2) && elements[1]->Opcode() == Op_LShiftX &&
aoqi@0 1198 elements[1]->in(2) == phase->intcon(shift))) {
aoqi@0 1199 ciObjArray* array = base_type->const_oop()->as_obj_array();
aoqi@0 1200 // Fetch the box object cache[0] at the base of the array and get its value
aoqi@0 1201 ciInstance* box = array->obj_at(0)->as_instance();
aoqi@0 1202 ciInstanceKlass* ik = box->klass()->as_instance_klass();
aoqi@0 1203 assert(ik->is_box_klass(), "sanity");
aoqi@0 1204 assert(ik->nof_nonstatic_fields() == 1, "change following code");
aoqi@0 1205 if (ik->nof_nonstatic_fields() == 1) {
aoqi@0 1206 // This should be true nonstatic_field_at requires calling
aoqi@0 1207 // nof_nonstatic_fields so check it anyway
aoqi@0 1208 ciConstant c = box->field_value(ik->nonstatic_field_at(0));
aoqi@0 1209 BasicType bt = c.basic_type();
aoqi@0 1210 // Only integer types have boxing cache.
aoqi@0 1211 assert(bt == T_BOOLEAN || bt == T_CHAR ||
aoqi@0 1212 bt == T_BYTE || bt == T_SHORT ||
aoqi@0 1213 bt == T_INT || bt == T_LONG, err_msg_res("wrong type = %s", type2name(bt)));
aoqi@0 1214 jlong cache_low = (bt == T_LONG) ? c.as_long() : c.as_int();
aoqi@0 1215 if (cache_low != (int)cache_low) {
aoqi@0 1216 return NULL; // should not happen since cache is array indexed by value
aoqi@0 1217 }
aoqi@0 1218 jlong offset = arrayOopDesc::base_offset_in_bytes(T_OBJECT) - (cache_low << shift);
aoqi@0 1219 if (offset != (int)offset) {
aoqi@0 1220 return NULL; // should not happen since cache is array indexed by value
aoqi@0 1221 }
aoqi@0 1222 // Add up all the offsets making of the address of the load
aoqi@0 1223 Node* result = elements[0];
aoqi@0 1224 for (int i = 1; i < count; i++) {
aoqi@0 1225 result = phase->transform(new (phase->C) AddXNode(result, elements[i]));
aoqi@0 1226 }
aoqi@0 1227 // Remove the constant offset from the address and then
aoqi@0 1228 result = phase->transform(new (phase->C) AddXNode(result, phase->MakeConX(-(int)offset)));
aoqi@0 1229 // remove the scaling of the offset to recover the original index.
aoqi@0 1230 if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) {
aoqi@0 1231 // Peel the shift off directly but wrap it in a dummy node
aoqi@0 1232 // since Ideal can't return existing nodes
aoqi@0 1233 result = new (phase->C) RShiftXNode(result->in(1), phase->intcon(0));
aoqi@0 1234 } else if (result->is_Add() && result->in(2)->is_Con() &&
aoqi@0 1235 result->in(1)->Opcode() == Op_LShiftX &&
aoqi@0 1236 result->in(1)->in(2) == phase->intcon(shift)) {
aoqi@0 1237 // We can't do general optimization: ((X<<Z) + Y) >> Z ==> X + (Y>>Z)
aoqi@0 1238 // but for boxing cache access we know that X<<Z will not overflow
aoqi@0 1239 // (there is range check) so we do this optimizatrion by hand here.
aoqi@0 1240 Node* add_con = new (phase->C) RShiftXNode(result->in(2), phase->intcon(shift));
aoqi@0 1241 result = new (phase->C) AddXNode(result->in(1)->in(1), phase->transform(add_con));
aoqi@0 1242 } else {
aoqi@0 1243 result = new (phase->C) RShiftXNode(result, phase->intcon(shift));
aoqi@0 1244 }
aoqi@0 1245 #ifdef _LP64
aoqi@0 1246 if (bt != T_LONG) {
aoqi@0 1247 result = new (phase->C) ConvL2INode(phase->transform(result));
aoqi@0 1248 }
aoqi@0 1249 #else
aoqi@0 1250 if (bt == T_LONG) {
aoqi@0 1251 result = new (phase->C) ConvI2LNode(phase->transform(result));
aoqi@0 1252 }
aoqi@0 1253 #endif
aoqi@0 1254 return result;
aoqi@0 1255 }
aoqi@0 1256 }
aoqi@0 1257 }
aoqi@0 1258 }
aoqi@0 1259 }
aoqi@0 1260 return NULL;
aoqi@0 1261 }
aoqi@0 1262
aoqi@0 1263 static bool stable_phi(PhiNode* phi, PhaseGVN *phase) {
aoqi@0 1264 Node* region = phi->in(0);
aoqi@0 1265 if (region == NULL) {
aoqi@0 1266 return false; // Wait stable graph
aoqi@0 1267 }
aoqi@0 1268 uint cnt = phi->req();
aoqi@0 1269 for (uint i = 1; i < cnt; i++) {
aoqi@0 1270 Node* rc = region->in(i);
aoqi@0 1271 if (rc == NULL || phase->type(rc) == Type::TOP)
aoqi@0 1272 return false; // Wait stable graph
aoqi@0 1273 Node* in = phi->in(i);
aoqi@0 1274 if (in == NULL || phase->type(in) == Type::TOP)
aoqi@0 1275 return false; // Wait stable graph
aoqi@0 1276 }
aoqi@0 1277 return true;
aoqi@0 1278 }
aoqi@0 1279 //------------------------------split_through_phi------------------------------
aoqi@0 1280 // Split instance or boxed field load through Phi.
aoqi@0 1281 Node *LoadNode::split_through_phi(PhaseGVN *phase) {
aoqi@0 1282 Node* mem = in(Memory);
aoqi@0 1283 Node* address = in(Address);
aoqi@0 1284 const TypeOopPtr *t_oop = phase->type(address)->isa_oopptr();
aoqi@0 1285
aoqi@0 1286 assert((t_oop != NULL) &&
aoqi@0 1287 (t_oop->is_known_instance_field() ||
aoqi@0 1288 t_oop->is_ptr_to_boxed_value()), "invalide conditions");
aoqi@0 1289
aoqi@0 1290 Compile* C = phase->C;
aoqi@0 1291 intptr_t ignore = 0;
aoqi@0 1292 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
aoqi@0 1293 bool base_is_phi = (base != NULL) && base->is_Phi();
aoqi@0 1294 bool load_boxed_values = t_oop->is_ptr_to_boxed_value() && C->aggressive_unboxing() &&
aoqi@0 1295 (base != NULL) && (base == address->in(AddPNode::Base)) &&
aoqi@0 1296 phase->type(base)->higher_equal(TypePtr::NOTNULL);
aoqi@0 1297
aoqi@0 1298 if (!((mem->is_Phi() || base_is_phi) &&
aoqi@0 1299 (load_boxed_values || t_oop->is_known_instance_field()))) {
aoqi@0 1300 return NULL; // memory is not Phi
aoqi@0 1301 }
aoqi@0 1302
aoqi@0 1303 if (mem->is_Phi()) {
aoqi@0 1304 if (!stable_phi(mem->as_Phi(), phase)) {
aoqi@0 1305 return NULL; // Wait stable graph
aoqi@0 1306 }
aoqi@0 1307 uint cnt = mem->req();
aoqi@0 1308 // Check for loop invariant memory.
aoqi@0 1309 if (cnt == 3) {
aoqi@0 1310 for (uint i = 1; i < cnt; i++) {
aoqi@0 1311 Node* in = mem->in(i);
aoqi@0 1312 Node* m = optimize_memory_chain(in, t_oop, this, phase);
aoqi@0 1313 if (m == mem) {
aoqi@0 1314 set_req(Memory, mem->in(cnt - i));
aoqi@0 1315 return this; // made change
aoqi@0 1316 }
aoqi@0 1317 }
aoqi@0 1318 }
aoqi@0 1319 }
aoqi@0 1320 if (base_is_phi) {
aoqi@0 1321 if (!stable_phi(base->as_Phi(), phase)) {
aoqi@0 1322 return NULL; // Wait stable graph
aoqi@0 1323 }
aoqi@0 1324 uint cnt = base->req();
aoqi@0 1325 // Check for loop invariant memory.
aoqi@0 1326 if (cnt == 3) {
aoqi@0 1327 for (uint i = 1; i < cnt; i++) {
aoqi@0 1328 if (base->in(i) == base) {
aoqi@0 1329 return NULL; // Wait stable graph
aoqi@0 1330 }
aoqi@0 1331 }
aoqi@0 1332 }
aoqi@0 1333 }
aoqi@0 1334
aoqi@0 1335 bool load_boxed_phi = load_boxed_values && base_is_phi && (base->in(0) == mem->in(0));
aoqi@0 1336
aoqi@0 1337 // Split through Phi (see original code in loopopts.cpp).
aoqi@0 1338 assert(C->have_alias_type(t_oop), "instance should have alias type");
aoqi@0 1339
aoqi@0 1340 // Do nothing here if Identity will find a value
aoqi@0 1341 // (to avoid infinite chain of value phis generation).
aoqi@0 1342 if (!phase->eqv(this, this->Identity(phase)))
aoqi@0 1343 return NULL;
aoqi@0 1344
aoqi@0 1345 // Select Region to split through.
aoqi@0 1346 Node* region;
aoqi@0 1347 if (!base_is_phi) {
aoqi@0 1348 assert(mem->is_Phi(), "sanity");
aoqi@0 1349 region = mem->in(0);
aoqi@0 1350 // Skip if the region dominates some control edge of the address.
aoqi@0 1351 if (!MemNode::all_controls_dominate(address, region))
aoqi@0 1352 return NULL;
aoqi@0 1353 } else if (!mem->is_Phi()) {
aoqi@0 1354 assert(base_is_phi, "sanity");
aoqi@0 1355 region = base->in(0);
aoqi@0 1356 // Skip if the region dominates some control edge of the memory.
aoqi@0 1357 if (!MemNode::all_controls_dominate(mem, region))
aoqi@0 1358 return NULL;
aoqi@0 1359 } else if (base->in(0) != mem->in(0)) {
aoqi@0 1360 assert(base_is_phi && mem->is_Phi(), "sanity");
aoqi@0 1361 if (MemNode::all_controls_dominate(mem, base->in(0))) {
aoqi@0 1362 region = base->in(0);
aoqi@0 1363 } else if (MemNode::all_controls_dominate(address, mem->in(0))) {
aoqi@0 1364 region = mem->in(0);
aoqi@0 1365 } else {
aoqi@0 1366 return NULL; // complex graph
aoqi@0 1367 }
aoqi@0 1368 } else {
aoqi@0 1369 assert(base->in(0) == mem->in(0), "sanity");
aoqi@0 1370 region = mem->in(0);
aoqi@0 1371 }
aoqi@0 1372
aoqi@0 1373 const Type* this_type = this->bottom_type();
aoqi@0 1374 int this_index = C->get_alias_index(t_oop);
aoqi@0 1375 int this_offset = t_oop->offset();
aoqi@0 1376 int this_iid = t_oop->instance_id();
aoqi@0 1377 if (!t_oop->is_known_instance() && load_boxed_values) {
aoqi@0 1378 // Use _idx of address base for boxed values.
aoqi@0 1379 this_iid = base->_idx;
aoqi@0 1380 }
aoqi@0 1381 PhaseIterGVN* igvn = phase->is_IterGVN();
aoqi@0 1382 Node* phi = new (C) PhiNode(region, this_type, NULL, this_iid, this_index, this_offset);
aoqi@0 1383 for (uint i = 1; i < region->req(); i++) {
aoqi@0 1384 Node* x;
aoqi@0 1385 Node* the_clone = NULL;
aoqi@0 1386 if (region->in(i) == C->top()) {
aoqi@0 1387 x = C->top(); // Dead path? Use a dead data op
aoqi@0 1388 } else {
aoqi@0 1389 x = this->clone(); // Else clone up the data op
aoqi@0 1390 the_clone = x; // Remember for possible deletion.
aoqi@0 1391 // Alter data node to use pre-phi inputs
aoqi@0 1392 if (this->in(0) == region) {
aoqi@0 1393 x->set_req(0, region->in(i));
aoqi@0 1394 } else {
aoqi@0 1395 x->set_req(0, NULL);
aoqi@0 1396 }
aoqi@0 1397 if (mem->is_Phi() && (mem->in(0) == region)) {
aoqi@0 1398 x->set_req(Memory, mem->in(i)); // Use pre-Phi input for the clone.
aoqi@0 1399 }
aoqi@0 1400 if (address->is_Phi() && address->in(0) == region) {
aoqi@0 1401 x->set_req(Address, address->in(i)); // Use pre-Phi input for the clone
aoqi@0 1402 }
aoqi@0 1403 if (base_is_phi && (base->in(0) == region)) {
aoqi@0 1404 Node* base_x = base->in(i); // Clone address for loads from boxed objects.
aoqi@0 1405 Node* adr_x = phase->transform(new (C) AddPNode(base_x,base_x,address->in(AddPNode::Offset)));
aoqi@0 1406 x->set_req(Address, adr_x);
aoqi@0 1407 }
aoqi@0 1408 }
aoqi@0 1409 // Check for a 'win' on some paths
aoqi@0 1410 const Type *t = x->Value(igvn);
aoqi@0 1411
aoqi@0 1412 bool singleton = t->singleton();
aoqi@0 1413
aoqi@0 1414 // See comments in PhaseIdealLoop::split_thru_phi().
aoqi@0 1415 if (singleton && t == Type::TOP) {
aoqi@0 1416 singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
aoqi@0 1417 }
aoqi@0 1418
aoqi@0 1419 if (singleton) {
aoqi@0 1420 x = igvn->makecon(t);
aoqi@0 1421 } else {
aoqi@0 1422 // We now call Identity to try to simplify the cloned node.
aoqi@0 1423 // Note that some Identity methods call phase->type(this).
aoqi@0 1424 // Make sure that the type array is big enough for
aoqi@0 1425 // our new node, even though we may throw the node away.
aoqi@0 1426 // (This tweaking with igvn only works because x is a new node.)
aoqi@0 1427 igvn->set_type(x, t);
aoqi@0 1428 // If x is a TypeNode, capture any more-precise type permanently into Node
aoqi@0 1429 // otherwise it will be not updated during igvn->transform since
aoqi@0 1430 // igvn->type(x) is set to x->Value() already.
aoqi@0 1431 x->raise_bottom_type(t);
aoqi@0 1432 Node *y = x->Identity(igvn);
aoqi@0 1433 if (y != x) {
aoqi@0 1434 x = y;
aoqi@0 1435 } else {
aoqi@0 1436 y = igvn->hash_find_insert(x);
aoqi@0 1437 if (y) {
aoqi@0 1438 x = y;
aoqi@0 1439 } else {
aoqi@0 1440 // Else x is a new node we are keeping
aoqi@0 1441 // We do not need register_new_node_with_optimizer
aoqi@0 1442 // because set_type has already been called.
aoqi@0 1443 igvn->_worklist.push(x);
aoqi@0 1444 }
aoqi@0 1445 }
aoqi@0 1446 }
aoqi@0 1447 if (x != the_clone && the_clone != NULL) {
aoqi@0 1448 igvn->remove_dead_node(the_clone);
aoqi@0 1449 }
aoqi@0 1450 phi->set_req(i, x);
aoqi@0 1451 }
aoqi@0 1452 // Record Phi
aoqi@0 1453 igvn->register_new_node_with_optimizer(phi);
aoqi@0 1454 return phi;
aoqi@0 1455 }
aoqi@0 1456
aoqi@0 1457 //------------------------------Ideal------------------------------------------
aoqi@0 1458 // If the load is from Field memory and the pointer is non-null, we can
aoqi@0 1459 // zero out the control input.
aoqi@0 1460 // If the offset is constant and the base is an object allocation,
aoqi@0 1461 // try to hook me up to the exact initializing store.
aoqi@0 1462 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 1463 Node* p = MemNode::Ideal_common(phase, can_reshape);
aoqi@0 1464 if (p) return (p == NodeSentinel) ? NULL : p;
aoqi@0 1465
aoqi@0 1466 Node* ctrl = in(MemNode::Control);
aoqi@0 1467 Node* address = in(MemNode::Address);
aoqi@0 1468
aoqi@0 1469 // Skip up past a SafePoint control. Cannot do this for Stores because
aoqi@0 1470 // pointer stores & cardmarks must stay on the same side of a SafePoint.
aoqi@0 1471 if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint &&
aoqi@0 1472 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) {
aoqi@0 1473 ctrl = ctrl->in(0);
aoqi@0 1474 set_req(MemNode::Control,ctrl);
aoqi@0 1475 }
aoqi@0 1476
aoqi@0 1477 intptr_t ignore = 0;
aoqi@0 1478 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
aoqi@0 1479 if (base != NULL
aoqi@0 1480 && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) {
aoqi@0 1481 // Check for useless control edge in some common special cases
aoqi@0 1482 if (in(MemNode::Control) != NULL
aoqi@0 1483 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
aoqi@0 1484 && all_controls_dominate(base, phase->C->start())) {
aoqi@0 1485 // A method-invariant, non-null address (constant or 'this' argument).
aoqi@0 1486 set_req(MemNode::Control, NULL);
aoqi@0 1487 }
aoqi@0 1488 }
aoqi@0 1489
aoqi@0 1490 Node* mem = in(MemNode::Memory);
aoqi@0 1491 const TypePtr *addr_t = phase->type(address)->isa_ptr();
aoqi@0 1492
aoqi@0 1493 if (can_reshape && (addr_t != NULL)) {
aoqi@0 1494 // try to optimize our memory input
aoqi@0 1495 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase);
aoqi@0 1496 if (opt_mem != mem) {
aoqi@0 1497 set_req(MemNode::Memory, opt_mem);
aoqi@0 1498 if (phase->type( opt_mem ) == Type::TOP) return NULL;
aoqi@0 1499 return this;
aoqi@0 1500 }
aoqi@0 1501 const TypeOopPtr *t_oop = addr_t->isa_oopptr();
aoqi@0 1502 if ((t_oop != NULL) &&
aoqi@0 1503 (t_oop->is_known_instance_field() ||
aoqi@0 1504 t_oop->is_ptr_to_boxed_value())) {
aoqi@0 1505 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1506 if (igvn != NULL && igvn->_worklist.member(opt_mem)) {
aoqi@0 1507 // Delay this transformation until memory Phi is processed.
aoqi@0 1508 phase->is_IterGVN()->_worklist.push(this);
aoqi@0 1509 return NULL;
aoqi@0 1510 }
aoqi@0 1511 // Split instance field load through Phi.
aoqi@0 1512 Node* result = split_through_phi(phase);
aoqi@0 1513 if (result != NULL) return result;
aoqi@0 1514
aoqi@0 1515 if (t_oop->is_ptr_to_boxed_value()) {
aoqi@0 1516 Node* result = eliminate_autobox(phase);
aoqi@0 1517 if (result != NULL) return result;
aoqi@0 1518 }
aoqi@0 1519 }
aoqi@0 1520 }
aoqi@0 1521
aoqi@0 1522 // Check for prior store with a different base or offset; make Load
aoqi@0 1523 // independent. Skip through any number of them. Bail out if the stores
aoqi@0 1524 // are in an endless dead cycle and report no progress. This is a key
aoqi@0 1525 // transform for Reflection. However, if after skipping through the Stores
aoqi@0 1526 // we can't then fold up against a prior store do NOT do the transform as
aoqi@0 1527 // this amounts to using the 'Oracle' model of aliasing. It leaves the same
aoqi@0 1528 // array memory alive twice: once for the hoisted Load and again after the
aoqi@0 1529 // bypassed Store. This situation only works if EVERYBODY who does
aoqi@0 1530 // anti-dependence work knows how to bypass. I.e. we need all
aoqi@0 1531 // anti-dependence checks to ask the same Oracle. Right now, that Oracle is
aoqi@0 1532 // the alias index stuff. So instead, peek through Stores and IFF we can
aoqi@0 1533 // fold up, do so.
aoqi@0 1534 Node* prev_mem = find_previous_store(phase);
aoqi@0 1535 // Steps (a), (b): Walk past independent stores to find an exact match.
aoqi@0 1536 if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) {
aoqi@0 1537 // (c) See if we can fold up on the spot, but don't fold up here.
aoqi@0 1538 // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or
aoqi@0 1539 // just return a prior value, which is done by Identity calls.
aoqi@0 1540 if (can_see_stored_value(prev_mem, phase)) {
aoqi@0 1541 // Make ready for step (d):
aoqi@0 1542 set_req(MemNode::Memory, prev_mem);
aoqi@0 1543 return this;
aoqi@0 1544 }
aoqi@0 1545 }
aoqi@0 1546
aoqi@0 1547 return NULL; // No further progress
aoqi@0 1548 }
aoqi@0 1549
aoqi@0 1550 // Helper to recognize certain Klass fields which are invariant across
aoqi@0 1551 // some group of array types (e.g., int[] or all T[] where T < Object).
aoqi@0 1552 const Type*
aoqi@0 1553 LoadNode::load_array_final_field(const TypeKlassPtr *tkls,
aoqi@0 1554 ciKlass* klass) const {
aoqi@0 1555 if (tkls->offset() == in_bytes(Klass::modifier_flags_offset())) {
aoqi@0 1556 // The field is Klass::_modifier_flags. Return its (constant) value.
aoqi@0 1557 // (Folds up the 2nd indirection in aClassConstant.getModifiers().)
aoqi@0 1558 assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags");
aoqi@0 1559 return TypeInt::make(klass->modifier_flags());
aoqi@0 1560 }
aoqi@0 1561 if (tkls->offset() == in_bytes(Klass::access_flags_offset())) {
aoqi@0 1562 // The field is Klass::_access_flags. Return its (constant) value.
aoqi@0 1563 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).)
aoqi@0 1564 assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags");
aoqi@0 1565 return TypeInt::make(klass->access_flags());
aoqi@0 1566 }
aoqi@0 1567 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) {
aoqi@0 1568 // The field is Klass::_layout_helper. Return its constant value if known.
aoqi@0 1569 assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper");
aoqi@0 1570 return TypeInt::make(klass->layout_helper());
aoqi@0 1571 }
aoqi@0 1572
aoqi@0 1573 // No match.
aoqi@0 1574 return NULL;
aoqi@0 1575 }
aoqi@0 1576
aoqi@0 1577 // Try to constant-fold a stable array element.
aoqi@0 1578 static const Type* fold_stable_ary_elem(const TypeAryPtr* ary, int off, BasicType loadbt) {
aoqi@0 1579 assert(ary->const_oop(), "array should be constant");
aoqi@0 1580 assert(ary->is_stable(), "array should be stable");
aoqi@0 1581
aoqi@0 1582 // Decode the results of GraphKit::array_element_address.
aoqi@0 1583 ciArray* aobj = ary->const_oop()->as_array();
aoqi@0 1584 ciConstant con = aobj->element_value_by_offset(off);
aoqi@0 1585
aoqi@0 1586 if (con.basic_type() != T_ILLEGAL && !con.is_null_or_zero()) {
aoqi@0 1587 const Type* con_type = Type::make_from_constant(con);
aoqi@0 1588 if (con_type != NULL) {
aoqi@0 1589 if (con_type->isa_aryptr()) {
aoqi@0 1590 // Join with the array element type, in case it is also stable.
aoqi@0 1591 int dim = ary->stable_dimension();
aoqi@0 1592 con_type = con_type->is_aryptr()->cast_to_stable(true, dim-1);
aoqi@0 1593 }
aoqi@0 1594 if (loadbt == T_NARROWOOP && con_type->isa_oopptr()) {
aoqi@0 1595 con_type = con_type->make_narrowoop();
aoqi@0 1596 }
aoqi@0 1597 #ifndef PRODUCT
aoqi@0 1598 if (TraceIterativeGVN) {
aoqi@0 1599 tty->print("FoldStableValues: array element [off=%d]: con_type=", off);
aoqi@0 1600 con_type->dump(); tty->cr();
aoqi@0 1601 }
aoqi@0 1602 #endif //PRODUCT
aoqi@0 1603 return con_type;
aoqi@0 1604 }
aoqi@0 1605 }
aoqi@0 1606 return NULL;
aoqi@0 1607 }
aoqi@0 1608
aoqi@0 1609 //------------------------------Value-----------------------------------------
aoqi@0 1610 const Type *LoadNode::Value( PhaseTransform *phase ) const {
aoqi@0 1611 // Either input is TOP ==> the result is TOP
aoqi@0 1612 Node* mem = in(MemNode::Memory);
aoqi@0 1613 const Type *t1 = phase->type(mem);
aoqi@0 1614 if (t1 == Type::TOP) return Type::TOP;
aoqi@0 1615 Node* adr = in(MemNode::Address);
aoqi@0 1616 const TypePtr* tp = phase->type(adr)->isa_ptr();
aoqi@0 1617 if (tp == NULL || tp->empty()) return Type::TOP;
aoqi@0 1618 int off = tp->offset();
aoqi@0 1619 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
aoqi@0 1620 Compile* C = phase->C;
aoqi@0 1621
aoqi@0 1622 // Try to guess loaded type from pointer type
aoqi@0 1623 if (tp->isa_aryptr()) {
aoqi@0 1624 const TypeAryPtr* ary = tp->is_aryptr();
aoqi@0 1625 const Type* t = ary->elem();
aoqi@0 1626
aoqi@0 1627 // Determine whether the reference is beyond the header or not, by comparing
aoqi@0 1628 // the offset against the offset of the start of the array's data.
aoqi@0 1629 // Different array types begin at slightly different offsets (12 vs. 16).
aoqi@0 1630 // We choose T_BYTE as an example base type that is least restrictive
aoqi@0 1631 // as to alignment, which will therefore produce the smallest
aoqi@0 1632 // possible base offset.
aoqi@0 1633 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
aoqi@0 1634 const bool off_beyond_header = ((uint)off >= (uint)min_base_off);
aoqi@0 1635
aoqi@0 1636 // Try to constant-fold a stable array element.
aoqi@0 1637 if (FoldStableValues && ary->is_stable() && ary->const_oop() != NULL) {
aoqi@0 1638 // Make sure the reference is not into the header and the offset is constant
aoqi@0 1639 if (off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) {
aoqi@0 1640 const Type* con_type = fold_stable_ary_elem(ary, off, memory_type());
aoqi@0 1641 if (con_type != NULL) {
aoqi@0 1642 return con_type;
aoqi@0 1643 }
aoqi@0 1644 }
aoqi@0 1645 }
aoqi@0 1646
aoqi@0 1647 // Don't do this for integer types. There is only potential profit if
aoqi@0 1648 // the element type t is lower than _type; that is, for int types, if _type is
aoqi@0 1649 // more restrictive than t. This only happens here if one is short and the other
aoqi@0 1650 // char (both 16 bits), and in those cases we've made an intentional decision
aoqi@0 1651 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
aoqi@0 1652 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
aoqi@0 1653 //
aoqi@0 1654 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
aoqi@0 1655 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
aoqi@0 1656 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
aoqi@0 1657 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
aoqi@0 1658 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
aoqi@0 1659 // In fact, that could have been the original type of p1, and p1 could have
aoqi@0 1660 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
aoqi@0 1661 // expression (LShiftL quux 3) independently optimized to the constant 8.
aoqi@0 1662 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
aoqi@0 1663 && (_type->isa_vect() == NULL)
aoqi@0 1664 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
aoqi@0 1665 // t might actually be lower than _type, if _type is a unique
aoqi@0 1666 // concrete subclass of abstract class t.
aoqi@0 1667 if (off_beyond_header) { // is the offset beyond the header?
aoqi@0 1668 const Type* jt = t->join_speculative(_type);
aoqi@0 1669 // In any case, do not allow the join, per se, to empty out the type.
aoqi@0 1670 if (jt->empty() && !t->empty()) {
aoqi@0 1671 // This can happen if a interface-typed array narrows to a class type.
aoqi@0 1672 jt = _type;
aoqi@0 1673 }
aoqi@0 1674 #ifdef ASSERT
aoqi@0 1675 if (phase->C->eliminate_boxing() && adr->is_AddP()) {
aoqi@0 1676 // The pointers in the autobox arrays are always non-null
aoqi@0 1677 Node* base = adr->in(AddPNode::Base);
aoqi@0 1678 if ((base != NULL) && base->is_DecodeN()) {
aoqi@0 1679 // Get LoadN node which loads IntegerCache.cache field
aoqi@0 1680 base = base->in(1);
aoqi@0 1681 }
aoqi@0 1682 if ((base != NULL) && base->is_Con()) {
aoqi@0 1683 const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr();
aoqi@0 1684 if ((base_type != NULL) && base_type->is_autobox_cache()) {
aoqi@0 1685 // It could be narrow oop
aoqi@0 1686 assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity");
aoqi@0 1687 }
aoqi@0 1688 }
aoqi@0 1689 }
aoqi@0 1690 #endif
aoqi@0 1691 return jt;
aoqi@0 1692 }
aoqi@0 1693 }
aoqi@0 1694 } else if (tp->base() == Type::InstPtr) {
aoqi@0 1695 ciEnv* env = C->env();
aoqi@0 1696 const TypeInstPtr* tinst = tp->is_instptr();
aoqi@0 1697 ciKlass* klass = tinst->klass();
aoqi@0 1698 assert( off != Type::OffsetBot ||
aoqi@0 1699 // arrays can be cast to Objects
aoqi@0 1700 tp->is_oopptr()->klass()->is_java_lang_Object() ||
aoqi@0 1701 // unsafe field access may not have a constant offset
aoqi@0 1702 C->has_unsafe_access(),
aoqi@0 1703 "Field accesses must be precise" );
aoqi@0 1704 // For oop loads, we expect the _type to be precise
aoqi@0 1705 if (klass == env->String_klass() &&
aoqi@0 1706 adr->is_AddP() && off != Type::OffsetBot) {
aoqi@0 1707 // For constant Strings treat the final fields as compile time constants.
aoqi@0 1708 Node* base = adr->in(AddPNode::Base);
aoqi@0 1709 const TypeOopPtr* t = phase->type(base)->isa_oopptr();
aoqi@0 1710 if (t != NULL && t->singleton()) {
aoqi@0 1711 ciField* field = env->String_klass()->get_field_by_offset(off, false);
aoqi@0 1712 if (field != NULL && field->is_final()) {
aoqi@0 1713 ciObject* string = t->const_oop();
aoqi@0 1714 ciConstant constant = string->as_instance()->field_value(field);
aoqi@0 1715 if (constant.basic_type() == T_INT) {
aoqi@0 1716 return TypeInt::make(constant.as_int());
aoqi@0 1717 } else if (constant.basic_type() == T_ARRAY) {
aoqi@0 1718 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
aoqi@0 1719 return TypeNarrowOop::make_from_constant(constant.as_object(), true);
aoqi@0 1720 } else {
aoqi@0 1721 return TypeOopPtr::make_from_constant(constant.as_object(), true);
aoqi@0 1722 }
aoqi@0 1723 }
aoqi@0 1724 }
aoqi@0 1725 }
aoqi@0 1726 }
aoqi@0 1727 // Optimizations for constant objects
aoqi@0 1728 ciObject* const_oop = tinst->const_oop();
aoqi@0 1729 if (const_oop != NULL) {
aoqi@0 1730 // For constant Boxed value treat the target field as a compile time constant.
aoqi@0 1731 if (tinst->is_ptr_to_boxed_value()) {
aoqi@0 1732 return tinst->get_const_boxed_value();
aoqi@0 1733 } else
aoqi@0 1734 // For constant CallSites treat the target field as a compile time constant.
aoqi@0 1735 if (const_oop->is_call_site()) {
aoqi@0 1736 ciCallSite* call_site = const_oop->as_call_site();
aoqi@0 1737 ciField* field = call_site->klass()->as_instance_klass()->get_field_by_offset(off, /*is_static=*/ false);
aoqi@0 1738 if (field != NULL && field->is_call_site_target()) {
aoqi@0 1739 ciMethodHandle* target = call_site->get_target();
aoqi@0 1740 if (target != NULL) { // just in case
aoqi@0 1741 ciConstant constant(T_OBJECT, target);
aoqi@0 1742 const Type* t;
aoqi@0 1743 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
aoqi@0 1744 t = TypeNarrowOop::make_from_constant(constant.as_object(), true);
aoqi@0 1745 } else {
aoqi@0 1746 t = TypeOopPtr::make_from_constant(constant.as_object(), true);
aoqi@0 1747 }
aoqi@0 1748 // Add a dependence for invalidation of the optimization.
aoqi@0 1749 if (!call_site->is_constant_call_site()) {
aoqi@0 1750 C->dependencies()->assert_call_site_target_value(call_site, target);
aoqi@0 1751 }
aoqi@0 1752 return t;
aoqi@0 1753 }
aoqi@0 1754 }
aoqi@0 1755 }
aoqi@0 1756 }
aoqi@0 1757 } else if (tp->base() == Type::KlassPtr) {
aoqi@0 1758 assert( off != Type::OffsetBot ||
aoqi@0 1759 // arrays can be cast to Objects
aoqi@0 1760 tp->is_klassptr()->klass()->is_java_lang_Object() ||
aoqi@0 1761 // also allow array-loading from the primary supertype
aoqi@0 1762 // array during subtype checks
aoqi@0 1763 Opcode() == Op_LoadKlass,
aoqi@0 1764 "Field accesses must be precise" );
aoqi@0 1765 // For klass/static loads, we expect the _type to be precise
aoqi@0 1766 }
aoqi@0 1767
aoqi@0 1768 const TypeKlassPtr *tkls = tp->isa_klassptr();
aoqi@0 1769 if (tkls != NULL && !StressReflectiveCode) {
aoqi@0 1770 ciKlass* klass = tkls->klass();
aoqi@0 1771 if (klass->is_loaded() && tkls->klass_is_exact()) {
aoqi@0 1772 // We are loading a field from a Klass metaobject whose identity
aoqi@0 1773 // is known at compile time (the type is "exact" or "precise").
aoqi@0 1774 // Check for fields we know are maintained as constants by the VM.
aoqi@0 1775 if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) {
aoqi@0 1776 // The field is Klass::_super_check_offset. Return its (constant) value.
aoqi@0 1777 // (Folds up type checking code.)
aoqi@0 1778 assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset");
aoqi@0 1779 return TypeInt::make(klass->super_check_offset());
aoqi@0 1780 }
aoqi@0 1781 // Compute index into primary_supers array
aoqi@0 1782 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
aoqi@0 1783 // Check for overflowing; use unsigned compare to handle the negative case.
aoqi@0 1784 if( depth < ciKlass::primary_super_limit() ) {
aoqi@0 1785 // The field is an element of Klass::_primary_supers. Return its (constant) value.
aoqi@0 1786 // (Folds up type checking code.)
aoqi@0 1787 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
aoqi@0 1788 ciKlass *ss = klass->super_of_depth(depth);
aoqi@0 1789 return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
aoqi@0 1790 }
aoqi@0 1791 const Type* aift = load_array_final_field(tkls, klass);
aoqi@0 1792 if (aift != NULL) return aift;
aoqi@0 1793 if (tkls->offset() == in_bytes(ArrayKlass::component_mirror_offset())
aoqi@0 1794 && klass->is_array_klass()) {
aoqi@0 1795 // The field is ArrayKlass::_component_mirror. Return its (constant) value.
aoqi@0 1796 // (Folds up aClassConstant.getComponentType, common in Arrays.copyOf.)
aoqi@0 1797 assert(Opcode() == Op_LoadP, "must load an oop from _component_mirror");
aoqi@0 1798 return TypeInstPtr::make(klass->as_array_klass()->component_mirror());
aoqi@0 1799 }
aoqi@0 1800 if (tkls->offset() == in_bytes(Klass::java_mirror_offset())) {
aoqi@0 1801 // The field is Klass::_java_mirror. Return its (constant) value.
aoqi@0 1802 // (Folds up the 2nd indirection in anObjConstant.getClass().)
aoqi@0 1803 assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror");
aoqi@0 1804 return TypeInstPtr::make(klass->java_mirror());
aoqi@0 1805 }
aoqi@0 1806 }
aoqi@0 1807
aoqi@0 1808 // We can still check if we are loading from the primary_supers array at a
aoqi@0 1809 // shallow enough depth. Even though the klass is not exact, entries less
aoqi@0 1810 // than or equal to its super depth are correct.
aoqi@0 1811 if (klass->is_loaded() ) {
aoqi@0 1812 ciType *inner = klass;
aoqi@0 1813 while( inner->is_obj_array_klass() )
aoqi@0 1814 inner = inner->as_obj_array_klass()->base_element_type();
aoqi@0 1815 if( inner->is_instance_klass() &&
aoqi@0 1816 !inner->as_instance_klass()->flags().is_interface() ) {
aoqi@0 1817 // Compute index into primary_supers array
aoqi@0 1818 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
aoqi@0 1819 // Check for overflowing; use unsigned compare to handle the negative case.
aoqi@0 1820 if( depth < ciKlass::primary_super_limit() &&
aoqi@0 1821 depth <= klass->super_depth() ) { // allow self-depth checks to handle self-check case
aoqi@0 1822 // The field is an element of Klass::_primary_supers. Return its (constant) value.
aoqi@0 1823 // (Folds up type checking code.)
aoqi@0 1824 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
aoqi@0 1825 ciKlass *ss = klass->super_of_depth(depth);
aoqi@0 1826 return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
aoqi@0 1827 }
aoqi@0 1828 }
aoqi@0 1829 }
aoqi@0 1830
aoqi@0 1831 // If the type is enough to determine that the thing is not an array,
aoqi@0 1832 // we can give the layout_helper a positive interval type.
aoqi@0 1833 // This will help short-circuit some reflective code.
aoqi@0 1834 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())
aoqi@0 1835 && !klass->is_array_klass() // not directly typed as an array
aoqi@0 1836 && !klass->is_interface() // specifically not Serializable & Cloneable
aoqi@0 1837 && !klass->is_java_lang_Object() // not the supertype of all T[]
aoqi@0 1838 ) {
aoqi@0 1839 // Note: When interfaces are reliable, we can narrow the interface
aoqi@0 1840 // test to (klass != Serializable && klass != Cloneable).
aoqi@0 1841 assert(Opcode() == Op_LoadI, "must load an int from _layout_helper");
aoqi@0 1842 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
aoqi@0 1843 // The key property of this type is that it folds up tests
aoqi@0 1844 // for array-ness, since it proves that the layout_helper is positive.
aoqi@0 1845 // Thus, a generic value like the basic object layout helper works fine.
aoqi@0 1846 return TypeInt::make(min_size, max_jint, Type::WidenMin);
aoqi@0 1847 }
aoqi@0 1848 }
aoqi@0 1849
aoqi@0 1850 // If we are loading from a freshly-allocated object, produce a zero,
aoqi@0 1851 // if the load is provably beyond the header of the object.
aoqi@0 1852 // (Also allow a variable load from a fresh array to produce zero.)
aoqi@0 1853 const TypeOopPtr *tinst = tp->isa_oopptr();
aoqi@0 1854 bool is_instance = (tinst != NULL) && tinst->is_known_instance_field();
aoqi@0 1855 bool is_boxed_value = (tinst != NULL) && tinst->is_ptr_to_boxed_value();
aoqi@0 1856 if (ReduceFieldZeroing || is_instance || is_boxed_value) {
aoqi@0 1857 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1858 if (value != NULL && value->is_Con()) {
aoqi@0 1859 assert(value->bottom_type()->higher_equal(_type),"sanity");
aoqi@0 1860 return value->bottom_type();
aoqi@0 1861 }
aoqi@0 1862 }
aoqi@0 1863
aoqi@0 1864 if (is_instance) {
aoqi@0 1865 // If we have an instance type and our memory input is the
aoqi@0 1866 // programs's initial memory state, there is no matching store,
aoqi@0 1867 // so just return a zero of the appropriate type
aoqi@0 1868 Node *mem = in(MemNode::Memory);
aoqi@0 1869 if (mem->is_Parm() && mem->in(0)->is_Start()) {
aoqi@0 1870 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
aoqi@0 1871 return Type::get_zero_type(_type->basic_type());
aoqi@0 1872 }
aoqi@0 1873 }
aoqi@0 1874 return _type;
aoqi@0 1875 }
aoqi@0 1876
aoqi@0 1877 //------------------------------match_edge-------------------------------------
aoqi@0 1878 // Do we Match on this edge index or not? Match only the address.
aoqi@0 1879 uint LoadNode::match_edge(uint idx) const {
aoqi@0 1880 return idx == MemNode::Address;
aoqi@0 1881 }
aoqi@0 1882
aoqi@0 1883 //--------------------------LoadBNode::Ideal--------------------------------------
aoqi@0 1884 //
aoqi@0 1885 // If the previous store is to the same address as this load,
aoqi@0 1886 // and the value stored was larger than a byte, replace this load
aoqi@0 1887 // with the value stored truncated to a byte. If no truncation is
aoqi@0 1888 // needed, the replacement is done in LoadNode::Identity().
aoqi@0 1889 //
aoqi@0 1890 Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 1891 Node* mem = in(MemNode::Memory);
aoqi@0 1892 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1893 if( value && !phase->type(value)->higher_equal( _type ) ) {
aoqi@0 1894 Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(24)) );
aoqi@0 1895 return new (phase->C) RShiftINode(result, phase->intcon(24));
aoqi@0 1896 }
aoqi@0 1897 // Identity call will handle the case where truncation is not needed.
aoqi@0 1898 return LoadNode::Ideal(phase, can_reshape);
aoqi@0 1899 }
aoqi@0 1900
aoqi@0 1901 const Type* LoadBNode::Value(PhaseTransform *phase) const {
aoqi@0 1902 Node* mem = in(MemNode::Memory);
aoqi@0 1903 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1904 if (value != NULL && value->is_Con() &&
aoqi@0 1905 !value->bottom_type()->higher_equal(_type)) {
aoqi@0 1906 // If the input to the store does not fit with the load's result type,
aoqi@0 1907 // it must be truncated. We can't delay until Ideal call since
aoqi@0 1908 // a singleton Value is needed for split_thru_phi optimization.
aoqi@0 1909 int con = value->get_int();
aoqi@0 1910 return TypeInt::make((con << 24) >> 24);
aoqi@0 1911 }
aoqi@0 1912 return LoadNode::Value(phase);
aoqi@0 1913 }
aoqi@0 1914
aoqi@0 1915 //--------------------------LoadUBNode::Ideal-------------------------------------
aoqi@0 1916 //
aoqi@0 1917 // If the previous store is to the same address as this load,
aoqi@0 1918 // and the value stored was larger than a byte, replace this load
aoqi@0 1919 // with the value stored truncated to a byte. If no truncation is
aoqi@0 1920 // needed, the replacement is done in LoadNode::Identity().
aoqi@0 1921 //
aoqi@0 1922 Node* LoadUBNode::Ideal(PhaseGVN* phase, bool can_reshape) {
aoqi@0 1923 Node* mem = in(MemNode::Memory);
aoqi@0 1924 Node* value = can_see_stored_value(mem, phase);
aoqi@0 1925 if (value && !phase->type(value)->higher_equal(_type))
aoqi@0 1926 return new (phase->C) AndINode(value, phase->intcon(0xFF));
aoqi@0 1927 // Identity call will handle the case where truncation is not needed.
aoqi@0 1928 return LoadNode::Ideal(phase, can_reshape);
aoqi@0 1929 }
aoqi@0 1930
aoqi@0 1931 const Type* LoadUBNode::Value(PhaseTransform *phase) const {
aoqi@0 1932 Node* mem = in(MemNode::Memory);
aoqi@0 1933 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1934 if (value != NULL && value->is_Con() &&
aoqi@0 1935 !value->bottom_type()->higher_equal(_type)) {
aoqi@0 1936 // If the input to the store does not fit with the load's result type,
aoqi@0 1937 // it must be truncated. We can't delay until Ideal call since
aoqi@0 1938 // a singleton Value is needed for split_thru_phi optimization.
aoqi@0 1939 int con = value->get_int();
aoqi@0 1940 return TypeInt::make(con & 0xFF);
aoqi@0 1941 }
aoqi@0 1942 return LoadNode::Value(phase);
aoqi@0 1943 }
aoqi@0 1944
aoqi@0 1945 //--------------------------LoadUSNode::Ideal-------------------------------------
aoqi@0 1946 //
aoqi@0 1947 // If the previous store is to the same address as this load,
aoqi@0 1948 // and the value stored was larger than a char, replace this load
aoqi@0 1949 // with the value stored truncated to a char. If no truncation is
aoqi@0 1950 // needed, the replacement is done in LoadNode::Identity().
aoqi@0 1951 //
aoqi@0 1952 Node *LoadUSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 1953 Node* mem = in(MemNode::Memory);
aoqi@0 1954 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1955 if( value && !phase->type(value)->higher_equal( _type ) )
aoqi@0 1956 return new (phase->C) AndINode(value,phase->intcon(0xFFFF));
aoqi@0 1957 // Identity call will handle the case where truncation is not needed.
aoqi@0 1958 return LoadNode::Ideal(phase, can_reshape);
aoqi@0 1959 }
aoqi@0 1960
aoqi@0 1961 const Type* LoadUSNode::Value(PhaseTransform *phase) const {
aoqi@0 1962 Node* mem = in(MemNode::Memory);
aoqi@0 1963 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1964 if (value != NULL && value->is_Con() &&
aoqi@0 1965 !value->bottom_type()->higher_equal(_type)) {
aoqi@0 1966 // If the input to the store does not fit with the load's result type,
aoqi@0 1967 // it must be truncated. We can't delay until Ideal call since
aoqi@0 1968 // a singleton Value is needed for split_thru_phi optimization.
aoqi@0 1969 int con = value->get_int();
aoqi@0 1970 return TypeInt::make(con & 0xFFFF);
aoqi@0 1971 }
aoqi@0 1972 return LoadNode::Value(phase);
aoqi@0 1973 }
aoqi@0 1974
aoqi@0 1975 //--------------------------LoadSNode::Ideal--------------------------------------
aoqi@0 1976 //
aoqi@0 1977 // If the previous store is to the same address as this load,
aoqi@0 1978 // and the value stored was larger than a short, replace this load
aoqi@0 1979 // with the value stored truncated to a short. If no truncation is
aoqi@0 1980 // needed, the replacement is done in LoadNode::Identity().
aoqi@0 1981 //
aoqi@0 1982 Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 1983 Node* mem = in(MemNode::Memory);
aoqi@0 1984 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1985 if( value && !phase->type(value)->higher_equal( _type ) ) {
aoqi@0 1986 Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(16)) );
aoqi@0 1987 return new (phase->C) RShiftINode(result, phase->intcon(16));
aoqi@0 1988 }
aoqi@0 1989 // Identity call will handle the case where truncation is not needed.
aoqi@0 1990 return LoadNode::Ideal(phase, can_reshape);
aoqi@0 1991 }
aoqi@0 1992
aoqi@0 1993 const Type* LoadSNode::Value(PhaseTransform *phase) const {
aoqi@0 1994 Node* mem = in(MemNode::Memory);
aoqi@0 1995 Node* value = can_see_stored_value(mem,phase);
aoqi@0 1996 if (value != NULL && value->is_Con() &&
aoqi@0 1997 !value->bottom_type()->higher_equal(_type)) {
aoqi@0 1998 // If the input to the store does not fit with the load's result type,
aoqi@0 1999 // it must be truncated. We can't delay until Ideal call since
aoqi@0 2000 // a singleton Value is needed for split_thru_phi optimization.
aoqi@0 2001 int con = value->get_int();
aoqi@0 2002 return TypeInt::make((con << 16) >> 16);
aoqi@0 2003 }
aoqi@0 2004 return LoadNode::Value(phase);
aoqi@0 2005 }
aoqi@0 2006
aoqi@0 2007 //=============================================================================
aoqi@0 2008 //----------------------------LoadKlassNode::make------------------------------
aoqi@0 2009 // Polymorphic factory method:
aoqi@0 2010 Node *LoadKlassNode::make( PhaseGVN& gvn, Node *mem, Node *adr, const TypePtr* at, const TypeKlassPtr *tk ) {
aoqi@0 2011 Compile* C = gvn.C;
aoqi@0 2012 Node *ctl = NULL;
aoqi@0 2013 // sanity check the alias category against the created node type
aoqi@0 2014 const TypePtr *adr_type = adr->bottom_type()->isa_ptr();
aoqi@0 2015 assert(adr_type != NULL, "expecting TypeKlassPtr");
aoqi@0 2016 #ifdef _LP64
aoqi@0 2017 if (adr_type->is_ptr_to_narrowklass()) {
aoqi@0 2018 assert(UseCompressedClassPointers, "no compressed klasses");
aoqi@0 2019 Node* load_klass = gvn.transform(new (C) LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowklass(), MemNode::unordered));
aoqi@0 2020 return new (C) DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr());
aoqi@0 2021 }
aoqi@0 2022 #endif
aoqi@0 2023 assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
aoqi@0 2024 return new (C) LoadKlassNode(ctl, mem, adr, at, tk, MemNode::unordered);
aoqi@0 2025 }
aoqi@0 2026
aoqi@0 2027 //------------------------------Value------------------------------------------
aoqi@0 2028 const Type *LoadKlassNode::Value( PhaseTransform *phase ) const {
aoqi@0 2029 return klass_value_common(phase);
aoqi@0 2030 }
aoqi@0 2031
aoqi@0 2032 const Type *LoadNode::klass_value_common( PhaseTransform *phase ) const {
aoqi@0 2033 // Either input is TOP ==> the result is TOP
aoqi@0 2034 const Type *t1 = phase->type( in(MemNode::Memory) );
aoqi@0 2035 if (t1 == Type::TOP) return Type::TOP;
aoqi@0 2036 Node *adr = in(MemNode::Address);
aoqi@0 2037 const Type *t2 = phase->type( adr );
aoqi@0 2038 if (t2 == Type::TOP) return Type::TOP;
aoqi@0 2039 const TypePtr *tp = t2->is_ptr();
aoqi@0 2040 if (TypePtr::above_centerline(tp->ptr()) ||
aoqi@0 2041 tp->ptr() == TypePtr::Null) return Type::TOP;
aoqi@0 2042
aoqi@0 2043 // Return a more precise klass, if possible
aoqi@0 2044 const TypeInstPtr *tinst = tp->isa_instptr();
aoqi@0 2045 if (tinst != NULL) {
aoqi@0 2046 ciInstanceKlass* ik = tinst->klass()->as_instance_klass();
aoqi@0 2047 int offset = tinst->offset();
aoqi@0 2048 if (ik == phase->C->env()->Class_klass()
aoqi@0 2049 && (offset == java_lang_Class::klass_offset_in_bytes() ||
aoqi@0 2050 offset == java_lang_Class::array_klass_offset_in_bytes())) {
aoqi@0 2051 // We are loading a special hidden field from a Class mirror object,
aoqi@0 2052 // the field which points to the VM's Klass metaobject.
aoqi@0 2053 ciType* t = tinst->java_mirror_type();
aoqi@0 2054 // java_mirror_type returns non-null for compile-time Class constants.
aoqi@0 2055 if (t != NULL) {
aoqi@0 2056 // constant oop => constant klass
aoqi@0 2057 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
aoqi@0 2058 if (t->is_void()) {
aoqi@0 2059 // We cannot create a void array. Since void is a primitive type return null
aoqi@0 2060 // klass. Users of this result need to do a null check on the returned klass.
aoqi@0 2061 return TypePtr::NULL_PTR;
aoqi@0 2062 }
aoqi@0 2063 return TypeKlassPtr::make(ciArrayKlass::make(t));
aoqi@0 2064 }
aoqi@0 2065 if (!t->is_klass()) {
aoqi@0 2066 // a primitive Class (e.g., int.class) has NULL for a klass field
aoqi@0 2067 return TypePtr::NULL_PTR;
aoqi@0 2068 }
aoqi@0 2069 // (Folds up the 1st indirection in aClassConstant.getModifiers().)
aoqi@0 2070 return TypeKlassPtr::make(t->as_klass());
aoqi@0 2071 }
aoqi@0 2072 // non-constant mirror, so we can't tell what's going on
aoqi@0 2073 }
aoqi@0 2074 if( !ik->is_loaded() )
aoqi@0 2075 return _type; // Bail out if not loaded
aoqi@0 2076 if (offset == oopDesc::klass_offset_in_bytes()) {
aoqi@0 2077 if (tinst->klass_is_exact()) {
aoqi@0 2078 return TypeKlassPtr::make(ik);
aoqi@0 2079 }
aoqi@0 2080 // See if we can become precise: no subklasses and no interface
aoqi@0 2081 // (Note: We need to support verified interfaces.)
aoqi@0 2082 if (!ik->is_interface() && !ik->has_subklass()) {
aoqi@0 2083 //assert(!UseExactTypes, "this code should be useless with exact types");
aoqi@0 2084 // Add a dependence; if any subclass added we need to recompile
aoqi@0 2085 if (!ik->is_final()) {
aoqi@0 2086 // %%% should use stronger assert_unique_concrete_subtype instead
aoqi@0 2087 phase->C->dependencies()->assert_leaf_type(ik);
aoqi@0 2088 }
aoqi@0 2089 // Return precise klass
aoqi@0 2090 return TypeKlassPtr::make(ik);
aoqi@0 2091 }
aoqi@0 2092
aoqi@0 2093 // Return root of possible klass
aoqi@0 2094 return TypeKlassPtr::make(TypePtr::NotNull, ik, 0/*offset*/);
aoqi@0 2095 }
aoqi@0 2096 }
aoqi@0 2097
aoqi@0 2098 // Check for loading klass from an array
aoqi@0 2099 const TypeAryPtr *tary = tp->isa_aryptr();
aoqi@0 2100 if( tary != NULL ) {
aoqi@0 2101 ciKlass *tary_klass = tary->klass();
aoqi@0 2102 if (tary_klass != NULL // can be NULL when at BOTTOM or TOP
aoqi@0 2103 && tary->offset() == oopDesc::klass_offset_in_bytes()) {
aoqi@0 2104 if (tary->klass_is_exact()) {
aoqi@0 2105 return TypeKlassPtr::make(tary_klass);
aoqi@0 2106 }
aoqi@0 2107 ciArrayKlass *ak = tary->klass()->as_array_klass();
aoqi@0 2108 // If the klass is an object array, we defer the question to the
aoqi@0 2109 // array component klass.
aoqi@0 2110 if( ak->is_obj_array_klass() ) {
aoqi@0 2111 assert( ak->is_loaded(), "" );
aoqi@0 2112 ciKlass *base_k = ak->as_obj_array_klass()->base_element_klass();
aoqi@0 2113 if( base_k->is_loaded() && base_k->is_instance_klass() ) {
aoqi@0 2114 ciInstanceKlass* ik = base_k->as_instance_klass();
aoqi@0 2115 // See if we can become precise: no subklasses and no interface
aoqi@0 2116 if (!ik->is_interface() && !ik->has_subklass()) {
aoqi@0 2117 //assert(!UseExactTypes, "this code should be useless with exact types");
aoqi@0 2118 // Add a dependence; if any subclass added we need to recompile
aoqi@0 2119 if (!ik->is_final()) {
aoqi@0 2120 phase->C->dependencies()->assert_leaf_type(ik);
aoqi@0 2121 }
aoqi@0 2122 // Return precise array klass
aoqi@0 2123 return TypeKlassPtr::make(ak);
aoqi@0 2124 }
aoqi@0 2125 }
aoqi@0 2126 return TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
aoqi@0 2127 } else { // Found a type-array?
aoqi@0 2128 //assert(!UseExactTypes, "this code should be useless with exact types");
aoqi@0 2129 assert( ak->is_type_array_klass(), "" );
aoqi@0 2130 return TypeKlassPtr::make(ak); // These are always precise
aoqi@0 2131 }
aoqi@0 2132 }
aoqi@0 2133 }
aoqi@0 2134
aoqi@0 2135 // Check for loading klass from an array klass
aoqi@0 2136 const TypeKlassPtr *tkls = tp->isa_klassptr();
aoqi@0 2137 if (tkls != NULL && !StressReflectiveCode) {
aoqi@0 2138 ciKlass* klass = tkls->klass();
aoqi@0 2139 if( !klass->is_loaded() )
aoqi@0 2140 return _type; // Bail out if not loaded
aoqi@0 2141 if( klass->is_obj_array_klass() &&
aoqi@0 2142 tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) {
aoqi@0 2143 ciKlass* elem = klass->as_obj_array_klass()->element_klass();
aoqi@0 2144 // // Always returning precise element type is incorrect,
aoqi@0 2145 // // e.g., element type could be object and array may contain strings
aoqi@0 2146 // return TypeKlassPtr::make(TypePtr::Constant, elem, 0);
aoqi@0 2147
aoqi@0 2148 // The array's TypeKlassPtr was declared 'precise' or 'not precise'
aoqi@0 2149 // according to the element type's subclassing.
aoqi@0 2150 return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/);
aoqi@0 2151 }
aoqi@0 2152 if( klass->is_instance_klass() && tkls->klass_is_exact() &&
aoqi@0 2153 tkls->offset() == in_bytes(Klass::super_offset())) {
aoqi@0 2154 ciKlass* sup = klass->as_instance_klass()->super();
aoqi@0 2155 // The field is Klass::_super. Return its (constant) value.
aoqi@0 2156 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
aoqi@0 2157 return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR;
aoqi@0 2158 }
aoqi@0 2159 }
aoqi@0 2160
aoqi@0 2161 // Bailout case
aoqi@0 2162 return LoadNode::Value(phase);
aoqi@0 2163 }
aoqi@0 2164
aoqi@0 2165 //------------------------------Identity---------------------------------------
aoqi@0 2166 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k.
aoqi@0 2167 // Also feed through the klass in Allocate(...klass...)._klass.
aoqi@0 2168 Node* LoadKlassNode::Identity( PhaseTransform *phase ) {
aoqi@0 2169 return klass_identity_common(phase);
aoqi@0 2170 }
aoqi@0 2171
aoqi@0 2172 Node* LoadNode::klass_identity_common(PhaseTransform *phase ) {
aoqi@0 2173 Node* x = LoadNode::Identity(phase);
aoqi@0 2174 if (x != this) return x;
aoqi@0 2175
aoqi@0 2176 // Take apart the address into an oop and and offset.
aoqi@0 2177 // Return 'this' if we cannot.
aoqi@0 2178 Node* adr = in(MemNode::Address);
aoqi@0 2179 intptr_t offset = 0;
aoqi@0 2180 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
aoqi@0 2181 if (base == NULL) return this;
aoqi@0 2182 const TypeOopPtr* toop = phase->type(adr)->isa_oopptr();
aoqi@0 2183 if (toop == NULL) return this;
aoqi@0 2184
aoqi@0 2185 // We can fetch the klass directly through an AllocateNode.
aoqi@0 2186 // This works even if the klass is not constant (clone or newArray).
aoqi@0 2187 if (offset == oopDesc::klass_offset_in_bytes()) {
aoqi@0 2188 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
aoqi@0 2189 if (allocated_klass != NULL) {
aoqi@0 2190 return allocated_klass;
aoqi@0 2191 }
aoqi@0 2192 }
aoqi@0 2193
aoqi@0 2194 // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*.
aoqi@0 2195 // Simplify ak.component_mirror.array_klass to plain ak, ak an ArrayKlass.
aoqi@0 2196 // See inline_native_Class_query for occurrences of these patterns.
aoqi@0 2197 // Java Example: x.getClass().isAssignableFrom(y)
aoqi@0 2198 // Java Example: Array.newInstance(x.getClass().getComponentType(), n)
aoqi@0 2199 //
aoqi@0 2200 // This improves reflective code, often making the Class
aoqi@0 2201 // mirror go completely dead. (Current exception: Class
aoqi@0 2202 // mirrors may appear in debug info, but we could clean them out by
aoqi@0 2203 // introducing a new debug info operator for Klass*.java_mirror).
aoqi@0 2204 if (toop->isa_instptr() && toop->klass() == phase->C->env()->Class_klass()
aoqi@0 2205 && (offset == java_lang_Class::klass_offset_in_bytes() ||
aoqi@0 2206 offset == java_lang_Class::array_klass_offset_in_bytes())) {
aoqi@0 2207 // We are loading a special hidden field from a Class mirror,
aoqi@0 2208 // the field which points to its Klass or ArrayKlass metaobject.
aoqi@0 2209 if (base->is_Load()) {
aoqi@0 2210 Node* adr2 = base->in(MemNode::Address);
aoqi@0 2211 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
aoqi@0 2212 if (tkls != NULL && !tkls->empty()
aoqi@0 2213 && (tkls->klass()->is_instance_klass() ||
aoqi@0 2214 tkls->klass()->is_array_klass())
aoqi@0 2215 && adr2->is_AddP()
aoqi@0 2216 ) {
aoqi@0 2217 int mirror_field = in_bytes(Klass::java_mirror_offset());
aoqi@0 2218 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
aoqi@0 2219 mirror_field = in_bytes(ArrayKlass::component_mirror_offset());
aoqi@0 2220 }
aoqi@0 2221 if (tkls->offset() == mirror_field) {
aoqi@0 2222 return adr2->in(AddPNode::Base);
aoqi@0 2223 }
aoqi@0 2224 }
aoqi@0 2225 }
aoqi@0 2226 }
aoqi@0 2227
aoqi@0 2228 return this;
aoqi@0 2229 }
aoqi@0 2230
aoqi@0 2231
aoqi@0 2232 //------------------------------Value------------------------------------------
aoqi@0 2233 const Type *LoadNKlassNode::Value( PhaseTransform *phase ) const {
aoqi@0 2234 const Type *t = klass_value_common(phase);
aoqi@0 2235 if (t == Type::TOP)
aoqi@0 2236 return t;
aoqi@0 2237
aoqi@0 2238 return t->make_narrowklass();
aoqi@0 2239 }
aoqi@0 2240
aoqi@0 2241 //------------------------------Identity---------------------------------------
aoqi@0 2242 // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k.
aoqi@0 2243 // Also feed through the klass in Allocate(...klass...)._klass.
aoqi@0 2244 Node* LoadNKlassNode::Identity( PhaseTransform *phase ) {
aoqi@0 2245 Node *x = klass_identity_common(phase);
aoqi@0 2246
aoqi@0 2247 const Type *t = phase->type( x );
aoqi@0 2248 if( t == Type::TOP ) return x;
aoqi@0 2249 if( t->isa_narrowklass()) return x;
aoqi@0 2250 assert (!t->isa_narrowoop(), "no narrow oop here");
aoqi@0 2251
aoqi@0 2252 return phase->transform(new (phase->C) EncodePKlassNode(x, t->make_narrowklass()));
aoqi@0 2253 }
aoqi@0 2254
aoqi@0 2255 //------------------------------Value-----------------------------------------
aoqi@0 2256 const Type *LoadRangeNode::Value( PhaseTransform *phase ) const {
aoqi@0 2257 // Either input is TOP ==> the result is TOP
aoqi@0 2258 const Type *t1 = phase->type( in(MemNode::Memory) );
aoqi@0 2259 if( t1 == Type::TOP ) return Type::TOP;
aoqi@0 2260 Node *adr = in(MemNode::Address);
aoqi@0 2261 const Type *t2 = phase->type( adr );
aoqi@0 2262 if( t2 == Type::TOP ) return Type::TOP;
aoqi@0 2263 const TypePtr *tp = t2->is_ptr();
aoqi@0 2264 if (TypePtr::above_centerline(tp->ptr())) return Type::TOP;
aoqi@0 2265 const TypeAryPtr *tap = tp->isa_aryptr();
aoqi@0 2266 if( !tap ) return _type;
aoqi@0 2267 return tap->size();
aoqi@0 2268 }
aoqi@0 2269
aoqi@0 2270 //-------------------------------Ideal---------------------------------------
aoqi@0 2271 // Feed through the length in AllocateArray(...length...)._length.
aoqi@0 2272 Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2273 Node* p = MemNode::Ideal_common(phase, can_reshape);
aoqi@0 2274 if (p) return (p == NodeSentinel) ? NULL : p;
aoqi@0 2275
aoqi@0 2276 // Take apart the address into an oop and and offset.
aoqi@0 2277 // Return 'this' if we cannot.
aoqi@0 2278 Node* adr = in(MemNode::Address);
aoqi@0 2279 intptr_t offset = 0;
aoqi@0 2280 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
aoqi@0 2281 if (base == NULL) return NULL;
aoqi@0 2282 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
aoqi@0 2283 if (tary == NULL) return NULL;
aoqi@0 2284
aoqi@0 2285 // We can fetch the length directly through an AllocateArrayNode.
aoqi@0 2286 // This works even if the length is not constant (clone or newArray).
aoqi@0 2287 if (offset == arrayOopDesc::length_offset_in_bytes()) {
aoqi@0 2288 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
aoqi@0 2289 if (alloc != NULL) {
aoqi@0 2290 Node* allocated_length = alloc->Ideal_length();
aoqi@0 2291 Node* len = alloc->make_ideal_length(tary, phase);
aoqi@0 2292 if (allocated_length != len) {
aoqi@0 2293 // New CastII improves on this.
aoqi@0 2294 return len;
aoqi@0 2295 }
aoqi@0 2296 }
aoqi@0 2297 }
aoqi@0 2298
aoqi@0 2299 return NULL;
aoqi@0 2300 }
aoqi@0 2301
aoqi@0 2302 //------------------------------Identity---------------------------------------
aoqi@0 2303 // Feed through the length in AllocateArray(...length...)._length.
aoqi@0 2304 Node* LoadRangeNode::Identity( PhaseTransform *phase ) {
aoqi@0 2305 Node* x = LoadINode::Identity(phase);
aoqi@0 2306 if (x != this) return x;
aoqi@0 2307
aoqi@0 2308 // Take apart the address into an oop and and offset.
aoqi@0 2309 // Return 'this' if we cannot.
aoqi@0 2310 Node* adr = in(MemNode::Address);
aoqi@0 2311 intptr_t offset = 0;
aoqi@0 2312 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
aoqi@0 2313 if (base == NULL) return this;
aoqi@0 2314 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
aoqi@0 2315 if (tary == NULL) return this;
aoqi@0 2316
aoqi@0 2317 // We can fetch the length directly through an AllocateArrayNode.
aoqi@0 2318 // This works even if the length is not constant (clone or newArray).
aoqi@0 2319 if (offset == arrayOopDesc::length_offset_in_bytes()) {
aoqi@0 2320 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
aoqi@0 2321 if (alloc != NULL) {
aoqi@0 2322 Node* allocated_length = alloc->Ideal_length();
aoqi@0 2323 // Do not allow make_ideal_length to allocate a CastII node.
aoqi@0 2324 Node* len = alloc->make_ideal_length(tary, phase, false);
aoqi@0 2325 if (allocated_length == len) {
aoqi@0 2326 // Return allocated_length only if it would not be improved by a CastII.
aoqi@0 2327 return allocated_length;
aoqi@0 2328 }
aoqi@0 2329 }
aoqi@0 2330 }
aoqi@0 2331
aoqi@0 2332 return this;
aoqi@0 2333
aoqi@0 2334 }
aoqi@0 2335
aoqi@0 2336 //=============================================================================
aoqi@0 2337 //---------------------------StoreNode::make-----------------------------------
aoqi@0 2338 // Polymorphic factory method:
aoqi@0 2339 StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo) {
aoqi@0 2340 assert((mo == unordered || mo == release), "unexpected");
aoqi@0 2341 Compile* C = gvn.C;
aoqi@0 2342 assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
aoqi@0 2343 ctl != NULL, "raw memory operations should have control edge");
aoqi@0 2344
aoqi@0 2345 switch (bt) {
aoqi@0 2346 case T_BOOLEAN:
aoqi@0 2347 case T_BYTE: return new (C) StoreBNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2348 case T_INT: return new (C) StoreINode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2349 case T_CHAR:
aoqi@0 2350 case T_SHORT: return new (C) StoreCNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2351 case T_LONG: return new (C) StoreLNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2352 case T_FLOAT: return new (C) StoreFNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2353 case T_DOUBLE: return new (C) StoreDNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2354 case T_METADATA:
aoqi@0 2355 case T_ADDRESS:
aoqi@0 2356 case T_OBJECT:
aoqi@0 2357 #ifdef _LP64
aoqi@0 2358 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
aoqi@0 2359 val = gvn.transform(new (C) EncodePNode(val, val->bottom_type()->make_narrowoop()));
aoqi@0 2360 return new (C) StoreNNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2361 } else if (adr->bottom_type()->is_ptr_to_narrowklass() ||
aoqi@0 2362 (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() &&
aoqi@0 2363 adr->bottom_type()->isa_rawptr())) {
aoqi@0 2364 val = gvn.transform(new (C) EncodePKlassNode(val, val->bottom_type()->make_narrowklass()));
aoqi@0 2365 return new (C) StoreNKlassNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2366 }
aoqi@0 2367 #endif
aoqi@0 2368 {
aoqi@0 2369 return new (C) StorePNode(ctl, mem, adr, adr_type, val, mo);
aoqi@0 2370 }
aoqi@0 2371 }
aoqi@0 2372 ShouldNotReachHere();
aoqi@0 2373 return (StoreNode*)NULL;
aoqi@0 2374 }
aoqi@0 2375
aoqi@0 2376 StoreLNode* StoreLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo) {
aoqi@0 2377 bool require_atomic = true;
aoqi@0 2378 return new (C) StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic);
aoqi@0 2379 }
aoqi@0 2380
aoqi@0 2381
aoqi@0 2382 //--------------------------bottom_type----------------------------------------
aoqi@0 2383 const Type *StoreNode::bottom_type() const {
aoqi@0 2384 return Type::MEMORY;
aoqi@0 2385 }
aoqi@0 2386
aoqi@0 2387 //------------------------------hash-------------------------------------------
aoqi@0 2388 uint StoreNode::hash() const {
aoqi@0 2389 // unroll addition of interesting fields
aoqi@0 2390 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
aoqi@0 2391
aoqi@0 2392 // Since they are not commoned, do not hash them:
aoqi@0 2393 return NO_HASH;
aoqi@0 2394 }
aoqi@0 2395
aoqi@0 2396 //------------------------------Ideal------------------------------------------
aoqi@0 2397 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x).
aoqi@0 2398 // When a store immediately follows a relevant allocation/initialization,
aoqi@0 2399 // try to capture it into the initialization, or hoist it above.
aoqi@0 2400 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2401 Node* p = MemNode::Ideal_common(phase, can_reshape);
aoqi@0 2402 if (p) return (p == NodeSentinel) ? NULL : p;
aoqi@0 2403
aoqi@0 2404 Node* mem = in(MemNode::Memory);
aoqi@0 2405 Node* address = in(MemNode::Address);
aoqi@0 2406
aoqi@0 2407 // Back-to-back stores to same address? Fold em up. Generally
aoqi@0 2408 // unsafe if I have intervening uses... Also disallowed for StoreCM
aoqi@0 2409 // since they must follow each StoreP operation. Redundant StoreCMs
aoqi@0 2410 // are eliminated just before matching in final_graph_reshape.
aoqi@0 2411 if (mem->is_Store() && mem->in(MemNode::Address)->eqv_uncast(address) &&
aoqi@0 2412 mem->Opcode() != Op_StoreCM) {
aoqi@0 2413 // Looking at a dead closed cycle of memory?
aoqi@0 2414 assert(mem != mem->in(MemNode::Memory), "dead loop in StoreNode::Ideal");
aoqi@0 2415
aoqi@0 2416 assert(Opcode() == mem->Opcode() ||
aoqi@0 2417 phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw,
aoqi@0 2418 "no mismatched stores, except on raw memory");
aoqi@0 2419
aoqi@0 2420 if (mem->outcnt() == 1 && // check for intervening uses
aoqi@0 2421 mem->as_Store()->memory_size() <= this->memory_size()) {
aoqi@0 2422 // If anybody other than 'this' uses 'mem', we cannot fold 'mem' away.
aoqi@0 2423 // For example, 'mem' might be the final state at a conditional return.
aoqi@0 2424 // Or, 'mem' might be used by some node which is live at the same time
aoqi@0 2425 // 'this' is live, which might be unschedulable. So, require exactly
aoqi@0 2426 // ONE user, the 'this' store, until such time as we clone 'mem' for
aoqi@0 2427 // each of 'mem's uses (thus making the exactly-1-user-rule hold true).
aoqi@0 2428 if (can_reshape) { // (%%% is this an anachronism?)
aoqi@0 2429 set_req_X(MemNode::Memory, mem->in(MemNode::Memory),
aoqi@0 2430 phase->is_IterGVN());
aoqi@0 2431 } else {
aoqi@0 2432 // It's OK to do this in the parser, since DU info is always accurate,
aoqi@0 2433 // and the parser always refers to nodes via SafePointNode maps.
aoqi@0 2434 set_req(MemNode::Memory, mem->in(MemNode::Memory));
aoqi@0 2435 }
aoqi@0 2436 return this;
aoqi@0 2437 }
aoqi@0 2438 }
aoqi@0 2439
aoqi@0 2440 // Capture an unaliased, unconditional, simple store into an initializer.
aoqi@0 2441 // Or, if it is independent of the allocation, hoist it above the allocation.
aoqi@0 2442 if (ReduceFieldZeroing && /*can_reshape &&*/
aoqi@0 2443 mem->is_Proj() && mem->in(0)->is_Initialize()) {
aoqi@0 2444 InitializeNode* init = mem->in(0)->as_Initialize();
aoqi@0 2445 intptr_t offset = init->can_capture_store(this, phase, can_reshape);
aoqi@0 2446 if (offset > 0) {
aoqi@0 2447 Node* moved = init->capture_store(this, offset, phase, can_reshape);
aoqi@0 2448 // If the InitializeNode captured me, it made a raw copy of me,
aoqi@0 2449 // and I need to disappear.
aoqi@0 2450 if (moved != NULL) {
aoqi@0 2451 // %%% hack to ensure that Ideal returns a new node:
aoqi@0 2452 mem = MergeMemNode::make(phase->C, mem);
aoqi@0 2453 return mem; // fold me away
aoqi@0 2454 }
aoqi@0 2455 }
aoqi@0 2456 }
aoqi@0 2457
aoqi@0 2458 return NULL; // No further progress
aoqi@0 2459 }
aoqi@0 2460
aoqi@0 2461 //------------------------------Value-----------------------------------------
aoqi@0 2462 const Type *StoreNode::Value( PhaseTransform *phase ) const {
aoqi@0 2463 // Either input is TOP ==> the result is TOP
aoqi@0 2464 const Type *t1 = phase->type( in(MemNode::Memory) );
aoqi@0 2465 if( t1 == Type::TOP ) return Type::TOP;
aoqi@0 2466 const Type *t2 = phase->type( in(MemNode::Address) );
aoqi@0 2467 if( t2 == Type::TOP ) return Type::TOP;
aoqi@0 2468 const Type *t3 = phase->type( in(MemNode::ValueIn) );
aoqi@0 2469 if( t3 == Type::TOP ) return Type::TOP;
aoqi@0 2470 return Type::MEMORY;
aoqi@0 2471 }
aoqi@0 2472
aoqi@0 2473 //------------------------------Identity---------------------------------------
aoqi@0 2474 // Remove redundant stores:
aoqi@0 2475 // Store(m, p, Load(m, p)) changes to m.
aoqi@0 2476 // Store(, p, x) -> Store(m, p, x) changes to Store(m, p, x).
aoqi@0 2477 Node *StoreNode::Identity( PhaseTransform *phase ) {
aoqi@0 2478 Node* mem = in(MemNode::Memory);
aoqi@0 2479 Node* adr = in(MemNode::Address);
aoqi@0 2480 Node* val = in(MemNode::ValueIn);
aoqi@0 2481
aoqi@0 2482 // Load then Store? Then the Store is useless
aoqi@0 2483 if (val->is_Load() &&
aoqi@0 2484 val->in(MemNode::Address)->eqv_uncast(adr) &&
aoqi@0 2485 val->in(MemNode::Memory )->eqv_uncast(mem) &&
aoqi@0 2486 val->as_Load()->store_Opcode() == Opcode()) {
aoqi@0 2487 return mem;
aoqi@0 2488 }
aoqi@0 2489
aoqi@0 2490 // Two stores in a row of the same value?
aoqi@0 2491 if (mem->is_Store() &&
aoqi@0 2492 mem->in(MemNode::Address)->eqv_uncast(adr) &&
aoqi@0 2493 mem->in(MemNode::ValueIn)->eqv_uncast(val) &&
aoqi@0 2494 mem->Opcode() == Opcode()) {
aoqi@0 2495 return mem;
aoqi@0 2496 }
aoqi@0 2497
aoqi@0 2498 // Store of zero anywhere into a freshly-allocated object?
aoqi@0 2499 // Then the store is useless.
aoqi@0 2500 // (It must already have been captured by the InitializeNode.)
aoqi@0 2501 if (ReduceFieldZeroing && phase->type(val)->is_zero_type()) {
aoqi@0 2502 // a newly allocated object is already all-zeroes everywhere
aoqi@0 2503 if (mem->is_Proj() && mem->in(0)->is_Allocate()) {
aoqi@0 2504 return mem;
aoqi@0 2505 }
aoqi@0 2506
aoqi@0 2507 // the store may also apply to zero-bits in an earlier object
aoqi@0 2508 Node* prev_mem = find_previous_store(phase);
aoqi@0 2509 // Steps (a), (b): Walk past independent stores to find an exact match.
aoqi@0 2510 if (prev_mem != NULL) {
aoqi@0 2511 Node* prev_val = can_see_stored_value(prev_mem, phase);
aoqi@0 2512 if (prev_val != NULL && phase->eqv(prev_val, val)) {
aoqi@0 2513 // prev_val and val might differ by a cast; it would be good
aoqi@0 2514 // to keep the more informative of the two.
aoqi@0 2515 return mem;
aoqi@0 2516 }
aoqi@0 2517 }
aoqi@0 2518 }
aoqi@0 2519
aoqi@0 2520 return this;
aoqi@0 2521 }
aoqi@0 2522
aoqi@0 2523 //------------------------------match_edge-------------------------------------
aoqi@0 2524 // Do we Match on this edge index or not? Match only memory & value
aoqi@0 2525 uint StoreNode::match_edge(uint idx) const {
aoqi@0 2526 return idx == MemNode::Address || idx == MemNode::ValueIn;
aoqi@0 2527 }
aoqi@0 2528
aoqi@0 2529 //------------------------------cmp--------------------------------------------
aoqi@0 2530 // Do not common stores up together. They generally have to be split
aoqi@0 2531 // back up anyways, so do not bother.
aoqi@0 2532 uint StoreNode::cmp( const Node &n ) const {
aoqi@0 2533 return (&n == this); // Always fail except on self
aoqi@0 2534 }
aoqi@0 2535
aoqi@0 2536 //------------------------------Ideal_masked_input-----------------------------
aoqi@0 2537 // Check for a useless mask before a partial-word store
aoqi@0 2538 // (StoreB ... (AndI valIn conIa) )
aoqi@0 2539 // If (conIa & mask == mask) this simplifies to
aoqi@0 2540 // (StoreB ... (valIn) )
aoqi@0 2541 Node *StoreNode::Ideal_masked_input(PhaseGVN *phase, uint mask) {
aoqi@0 2542 Node *val = in(MemNode::ValueIn);
aoqi@0 2543 if( val->Opcode() == Op_AndI ) {
aoqi@0 2544 const TypeInt *t = phase->type( val->in(2) )->isa_int();
aoqi@0 2545 if( t && t->is_con() && (t->get_con() & mask) == mask ) {
aoqi@0 2546 set_req(MemNode::ValueIn, val->in(1));
aoqi@0 2547 return this;
aoqi@0 2548 }
aoqi@0 2549 }
aoqi@0 2550 return NULL;
aoqi@0 2551 }
aoqi@0 2552
aoqi@0 2553
aoqi@0 2554 //------------------------------Ideal_sign_extended_input----------------------
aoqi@0 2555 // Check for useless sign-extension before a partial-word store
aoqi@0 2556 // (StoreB ... (RShiftI _ (LShiftI _ valIn conIL ) conIR) )
aoqi@0 2557 // If (conIL == conIR && conIR <= num_bits) this simplifies to
aoqi@0 2558 // (StoreB ... (valIn) )
aoqi@0 2559 Node *StoreNode::Ideal_sign_extended_input(PhaseGVN *phase, int num_bits) {
aoqi@0 2560 Node *val = in(MemNode::ValueIn);
aoqi@0 2561 if( val->Opcode() == Op_RShiftI ) {
aoqi@0 2562 const TypeInt *t = phase->type( val->in(2) )->isa_int();
aoqi@0 2563 if( t && t->is_con() && (t->get_con() <= num_bits) ) {
aoqi@0 2564 Node *shl = val->in(1);
aoqi@0 2565 if( shl->Opcode() == Op_LShiftI ) {
aoqi@0 2566 const TypeInt *t2 = phase->type( shl->in(2) )->isa_int();
aoqi@0 2567 if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) {
aoqi@0 2568 set_req(MemNode::ValueIn, shl->in(1));
aoqi@0 2569 return this;
aoqi@0 2570 }
aoqi@0 2571 }
aoqi@0 2572 }
aoqi@0 2573 }
aoqi@0 2574 return NULL;
aoqi@0 2575 }
aoqi@0 2576
aoqi@0 2577 //------------------------------value_never_loaded-----------------------------------
aoqi@0 2578 // Determine whether there are any possible loads of the value stored.
aoqi@0 2579 // For simplicity, we actually check if there are any loads from the
aoqi@0 2580 // address stored to, not just for loads of the value stored by this node.
aoqi@0 2581 //
aoqi@0 2582 bool StoreNode::value_never_loaded( PhaseTransform *phase) const {
aoqi@0 2583 Node *adr = in(Address);
aoqi@0 2584 const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr();
aoqi@0 2585 if (adr_oop == NULL)
aoqi@0 2586 return false;
aoqi@0 2587 if (!adr_oop->is_known_instance_field())
aoqi@0 2588 return false; // if not a distinct instance, there may be aliases of the address
aoqi@0 2589 for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) {
aoqi@0 2590 Node *use = adr->fast_out(i);
aoqi@0 2591 int opc = use->Opcode();
aoqi@0 2592 if (use->is_Load() || use->is_LoadStore()) {
aoqi@0 2593 return false;
aoqi@0 2594 }
aoqi@0 2595 }
aoqi@0 2596 return true;
aoqi@0 2597 }
aoqi@0 2598
aoqi@0 2599 //=============================================================================
aoqi@0 2600 //------------------------------Ideal------------------------------------------
aoqi@0 2601 // If the store is from an AND mask that leaves the low bits untouched, then
aoqi@0 2602 // we can skip the AND operation. If the store is from a sign-extension
aoqi@0 2603 // (a left shift, then right shift) we can skip both.
aoqi@0 2604 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){
aoqi@0 2605 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF);
aoqi@0 2606 if( progress != NULL ) return progress;
aoqi@0 2607
aoqi@0 2608 progress = StoreNode::Ideal_sign_extended_input(phase, 24);
aoqi@0 2609 if( progress != NULL ) return progress;
aoqi@0 2610
aoqi@0 2611 // Finally check the default case
aoqi@0 2612 return StoreNode::Ideal(phase, can_reshape);
aoqi@0 2613 }
aoqi@0 2614
aoqi@0 2615 //=============================================================================
aoqi@0 2616 //------------------------------Ideal------------------------------------------
aoqi@0 2617 // If the store is from an AND mask that leaves the low bits untouched, then
aoqi@0 2618 // we can skip the AND operation
aoqi@0 2619 Node *StoreCNode::Ideal(PhaseGVN *phase, bool can_reshape){
aoqi@0 2620 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFFFF);
aoqi@0 2621 if( progress != NULL ) return progress;
aoqi@0 2622
aoqi@0 2623 progress = StoreNode::Ideal_sign_extended_input(phase, 16);
aoqi@0 2624 if( progress != NULL ) return progress;
aoqi@0 2625
aoqi@0 2626 // Finally check the default case
aoqi@0 2627 return StoreNode::Ideal(phase, can_reshape);
aoqi@0 2628 }
aoqi@0 2629
aoqi@0 2630 //=============================================================================
aoqi@0 2631 //------------------------------Identity---------------------------------------
aoqi@0 2632 Node *StoreCMNode::Identity( PhaseTransform *phase ) {
aoqi@0 2633 // No need to card mark when storing a null ptr
aoqi@0 2634 Node* my_store = in(MemNode::OopStore);
aoqi@0 2635 if (my_store->is_Store()) {
aoqi@0 2636 const Type *t1 = phase->type( my_store->in(MemNode::ValueIn) );
aoqi@0 2637 if( t1 == TypePtr::NULL_PTR ) {
aoqi@0 2638 return in(MemNode::Memory);
aoqi@0 2639 }
aoqi@0 2640 }
aoqi@0 2641 return this;
aoqi@0 2642 }
aoqi@0 2643
aoqi@0 2644 //=============================================================================
aoqi@0 2645 //------------------------------Ideal---------------------------------------
aoqi@0 2646 Node *StoreCMNode::Ideal(PhaseGVN *phase, bool can_reshape){
aoqi@0 2647 Node* progress = StoreNode::Ideal(phase, can_reshape);
aoqi@0 2648 if (progress != NULL) return progress;
aoqi@0 2649
aoqi@0 2650 Node* my_store = in(MemNode::OopStore);
aoqi@0 2651 if (my_store->is_MergeMem()) {
aoqi@0 2652 Node* mem = my_store->as_MergeMem()->memory_at(oop_alias_idx());
aoqi@0 2653 set_req(MemNode::OopStore, mem);
aoqi@0 2654 return this;
aoqi@0 2655 }
aoqi@0 2656
aoqi@0 2657 return NULL;
aoqi@0 2658 }
aoqi@0 2659
aoqi@0 2660 //------------------------------Value-----------------------------------------
aoqi@0 2661 const Type *StoreCMNode::Value( PhaseTransform *phase ) const {
aoqi@0 2662 // Either input is TOP ==> the result is TOP
aoqi@0 2663 const Type *t = phase->type( in(MemNode::Memory) );
aoqi@0 2664 if( t == Type::TOP ) return Type::TOP;
aoqi@0 2665 t = phase->type( in(MemNode::Address) );
aoqi@0 2666 if( t == Type::TOP ) return Type::TOP;
aoqi@0 2667 t = phase->type( in(MemNode::ValueIn) );
aoqi@0 2668 if( t == Type::TOP ) return Type::TOP;
aoqi@0 2669 // If extra input is TOP ==> the result is TOP
aoqi@0 2670 t = phase->type( in(MemNode::OopStore) );
aoqi@0 2671 if( t == Type::TOP ) return Type::TOP;
aoqi@0 2672
aoqi@0 2673 return StoreNode::Value( phase );
aoqi@0 2674 }
aoqi@0 2675
aoqi@0 2676
aoqi@0 2677 //=============================================================================
aoqi@0 2678 //----------------------------------SCMemProjNode------------------------------
aoqi@0 2679 const Type * SCMemProjNode::Value( PhaseTransform *phase ) const
aoqi@0 2680 {
aoqi@0 2681 return bottom_type();
aoqi@0 2682 }
aoqi@0 2683
aoqi@0 2684 //=============================================================================
aoqi@0 2685 //----------------------------------LoadStoreNode------------------------------
aoqi@0 2686 LoadStoreNode::LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required )
aoqi@0 2687 : Node(required),
aoqi@0 2688 _type(rt),
aoqi@0 2689 _adr_type(at)
aoqi@0 2690 {
aoqi@0 2691 init_req(MemNode::Control, c );
aoqi@0 2692 init_req(MemNode::Memory , mem);
aoqi@0 2693 init_req(MemNode::Address, adr);
aoqi@0 2694 init_req(MemNode::ValueIn, val);
aoqi@0 2695 init_class_id(Class_LoadStore);
aoqi@0 2696 }
aoqi@0 2697
aoqi@0 2698 uint LoadStoreNode::ideal_reg() const {
aoqi@0 2699 return _type->ideal_reg();
aoqi@0 2700 }
aoqi@0 2701
aoqi@0 2702 bool LoadStoreNode::result_not_used() const {
aoqi@0 2703 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
aoqi@0 2704 Node *x = fast_out(i);
aoqi@0 2705 if (x->Opcode() == Op_SCMemProj) continue;
aoqi@0 2706 return false;
aoqi@0 2707 }
aoqi@0 2708 return true;
aoqi@0 2709 }
aoqi@0 2710
aoqi@0 2711 uint LoadStoreNode::size_of() const { return sizeof(*this); }
aoqi@0 2712
aoqi@0 2713 //=============================================================================
aoqi@0 2714 //----------------------------------LoadStoreConditionalNode--------------------
aoqi@0 2715 LoadStoreConditionalNode::LoadStoreConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex ) : LoadStoreNode(c, mem, adr, val, NULL, TypeInt::BOOL, 5) {
aoqi@0 2716 init_req(ExpectedIn, ex );
aoqi@0 2717 }
aoqi@0 2718
aoqi@0 2719 //=============================================================================
aoqi@0 2720 //-------------------------------adr_type--------------------------------------
aoqi@0 2721 // Do we Match on this edge index or not? Do not match memory
aoqi@0 2722 const TypePtr* ClearArrayNode::adr_type() const {
aoqi@0 2723 Node *adr = in(3);
aoqi@0 2724 return MemNode::calculate_adr_type(adr->bottom_type());
aoqi@0 2725 }
aoqi@0 2726
aoqi@0 2727 //------------------------------match_edge-------------------------------------
aoqi@0 2728 // Do we Match on this edge index or not? Do not match memory
aoqi@0 2729 uint ClearArrayNode::match_edge(uint idx) const {
aoqi@0 2730 return idx > 1;
aoqi@0 2731 }
aoqi@0 2732
aoqi@0 2733 //------------------------------Identity---------------------------------------
aoqi@0 2734 // Clearing a zero length array does nothing
aoqi@0 2735 Node *ClearArrayNode::Identity( PhaseTransform *phase ) {
aoqi@0 2736 return phase->type(in(2))->higher_equal(TypeX::ZERO) ? in(1) : this;
aoqi@0 2737 }
aoqi@0 2738
aoqi@0 2739 //------------------------------Idealize---------------------------------------
aoqi@0 2740 // Clearing a short array is faster with stores
aoqi@0 2741 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){
aoqi@0 2742 const int unit = BytesPerLong;
aoqi@0 2743 const TypeX* t = phase->type(in(2))->isa_intptr_t();
aoqi@0 2744 if (!t) return NULL;
aoqi@0 2745 if (!t->is_con()) return NULL;
aoqi@0 2746 intptr_t raw_count = t->get_con();
aoqi@0 2747 intptr_t size = raw_count;
aoqi@0 2748 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
aoqi@0 2749 // Clearing nothing uses the Identity call.
aoqi@0 2750 // Negative clears are possible on dead ClearArrays
aoqi@0 2751 // (see jck test stmt114.stmt11402.val).
aoqi@0 2752 if (size <= 0 || size % unit != 0) return NULL;
aoqi@0 2753 intptr_t count = size / unit;
aoqi@0 2754 // Length too long; use fast hardware clear
aoqi@0 2755 if (size > Matcher::init_array_short_size) return NULL;
aoqi@0 2756 Node *mem = in(1);
aoqi@0 2757 if( phase->type(mem)==Type::TOP ) return NULL;
aoqi@0 2758 Node *adr = in(3);
aoqi@0 2759 const Type* at = phase->type(adr);
aoqi@0 2760 if( at==Type::TOP ) return NULL;
aoqi@0 2761 const TypePtr* atp = at->isa_ptr();
aoqi@0 2762 // adjust atp to be the correct array element address type
aoqi@0 2763 if (atp == NULL) atp = TypePtr::BOTTOM;
aoqi@0 2764 else atp = atp->add_offset(Type::OffsetBot);
aoqi@0 2765 // Get base for derived pointer purposes
aoqi@0 2766 if( adr->Opcode() != Op_AddP ) Unimplemented();
aoqi@0 2767 Node *base = adr->in(1);
aoqi@0 2768
aoqi@0 2769 Node *zero = phase->makecon(TypeLong::ZERO);
aoqi@0 2770 Node *off = phase->MakeConX(BytesPerLong);
aoqi@0 2771 mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
aoqi@0 2772 count--;
aoqi@0 2773 while( count-- ) {
aoqi@0 2774 mem = phase->transform(mem);
aoqi@0 2775 adr = phase->transform(new (phase->C) AddPNode(base,adr,off));
aoqi@0 2776 mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
aoqi@0 2777 }
aoqi@0 2778 return mem;
aoqi@0 2779 }
aoqi@0 2780
aoqi@0 2781 //----------------------------step_through----------------------------------
aoqi@0 2782 // Return allocation input memory edge if it is different instance
aoqi@0 2783 // or itself if it is the one we are looking for.
aoqi@0 2784 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseTransform* phase) {
aoqi@0 2785 Node* n = *np;
aoqi@0 2786 assert(n->is_ClearArray(), "sanity");
aoqi@0 2787 intptr_t offset;
aoqi@0 2788 AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset);
aoqi@0 2789 // This method is called only before Allocate nodes are expanded during
aoqi@0 2790 // macro nodes expansion. Before that ClearArray nodes are only generated
aoqi@0 2791 // in LibraryCallKit::generate_arraycopy() which follows allocations.
aoqi@0 2792 assert(alloc != NULL, "should have allocation");
aoqi@0 2793 if (alloc->_idx == instance_id) {
aoqi@0 2794 // Can not bypass initialization of the instance we are looking for.
aoqi@0 2795 return false;
aoqi@0 2796 }
aoqi@0 2797 // Otherwise skip it.
aoqi@0 2798 InitializeNode* init = alloc->initialization();
aoqi@0 2799 if (init != NULL)
aoqi@0 2800 *np = init->in(TypeFunc::Memory);
aoqi@0 2801 else
aoqi@0 2802 *np = alloc->in(TypeFunc::Memory);
aoqi@0 2803 return true;
aoqi@0 2804 }
aoqi@0 2805
aoqi@0 2806 //----------------------------clear_memory-------------------------------------
aoqi@0 2807 // Generate code to initialize object storage to zero.
aoqi@0 2808 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
aoqi@0 2809 intptr_t start_offset,
aoqi@0 2810 Node* end_offset,
aoqi@0 2811 PhaseGVN* phase) {
aoqi@0 2812 Compile* C = phase->C;
aoqi@0 2813 intptr_t offset = start_offset;
aoqi@0 2814
aoqi@0 2815 int unit = BytesPerLong;
aoqi@0 2816 if ((offset % unit) != 0) {
aoqi@0 2817 Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(offset));
aoqi@0 2818 adr = phase->transform(adr);
aoqi@0 2819 const TypePtr* atp = TypeRawPtr::BOTTOM;
aoqi@0 2820 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
aoqi@0 2821 mem = phase->transform(mem);
aoqi@0 2822 offset += BytesPerInt;
aoqi@0 2823 }
aoqi@0 2824 assert((offset % unit) == 0, "");
aoqi@0 2825
aoqi@0 2826 // Initialize the remaining stuff, if any, with a ClearArray.
aoqi@0 2827 return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
aoqi@0 2828 }
aoqi@0 2829
aoqi@0 2830 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
aoqi@0 2831 Node* start_offset,
aoqi@0 2832 Node* end_offset,
aoqi@0 2833 PhaseGVN* phase) {
aoqi@0 2834 if (start_offset == end_offset) {
aoqi@0 2835 // nothing to do
aoqi@0 2836 return mem;
aoqi@0 2837 }
aoqi@0 2838
aoqi@0 2839 Compile* C = phase->C;
aoqi@0 2840 int unit = BytesPerLong;
aoqi@0 2841 Node* zbase = start_offset;
aoqi@0 2842 Node* zend = end_offset;
aoqi@0 2843
aoqi@0 2844 // Scale to the unit required by the CPU:
aoqi@0 2845 if (!Matcher::init_array_count_is_in_bytes) {
aoqi@0 2846 Node* shift = phase->intcon(exact_log2(unit));
aoqi@0 2847 zbase = phase->transform( new(C) URShiftXNode(zbase, shift) );
aoqi@0 2848 zend = phase->transform( new(C) URShiftXNode(zend, shift) );
aoqi@0 2849 }
aoqi@0 2850
aoqi@0 2851 // Bulk clear double-words
aoqi@0 2852 Node* zsize = phase->transform( new(C) SubXNode(zend, zbase) );
aoqi@0 2853 Node* adr = phase->transform( new(C) AddPNode(dest, dest, start_offset) );
aoqi@0 2854 mem = new (C) ClearArrayNode(ctl, mem, zsize, adr);
aoqi@0 2855 return phase->transform(mem);
aoqi@0 2856 }
aoqi@0 2857
aoqi@0 2858 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
aoqi@0 2859 intptr_t start_offset,
aoqi@0 2860 intptr_t end_offset,
aoqi@0 2861 PhaseGVN* phase) {
aoqi@0 2862 if (start_offset == end_offset) {
aoqi@0 2863 // nothing to do
aoqi@0 2864 return mem;
aoqi@0 2865 }
aoqi@0 2866
aoqi@0 2867 Compile* C = phase->C;
aoqi@0 2868 assert((end_offset % BytesPerInt) == 0, "odd end offset");
aoqi@0 2869 intptr_t done_offset = end_offset;
aoqi@0 2870 if ((done_offset % BytesPerLong) != 0) {
aoqi@0 2871 done_offset -= BytesPerInt;
aoqi@0 2872 }
aoqi@0 2873 if (done_offset > start_offset) {
aoqi@0 2874 mem = clear_memory(ctl, mem, dest,
aoqi@0 2875 start_offset, phase->MakeConX(done_offset), phase);
aoqi@0 2876 }
aoqi@0 2877 if (done_offset < end_offset) { // emit the final 32-bit store
aoqi@0 2878 Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(done_offset));
aoqi@0 2879 adr = phase->transform(adr);
aoqi@0 2880 const TypePtr* atp = TypeRawPtr::BOTTOM;
aoqi@0 2881 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
aoqi@0 2882 mem = phase->transform(mem);
aoqi@0 2883 done_offset += BytesPerInt;
aoqi@0 2884 }
aoqi@0 2885 assert(done_offset == end_offset, "");
aoqi@0 2886 return mem;
aoqi@0 2887 }
aoqi@0 2888
aoqi@0 2889 //=============================================================================
aoqi@0 2890 // Do not match memory edge.
aoqi@0 2891 uint StrIntrinsicNode::match_edge(uint idx) const {
aoqi@0 2892 return idx == 2 || idx == 3;
aoqi@0 2893 }
aoqi@0 2894
aoqi@0 2895 //------------------------------Ideal------------------------------------------
aoqi@0 2896 // Return a node which is more "ideal" than the current node. Strip out
aoqi@0 2897 // control copies
aoqi@0 2898 Node *StrIntrinsicNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2899 if (remove_dead_region(phase, can_reshape)) return this;
aoqi@0 2900 // Don't bother trying to transform a dead node
aoqi@0 2901 if (in(0) && in(0)->is_top()) return NULL;
aoqi@0 2902
aoqi@0 2903 if (can_reshape) {
aoqi@0 2904 Node* mem = phase->transform(in(MemNode::Memory));
aoqi@0 2905 // If transformed to a MergeMem, get the desired slice
aoqi@0 2906 uint alias_idx = phase->C->get_alias_index(adr_type());
aoqi@0 2907 mem = mem->is_MergeMem() ? mem->as_MergeMem()->memory_at(alias_idx) : mem;
aoqi@0 2908 if (mem != in(MemNode::Memory)) {
aoqi@0 2909 set_req(MemNode::Memory, mem);
aoqi@0 2910 return this;
aoqi@0 2911 }
aoqi@0 2912 }
aoqi@0 2913 return NULL;
aoqi@0 2914 }
aoqi@0 2915
aoqi@0 2916 //------------------------------Value------------------------------------------
aoqi@0 2917 const Type *StrIntrinsicNode::Value( PhaseTransform *phase ) const {
aoqi@0 2918 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
aoqi@0 2919 return bottom_type();
aoqi@0 2920 }
aoqi@0 2921
aoqi@0 2922 //=============================================================================
aoqi@0 2923 //------------------------------match_edge-------------------------------------
aoqi@0 2924 // Do not match memory edge
aoqi@0 2925 uint EncodeISOArrayNode::match_edge(uint idx) const {
aoqi@0 2926 return idx == 2 || idx == 3; // EncodeISOArray src (Binary dst len)
aoqi@0 2927 }
aoqi@0 2928
aoqi@0 2929 //------------------------------Ideal------------------------------------------
aoqi@0 2930 // Return a node which is more "ideal" than the current node. Strip out
aoqi@0 2931 // control copies
aoqi@0 2932 Node *EncodeISOArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2933 return remove_dead_region(phase, can_reshape) ? this : NULL;
aoqi@0 2934 }
aoqi@0 2935
aoqi@0 2936 //------------------------------Value------------------------------------------
aoqi@0 2937 const Type *EncodeISOArrayNode::Value(PhaseTransform *phase) const {
aoqi@0 2938 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
aoqi@0 2939 return bottom_type();
aoqi@0 2940 }
aoqi@0 2941
aoqi@0 2942 //=============================================================================
aoqi@0 2943 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
aoqi@0 2944 : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)),
aoqi@0 2945 _adr_type(C->get_adr_type(alias_idx))
aoqi@0 2946 {
aoqi@0 2947 init_class_id(Class_MemBar);
aoqi@0 2948 Node* top = C->top();
aoqi@0 2949 init_req(TypeFunc::I_O,top);
aoqi@0 2950 init_req(TypeFunc::FramePtr,top);
aoqi@0 2951 init_req(TypeFunc::ReturnAdr,top);
aoqi@0 2952 if (precedent != NULL)
aoqi@0 2953 init_req(TypeFunc::Parms, precedent);
aoqi@0 2954 }
aoqi@0 2955
aoqi@0 2956 //------------------------------cmp--------------------------------------------
aoqi@0 2957 uint MemBarNode::hash() const { return NO_HASH; }
aoqi@0 2958 uint MemBarNode::cmp( const Node &n ) const {
aoqi@0 2959 return (&n == this); // Always fail except on self
aoqi@0 2960 }
aoqi@0 2961
aoqi@0 2962 //------------------------------make-------------------------------------------
aoqi@0 2963 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
aoqi@0 2964 switch (opcode) {
aoqi@0 2965 case Op_MemBarAcquire: return new(C) MemBarAcquireNode(C, atp, pn);
aoqi@0 2966 case Op_LoadFence: return new(C) LoadFenceNode(C, atp, pn);
aoqi@0 2967 case Op_MemBarRelease: return new(C) MemBarReleaseNode(C, atp, pn);
aoqi@0 2968 case Op_StoreFence: return new(C) StoreFenceNode(C, atp, pn);
aoqi@0 2969 case Op_MemBarAcquireLock: return new(C) MemBarAcquireLockNode(C, atp, pn);
aoqi@0 2970 case Op_MemBarReleaseLock: return new(C) MemBarReleaseLockNode(C, atp, pn);
aoqi@0 2971 case Op_MemBarVolatile: return new(C) MemBarVolatileNode(C, atp, pn);
aoqi@0 2972 case Op_MemBarCPUOrder: return new(C) MemBarCPUOrderNode(C, atp, pn);
aoqi@0 2973 case Op_Initialize: return new(C) InitializeNode(C, atp, pn);
aoqi@0 2974 case Op_MemBarStoreStore: return new(C) MemBarStoreStoreNode(C, atp, pn);
aoqi@0 2975 default: ShouldNotReachHere(); return NULL;
aoqi@0 2976 }
aoqi@0 2977 }
aoqi@0 2978
aoqi@0 2979 //------------------------------Ideal------------------------------------------
aoqi@0 2980 // Return a node which is more "ideal" than the current node. Strip out
aoqi@0 2981 // control copies
aoqi@0 2982 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 2983 if (remove_dead_region(phase, can_reshape)) return this;
aoqi@0 2984 // Don't bother trying to transform a dead node
aoqi@0 2985 if (in(0) && in(0)->is_top()) {
aoqi@0 2986 return NULL;
aoqi@0 2987 }
aoqi@0 2988
aoqi@0 2989 // Eliminate volatile MemBars for scalar replaced objects.
aoqi@0 2990 if (can_reshape && req() == (Precedent+1)) {
aoqi@0 2991 bool eliminate = false;
aoqi@0 2992 int opc = Opcode();
aoqi@0 2993 if ((opc == Op_MemBarAcquire || opc == Op_MemBarVolatile)) {
aoqi@0 2994 // Volatile field loads and stores.
aoqi@0 2995 Node* my_mem = in(MemBarNode::Precedent);
aoqi@0 2996 // The MembarAquire may keep an unused LoadNode alive through the Precedent edge
aoqi@0 2997 if ((my_mem != NULL) && (opc == Op_MemBarAcquire) && (my_mem->outcnt() == 1)) {
aoqi@0 2998 // if the Precedent is a decodeN and its input (a Load) is used at more than one place,
aoqi@0 2999 // replace this Precedent (decodeN) with the Load instead.
aoqi@0 3000 if ((my_mem->Opcode() == Op_DecodeN) && (my_mem->in(1)->outcnt() > 1)) {
aoqi@0 3001 Node* load_node = my_mem->in(1);
aoqi@0 3002 set_req(MemBarNode::Precedent, load_node);
aoqi@0 3003 phase->is_IterGVN()->_worklist.push(my_mem);
aoqi@0 3004 my_mem = load_node;
aoqi@0 3005 } else {
aoqi@0 3006 assert(my_mem->unique_out() == this, "sanity");
aoqi@0 3007 del_req(Precedent);
aoqi@0 3008 phase->is_IterGVN()->_worklist.push(my_mem); // remove dead node later
aoqi@0 3009 my_mem = NULL;
aoqi@0 3010 }
aoqi@0 3011 }
aoqi@0 3012 if (my_mem != NULL && my_mem->is_Mem()) {
aoqi@0 3013 const TypeOopPtr* t_oop = my_mem->in(MemNode::Address)->bottom_type()->isa_oopptr();
aoqi@0 3014 // Check for scalar replaced object reference.
aoqi@0 3015 if( t_oop != NULL && t_oop->is_known_instance_field() &&
aoqi@0 3016 t_oop->offset() != Type::OffsetBot &&
aoqi@0 3017 t_oop->offset() != Type::OffsetTop) {
aoqi@0 3018 eliminate = true;
aoqi@0 3019 }
aoqi@0 3020 }
aoqi@0 3021 } else if (opc == Op_MemBarRelease) {
aoqi@0 3022 // Final field stores.
aoqi@0 3023 Node* alloc = AllocateNode::Ideal_allocation(in(MemBarNode::Precedent), phase);
aoqi@0 3024 if ((alloc != NULL) && alloc->is_Allocate() &&
aoqi@0 3025 alloc->as_Allocate()->_is_non_escaping) {
aoqi@0 3026 // The allocated object does not escape.
aoqi@0 3027 eliminate = true;
aoqi@0 3028 }
aoqi@0 3029 }
aoqi@0 3030 if (eliminate) {
aoqi@0 3031 // Replace MemBar projections by its inputs.
aoqi@0 3032 PhaseIterGVN* igvn = phase->is_IterGVN();
aoqi@0 3033 igvn->replace_node(proj_out(TypeFunc::Memory), in(TypeFunc::Memory));
aoqi@0 3034 igvn->replace_node(proj_out(TypeFunc::Control), in(TypeFunc::Control));
aoqi@0 3035 // Must return either the original node (now dead) or a new node
aoqi@0 3036 // (Do not return a top here, since that would break the uniqueness of top.)
aoqi@0 3037 return new (phase->C) ConINode(TypeInt::ZERO);
aoqi@0 3038 }
aoqi@0 3039 }
aoqi@0 3040 return NULL;
aoqi@0 3041 }
aoqi@0 3042
aoqi@0 3043 //------------------------------Value------------------------------------------
aoqi@0 3044 const Type *MemBarNode::Value( PhaseTransform *phase ) const {
aoqi@0 3045 if( !in(0) ) return Type::TOP;
aoqi@0 3046 if( phase->type(in(0)) == Type::TOP )
aoqi@0 3047 return Type::TOP;
aoqi@0 3048 return TypeTuple::MEMBAR;
aoqi@0 3049 }
aoqi@0 3050
aoqi@0 3051 //------------------------------match------------------------------------------
aoqi@0 3052 // Construct projections for memory.
aoqi@0 3053 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
aoqi@0 3054 switch (proj->_con) {
aoqi@0 3055 case TypeFunc::Control:
aoqi@0 3056 case TypeFunc::Memory:
aoqi@0 3057 return new (m->C) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
aoqi@0 3058 }
aoqi@0 3059 ShouldNotReachHere();
aoqi@0 3060 return NULL;
aoqi@0 3061 }
aoqi@0 3062
aoqi@0 3063 //===========================InitializeNode====================================
aoqi@0 3064 // SUMMARY:
aoqi@0 3065 // This node acts as a memory barrier on raw memory, after some raw stores.
aoqi@0 3066 // The 'cooked' oop value feeds from the Initialize, not the Allocation.
aoqi@0 3067 // The Initialize can 'capture' suitably constrained stores as raw inits.
aoqi@0 3068 // It can coalesce related raw stores into larger units (called 'tiles').
aoqi@0 3069 // It can avoid zeroing new storage for memory units which have raw inits.
aoqi@0 3070 // At macro-expansion, it is marked 'complete', and does not optimize further.
aoqi@0 3071 //
aoqi@0 3072 // EXAMPLE:
aoqi@0 3073 // The object 'new short[2]' occupies 16 bytes in a 32-bit machine.
aoqi@0 3074 // ctl = incoming control; mem* = incoming memory
aoqi@0 3075 // (Note: A star * on a memory edge denotes I/O and other standard edges.)
aoqi@0 3076 // First allocate uninitialized memory and fill in the header:
aoqi@0 3077 // alloc = (Allocate ctl mem* 16 #short[].klass ...)
aoqi@0 3078 // ctl := alloc.Control; mem* := alloc.Memory*
aoqi@0 3079 // rawmem = alloc.Memory; rawoop = alloc.RawAddress
aoqi@0 3080 // Then initialize to zero the non-header parts of the raw memory block:
aoqi@0 3081 // init = (Initialize alloc.Control alloc.Memory* alloc.RawAddress)
aoqi@0 3082 // ctl := init.Control; mem.SLICE(#short[*]) := init.Memory
aoqi@0 3083 // After the initialize node executes, the object is ready for service:
aoqi@0 3084 // oop := (CheckCastPP init.Control alloc.RawAddress #short[])
aoqi@0 3085 // Suppose its body is immediately initialized as {1,2}:
aoqi@0 3086 // store1 = (StoreC init.Control init.Memory (+ oop 12) 1)
aoqi@0 3087 // store2 = (StoreC init.Control store1 (+ oop 14) 2)
aoqi@0 3088 // mem.SLICE(#short[*]) := store2
aoqi@0 3089 //
aoqi@0 3090 // DETAILS:
aoqi@0 3091 // An InitializeNode collects and isolates object initialization after
aoqi@0 3092 // an AllocateNode and before the next possible safepoint. As a
aoqi@0 3093 // memory barrier (MemBarNode), it keeps critical stores from drifting
aoqi@0 3094 // down past any safepoint or any publication of the allocation.
aoqi@0 3095 // Before this barrier, a newly-allocated object may have uninitialized bits.
aoqi@0 3096 // After this barrier, it may be treated as a real oop, and GC is allowed.
aoqi@0 3097 //
aoqi@0 3098 // The semantics of the InitializeNode include an implicit zeroing of
aoqi@0 3099 // the new object from object header to the end of the object.
aoqi@0 3100 // (The object header and end are determined by the AllocateNode.)
aoqi@0 3101 //
aoqi@0 3102 // Certain stores may be added as direct inputs to the InitializeNode.
aoqi@0 3103 // These stores must update raw memory, and they must be to addresses
aoqi@0 3104 // derived from the raw address produced by AllocateNode, and with
aoqi@0 3105 // a constant offset. They must be ordered by increasing offset.
aoqi@0 3106 // The first one is at in(RawStores), the last at in(req()-1).
aoqi@0 3107 // Unlike most memory operations, they are not linked in a chain,
aoqi@0 3108 // but are displayed in parallel as users of the rawmem output of
aoqi@0 3109 // the allocation.
aoqi@0 3110 //
aoqi@0 3111 // (See comments in InitializeNode::capture_store, which continue
aoqi@0 3112 // the example given above.)
aoqi@0 3113 //
aoqi@0 3114 // When the associated Allocate is macro-expanded, the InitializeNode
aoqi@0 3115 // may be rewritten to optimize collected stores. A ClearArrayNode
aoqi@0 3116 // may also be created at that point to represent any required zeroing.
aoqi@0 3117 // The InitializeNode is then marked 'complete', prohibiting further
aoqi@0 3118 // capturing of nearby memory operations.
aoqi@0 3119 //
aoqi@0 3120 // During macro-expansion, all captured initializations which store
aoqi@0 3121 // constant values of 32 bits or smaller are coalesced (if advantageous)
aoqi@0 3122 // into larger 'tiles' 32 or 64 bits. This allows an object to be
aoqi@0 3123 // initialized in fewer memory operations. Memory words which are
aoqi@0 3124 // covered by neither tiles nor non-constant stores are pre-zeroed
aoqi@0 3125 // by explicit stores of zero. (The code shape happens to do all
aoqi@0 3126 // zeroing first, then all other stores, with both sequences occurring
aoqi@0 3127 // in order of ascending offsets.)
aoqi@0 3128 //
aoqi@0 3129 // Alternatively, code may be inserted between an AllocateNode and its
aoqi@0 3130 // InitializeNode, to perform arbitrary initialization of the new object.
aoqi@0 3131 // E.g., the object copying intrinsics insert complex data transfers here.
aoqi@0 3132 // The initialization must then be marked as 'complete' disable the
aoqi@0 3133 // built-in zeroing semantics and the collection of initializing stores.
aoqi@0 3134 //
aoqi@0 3135 // While an InitializeNode is incomplete, reads from the memory state
aoqi@0 3136 // produced by it are optimizable if they match the control edge and
aoqi@0 3137 // new oop address associated with the allocation/initialization.
aoqi@0 3138 // They return a stored value (if the offset matches) or else zero.
aoqi@0 3139 // A write to the memory state, if it matches control and address,
aoqi@0 3140 // and if it is to a constant offset, may be 'captured' by the
aoqi@0 3141 // InitializeNode. It is cloned as a raw memory operation and rewired
aoqi@0 3142 // inside the initialization, to the raw oop produced by the allocation.
aoqi@0 3143 // Operations on addresses which are provably distinct (e.g., to
aoqi@0 3144 // other AllocateNodes) are allowed to bypass the initialization.
aoqi@0 3145 //
aoqi@0 3146 // The effect of all this is to consolidate object initialization
aoqi@0 3147 // (both arrays and non-arrays, both piecewise and bulk) into a
aoqi@0 3148 // single location, where it can be optimized as a unit.
aoqi@0 3149 //
aoqi@0 3150 // Only stores with an offset less than TrackedInitializationLimit words
aoqi@0 3151 // will be considered for capture by an InitializeNode. This puts a
aoqi@0 3152 // reasonable limit on the complexity of optimized initializations.
aoqi@0 3153
aoqi@0 3154 //---------------------------InitializeNode------------------------------------
aoqi@0 3155 InitializeNode::InitializeNode(Compile* C, int adr_type, Node* rawoop)
aoqi@0 3156 : _is_complete(Incomplete), _does_not_escape(false),
aoqi@0 3157 MemBarNode(C, adr_type, rawoop)
aoqi@0 3158 {
aoqi@0 3159 init_class_id(Class_Initialize);
aoqi@0 3160
aoqi@0 3161 assert(adr_type == Compile::AliasIdxRaw, "only valid atp");
aoqi@0 3162 assert(in(RawAddress) == rawoop, "proper init");
aoqi@0 3163 // Note: allocation() can be NULL, for secondary initialization barriers
aoqi@0 3164 }
aoqi@0 3165
aoqi@0 3166 // Since this node is not matched, it will be processed by the
aoqi@0 3167 // register allocator. Declare that there are no constraints
aoqi@0 3168 // on the allocation of the RawAddress edge.
aoqi@0 3169 const RegMask &InitializeNode::in_RegMask(uint idx) const {
aoqi@0 3170 // This edge should be set to top, by the set_complete. But be conservative.
aoqi@0 3171 if (idx == InitializeNode::RawAddress)
aoqi@0 3172 return *(Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()]);
aoqi@0 3173 return RegMask::Empty;
aoqi@0 3174 }
aoqi@0 3175
aoqi@0 3176 Node* InitializeNode::memory(uint alias_idx) {
aoqi@0 3177 Node* mem = in(Memory);
aoqi@0 3178 if (mem->is_MergeMem()) {
aoqi@0 3179 return mem->as_MergeMem()->memory_at(alias_idx);
aoqi@0 3180 } else {
aoqi@0 3181 // incoming raw memory is not split
aoqi@0 3182 return mem;
aoqi@0 3183 }
aoqi@0 3184 }
aoqi@0 3185
aoqi@0 3186 bool InitializeNode::is_non_zero() {
aoqi@0 3187 if (is_complete()) return false;
aoqi@0 3188 remove_extra_zeroes();
aoqi@0 3189 return (req() > RawStores);
aoqi@0 3190 }
aoqi@0 3191
aoqi@0 3192 void InitializeNode::set_complete(PhaseGVN* phase) {
aoqi@0 3193 assert(!is_complete(), "caller responsibility");
aoqi@0 3194 _is_complete = Complete;
aoqi@0 3195
aoqi@0 3196 // After this node is complete, it contains a bunch of
aoqi@0 3197 // raw-memory initializations. There is no need for
aoqi@0 3198 // it to have anything to do with non-raw memory effects.
aoqi@0 3199 // Therefore, tell all non-raw users to re-optimize themselves,
aoqi@0 3200 // after skipping the memory effects of this initialization.
aoqi@0 3201 PhaseIterGVN* igvn = phase->is_IterGVN();
aoqi@0 3202 if (igvn) igvn->add_users_to_worklist(this);
aoqi@0 3203 }
aoqi@0 3204
aoqi@0 3205 // convenience function
aoqi@0 3206 // return false if the init contains any stores already
aoqi@0 3207 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) {
aoqi@0 3208 InitializeNode* init = initialization();
aoqi@0 3209 if (init == NULL || init->is_complete()) return false;
aoqi@0 3210 init->remove_extra_zeroes();
aoqi@0 3211 // for now, if this allocation has already collected any inits, bail:
aoqi@0 3212 if (init->is_non_zero()) return false;
aoqi@0 3213 init->set_complete(phase);
aoqi@0 3214 return true;
aoqi@0 3215 }
aoqi@0 3216
aoqi@0 3217 void InitializeNode::remove_extra_zeroes() {
aoqi@0 3218 if (req() == RawStores) return;
aoqi@0 3219 Node* zmem = zero_memory();
aoqi@0 3220 uint fill = RawStores;
aoqi@0 3221 for (uint i = fill; i < req(); i++) {
aoqi@0 3222 Node* n = in(i);
aoqi@0 3223 if (n->is_top() || n == zmem) continue; // skip
aoqi@0 3224 if (fill < i) set_req(fill, n); // compact
aoqi@0 3225 ++fill;
aoqi@0 3226 }
aoqi@0 3227 // delete any empty spaces created:
aoqi@0 3228 while (fill < req()) {
aoqi@0 3229 del_req(fill);
aoqi@0 3230 }
aoqi@0 3231 }
aoqi@0 3232
aoqi@0 3233 // Helper for remembering which stores go with which offsets.
aoqi@0 3234 intptr_t InitializeNode::get_store_offset(Node* st, PhaseTransform* phase) {
aoqi@0 3235 if (!st->is_Store()) return -1; // can happen to dead code via subsume_node
aoqi@0 3236 intptr_t offset = -1;
aoqi@0 3237 Node* base = AddPNode::Ideal_base_and_offset(st->in(MemNode::Address),
aoqi@0 3238 phase, offset);
aoqi@0 3239 if (base == NULL) return -1; // something is dead,
aoqi@0 3240 if (offset < 0) return -1; // dead, dead
aoqi@0 3241 return offset;
aoqi@0 3242 }
aoqi@0 3243
aoqi@0 3244 // Helper for proving that an initialization expression is
aoqi@0 3245 // "simple enough" to be folded into an object initialization.
aoqi@0 3246 // Attempts to prove that a store's initial value 'n' can be captured
aoqi@0 3247 // within the initialization without creating a vicious cycle, such as:
aoqi@0 3248 // { Foo p = new Foo(); p.next = p; }
aoqi@0 3249 // True for constants and parameters and small combinations thereof.
aoqi@0 3250 bool InitializeNode::detect_init_independence(Node* n, int& count) {
aoqi@0 3251 if (n == NULL) return true; // (can this really happen?)
aoqi@0 3252 if (n->is_Proj()) n = n->in(0);
aoqi@0 3253 if (n == this) return false; // found a cycle
aoqi@0 3254 if (n->is_Con()) return true;
aoqi@0 3255 if (n->is_Start()) return true; // params, etc., are OK
aoqi@0 3256 if (n->is_Root()) return true; // even better
aoqi@0 3257
aoqi@0 3258 Node* ctl = n->in(0);
aoqi@0 3259 if (ctl != NULL && !ctl->is_top()) {
aoqi@0 3260 if (ctl->is_Proj()) ctl = ctl->in(0);
aoqi@0 3261 if (ctl == this) return false;
aoqi@0 3262
aoqi@0 3263 // If we already know that the enclosing memory op is pinned right after
aoqi@0 3264 // the init, then any control flow that the store has picked up
aoqi@0 3265 // must have preceded the init, or else be equal to the init.
aoqi@0 3266 // Even after loop optimizations (which might change control edges)
aoqi@0 3267 // a store is never pinned *before* the availability of its inputs.
aoqi@0 3268 if (!MemNode::all_controls_dominate(n, this))
aoqi@0 3269 return false; // failed to prove a good control
aoqi@0 3270 }
aoqi@0 3271
aoqi@0 3272 // Check data edges for possible dependencies on 'this'.
aoqi@0 3273 if ((count += 1) > 20) return false; // complexity limit
aoqi@0 3274 for (uint i = 1; i < n->req(); i++) {
aoqi@0 3275 Node* m = n->in(i);
aoqi@0 3276 if (m == NULL || m == n || m->is_top()) continue;
aoqi@0 3277 uint first_i = n->find_edge(m);
aoqi@0 3278 if (i != first_i) continue; // process duplicate edge just once
aoqi@0 3279 if (!detect_init_independence(m, count)) {
aoqi@0 3280 return false;
aoqi@0 3281 }
aoqi@0 3282 }
aoqi@0 3283
aoqi@0 3284 return true;
aoqi@0 3285 }
aoqi@0 3286
aoqi@0 3287 // Here are all the checks a Store must pass before it can be moved into
aoqi@0 3288 // an initialization. Returns zero if a check fails.
aoqi@0 3289 // On success, returns the (constant) offset to which the store applies,
aoqi@0 3290 // within the initialized memory.
aoqi@0 3291 intptr_t InitializeNode::can_capture_store(StoreNode* st, PhaseTransform* phase, bool can_reshape) {
aoqi@0 3292 const int FAIL = 0;
aoqi@0 3293 if (st->req() != MemNode::ValueIn + 1)
aoqi@0 3294 return FAIL; // an inscrutable StoreNode (card mark?)
aoqi@0 3295 Node* ctl = st->in(MemNode::Control);
aoqi@0 3296 if (!(ctl != NULL && ctl->is_Proj() && ctl->in(0) == this))
aoqi@0 3297 return FAIL; // must be unconditional after the initialization
aoqi@0 3298 Node* mem = st->in(MemNode::Memory);
aoqi@0 3299 if (!(mem->is_Proj() && mem->in(0) == this))
aoqi@0 3300 return FAIL; // must not be preceded by other stores
aoqi@0 3301 Node* adr = st->in(MemNode::Address);
aoqi@0 3302 intptr_t offset;
aoqi@0 3303 AllocateNode* alloc = AllocateNode::Ideal_allocation(adr, phase, offset);
aoqi@0 3304 if (alloc == NULL)
aoqi@0 3305 return FAIL; // inscrutable address
aoqi@0 3306 if (alloc != allocation())
aoqi@0 3307 return FAIL; // wrong allocation! (store needs to float up)
aoqi@0 3308 Node* val = st->in(MemNode::ValueIn);
aoqi@0 3309 int complexity_count = 0;
aoqi@0 3310 if (!detect_init_independence(val, complexity_count))
aoqi@0 3311 return FAIL; // stored value must be 'simple enough'
aoqi@0 3312
aoqi@0 3313 // The Store can be captured only if nothing after the allocation
aoqi@0 3314 // and before the Store is using the memory location that the store
aoqi@0 3315 // overwrites.
aoqi@0 3316 bool failed = false;
aoqi@0 3317 // If is_complete_with_arraycopy() is true the shape of the graph is
aoqi@0 3318 // well defined and is safe so no need for extra checks.
aoqi@0 3319 if (!is_complete_with_arraycopy()) {
aoqi@0 3320 // We are going to look at each use of the memory state following
aoqi@0 3321 // the allocation to make sure nothing reads the memory that the
aoqi@0 3322 // Store writes.
aoqi@0 3323 const TypePtr* t_adr = phase->type(adr)->isa_ptr();
aoqi@0 3324 int alias_idx = phase->C->get_alias_index(t_adr);
aoqi@0 3325 ResourceMark rm;
aoqi@0 3326 Unique_Node_List mems;
aoqi@0 3327 mems.push(mem);
aoqi@0 3328 Node* unique_merge = NULL;
aoqi@0 3329 for (uint next = 0; next < mems.size(); ++next) {
aoqi@0 3330 Node *m = mems.at(next);
aoqi@0 3331 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
aoqi@0 3332 Node *n = m->fast_out(j);
aoqi@0 3333 if (n->outcnt() == 0) {
aoqi@0 3334 continue;
aoqi@0 3335 }
aoqi@0 3336 if (n == st) {
aoqi@0 3337 continue;
aoqi@0 3338 } else if (n->in(0) != NULL && n->in(0) != ctl) {
aoqi@0 3339 // If the control of this use is different from the control
aoqi@0 3340 // of the Store which is right after the InitializeNode then
aoqi@0 3341 // this node cannot be between the InitializeNode and the
aoqi@0 3342 // Store.
aoqi@0 3343 continue;
aoqi@0 3344 } else if (n->is_MergeMem()) {
aoqi@0 3345 if (n->as_MergeMem()->memory_at(alias_idx) == m) {
aoqi@0 3346 // We can hit a MergeMemNode (that will likely go away
aoqi@0 3347 // later) that is a direct use of the memory state
aoqi@0 3348 // following the InitializeNode on the same slice as the
aoqi@0 3349 // store node that we'd like to capture. We need to check
aoqi@0 3350 // the uses of the MergeMemNode.
aoqi@0 3351 mems.push(n);
aoqi@0 3352 }
aoqi@0 3353 } else if (n->is_Mem()) {
aoqi@0 3354 Node* other_adr = n->in(MemNode::Address);
aoqi@0 3355 if (other_adr == adr) {
aoqi@0 3356 failed = true;
aoqi@0 3357 break;
aoqi@0 3358 } else {
aoqi@0 3359 const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr();
aoqi@0 3360 if (other_t_adr != NULL) {
aoqi@0 3361 int other_alias_idx = phase->C->get_alias_index(other_t_adr);
aoqi@0 3362 if (other_alias_idx == alias_idx) {
aoqi@0 3363 // A load from the same memory slice as the store right
aoqi@0 3364 // after the InitializeNode. We check the control of the
aoqi@0 3365 // object/array that is loaded from. If it's the same as
aoqi@0 3366 // the store control then we cannot capture the store.
aoqi@0 3367 assert(!n->is_Store(), "2 stores to same slice on same control?");
aoqi@0 3368 Node* base = other_adr;
aoqi@0 3369 assert(base->is_AddP(), err_msg_res("should be addp but is %s", base->Name()));
aoqi@0 3370 base = base->in(AddPNode::Base);
aoqi@0 3371 if (base != NULL) {
aoqi@0 3372 base = base->uncast();
aoqi@0 3373 if (base->is_Proj() && base->in(0) == alloc) {
aoqi@0 3374 failed = true;
aoqi@0 3375 break;
aoqi@0 3376 }
aoqi@0 3377 }
aoqi@0 3378 }
aoqi@0 3379 }
aoqi@0 3380 }
aoqi@0 3381 } else {
aoqi@0 3382 failed = true;
aoqi@0 3383 break;
aoqi@0 3384 }
aoqi@0 3385 }
aoqi@0 3386 }
aoqi@0 3387 }
aoqi@0 3388 if (failed) {
aoqi@0 3389 if (!can_reshape) {
aoqi@0 3390 // We decided we couldn't capture the store during parsing. We
aoqi@0 3391 // should try again during the next IGVN once the graph is
aoqi@0 3392 // cleaner.
aoqi@0 3393 phase->C->record_for_igvn(st);
aoqi@0 3394 }
aoqi@0 3395 return FAIL;
aoqi@0 3396 }
aoqi@0 3397
aoqi@0 3398 return offset; // success
aoqi@0 3399 }
aoqi@0 3400
aoqi@0 3401 // Find the captured store in(i) which corresponds to the range
aoqi@0 3402 // [start..start+size) in the initialized object.
aoqi@0 3403 // If there is one, return its index i. If there isn't, return the
aoqi@0 3404 // negative of the index where it should be inserted.
aoqi@0 3405 // Return 0 if the queried range overlaps an initialization boundary
aoqi@0 3406 // or if dead code is encountered.
aoqi@0 3407 // If size_in_bytes is zero, do not bother with overlap checks.
aoqi@0 3408 int InitializeNode::captured_store_insertion_point(intptr_t start,
aoqi@0 3409 int size_in_bytes,
aoqi@0 3410 PhaseTransform* phase) {
aoqi@0 3411 const int FAIL = 0, MAX_STORE = BytesPerLong;
aoqi@0 3412
aoqi@0 3413 if (is_complete())
aoqi@0 3414 return FAIL; // arraycopy got here first; punt
aoqi@0 3415
aoqi@0 3416 assert(allocation() != NULL, "must be present");
aoqi@0 3417
aoqi@0 3418 // no negatives, no header fields:
aoqi@0 3419 if (start < (intptr_t) allocation()->minimum_header_size()) return FAIL;
aoqi@0 3420
aoqi@0 3421 // after a certain size, we bail out on tracking all the stores:
aoqi@0 3422 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
aoqi@0 3423 if (start >= ti_limit) return FAIL;
aoqi@0 3424
aoqi@0 3425 for (uint i = InitializeNode::RawStores, limit = req(); ; ) {
aoqi@0 3426 if (i >= limit) return -(int)i; // not found; here is where to put it
aoqi@0 3427
aoqi@0 3428 Node* st = in(i);
aoqi@0 3429 intptr_t st_off = get_store_offset(st, phase);
aoqi@0 3430 if (st_off < 0) {
aoqi@0 3431 if (st != zero_memory()) {
aoqi@0 3432 return FAIL; // bail out if there is dead garbage
aoqi@0 3433 }
aoqi@0 3434 } else if (st_off > start) {
aoqi@0 3435 // ...we are done, since stores are ordered
aoqi@0 3436 if (st_off < start + size_in_bytes) {
aoqi@0 3437 return FAIL; // the next store overlaps
aoqi@0 3438 }
aoqi@0 3439 return -(int)i; // not found; here is where to put it
aoqi@0 3440 } else if (st_off < start) {
aoqi@0 3441 if (size_in_bytes != 0 &&
aoqi@0 3442 start < st_off + MAX_STORE &&
aoqi@0 3443 start < st_off + st->as_Store()->memory_size()) {
aoqi@0 3444 return FAIL; // the previous store overlaps
aoqi@0 3445 }
aoqi@0 3446 } else {
aoqi@0 3447 if (size_in_bytes != 0 &&
aoqi@0 3448 st->as_Store()->memory_size() != size_in_bytes) {
aoqi@0 3449 return FAIL; // mismatched store size
aoqi@0 3450 }
aoqi@0 3451 return i;
aoqi@0 3452 }
aoqi@0 3453
aoqi@0 3454 ++i;
aoqi@0 3455 }
aoqi@0 3456 }
aoqi@0 3457
aoqi@0 3458 // Look for a captured store which initializes at the offset 'start'
aoqi@0 3459 // with the given size. If there is no such store, and no other
aoqi@0 3460 // initialization interferes, then return zero_memory (the memory
aoqi@0 3461 // projection of the AllocateNode).
aoqi@0 3462 Node* InitializeNode::find_captured_store(intptr_t start, int size_in_bytes,
aoqi@0 3463 PhaseTransform* phase) {
aoqi@0 3464 assert(stores_are_sane(phase), "");
aoqi@0 3465 int i = captured_store_insertion_point(start, size_in_bytes, phase);
aoqi@0 3466 if (i == 0) {
aoqi@0 3467 return NULL; // something is dead
aoqi@0 3468 } else if (i < 0) {
aoqi@0 3469 return zero_memory(); // just primordial zero bits here
aoqi@0 3470 } else {
aoqi@0 3471 Node* st = in(i); // here is the store at this position
aoqi@0 3472 assert(get_store_offset(st->as_Store(), phase) == start, "sanity");
aoqi@0 3473 return st;
aoqi@0 3474 }
aoqi@0 3475 }
aoqi@0 3476
aoqi@0 3477 // Create, as a raw pointer, an address within my new object at 'offset'.
aoqi@0 3478 Node* InitializeNode::make_raw_address(intptr_t offset,
aoqi@0 3479 PhaseTransform* phase) {
aoqi@0 3480 Node* addr = in(RawAddress);
aoqi@0 3481 if (offset != 0) {
aoqi@0 3482 Compile* C = phase->C;
aoqi@0 3483 addr = phase->transform( new (C) AddPNode(C->top(), addr,
aoqi@0 3484 phase->MakeConX(offset)) );
aoqi@0 3485 }
aoqi@0 3486 return addr;
aoqi@0 3487 }
aoqi@0 3488
aoqi@0 3489 // Clone the given store, converting it into a raw store
aoqi@0 3490 // initializing a field or element of my new object.
aoqi@0 3491 // Caller is responsible for retiring the original store,
aoqi@0 3492 // with subsume_node or the like.
aoqi@0 3493 //
aoqi@0 3494 // From the example above InitializeNode::InitializeNode,
aoqi@0 3495 // here are the old stores to be captured:
aoqi@0 3496 // store1 = (StoreC init.Control init.Memory (+ oop 12) 1)
aoqi@0 3497 // store2 = (StoreC init.Control store1 (+ oop 14) 2)
aoqi@0 3498 //
aoqi@0 3499 // Here is the changed code; note the extra edges on init:
aoqi@0 3500 // alloc = (Allocate ...)
aoqi@0 3501 // rawoop = alloc.RawAddress
aoqi@0 3502 // rawstore1 = (StoreC alloc.Control alloc.Memory (+ rawoop 12) 1)
aoqi@0 3503 // rawstore2 = (StoreC alloc.Control alloc.Memory (+ rawoop 14) 2)
aoqi@0 3504 // init = (Initialize alloc.Control alloc.Memory rawoop
aoqi@0 3505 // rawstore1 rawstore2)
aoqi@0 3506 //
aoqi@0 3507 Node* InitializeNode::capture_store(StoreNode* st, intptr_t start,
aoqi@0 3508 PhaseTransform* phase, bool can_reshape) {
aoqi@0 3509 assert(stores_are_sane(phase), "");
aoqi@0 3510
aoqi@0 3511 if (start < 0) return NULL;
aoqi@0 3512 assert(can_capture_store(st, phase, can_reshape) == start, "sanity");
aoqi@0 3513
aoqi@0 3514 Compile* C = phase->C;
aoqi@0 3515 int size_in_bytes = st->memory_size();
aoqi@0 3516 int i = captured_store_insertion_point(start, size_in_bytes, phase);
aoqi@0 3517 if (i == 0) return NULL; // bail out
aoqi@0 3518 Node* prev_mem = NULL; // raw memory for the captured store
aoqi@0 3519 if (i > 0) {
aoqi@0 3520 prev_mem = in(i); // there is a pre-existing store under this one
aoqi@0 3521 set_req(i, C->top()); // temporarily disconnect it
aoqi@0 3522 // See StoreNode::Ideal 'st->outcnt() == 1' for the reason to disconnect.
aoqi@0 3523 } else {
aoqi@0 3524 i = -i; // no pre-existing store
aoqi@0 3525 prev_mem = zero_memory(); // a slice of the newly allocated object
aoqi@0 3526 if (i > InitializeNode::RawStores && in(i-1) == prev_mem)
aoqi@0 3527 set_req(--i, C->top()); // reuse this edge; it has been folded away
aoqi@0 3528 else
aoqi@0 3529 ins_req(i, C->top()); // build a new edge
aoqi@0 3530 }
aoqi@0 3531 Node* new_st = st->clone();
aoqi@0 3532 new_st->set_req(MemNode::Control, in(Control));
aoqi@0 3533 new_st->set_req(MemNode::Memory, prev_mem);
aoqi@0 3534 new_st->set_req(MemNode::Address, make_raw_address(start, phase));
aoqi@0 3535 new_st = phase->transform(new_st);
aoqi@0 3536
aoqi@0 3537 // At this point, new_st might have swallowed a pre-existing store
aoqi@0 3538 // at the same offset, or perhaps new_st might have disappeared,
aoqi@0 3539 // if it redundantly stored the same value (or zero to fresh memory).
aoqi@0 3540
aoqi@0 3541 // In any case, wire it in:
aoqi@0 3542 set_req(i, new_st);
aoqi@0 3543
aoqi@0 3544 // The caller may now kill the old guy.
aoqi@0 3545 DEBUG_ONLY(Node* check_st = find_captured_store(start, size_in_bytes, phase));
aoqi@0 3546 assert(check_st == new_st || check_st == NULL, "must be findable");
aoqi@0 3547 assert(!is_complete(), "");
aoqi@0 3548 return new_st;
aoqi@0 3549 }
aoqi@0 3550
aoqi@0 3551 static bool store_constant(jlong* tiles, int num_tiles,
aoqi@0 3552 intptr_t st_off, int st_size,
aoqi@0 3553 jlong con) {
aoqi@0 3554 if ((st_off & (st_size-1)) != 0)
aoqi@0 3555 return false; // strange store offset (assume size==2**N)
aoqi@0 3556 address addr = (address)tiles + st_off;
aoqi@0 3557 assert(st_off >= 0 && addr+st_size <= (address)&tiles[num_tiles], "oob");
aoqi@0 3558 switch (st_size) {
aoqi@0 3559 case sizeof(jbyte): *(jbyte*) addr = (jbyte) con; break;
aoqi@0 3560 case sizeof(jchar): *(jchar*) addr = (jchar) con; break;
aoqi@0 3561 case sizeof(jint): *(jint*) addr = (jint) con; break;
aoqi@0 3562 case sizeof(jlong): *(jlong*) addr = (jlong) con; break;
aoqi@0 3563 default: return false; // strange store size (detect size!=2**N here)
aoqi@0 3564 }
aoqi@0 3565 return true; // return success to caller
aoqi@0 3566 }
aoqi@0 3567
aoqi@0 3568 // Coalesce subword constants into int constants and possibly
aoqi@0 3569 // into long constants. The goal, if the CPU permits,
aoqi@0 3570 // is to initialize the object with a small number of 64-bit tiles.
aoqi@0 3571 // Also, convert floating-point constants to bit patterns.
aoqi@0 3572 // Non-constants are not relevant to this pass.
aoqi@0 3573 //
aoqi@0 3574 // In terms of the running example on InitializeNode::InitializeNode
aoqi@0 3575 // and InitializeNode::capture_store, here is the transformation
aoqi@0 3576 // of rawstore1 and rawstore2 into rawstore12:
aoqi@0 3577 // alloc = (Allocate ...)
aoqi@0 3578 // rawoop = alloc.RawAddress
aoqi@0 3579 // tile12 = 0x00010002
aoqi@0 3580 // rawstore12 = (StoreI alloc.Control alloc.Memory (+ rawoop 12) tile12)
aoqi@0 3581 // init = (Initialize alloc.Control alloc.Memory rawoop rawstore12)
aoqi@0 3582 //
aoqi@0 3583 void
aoqi@0 3584 InitializeNode::coalesce_subword_stores(intptr_t header_size,
aoqi@0 3585 Node* size_in_bytes,
aoqi@0 3586 PhaseGVN* phase) {
aoqi@0 3587 Compile* C = phase->C;
aoqi@0 3588
aoqi@0 3589 assert(stores_are_sane(phase), "");
aoqi@0 3590 // Note: After this pass, they are not completely sane,
aoqi@0 3591 // since there may be some overlaps.
aoqi@0 3592
aoqi@0 3593 int old_subword = 0, old_long = 0, new_int = 0, new_long = 0;
aoqi@0 3594
aoqi@0 3595 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
aoqi@0 3596 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, ti_limit);
aoqi@0 3597 size_limit = MIN2(size_limit, ti_limit);
aoqi@0 3598 size_limit = align_size_up(size_limit, BytesPerLong);
aoqi@0 3599 int num_tiles = size_limit / BytesPerLong;
aoqi@0 3600
aoqi@0 3601 // allocate space for the tile map:
aoqi@0 3602 const int small_len = DEBUG_ONLY(true ? 3 :) 30; // keep stack frames small
aoqi@0 3603 jlong tiles_buf[small_len];
aoqi@0 3604 Node* nodes_buf[small_len];
aoqi@0 3605 jlong inits_buf[small_len];
aoqi@0 3606 jlong* tiles = ((num_tiles <= small_len) ? &tiles_buf[0]
aoqi@0 3607 : NEW_RESOURCE_ARRAY(jlong, num_tiles));
aoqi@0 3608 Node** nodes = ((num_tiles <= small_len) ? &nodes_buf[0]
aoqi@0 3609 : NEW_RESOURCE_ARRAY(Node*, num_tiles));
aoqi@0 3610 jlong* inits = ((num_tiles <= small_len) ? &inits_buf[0]
aoqi@0 3611 : NEW_RESOURCE_ARRAY(jlong, num_tiles));
aoqi@0 3612 // tiles: exact bitwise model of all primitive constants
aoqi@0 3613 // nodes: last constant-storing node subsumed into the tiles model
aoqi@0 3614 // inits: which bytes (in each tile) are touched by any initializations
aoqi@0 3615
aoqi@0 3616 //// Pass A: Fill in the tile model with any relevant stores.
aoqi@0 3617
aoqi@0 3618 Copy::zero_to_bytes(tiles, sizeof(tiles[0]) * num_tiles);
aoqi@0 3619 Copy::zero_to_bytes(nodes, sizeof(nodes[0]) * num_tiles);
aoqi@0 3620 Copy::zero_to_bytes(inits, sizeof(inits[0]) * num_tiles);
aoqi@0 3621 Node* zmem = zero_memory(); // initially zero memory state
aoqi@0 3622 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
aoqi@0 3623 Node* st = in(i);
aoqi@0 3624 intptr_t st_off = get_store_offset(st, phase);
aoqi@0 3625
aoqi@0 3626 // Figure out the store's offset and constant value:
aoqi@0 3627 if (st_off < header_size) continue; //skip (ignore header)
aoqi@0 3628 if (st->in(MemNode::Memory) != zmem) continue; //skip (odd store chain)
aoqi@0 3629 int st_size = st->as_Store()->memory_size();
aoqi@0 3630 if (st_off + st_size > size_limit) break;
aoqi@0 3631
aoqi@0 3632 // Record which bytes are touched, whether by constant or not.
aoqi@0 3633 if (!store_constant(inits, num_tiles, st_off, st_size, (jlong) -1))
aoqi@0 3634 continue; // skip (strange store size)
aoqi@0 3635
aoqi@0 3636 const Type* val = phase->type(st->in(MemNode::ValueIn));
aoqi@0 3637 if (!val->singleton()) continue; //skip (non-con store)
aoqi@0 3638 BasicType type = val->basic_type();
aoqi@0 3639
aoqi@0 3640 jlong con = 0;
aoqi@0 3641 switch (type) {
aoqi@0 3642 case T_INT: con = val->is_int()->get_con(); break;
aoqi@0 3643 case T_LONG: con = val->is_long()->get_con(); break;
aoqi@0 3644 case T_FLOAT: con = jint_cast(val->getf()); break;
aoqi@0 3645 case T_DOUBLE: con = jlong_cast(val->getd()); break;
aoqi@0 3646 default: continue; //skip (odd store type)
aoqi@0 3647 }
aoqi@0 3648
aoqi@0 3649 if (type == T_LONG && Matcher::isSimpleConstant64(con) &&
aoqi@0 3650 st->Opcode() == Op_StoreL) {
aoqi@0 3651 continue; // This StoreL is already optimal.
aoqi@0 3652 }
aoqi@0 3653
aoqi@0 3654 // Store down the constant.
aoqi@0 3655 store_constant(tiles, num_tiles, st_off, st_size, con);
aoqi@0 3656
aoqi@0 3657 intptr_t j = st_off >> LogBytesPerLong;
aoqi@0 3658
aoqi@0 3659 if (type == T_INT && st_size == BytesPerInt
aoqi@0 3660 && (st_off & BytesPerInt) == BytesPerInt) {
aoqi@0 3661 jlong lcon = tiles[j];
aoqi@0 3662 if (!Matcher::isSimpleConstant64(lcon) &&
aoqi@0 3663 st->Opcode() == Op_StoreI) {
aoqi@0 3664 // This StoreI is already optimal by itself.
aoqi@0 3665 jint* intcon = (jint*) &tiles[j];
aoqi@0 3666 intcon[1] = 0; // undo the store_constant()
aoqi@0 3667
aoqi@0 3668 // If the previous store is also optimal by itself, back up and
aoqi@0 3669 // undo the action of the previous loop iteration... if we can.
aoqi@0 3670 // But if we can't, just let the previous half take care of itself.
aoqi@0 3671 st = nodes[j];
aoqi@0 3672 st_off -= BytesPerInt;
aoqi@0 3673 con = intcon[0];
aoqi@0 3674 if (con != 0 && st != NULL && st->Opcode() == Op_StoreI) {
aoqi@0 3675 assert(st_off >= header_size, "still ignoring header");
aoqi@0 3676 assert(get_store_offset(st, phase) == st_off, "must be");
aoqi@0 3677 assert(in(i-1) == zmem, "must be");
aoqi@0 3678 DEBUG_ONLY(const Type* tcon = phase->type(st->in(MemNode::ValueIn)));
aoqi@0 3679 assert(con == tcon->is_int()->get_con(), "must be");
aoqi@0 3680 // Undo the effects of the previous loop trip, which swallowed st:
aoqi@0 3681 intcon[0] = 0; // undo store_constant()
aoqi@0 3682 set_req(i-1, st); // undo set_req(i, zmem)
aoqi@0 3683 nodes[j] = NULL; // undo nodes[j] = st
aoqi@0 3684 --old_subword; // undo ++old_subword
aoqi@0 3685 }
aoqi@0 3686 continue; // This StoreI is already optimal.
aoqi@0 3687 }
aoqi@0 3688 }
aoqi@0 3689
aoqi@0 3690 // This store is not needed.
aoqi@0 3691 set_req(i, zmem);
aoqi@0 3692 nodes[j] = st; // record for the moment
aoqi@0 3693 if (st_size < BytesPerLong) // something has changed
aoqi@0 3694 ++old_subword; // includes int/float, but who's counting...
aoqi@0 3695 else ++old_long;
aoqi@0 3696 }
aoqi@0 3697
aoqi@0 3698 if ((old_subword + old_long) == 0)
aoqi@0 3699 return; // nothing more to do
aoqi@0 3700
aoqi@0 3701 //// Pass B: Convert any non-zero tiles into optimal constant stores.
aoqi@0 3702 // Be sure to insert them before overlapping non-constant stores.
aoqi@0 3703 // (E.g., byte[] x = { 1,2,y,4 } => x[int 0] = 0x01020004, x[2]=y.)
aoqi@0 3704 for (int j = 0; j < num_tiles; j++) {
aoqi@0 3705 jlong con = tiles[j];
aoqi@0 3706 jlong init = inits[j];
aoqi@0 3707 if (con == 0) continue;
aoqi@0 3708 jint con0, con1; // split the constant, address-wise
aoqi@0 3709 jint init0, init1; // split the init map, address-wise
aoqi@0 3710 { union { jlong con; jint intcon[2]; } u;
aoqi@0 3711 u.con = con;
aoqi@0 3712 con0 = u.intcon[0];
aoqi@0 3713 con1 = u.intcon[1];
aoqi@0 3714 u.con = init;
aoqi@0 3715 init0 = u.intcon[0];
aoqi@0 3716 init1 = u.intcon[1];
aoqi@0 3717 }
aoqi@0 3718
aoqi@0 3719 Node* old = nodes[j];
aoqi@0 3720 assert(old != NULL, "need the prior store");
aoqi@0 3721 intptr_t offset = (j * BytesPerLong);
aoqi@0 3722
aoqi@0 3723 bool split = !Matcher::isSimpleConstant64(con);
aoqi@0 3724
aoqi@0 3725 if (offset < header_size) {
aoqi@0 3726 assert(offset + BytesPerInt >= header_size, "second int counts");
aoqi@0 3727 assert(*(jint*)&tiles[j] == 0, "junk in header");
aoqi@0 3728 split = true; // only the second word counts
aoqi@0 3729 // Example: int a[] = { 42 ... }
aoqi@0 3730 } else if (con0 == 0 && init0 == -1) {
aoqi@0 3731 split = true; // first word is covered by full inits
aoqi@0 3732 // Example: int a[] = { ... foo(), 42 ... }
aoqi@0 3733 } else if (con1 == 0 && init1 == -1) {
aoqi@0 3734 split = true; // second word is covered by full inits
aoqi@0 3735 // Example: int a[] = { ... 42, foo() ... }
aoqi@0 3736 }
aoqi@0 3737
aoqi@0 3738 // Here's a case where init0 is neither 0 nor -1:
aoqi@0 3739 // byte a[] = { ... 0,0,foo(),0, 0,0,0,42 ... }
aoqi@0 3740 // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF.
aoqi@0 3741 // In this case the tile is not split; it is (jlong)42.
aoqi@0 3742 // The big tile is stored down, and then the foo() value is inserted.
aoqi@0 3743 // (If there were foo(),foo() instead of foo(),0, init0 would be -1.)
aoqi@0 3744
aoqi@0 3745 Node* ctl = old->in(MemNode::Control);
aoqi@0 3746 Node* adr = make_raw_address(offset, phase);
aoqi@0 3747 const TypePtr* atp = TypeRawPtr::BOTTOM;
aoqi@0 3748
aoqi@0 3749 // One or two coalesced stores to plop down.
aoqi@0 3750 Node* st[2];
aoqi@0 3751 intptr_t off[2];
aoqi@0 3752 int nst = 0;
aoqi@0 3753 if (!split) {
aoqi@0 3754 ++new_long;
aoqi@0 3755 off[nst] = offset;
aoqi@0 3756 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
aoqi@0 3757 phase->longcon(con), T_LONG, MemNode::unordered);
aoqi@0 3758 } else {
aoqi@0 3759 // Omit either if it is a zero.
aoqi@0 3760 if (con0 != 0) {
aoqi@0 3761 ++new_int;
aoqi@0 3762 off[nst] = offset;
aoqi@0 3763 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
aoqi@0 3764 phase->intcon(con0), T_INT, MemNode::unordered);
aoqi@0 3765 }
aoqi@0 3766 if (con1 != 0) {
aoqi@0 3767 ++new_int;
aoqi@0 3768 offset += BytesPerInt;
aoqi@0 3769 adr = make_raw_address(offset, phase);
aoqi@0 3770 off[nst] = offset;
aoqi@0 3771 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
aoqi@0 3772 phase->intcon(con1), T_INT, MemNode::unordered);
aoqi@0 3773 }
aoqi@0 3774 }
aoqi@0 3775
aoqi@0 3776 // Insert second store first, then the first before the second.
aoqi@0 3777 // Insert each one just before any overlapping non-constant stores.
aoqi@0 3778 while (nst > 0) {
aoqi@0 3779 Node* st1 = st[--nst];
aoqi@0 3780 C->copy_node_notes_to(st1, old);
aoqi@0 3781 st1 = phase->transform(st1);
aoqi@0 3782 offset = off[nst];
aoqi@0 3783 assert(offset >= header_size, "do not smash header");
aoqi@0 3784 int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase);
aoqi@0 3785 guarantee(ins_idx != 0, "must re-insert constant store");
aoqi@0 3786 if (ins_idx < 0) ins_idx = -ins_idx; // never overlap
aoqi@0 3787 if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem)
aoqi@0 3788 set_req(--ins_idx, st1);
aoqi@0 3789 else
aoqi@0 3790 ins_req(ins_idx, st1);
aoqi@0 3791 }
aoqi@0 3792 }
aoqi@0 3793
aoqi@0 3794 if (PrintCompilation && WizardMode)
aoqi@0 3795 tty->print_cr("Changed %d/%d subword/long constants into %d/%d int/long",
aoqi@0 3796 old_subword, old_long, new_int, new_long);
aoqi@0 3797 if (C->log() != NULL)
aoqi@0 3798 C->log()->elem("comment that='%d/%d subword/long to %d/%d int/long'",
aoqi@0 3799 old_subword, old_long, new_int, new_long);
aoqi@0 3800
aoqi@0 3801 // Clean up any remaining occurrences of zmem:
aoqi@0 3802 remove_extra_zeroes();
aoqi@0 3803 }
aoqi@0 3804
aoqi@0 3805 // Explore forward from in(start) to find the first fully initialized
aoqi@0 3806 // word, and return its offset. Skip groups of subword stores which
aoqi@0 3807 // together initialize full words. If in(start) is itself part of a
aoqi@0 3808 // fully initialized word, return the offset of in(start). If there
aoqi@0 3809 // are no following full-word stores, or if something is fishy, return
aoqi@0 3810 // a negative value.
aoqi@0 3811 intptr_t InitializeNode::find_next_fullword_store(uint start, PhaseGVN* phase) {
aoqi@0 3812 int int_map = 0;
aoqi@0 3813 intptr_t int_map_off = 0;
aoqi@0 3814 const int FULL_MAP = right_n_bits(BytesPerInt); // the int_map we hope for
aoqi@0 3815
aoqi@0 3816 for (uint i = start, limit = req(); i < limit; i++) {
aoqi@0 3817 Node* st = in(i);
aoqi@0 3818
aoqi@0 3819 intptr_t st_off = get_store_offset(st, phase);
aoqi@0 3820 if (st_off < 0) break; // return conservative answer
aoqi@0 3821
aoqi@0 3822 int st_size = st->as_Store()->memory_size();
aoqi@0 3823 if (st_size >= BytesPerInt && (st_off % BytesPerInt) == 0) {
aoqi@0 3824 return st_off; // we found a complete word init
aoqi@0 3825 }
aoqi@0 3826
aoqi@0 3827 // update the map:
aoqi@0 3828
aoqi@0 3829 intptr_t this_int_off = align_size_down(st_off, BytesPerInt);
aoqi@0 3830 if (this_int_off != int_map_off) {
aoqi@0 3831 // reset the map:
aoqi@0 3832 int_map = 0;
aoqi@0 3833 int_map_off = this_int_off;
aoqi@0 3834 }
aoqi@0 3835
aoqi@0 3836 int subword_off = st_off - this_int_off;
aoqi@0 3837 int_map |= right_n_bits(st_size) << subword_off;
aoqi@0 3838 if ((int_map & FULL_MAP) == FULL_MAP) {
aoqi@0 3839 return this_int_off; // we found a complete word init
aoqi@0 3840 }
aoqi@0 3841
aoqi@0 3842 // Did this store hit or cross the word boundary?
aoqi@0 3843 intptr_t next_int_off = align_size_down(st_off + st_size, BytesPerInt);
aoqi@0 3844 if (next_int_off == this_int_off + BytesPerInt) {
aoqi@0 3845 // We passed the current int, without fully initializing it.
aoqi@0 3846 int_map_off = next_int_off;
aoqi@0 3847 int_map >>= BytesPerInt;
aoqi@0 3848 } else if (next_int_off > this_int_off + BytesPerInt) {
aoqi@0 3849 // We passed the current and next int.
aoqi@0 3850 return this_int_off + BytesPerInt;
aoqi@0 3851 }
aoqi@0 3852 }
aoqi@0 3853
aoqi@0 3854 return -1;
aoqi@0 3855 }
aoqi@0 3856
aoqi@0 3857
aoqi@0 3858 // Called when the associated AllocateNode is expanded into CFG.
aoqi@0 3859 // At this point, we may perform additional optimizations.
aoqi@0 3860 // Linearize the stores by ascending offset, to make memory
aoqi@0 3861 // activity as coherent as possible.
aoqi@0 3862 Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
aoqi@0 3863 intptr_t header_size,
aoqi@0 3864 Node* size_in_bytes,
aoqi@0 3865 PhaseGVN* phase) {
aoqi@0 3866 assert(!is_complete(), "not already complete");
aoqi@0 3867 assert(stores_are_sane(phase), "");
aoqi@0 3868 assert(allocation() != NULL, "must be present");
aoqi@0 3869
aoqi@0 3870 remove_extra_zeroes();
aoqi@0 3871
aoqi@0 3872 if (ReduceFieldZeroing || ReduceBulkZeroing)
aoqi@0 3873 // reduce instruction count for common initialization patterns
aoqi@0 3874 coalesce_subword_stores(header_size, size_in_bytes, phase);
aoqi@0 3875
aoqi@0 3876 Node* zmem = zero_memory(); // initially zero memory state
aoqi@0 3877 Node* inits = zmem; // accumulating a linearized chain of inits
aoqi@0 3878 #ifdef ASSERT
aoqi@0 3879 intptr_t first_offset = allocation()->minimum_header_size();
aoqi@0 3880 intptr_t last_init_off = first_offset; // previous init offset
aoqi@0 3881 intptr_t last_init_end = first_offset; // previous init offset+size
aoqi@0 3882 intptr_t last_tile_end = first_offset; // previous tile offset+size
aoqi@0 3883 #endif
aoqi@0 3884 intptr_t zeroes_done = header_size;
aoqi@0 3885
aoqi@0 3886 bool do_zeroing = true; // we might give up if inits are very sparse
aoqi@0 3887 int big_init_gaps = 0; // how many large gaps have we seen?
aoqi@0 3888
aoqi@0 3889 if (ZeroTLAB) do_zeroing = false;
aoqi@0 3890 if (!ReduceFieldZeroing && !ReduceBulkZeroing) do_zeroing = false;
aoqi@0 3891
aoqi@0 3892 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
aoqi@0 3893 Node* st = in(i);
aoqi@0 3894 intptr_t st_off = get_store_offset(st, phase);
aoqi@0 3895 if (st_off < 0)
aoqi@0 3896 break; // unknown junk in the inits
aoqi@0 3897 if (st->in(MemNode::Memory) != zmem)
aoqi@0 3898 break; // complicated store chains somehow in list
aoqi@0 3899
aoqi@0 3900 int st_size = st->as_Store()->memory_size();
aoqi@0 3901 intptr_t next_init_off = st_off + st_size;
aoqi@0 3902
aoqi@0 3903 if (do_zeroing && zeroes_done < next_init_off) {
aoqi@0 3904 // See if this store needs a zero before it or under it.
aoqi@0 3905 intptr_t zeroes_needed = st_off;
aoqi@0 3906
aoqi@0 3907 if (st_size < BytesPerInt) {
aoqi@0 3908 // Look for subword stores which only partially initialize words.
aoqi@0 3909 // If we find some, we must lay down some word-level zeroes first,
aoqi@0 3910 // underneath the subword stores.
aoqi@0 3911 //
aoqi@0 3912 // Examples:
aoqi@0 3913 // byte[] a = { p,q,r,s } => a[0]=p,a[1]=q,a[2]=r,a[3]=s
aoqi@0 3914 // byte[] a = { x,y,0,0 } => a[0..3] = 0, a[0]=x,a[1]=y
aoqi@0 3915 // byte[] a = { 0,0,z,0 } => a[0..3] = 0, a[2]=z
aoqi@0 3916 //
aoqi@0 3917 // Note: coalesce_subword_stores may have already done this,
aoqi@0 3918 // if it was prompted by constant non-zero subword initializers.
aoqi@0 3919 // But this case can still arise with non-constant stores.
aoqi@0 3920
aoqi@0 3921 intptr_t next_full_store = find_next_fullword_store(i, phase);
aoqi@0 3922
aoqi@0 3923 // In the examples above:
aoqi@0 3924 // in(i) p q r s x y z
aoqi@0 3925 // st_off 12 13 14 15 12 13 14
aoqi@0 3926 // st_size 1 1 1 1 1 1 1
aoqi@0 3927 // next_full_s. 12 16 16 16 16 16 16
aoqi@0 3928 // z's_done 12 16 16 16 12 16 12
aoqi@0 3929 // z's_needed 12 16 16 16 16 16 16
aoqi@0 3930 // zsize 0 0 0 0 4 0 4
aoqi@0 3931 if (next_full_store < 0) {
aoqi@0 3932 // Conservative tack: Zero to end of current word.
aoqi@0 3933 zeroes_needed = align_size_up(zeroes_needed, BytesPerInt);
aoqi@0 3934 } else {
aoqi@0 3935 // Zero to beginning of next fully initialized word.
aoqi@0 3936 // Or, don't zero at all, if we are already in that word.
aoqi@0 3937 assert(next_full_store >= zeroes_needed, "must go forward");
aoqi@0 3938 assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary");
aoqi@0 3939 zeroes_needed = next_full_store;
aoqi@0 3940 }
aoqi@0 3941 }
aoqi@0 3942
aoqi@0 3943 if (zeroes_needed > zeroes_done) {
aoqi@0 3944 intptr_t zsize = zeroes_needed - zeroes_done;
aoqi@0 3945 // Do some incremental zeroing on rawmem, in parallel with inits.
aoqi@0 3946 zeroes_done = align_size_down(zeroes_done, BytesPerInt);
aoqi@0 3947 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
aoqi@0 3948 zeroes_done, zeroes_needed,
aoqi@0 3949 phase);
aoqi@0 3950 zeroes_done = zeroes_needed;
aoqi@0 3951 if (zsize > Matcher::init_array_short_size && ++big_init_gaps > 2)
aoqi@0 3952 do_zeroing = false; // leave the hole, next time
aoqi@0 3953 }
aoqi@0 3954 }
aoqi@0 3955
aoqi@0 3956 // Collect the store and move on:
aoqi@0 3957 st->set_req(MemNode::Memory, inits);
aoqi@0 3958 inits = st; // put it on the linearized chain
aoqi@0 3959 set_req(i, zmem); // unhook from previous position
aoqi@0 3960
aoqi@0 3961 if (zeroes_done == st_off)
aoqi@0 3962 zeroes_done = next_init_off;
aoqi@0 3963
aoqi@0 3964 assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any");
aoqi@0 3965
aoqi@0 3966 #ifdef ASSERT
aoqi@0 3967 // Various order invariants. Weaker than stores_are_sane because
aoqi@0 3968 // a large constant tile can be filled in by smaller non-constant stores.
aoqi@0 3969 assert(st_off >= last_init_off, "inits do not reverse");
aoqi@0 3970 last_init_off = st_off;
aoqi@0 3971 const Type* val = NULL;
aoqi@0 3972 if (st_size >= BytesPerInt &&
aoqi@0 3973 (val = phase->type(st->in(MemNode::ValueIn)))->singleton() &&
aoqi@0 3974 (int)val->basic_type() < (int)T_OBJECT) {
aoqi@0 3975 assert(st_off >= last_tile_end, "tiles do not overlap");
aoqi@0 3976 assert(st_off >= last_init_end, "tiles do not overwrite inits");
aoqi@0 3977 last_tile_end = MAX2(last_tile_end, next_init_off);
aoqi@0 3978 } else {
aoqi@0 3979 intptr_t st_tile_end = align_size_up(next_init_off, BytesPerLong);
aoqi@0 3980 assert(st_tile_end >= last_tile_end, "inits stay with tiles");
aoqi@0 3981 assert(st_off >= last_init_end, "inits do not overlap");
aoqi@0 3982 last_init_end = next_init_off; // it's a non-tile
aoqi@0 3983 }
aoqi@0 3984 #endif //ASSERT
aoqi@0 3985 }
aoqi@0 3986
aoqi@0 3987 remove_extra_zeroes(); // clear out all the zmems left over
aoqi@0 3988 add_req(inits);
aoqi@0 3989
aoqi@0 3990 if (!ZeroTLAB) {
aoqi@0 3991 // If anything remains to be zeroed, zero it all now.
aoqi@0 3992 zeroes_done = align_size_down(zeroes_done, BytesPerInt);
aoqi@0 3993 // if it is the last unused 4 bytes of an instance, forget about it
aoqi@0 3994 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint);
aoqi@0 3995 if (zeroes_done + BytesPerLong >= size_limit) {
aoqi@0 3996 assert(allocation() != NULL, "");
aoqi@0 3997 if (allocation()->Opcode() == Op_Allocate) {
aoqi@0 3998 Node* klass_node = allocation()->in(AllocateNode::KlassNode);
aoqi@0 3999 ciKlass* k = phase->type(klass_node)->is_klassptr()->klass();
aoqi@0 4000 if (zeroes_done == k->layout_helper())
aoqi@0 4001 zeroes_done = size_limit;
aoqi@0 4002 }
aoqi@0 4003 }
aoqi@0 4004 if (zeroes_done < size_limit) {
aoqi@0 4005 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
aoqi@0 4006 zeroes_done, size_in_bytes, phase);
aoqi@0 4007 }
aoqi@0 4008 }
aoqi@0 4009
aoqi@0 4010 set_complete(phase);
aoqi@0 4011 return rawmem;
aoqi@0 4012 }
aoqi@0 4013
aoqi@0 4014
aoqi@0 4015 #ifdef ASSERT
aoqi@0 4016 bool InitializeNode::stores_are_sane(PhaseTransform* phase) {
aoqi@0 4017 if (is_complete())
aoqi@0 4018 return true; // stores could be anything at this point
aoqi@0 4019 assert(allocation() != NULL, "must be present");
aoqi@0 4020 intptr_t last_off = allocation()->minimum_header_size();
aoqi@0 4021 for (uint i = InitializeNode::RawStores; i < req(); i++) {
aoqi@0 4022 Node* st = in(i);
aoqi@0 4023 intptr_t st_off = get_store_offset(st, phase);
aoqi@0 4024 if (st_off < 0) continue; // ignore dead garbage
aoqi@0 4025 if (last_off > st_off) {
aoqi@0 4026 tty->print_cr("*** bad store offset at %d: " INTX_FORMAT " > " INTX_FORMAT, i, last_off, st_off);
aoqi@0 4027 this->dump(2);
aoqi@0 4028 assert(false, "ascending store offsets");
aoqi@0 4029 return false;
aoqi@0 4030 }
aoqi@0 4031 last_off = st_off + st->as_Store()->memory_size();
aoqi@0 4032 }
aoqi@0 4033 return true;
aoqi@0 4034 }
aoqi@0 4035 #endif //ASSERT
aoqi@0 4036
aoqi@0 4037
aoqi@0 4038
aoqi@0 4039
aoqi@0 4040 //============================MergeMemNode=====================================
aoqi@0 4041 //
aoqi@0 4042 // SEMANTICS OF MEMORY MERGES: A MergeMem is a memory state assembled from several
aoqi@0 4043 // contributing store or call operations. Each contributor provides the memory
aoqi@0 4044 // state for a particular "alias type" (see Compile::alias_type). For example,
aoqi@0 4045 // if a MergeMem has an input X for alias category #6, then any memory reference
aoqi@0 4046 // to alias category #6 may use X as its memory state input, as an exact equivalent
aoqi@0 4047 // to using the MergeMem as a whole.
aoqi@0 4048 // Load<6>( MergeMem(<6>: X, ...), p ) <==> Load<6>(X,p)
aoqi@0 4049 //
aoqi@0 4050 // (Here, the <N> notation gives the index of the relevant adr_type.)
aoqi@0 4051 //
aoqi@0 4052 // In one special case (and more cases in the future), alias categories overlap.
aoqi@0 4053 // The special alias category "Bot" (Compile::AliasIdxBot) includes all memory
aoqi@0 4054 // states. Therefore, if a MergeMem has only one contributing input W for Bot,
aoqi@0 4055 // it is exactly equivalent to that state W:
aoqi@0 4056 // MergeMem(<Bot>: W) <==> W
aoqi@0 4057 //
aoqi@0 4058 // Usually, the merge has more than one input. In that case, where inputs
aoqi@0 4059 // overlap (i.e., one is Bot), the narrower alias type determines the memory
aoqi@0 4060 // state for that type, and the wider alias type (Bot) fills in everywhere else:
aoqi@0 4061 // Load<5>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<5>(W,p)
aoqi@0 4062 // Load<6>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<6>(X,p)
aoqi@0 4063 //
aoqi@0 4064 // A merge can take a "wide" memory state as one of its narrow inputs.
aoqi@0 4065 // This simply means that the merge observes out only the relevant parts of
aoqi@0 4066 // the wide input. That is, wide memory states arriving at narrow merge inputs
aoqi@0 4067 // are implicitly "filtered" or "sliced" as necessary. (This is rare.)
aoqi@0 4068 //
aoqi@0 4069 // These rules imply that MergeMem nodes may cascade (via their <Bot> links),
aoqi@0 4070 // and that memory slices "leak through":
aoqi@0 4071 // MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y)) <==> MergeMem(<Bot>: W, <7>: Y)
aoqi@0 4072 //
aoqi@0 4073 // But, in such a cascade, repeated memory slices can "block the leak":
aoqi@0 4074 // MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y), <7>: Y') <==> MergeMem(<Bot>: W, <7>: Y')
aoqi@0 4075 //
aoqi@0 4076 // In the last example, Y is not part of the combined memory state of the
aoqi@0 4077 // outermost MergeMem. The system must, of course, prevent unschedulable
aoqi@0 4078 // memory states from arising, so you can be sure that the state Y is somehow
aoqi@0 4079 // a precursor to state Y'.
aoqi@0 4080 //
aoqi@0 4081 //
aoqi@0 4082 // REPRESENTATION OF MEMORY MERGES: The indexes used to address the Node::in array
aoqi@0 4083 // of each MergeMemNode array are exactly the numerical alias indexes, including
aoqi@0 4084 // but not limited to AliasIdxTop, AliasIdxBot, and AliasIdxRaw. The functions
aoqi@0 4085 // Compile::alias_type (and kin) produce and manage these indexes.
aoqi@0 4086 //
aoqi@0 4087 // By convention, the value of in(AliasIdxTop) (i.e., in(1)) is always the top node.
aoqi@0 4088 // (Note that this provides quick access to the top node inside MergeMem methods,
aoqi@0 4089 // without the need to reach out via TLS to Compile::current.)
aoqi@0 4090 //
aoqi@0 4091 // As a consequence of what was just described, a MergeMem that represents a full
aoqi@0 4092 // memory state has an edge in(AliasIdxBot) which is a "wide" memory state,
aoqi@0 4093 // containing all alias categories.
aoqi@0 4094 //
aoqi@0 4095 // MergeMem nodes never (?) have control inputs, so in(0) is NULL.
aoqi@0 4096 //
aoqi@0 4097 // All other edges in(N) (including in(AliasIdxRaw), which is in(3)) are either
aoqi@0 4098 // a memory state for the alias type <N>, or else the top node, meaning that
aoqi@0 4099 // there is no particular input for that alias type. Note that the length of
aoqi@0 4100 // a MergeMem is variable, and may be extended at any time to accommodate new
aoqi@0 4101 // memory states at larger alias indexes. When merges grow, they are of course
aoqi@0 4102 // filled with "top" in the unused in() positions.
aoqi@0 4103 //
aoqi@0 4104 // This use of top is named "empty_memory()", or "empty_mem" (no-memory) as a variable.
aoqi@0 4105 // (Top was chosen because it works smoothly with passes like GCM.)
aoqi@0 4106 //
aoqi@0 4107 // For convenience, we hardwire the alias index for TypeRawPtr::BOTTOM. (It is
aoqi@0 4108 // the type of random VM bits like TLS references.) Since it is always the
aoqi@0 4109 // first non-Bot memory slice, some low-level loops use it to initialize an
aoqi@0 4110 // index variable: for (i = AliasIdxRaw; i < req(); i++).
aoqi@0 4111 //
aoqi@0 4112 //
aoqi@0 4113 // ACCESSORS: There is a special accessor MergeMemNode::base_memory which returns
aoqi@0 4114 // the distinguished "wide" state. The accessor MergeMemNode::memory_at(N) returns
aoqi@0 4115 // the memory state for alias type <N>, or (if there is no particular slice at <N>,
aoqi@0 4116 // it returns the base memory. To prevent bugs, memory_at does not accept <Top>
aoqi@0 4117 // or <Bot> indexes. The iterator MergeMemStream provides robust iteration over
aoqi@0 4118 // MergeMem nodes or pairs of such nodes, ensuring that the non-top edges are visited.
aoqi@0 4119 //
aoqi@0 4120 // %%%% We may get rid of base_memory as a separate accessor at some point; it isn't
aoqi@0 4121 // really that different from the other memory inputs. An abbreviation called
aoqi@0 4122 // "bot_memory()" for "memory_at(AliasIdxBot)" would keep code tidy.
aoqi@0 4123 //
aoqi@0 4124 //
aoqi@0 4125 // PARTIAL MEMORY STATES: During optimization, MergeMem nodes may arise that represent
aoqi@0 4126 // partial memory states. When a Phi splits through a MergeMem, the copy of the Phi
aoqi@0 4127 // that "emerges though" the base memory will be marked as excluding the alias types
aoqi@0 4128 // of the other (narrow-memory) copies which "emerged through" the narrow edges:
aoqi@0 4129 //
aoqi@0 4130 // Phi<Bot>(U, MergeMem(<Bot>: W, <8>: Y))
aoqi@0 4131 // ==Ideal=> MergeMem(<Bot>: Phi<Bot-8>(U, W), Phi<8>(U, Y))
aoqi@0 4132 //
aoqi@0 4133 // This strange "subtraction" effect is necessary to ensure IGVN convergence.
aoqi@0 4134 // (It is currently unimplemented.) As you can see, the resulting merge is
aoqi@0 4135 // actually a disjoint union of memory states, rather than an overlay.
aoqi@0 4136 //
aoqi@0 4137
aoqi@0 4138 //------------------------------MergeMemNode-----------------------------------
aoqi@0 4139 Node* MergeMemNode::make_empty_memory() {
aoqi@0 4140 Node* empty_memory = (Node*) Compile::current()->top();
aoqi@0 4141 assert(empty_memory->is_top(), "correct sentinel identity");
aoqi@0 4142 return empty_memory;
aoqi@0 4143 }
aoqi@0 4144
aoqi@0 4145 MergeMemNode::MergeMemNode(Node *new_base) : Node(1+Compile::AliasIdxRaw) {
aoqi@0 4146 init_class_id(Class_MergeMem);
aoqi@0 4147 // all inputs are nullified in Node::Node(int)
aoqi@0 4148 // set_input(0, NULL); // no control input
aoqi@0 4149
aoqi@0 4150 // Initialize the edges uniformly to top, for starters.
aoqi@0 4151 Node* empty_mem = make_empty_memory();
aoqi@0 4152 for (uint i = Compile::AliasIdxTop; i < req(); i++) {
aoqi@0 4153 init_req(i,empty_mem);
aoqi@0 4154 }
aoqi@0 4155 assert(empty_memory() == empty_mem, "");
aoqi@0 4156
aoqi@0 4157 if( new_base != NULL && new_base->is_MergeMem() ) {
aoqi@0 4158 MergeMemNode* mdef = new_base->as_MergeMem();
aoqi@0 4159 assert(mdef->empty_memory() == empty_mem, "consistent sentinels");
aoqi@0 4160 for (MergeMemStream mms(this, mdef); mms.next_non_empty2(); ) {
aoqi@0 4161 mms.set_memory(mms.memory2());
aoqi@0 4162 }
aoqi@0 4163 assert(base_memory() == mdef->base_memory(), "");
aoqi@0 4164 } else {
aoqi@0 4165 set_base_memory(new_base);
aoqi@0 4166 }
aoqi@0 4167 }
aoqi@0 4168
aoqi@0 4169 // Make a new, untransformed MergeMem with the same base as 'mem'.
aoqi@0 4170 // If mem is itself a MergeMem, populate the result with the same edges.
aoqi@0 4171 MergeMemNode* MergeMemNode::make(Compile* C, Node* mem) {
aoqi@0 4172 return new(C) MergeMemNode(mem);
aoqi@0 4173 }
aoqi@0 4174
aoqi@0 4175 //------------------------------cmp--------------------------------------------
aoqi@0 4176 uint MergeMemNode::hash() const { return NO_HASH; }
aoqi@0 4177 uint MergeMemNode::cmp( const Node &n ) const {
aoqi@0 4178 return (&n == this); // Always fail except on self
aoqi@0 4179 }
aoqi@0 4180
aoqi@0 4181 //------------------------------Identity---------------------------------------
aoqi@0 4182 Node* MergeMemNode::Identity(PhaseTransform *phase) {
aoqi@0 4183 // Identity if this merge point does not record any interesting memory
aoqi@0 4184 // disambiguations.
aoqi@0 4185 Node* base_mem = base_memory();
aoqi@0 4186 Node* empty_mem = empty_memory();
aoqi@0 4187 if (base_mem != empty_mem) { // Memory path is not dead?
aoqi@0 4188 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
aoqi@0 4189 Node* mem = in(i);
aoqi@0 4190 if (mem != empty_mem && mem != base_mem) {
aoqi@0 4191 return this; // Many memory splits; no change
aoqi@0 4192 }
aoqi@0 4193 }
aoqi@0 4194 }
aoqi@0 4195 return base_mem; // No memory splits; ID on the one true input
aoqi@0 4196 }
aoqi@0 4197
aoqi@0 4198 //------------------------------Ideal------------------------------------------
aoqi@0 4199 // This method is invoked recursively on chains of MergeMem nodes
aoqi@0 4200 Node *MergeMemNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 4201 // Remove chain'd MergeMems
aoqi@0 4202 //
aoqi@0 4203 // This is delicate, because the each "in(i)" (i >= Raw) is interpreted
aoqi@0 4204 // relative to the "in(Bot)". Since we are patching both at the same time,
aoqi@0 4205 // we have to be careful to read each "in(i)" relative to the old "in(Bot)",
aoqi@0 4206 // but rewrite each "in(i)" relative to the new "in(Bot)".
aoqi@0 4207 Node *progress = NULL;
aoqi@0 4208
aoqi@0 4209
aoqi@0 4210 Node* old_base = base_memory();
aoqi@0 4211 Node* empty_mem = empty_memory();
aoqi@0 4212 if (old_base == empty_mem)
aoqi@0 4213 return NULL; // Dead memory path.
aoqi@0 4214
aoqi@0 4215 MergeMemNode* old_mbase;
aoqi@0 4216 if (old_base != NULL && old_base->is_MergeMem())
aoqi@0 4217 old_mbase = old_base->as_MergeMem();
aoqi@0 4218 else
aoqi@0 4219 old_mbase = NULL;
aoqi@0 4220 Node* new_base = old_base;
aoqi@0 4221
aoqi@0 4222 // simplify stacked MergeMems in base memory
aoqi@0 4223 if (old_mbase) new_base = old_mbase->base_memory();
aoqi@0 4224
aoqi@0 4225 // the base memory might contribute new slices beyond my req()
aoqi@0 4226 if (old_mbase) grow_to_match(old_mbase);
aoqi@0 4227
aoqi@0 4228 // Look carefully at the base node if it is a phi.
aoqi@0 4229 PhiNode* phi_base;
aoqi@0 4230 if (new_base != NULL && new_base->is_Phi())
aoqi@0 4231 phi_base = new_base->as_Phi();
aoqi@0 4232 else
aoqi@0 4233 phi_base = NULL;
aoqi@0 4234
aoqi@0 4235 Node* phi_reg = NULL;
aoqi@0 4236 uint phi_len = (uint)-1;
aoqi@0 4237 if (phi_base != NULL && !phi_base->is_copy()) {
aoqi@0 4238 // do not examine phi if degraded to a copy
aoqi@0 4239 phi_reg = phi_base->region();
aoqi@0 4240 phi_len = phi_base->req();
aoqi@0 4241 // see if the phi is unfinished
aoqi@0 4242 for (uint i = 1; i < phi_len; i++) {
aoqi@0 4243 if (phi_base->in(i) == NULL) {
aoqi@0 4244 // incomplete phi; do not look at it yet!
aoqi@0 4245 phi_reg = NULL;
aoqi@0 4246 phi_len = (uint)-1;
aoqi@0 4247 break;
aoqi@0 4248 }
aoqi@0 4249 }
aoqi@0 4250 }
aoqi@0 4251
aoqi@0 4252 // Note: We do not call verify_sparse on entry, because inputs
aoqi@0 4253 // can normalize to the base_memory via subsume_node or similar
aoqi@0 4254 // mechanisms. This method repairs that damage.
aoqi@0 4255
aoqi@0 4256 assert(!old_mbase || old_mbase->is_empty_memory(empty_mem), "consistent sentinels");
aoqi@0 4257
aoqi@0 4258 // Look at each slice.
aoqi@0 4259 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
aoqi@0 4260 Node* old_in = in(i);
aoqi@0 4261 // calculate the old memory value
aoqi@0 4262 Node* old_mem = old_in;
aoqi@0 4263 if (old_mem == empty_mem) old_mem = old_base;
aoqi@0 4264 assert(old_mem == memory_at(i), "");
aoqi@0 4265
aoqi@0 4266 // maybe update (reslice) the old memory value
aoqi@0 4267
aoqi@0 4268 // simplify stacked MergeMems
aoqi@0 4269 Node* new_mem = old_mem;
aoqi@0 4270 MergeMemNode* old_mmem;
aoqi@0 4271 if (old_mem != NULL && old_mem->is_MergeMem())
aoqi@0 4272 old_mmem = old_mem->as_MergeMem();
aoqi@0 4273 else
aoqi@0 4274 old_mmem = NULL;
aoqi@0 4275 if (old_mmem == this) {
aoqi@0 4276 // This can happen if loops break up and safepoints disappear.
aoqi@0 4277 // A merge of BotPtr (default) with a RawPtr memory derived from a
aoqi@0 4278 // safepoint can be rewritten to a merge of the same BotPtr with
aoqi@0 4279 // the BotPtr phi coming into the loop. If that phi disappears
aoqi@0 4280 // also, we can end up with a self-loop of the mergemem.
aoqi@0 4281 // In general, if loops degenerate and memory effects disappear,
aoqi@0 4282 // a mergemem can be left looking at itself. This simply means
aoqi@0 4283 // that the mergemem's default should be used, since there is
aoqi@0 4284 // no longer any apparent effect on this slice.
aoqi@0 4285 // Note: If a memory slice is a MergeMem cycle, it is unreachable
aoqi@0 4286 // from start. Update the input to TOP.
aoqi@0 4287 new_mem = (new_base == this || new_base == empty_mem)? empty_mem : new_base;
aoqi@0 4288 }
aoqi@0 4289 else if (old_mmem != NULL) {
aoqi@0 4290 new_mem = old_mmem->memory_at(i);
aoqi@0 4291 }
aoqi@0 4292 // else preceding memory was not a MergeMem
aoqi@0 4293
aoqi@0 4294 // replace equivalent phis (unfortunately, they do not GVN together)
aoqi@0 4295 if (new_mem != NULL && new_mem != new_base &&
aoqi@0 4296 new_mem->req() == phi_len && new_mem->in(0) == phi_reg) {
aoqi@0 4297 if (new_mem->is_Phi()) {
aoqi@0 4298 PhiNode* phi_mem = new_mem->as_Phi();
aoqi@0 4299 for (uint i = 1; i < phi_len; i++) {
aoqi@0 4300 if (phi_base->in(i) != phi_mem->in(i)) {
aoqi@0 4301 phi_mem = NULL;
aoqi@0 4302 break;
aoqi@0 4303 }
aoqi@0 4304 }
aoqi@0 4305 if (phi_mem != NULL) {
aoqi@0 4306 // equivalent phi nodes; revert to the def
aoqi@0 4307 new_mem = new_base;
aoqi@0 4308 }
aoqi@0 4309 }
aoqi@0 4310 }
aoqi@0 4311
aoqi@0 4312 // maybe store down a new value
aoqi@0 4313 Node* new_in = new_mem;
aoqi@0 4314 if (new_in == new_base) new_in = empty_mem;
aoqi@0 4315
aoqi@0 4316 if (new_in != old_in) {
aoqi@0 4317 // Warning: Do not combine this "if" with the previous "if"
aoqi@0 4318 // A memory slice might have be be rewritten even if it is semantically
aoqi@0 4319 // unchanged, if the base_memory value has changed.
aoqi@0 4320 set_req(i, new_in);
aoqi@0 4321 progress = this; // Report progress
aoqi@0 4322 }
aoqi@0 4323 }
aoqi@0 4324
aoqi@0 4325 if (new_base != old_base) {
aoqi@0 4326 set_req(Compile::AliasIdxBot, new_base);
aoqi@0 4327 // Don't use set_base_memory(new_base), because we need to update du.
aoqi@0 4328 assert(base_memory() == new_base, "");
aoqi@0 4329 progress = this;
aoqi@0 4330 }
aoqi@0 4331
aoqi@0 4332 if( base_memory() == this ) {
aoqi@0 4333 // a self cycle indicates this memory path is dead
aoqi@0 4334 set_req(Compile::AliasIdxBot, empty_mem);
aoqi@0 4335 }
aoqi@0 4336
aoqi@0 4337 // Resolve external cycles by calling Ideal on a MergeMem base_memory
aoqi@0 4338 // Recursion must occur after the self cycle check above
aoqi@0 4339 if( base_memory()->is_MergeMem() ) {
aoqi@0 4340 MergeMemNode *new_mbase = base_memory()->as_MergeMem();
aoqi@0 4341 Node *m = phase->transform(new_mbase); // Rollup any cycles
aoqi@0 4342 if( m != NULL && (m->is_top() ||
aoqi@0 4343 m->is_MergeMem() && m->as_MergeMem()->base_memory() == empty_mem) ) {
aoqi@0 4344 // propagate rollup of dead cycle to self
aoqi@0 4345 set_req(Compile::AliasIdxBot, empty_mem);
aoqi@0 4346 }
aoqi@0 4347 }
aoqi@0 4348
aoqi@0 4349 if( base_memory() == empty_mem ) {
aoqi@0 4350 progress = this;
aoqi@0 4351 // Cut inputs during Parse phase only.
aoqi@0 4352 // During Optimize phase a dead MergeMem node will be subsumed by Top.
aoqi@0 4353 if( !can_reshape ) {
aoqi@0 4354 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
aoqi@0 4355 if( in(i) != empty_mem ) { set_req(i, empty_mem); }
aoqi@0 4356 }
aoqi@0 4357 }
aoqi@0 4358 }
aoqi@0 4359
aoqi@0 4360 if( !progress && base_memory()->is_Phi() && can_reshape ) {
aoqi@0 4361 // Check if PhiNode::Ideal's "Split phis through memory merges"
aoqi@0 4362 // transform should be attempted. Look for this->phi->this cycle.
aoqi@0 4363 uint merge_width = req();
aoqi@0 4364 if (merge_width > Compile::AliasIdxRaw) {
aoqi@0 4365 PhiNode* phi = base_memory()->as_Phi();
aoqi@0 4366 for( uint i = 1; i < phi->req(); ++i ) {// For all paths in
aoqi@0 4367 if (phi->in(i) == this) {
aoqi@0 4368 phase->is_IterGVN()->_worklist.push(phi);
aoqi@0 4369 break;
aoqi@0 4370 }
aoqi@0 4371 }
aoqi@0 4372 }
aoqi@0 4373 }
aoqi@0 4374
aoqi@0 4375 assert(progress || verify_sparse(), "please, no dups of base");
aoqi@0 4376 return progress;
aoqi@0 4377 }
aoqi@0 4378
aoqi@0 4379 //-------------------------set_base_memory-------------------------------------
aoqi@0 4380 void MergeMemNode::set_base_memory(Node *new_base) {
aoqi@0 4381 Node* empty_mem = empty_memory();
aoqi@0 4382 set_req(Compile::AliasIdxBot, new_base);
aoqi@0 4383 assert(memory_at(req()) == new_base, "must set default memory");
aoqi@0 4384 // Clear out other occurrences of new_base:
aoqi@0 4385 if (new_base != empty_mem) {
aoqi@0 4386 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
aoqi@0 4387 if (in(i) == new_base) set_req(i, empty_mem);
aoqi@0 4388 }
aoqi@0 4389 }
aoqi@0 4390 }
aoqi@0 4391
aoqi@0 4392 //------------------------------out_RegMask------------------------------------
aoqi@0 4393 const RegMask &MergeMemNode::out_RegMask() const {
aoqi@0 4394 return RegMask::Empty;
aoqi@0 4395 }
aoqi@0 4396
aoqi@0 4397 //------------------------------dump_spec--------------------------------------
aoqi@0 4398 #ifndef PRODUCT
aoqi@0 4399 void MergeMemNode::dump_spec(outputStream *st) const {
aoqi@0 4400 st->print(" {");
aoqi@0 4401 Node* base_mem = base_memory();
aoqi@0 4402 for( uint i = Compile::AliasIdxRaw; i < req(); i++ ) {
aoqi@0 4403 Node* mem = memory_at(i);
aoqi@0 4404 if (mem == base_mem) { st->print(" -"); continue; }
aoqi@0 4405 st->print( " N%d:", mem->_idx );
aoqi@0 4406 Compile::current()->get_adr_type(i)->dump_on(st);
aoqi@0 4407 }
aoqi@0 4408 st->print(" }");
aoqi@0 4409 }
aoqi@0 4410 #endif // !PRODUCT
aoqi@0 4411
aoqi@0 4412
aoqi@0 4413 #ifdef ASSERT
aoqi@0 4414 static bool might_be_same(Node* a, Node* b) {
aoqi@0 4415 if (a == b) return true;
aoqi@0 4416 if (!(a->is_Phi() || b->is_Phi())) return false;
aoqi@0 4417 // phis shift around during optimization
aoqi@0 4418 return true; // pretty stupid...
aoqi@0 4419 }
aoqi@0 4420
aoqi@0 4421 // verify a narrow slice (either incoming or outgoing)
aoqi@0 4422 static void verify_memory_slice(const MergeMemNode* m, int alias_idx, Node* n) {
aoqi@0 4423 if (!VerifyAliases) return; // don't bother to verify unless requested
aoqi@0 4424 if (is_error_reported()) return; // muzzle asserts when debugging an error
aoqi@0 4425 if (Node::in_dump()) return; // muzzle asserts when printing
aoqi@0 4426 assert(alias_idx >= Compile::AliasIdxRaw, "must not disturb base_memory or sentinel");
aoqi@0 4427 assert(n != NULL, "");
aoqi@0 4428 // Elide intervening MergeMem's
aoqi@0 4429 while (n->is_MergeMem()) {
aoqi@0 4430 n = n->as_MergeMem()->memory_at(alias_idx);
aoqi@0 4431 }
aoqi@0 4432 Compile* C = Compile::current();
aoqi@0 4433 const TypePtr* n_adr_type = n->adr_type();
aoqi@0 4434 if (n == m->empty_memory()) {
aoqi@0 4435 // Implicit copy of base_memory()
aoqi@0 4436 } else if (n_adr_type != TypePtr::BOTTOM) {
aoqi@0 4437 assert(n_adr_type != NULL, "new memory must have a well-defined adr_type");
aoqi@0 4438 assert(C->must_alias(n_adr_type, alias_idx), "new memory must match selected slice");
aoqi@0 4439 } else {
aoqi@0 4440 // A few places like make_runtime_call "know" that VM calls are narrow,
aoqi@0 4441 // and can be used to update only the VM bits stored as TypeRawPtr::BOTTOM.
aoqi@0 4442 bool expected_wide_mem = false;
aoqi@0 4443 if (n == m->base_memory()) {
aoqi@0 4444 expected_wide_mem = true;
aoqi@0 4445 } else if (alias_idx == Compile::AliasIdxRaw ||
aoqi@0 4446 n == m->memory_at(Compile::AliasIdxRaw)) {
aoqi@0 4447 expected_wide_mem = true;
aoqi@0 4448 } else if (!C->alias_type(alias_idx)->is_rewritable()) {
aoqi@0 4449 // memory can "leak through" calls on channels that
aoqi@0 4450 // are write-once. Allow this also.
aoqi@0 4451 expected_wide_mem = true;
aoqi@0 4452 }
aoqi@0 4453 assert(expected_wide_mem, "expected narrow slice replacement");
aoqi@0 4454 }
aoqi@0 4455 }
aoqi@0 4456 #else // !ASSERT
aoqi@0 4457 #define verify_memory_slice(m,i,n) (void)(0) // PRODUCT version is no-op
aoqi@0 4458 #endif
aoqi@0 4459
aoqi@0 4460
aoqi@0 4461 //-----------------------------memory_at---------------------------------------
aoqi@0 4462 Node* MergeMemNode::memory_at(uint alias_idx) const {
aoqi@0 4463 assert(alias_idx >= Compile::AliasIdxRaw ||
aoqi@0 4464 alias_idx == Compile::AliasIdxBot && Compile::current()->AliasLevel() == 0,
aoqi@0 4465 "must avoid base_memory and AliasIdxTop");
aoqi@0 4466
aoqi@0 4467 // Otherwise, it is a narrow slice.
aoqi@0 4468 Node* n = alias_idx < req() ? in(alias_idx) : empty_memory();
aoqi@0 4469 Compile *C = Compile::current();
aoqi@0 4470 if (is_empty_memory(n)) {
aoqi@0 4471 // the array is sparse; empty slots are the "top" node
aoqi@0 4472 n = base_memory();
aoqi@0 4473 assert(Node::in_dump()
aoqi@0 4474 || n == NULL || n->bottom_type() == Type::TOP
aoqi@0 4475 || n->adr_type() == NULL // address is TOP
aoqi@0 4476 || n->adr_type() == TypePtr::BOTTOM
aoqi@0 4477 || n->adr_type() == TypeRawPtr::BOTTOM
aoqi@0 4478 || Compile::current()->AliasLevel() == 0,
aoqi@0 4479 "must be a wide memory");
aoqi@0 4480 // AliasLevel == 0 if we are organizing the memory states manually.
aoqi@0 4481 // See verify_memory_slice for comments on TypeRawPtr::BOTTOM.
aoqi@0 4482 } else {
aoqi@0 4483 // make sure the stored slice is sane
aoqi@0 4484 #ifdef ASSERT
aoqi@0 4485 if (is_error_reported() || Node::in_dump()) {
aoqi@0 4486 } else if (might_be_same(n, base_memory())) {
aoqi@0 4487 // Give it a pass: It is a mostly harmless repetition of the base.
aoqi@0 4488 // This can arise normally from node subsumption during optimization.
aoqi@0 4489 } else {
aoqi@0 4490 verify_memory_slice(this, alias_idx, n);
aoqi@0 4491 }
aoqi@0 4492 #endif
aoqi@0 4493 }
aoqi@0 4494 return n;
aoqi@0 4495 }
aoqi@0 4496
aoqi@0 4497 //---------------------------set_memory_at-------------------------------------
aoqi@0 4498 void MergeMemNode::set_memory_at(uint alias_idx, Node *n) {
aoqi@0 4499 verify_memory_slice(this, alias_idx, n);
aoqi@0 4500 Node* empty_mem = empty_memory();
aoqi@0 4501 if (n == base_memory()) n = empty_mem; // collapse default
aoqi@0 4502 uint need_req = alias_idx+1;
aoqi@0 4503 if (req() < need_req) {
aoqi@0 4504 if (n == empty_mem) return; // already the default, so do not grow me
aoqi@0 4505 // grow the sparse array
aoqi@0 4506 do {
aoqi@0 4507 add_req(empty_mem);
aoqi@0 4508 } while (req() < need_req);
aoqi@0 4509 }
aoqi@0 4510 set_req( alias_idx, n );
aoqi@0 4511 }
aoqi@0 4512
aoqi@0 4513
aoqi@0 4514
aoqi@0 4515 //--------------------------iteration_setup------------------------------------
aoqi@0 4516 void MergeMemNode::iteration_setup(const MergeMemNode* other) {
aoqi@0 4517 if (other != NULL) {
aoqi@0 4518 grow_to_match(other);
aoqi@0 4519 // invariant: the finite support of mm2 is within mm->req()
aoqi@0 4520 #ifdef ASSERT
aoqi@0 4521 for (uint i = req(); i < other->req(); i++) {
aoqi@0 4522 assert(other->is_empty_memory(other->in(i)), "slice left uncovered");
aoqi@0 4523 }
aoqi@0 4524 #endif
aoqi@0 4525 }
aoqi@0 4526 // Replace spurious copies of base_memory by top.
aoqi@0 4527 Node* base_mem = base_memory();
aoqi@0 4528 if (base_mem != NULL && !base_mem->is_top()) {
aoqi@0 4529 for (uint i = Compile::AliasIdxBot+1, imax = req(); i < imax; i++) {
aoqi@0 4530 if (in(i) == base_mem)
aoqi@0 4531 set_req(i, empty_memory());
aoqi@0 4532 }
aoqi@0 4533 }
aoqi@0 4534 }
aoqi@0 4535
aoqi@0 4536 //---------------------------grow_to_match-------------------------------------
aoqi@0 4537 void MergeMemNode::grow_to_match(const MergeMemNode* other) {
aoqi@0 4538 Node* empty_mem = empty_memory();
aoqi@0 4539 assert(other->is_empty_memory(empty_mem), "consistent sentinels");
aoqi@0 4540 // look for the finite support of the other memory
aoqi@0 4541 for (uint i = other->req(); --i >= req(); ) {
aoqi@0 4542 if (other->in(i) != empty_mem) {
aoqi@0 4543 uint new_len = i+1;
aoqi@0 4544 while (req() < new_len) add_req(empty_mem);
aoqi@0 4545 break;
aoqi@0 4546 }
aoqi@0 4547 }
aoqi@0 4548 }
aoqi@0 4549
aoqi@0 4550 //---------------------------verify_sparse-------------------------------------
aoqi@0 4551 #ifndef PRODUCT
aoqi@0 4552 bool MergeMemNode::verify_sparse() const {
aoqi@0 4553 assert(is_empty_memory(make_empty_memory()), "sane sentinel");
aoqi@0 4554 Node* base_mem = base_memory();
aoqi@0 4555 // The following can happen in degenerate cases, since empty==top.
aoqi@0 4556 if (is_empty_memory(base_mem)) return true;
aoqi@0 4557 for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
aoqi@0 4558 assert(in(i) != NULL, "sane slice");
aoqi@0 4559 if (in(i) == base_mem) return false; // should have been the sentinel value!
aoqi@0 4560 }
aoqi@0 4561 return true;
aoqi@0 4562 }
aoqi@0 4563
aoqi@0 4564 bool MergeMemStream::match_memory(Node* mem, const MergeMemNode* mm, int idx) {
aoqi@0 4565 Node* n;
aoqi@0 4566 n = mm->in(idx);
aoqi@0 4567 if (mem == n) return true; // might be empty_memory()
aoqi@0 4568 n = (idx == Compile::AliasIdxBot)? mm->base_memory(): mm->memory_at(idx);
aoqi@0 4569 if (mem == n) return true;
aoqi@0 4570 while (n->is_Phi() && (n = n->as_Phi()->is_copy()) != NULL) {
aoqi@0 4571 if (mem == n) return true;
aoqi@0 4572 if (n == NULL) break;
aoqi@0 4573 }
aoqi@0 4574 return false;
aoqi@0 4575 }
aoqi@0 4576 #endif // !PRODUCT

mercurial