src/share/vm/opto/memnode.cpp

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

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

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial