annotate src/share/vm/opto/memnode.cpp @ 5820:87ee5ee27509

Added tag jdk8-b132 for changeset 0c94c41dcd70
author katleman
date Tue, 04 Mar 2014 11:51:03 -0800
parents b87211e33ebb
children
rev   line source
duke@0 1 /*
mikael@5763 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/systemDictionary.hpp"
stefank@1879 27 #include "compiler/compileLog.hpp"
stefank@1879 28 #include "memory/allocation.inline.hpp"
stefank@1879 29 #include "oops/objArrayKlass.hpp"
stefank@1879 30 #include "opto/addnode.hpp"
stefank@1879 31 #include "opto/cfgnode.hpp"
stefank@1879 32 #include "opto/compile.hpp"
stefank@1879 33 #include "opto/connode.hpp"
stefank@1879 34 #include "opto/loopnode.hpp"
stefank@1879 35 #include "opto/machnode.hpp"
stefank@1879 36 #include "opto/matcher.hpp"
stefank@1879 37 #include "opto/memnode.hpp"
stefank@1879 38 #include "opto/mulnode.hpp"
stefank@1879 39 #include "opto/phaseX.hpp"
stefank@1879 40 #include "opto/regmask.hpp"
stefank@1879 41
duke@0 42 // Portions of code courtesy of Clifford Click
duke@0 43
duke@0 44 // Optimization - Graph Style
duke@0 45
kvn@74 46 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
kvn@74 47
duke@0 48 //=============================================================================
duke@0 49 uint MemNode::size_of() const { return sizeof(*this); }
duke@0 50
duke@0 51 const TypePtr *MemNode::adr_type() const {
duke@0 52 Node* adr = in(Address);
duke@0 53 const TypePtr* cross_check = NULL;
duke@0 54 DEBUG_ONLY(cross_check = _adr_type);
duke@0 55 return calculate_adr_type(adr->bottom_type(), cross_check);
duke@0 56 }
duke@0 57
duke@0 58 #ifndef PRODUCT
duke@0 59 void MemNode::dump_spec(outputStream *st) const {
duke@0 60 if (in(Address) == NULL) return; // node is dead
duke@0 61 #ifndef ASSERT
duke@0 62 // fake the missing field
duke@0 63 const TypePtr* _adr_type = NULL;
duke@0 64 if (in(Address) != NULL)
duke@0 65 _adr_type = in(Address)->bottom_type()->isa_ptr();
duke@0 66 #endif
duke@0 67 dump_adr_type(this, _adr_type, st);
duke@0 68
duke@0 69 Compile* C = Compile::current();
duke@0 70 if( C->alias_type(_adr_type)->is_volatile() )
duke@0 71 st->print(" Volatile!");
duke@0 72 }
duke@0 73
duke@0 74 void MemNode::dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st) {
duke@0 75 st->print(" @");
duke@0 76 if (adr_type == NULL) {
duke@0 77 st->print("NULL");
duke@0 78 } else {
duke@0 79 adr_type->dump_on(st);
duke@0 80 Compile* C = Compile::current();
duke@0 81 Compile::AliasType* atp = NULL;
duke@0 82 if (C->have_alias_type(adr_type)) atp = C->alias_type(adr_type);
duke@0 83 if (atp == NULL)
duke@0 84 st->print(", idx=?\?;");
duke@0 85 else if (atp->index() == Compile::AliasIdxBot)
duke@0 86 st->print(", idx=Bot;");
duke@0 87 else if (atp->index() == Compile::AliasIdxTop)
duke@0 88 st->print(", idx=Top;");
duke@0 89 else if (atp->index() == Compile::AliasIdxRaw)
duke@0 90 st->print(", idx=Raw;");
duke@0 91 else {
duke@0 92 ciField* field = atp->field();
duke@0 93 if (field) {
duke@0 94 st->print(", name=");
duke@0 95 field->print_name_on(st);
duke@0 96 }
duke@0 97 st->print(", idx=%d;", atp->index());
duke@0 98 }
duke@0 99 }
duke@0 100 }
duke@0 101
duke@0 102 extern void print_alias_types();
duke@0 103
duke@0 104 #endif
duke@0 105
kvn@4675 106 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) {
kvn@4675 107 assert((t_oop != NULL), "sanity");
kvn@4675 108 bool is_instance = t_oop->is_known_instance_field();
kvn@4675 109 bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() &&
kvn@4675 110 (load != NULL) && load->is_Load() &&
kvn@4675 111 (phase->is_IterGVN() != NULL);
kvn@4675 112 if (!(is_instance || is_boxed_value_load))
kvn@74 113 return mchain; // don't try to optimize non-instance types
kvn@4675 114 uint instance_id = t_oop->instance_id();
kvn@253 115 Node *start_mem = phase->C->start()->proj_out(TypeFunc::Memory);
kvn@74 116 Node *prev = NULL;
kvn@74 117 Node *result = mchain;
kvn@74 118 while (prev != result) {
kvn@74 119 prev = result;
kvn@253 120 if (result == start_mem)
twisti@605 121 break; // hit one of our sentinels
kvn@74 122 // skip over a call which does not affect this memory slice
kvn@74 123 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
kvn@74 124 Node *proj_in = result->in(0);
kvn@253 125 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
twisti@605 126 break; // hit one of our sentinels
kvn@253 127 } else if (proj_in->is_Call()) {
kvn@74 128 CallNode *call = proj_in->as_Call();
kvn@4675 129 if (!call->may_modify(t_oop, phase)) { // returns false for instances
kvn@74 130 result = call->in(TypeFunc::Memory);
kvn@74 131 }
kvn@74 132 } else if (proj_in->is_Initialize()) {
kvn@74 133 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
kvn@74 134 // Stop if this is the initialization for the object instance which
kvn@74 135 // which contains this memory slice, otherwise skip over it.
kvn@4675 136 if ((alloc == NULL) || (alloc->_idx == instance_id)) {
kvn@4675 137 break;
kvn@4675 138 }
kvn@4675 139 if (is_instance) {
kvn@74 140 result = proj_in->in(TypeFunc::Memory);
kvn@4675 141 } else if (is_boxed_value_load) {
kvn@4675 142 Node* klass = alloc->in(AllocateNode::KlassNode);
kvn@4675 143 const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr();
kvn@4675 144 if (tklass->klass_is_exact() && !tklass->klass()->equals(t_oop->klass())) {
kvn@4675 145 result = proj_in->in(TypeFunc::Memory); // not related allocation
kvn@4675 146 }
kvn@74 147 }
kvn@74 148 } else if (proj_in->is_MemBar()) {
kvn@74 149 result = proj_in->in(TypeFunc::Memory);
kvn@253 150 } else {
kvn@253 151 assert(false, "unexpected projection");
kvn@74 152 }
kvn@1100 153 } else if (result->is_ClearArray()) {
kvn@4675 154 if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) {
kvn@1100 155 // Can not bypass initialization of the instance
kvn@1100 156 // we are looking for.
kvn@1100 157 break;
kvn@1100 158 }
kvn@1100 159 // Otherwise skip it (the call updated 'result' value).
kvn@74 160 } else if (result->is_MergeMem()) {
kvn@4675 161 result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, NULL, tty);
kvn@74 162 }
kvn@74 163 }
kvn@74 164 return result;
kvn@74 165 }
kvn@74 166
kvn@4675 167 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) {
kvn@4675 168 const TypeOopPtr* t_oop = t_adr->isa_oopptr();
kvn@4675 169 if (t_oop == NULL)
kvn@4675 170 return mchain; // don't try to optimize non-oop types
kvn@4675 171 Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase);
kvn@4675 172 bool is_instance = t_oop->is_known_instance_field();
kvn@74 173 PhaseIterGVN *igvn = phase->is_IterGVN();
kvn@74 174 if (is_instance && igvn != NULL && result->is_Phi()) {
kvn@74 175 PhiNode *mphi = result->as_Phi();
kvn@74 176 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
kvn@74 177 const TypePtr *t = mphi->adr_type();
kvn@163 178 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||
kvn@223 179 t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&
kvn@247 180 t->is_oopptr()->cast_to_exactness(true)
kvn@247 181 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
kvn@247 182 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop) {
kvn@74 183 // clone the Phi with our address type
kvn@74 184 result = mphi->split_out_instance(t_adr, igvn);
kvn@74 185 } else {
kvn@74 186 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
kvn@74 187 }
kvn@74 188 }
kvn@74 189 return result;
kvn@74 190 }
kvn@74 191
kvn@64 192 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
kvn@64 193 uint alias_idx = phase->C->get_alias_index(tp);
kvn@64 194 Node *mem = mmem;
kvn@64 195 #ifdef ASSERT
kvn@64 196 {
kvn@64 197 // Check that current type is consistent with the alias index used during graph construction
kvn@64 198 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
kvn@64 199 bool consistent = adr_check == NULL || adr_check->empty() ||
kvn@64 200 phase->C->must_alias(adr_check, alias_idx );
kvn@64 201 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
kvn@64 202 if( !consistent && adr_check != NULL && !adr_check->empty() &&
rasbold@169 203 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
kvn@64 204 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
kvn@64 205 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
kvn@64 206 adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
kvn@64 207 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
kvn@64 208 // don't assert if it is dead code.
kvn@64 209 consistent = true;
kvn@64 210 }
kvn@64 211 if( !consistent ) {
kvn@64 212 st->print("alias_idx==%d, adr_check==", alias_idx);
kvn@64 213 if( adr_check == NULL ) {
kvn@64 214 st->print("NULL");
kvn@64 215 } else {
kvn@64 216 adr_check->dump();
kvn@64 217 }
kvn@64 218 st->cr();
kvn@64 219 print_alias_types();
kvn@64 220 assert(consistent, "adr_check must match alias idx");
kvn@64 221 }
kvn@64 222 }
kvn@64 223 #endif
never@1735 224 // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
kvn@64 225 // means an array I have not precisely typed yet. Do not do any
kvn@64 226 // alias stuff with it any time soon.
never@1735 227 const TypeOopPtr *toop = tp->isa_oopptr();
kvn@64 228 if( tp->base() != Type::AnyPtr &&
never@1735 229 !(toop &&
never@1735 230 toop->klass() != NULL &&
never@1735 231 toop->klass()->is_java_lang_Object() &&
never@1735 232 toop->offset() == Type::OffsetBot) ) {
kvn@64 233 // compress paths and change unreachable cycles to TOP
kvn@64 234 // If not, we can update the input infinitely along a MergeMem cycle
kvn@64 235 // Equivalent code in PhiNode::Ideal
kvn@64 236 Node* m = phase->transform(mmem);
twisti@605 237 // If transformed to a MergeMem, get the desired slice
kvn@64 238 // Otherwise the returned node represents memory for every slice
kvn@64 239 mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m;
kvn@64 240 // Update input if it is progress over what we have now
kvn@64 241 }
kvn@64 242 return mem;
kvn@64 243 }
kvn@64 244
duke@0 245 //--------------------------Ideal_common---------------------------------------
duke@0 246 // Look for degenerate control and memory inputs. Bypass MergeMem inputs.
duke@0 247 // Unhook non-raw memories from complete (macro-expanded) initializations.
duke@0 248 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
duke@0 249 // If our control input is a dead region, kill all below the region
duke@0 250 Node *ctl = in(MemNode::Control);
duke@0 251 if (ctl && remove_dead_region(phase, can_reshape))
duke@0 252 return this;
kvn@305 253 ctl = in(MemNode::Control);
kvn@305 254 // Don't bother trying to transform a dead node
kvn@4260 255 if (ctl && ctl->is_top()) return NodeSentinel;
duke@0 256
kvn@708 257 PhaseIterGVN *igvn = phase->is_IterGVN();
kvn@708 258 // Wait if control on the worklist.
kvn@708 259 if (ctl && can_reshape && igvn != NULL) {
kvn@708 260 Node* bol = NULL;
kvn@708 261 Node* cmp = NULL;
kvn@708 262 if (ctl->in(0)->is_If()) {
kvn@708 263 assert(ctl->is_IfTrue() || ctl->is_IfFalse(), "sanity");
kvn@708 264 bol = ctl->in(0)->in(1);
kvn@708 265 if (bol->is_Bool())
kvn@708 266 cmp = ctl->in(0)->in(1)->in(1);
kvn@708 267 }
kvn@708 268 if (igvn->_worklist.member(ctl) ||
kvn@708 269 (bol != NULL && igvn->_worklist.member(bol)) ||
kvn@708 270 (cmp != NULL && igvn->_worklist.member(cmp)) ) {
kvn@708 271 // This control path may be dead.
kvn@708 272 // Delay this memory node transformation until the control is processed.
kvn@708 273 phase->is_IterGVN()->_worklist.push(this);
kvn@708 274 return NodeSentinel; // caller will return NULL
kvn@708 275 }
kvn@708 276 }
duke@0 277 // Ignore if memory is dead, or self-loop
duke@0 278 Node *mem = in(MemNode::Memory);
kvn@4260 279 if (phase->type( mem ) == Type::TOP) return NodeSentinel; // caller will return NULL
kvn@4260 280 assert(mem != this, "dead loop in MemNode::Ideal");
duke@0 281
kvn@2885 282 if (can_reshape && igvn != NULL && igvn->_worklist.member(mem)) {
kvn@2885 283 // This memory slice may be dead.
kvn@2885 284 // Delay this mem node transformation until the memory is processed.
kvn@2885 285 phase->is_IterGVN()->_worklist.push(this);
kvn@2885 286 return NodeSentinel; // caller will return NULL
kvn@2885 287 }
kvn@2885 288
duke@0 289 Node *address = in(MemNode::Address);
kvn@4260 290 const Type *t_adr = phase->type(address);
kvn@4260 291 if (t_adr == Type::TOP) return NodeSentinel; // caller will return NULL
kvn@4260 292
kvn@4260 293 if (can_reshape && igvn != NULL &&
kvn@1746 294 (igvn->_worklist.member(address) ||
kvn@4260 295 igvn->_worklist.size() > 0 && (t_adr != adr_type())) ) {
kvn@420 296 // The address's base and type may change when the address is processed.
kvn@420 297 // Delay this mem node transformation until the address is processed.
kvn@420 298 phase->is_IterGVN()->_worklist.push(this);
kvn@420 299 return NodeSentinel; // caller will return NULL
kvn@420 300 }
kvn@420 301
kvn@1062 302 // Do NOT remove or optimize the next lines: ensure a new alias index
kvn@1062 303 // is allocated for an oop pointer type before Escape Analysis.
kvn@1062 304 // Note: C++ will not remove it since the call has side effect.
kvn@4260 305 if (t_adr->isa_oopptr()) {
kvn@1062 306 int alias_idx = phase->C->get_alias_index(t_adr->is_ptr());
kvn@1062 307 }
kvn@1062 308
kvn@708 309 #ifdef ASSERT
kvn@708 310 Node* base = NULL;
kvn@708 311 if (address->is_AddP())
kvn@708 312 base = address->in(AddPNode::Base);
kvn@4260 313 if (base != NULL && phase->type(base)->higher_equal(TypePtr::NULL_PTR) &&
kvn@4260 314 !t_adr->isa_rawptr()) {
kvn@4260 315 // Note: raw address has TOP base and top->higher_equal(TypePtr::NULL_PTR) is true.
kvn@4260 316 Compile* C = phase->C;
kvn@4260 317 tty->cr();
kvn@4260 318 tty->print_cr("===== NULL+offs not RAW address =====");
kvn@4260 319 if (C->is_dead_node(this->_idx)) tty->print_cr("'this' is dead");
kvn@4260 320 if ((ctl != NULL) && C->is_dead_node(ctl->_idx)) tty->print_cr("'ctl' is dead");
kvn@4260 321 if (C->is_dead_node(mem->_idx)) tty->print_cr("'mem' is dead");
kvn@4260 322 if (C->is_dead_node(address->_idx)) tty->print_cr("'address' is dead");
kvn@4260 323 if (C->is_dead_node(base->_idx)) tty->print_cr("'base' is dead");
kvn@4260 324 tty->cr();
kvn@4260 325 base->dump(1);
kvn@4260 326 tty->cr();
kvn@4260 327 this->dump(2);
kvn@4260 328 tty->print("this->adr_type(): "); adr_type()->dump(); tty->cr();
kvn@4260 329 tty->print("phase->type(address): "); t_adr->dump(); tty->cr();
kvn@4260 330 tty->print("phase->type(base): "); phase->type(address)->dump(); tty->cr();
kvn@4260 331 tty->cr();
kvn@4260 332 }
kvn@708 333 assert(base == NULL || t_adr->isa_rawptr() ||
kvn@708 334 !phase->type(base)->higher_equal(TypePtr::NULL_PTR), "NULL+offs not RAW address?");
kvn@708 335 #endif
kvn@708 336
duke@0 337 // Avoid independent memory operations
duke@0 338 Node* old_mem = mem;
duke@0 339
kvn@36 340 // The code which unhooks non-raw memories from complete (macro-expanded)
kvn@36 341 // initializations was removed. After macro-expansion all stores catched
kvn@36 342 // by Initialize node became raw stores and there is no information
kvn@36 343 // which memory slices they modify. So it is unsafe to move any memory
kvn@36 344 // operation above these stores. Also in most cases hooked non-raw memories
kvn@36 345 // were already unhooked by using information from detect_ptr_independence()
kvn@36 346 // and find_previous_store().
duke@0 347
duke@0 348 if (mem->is_MergeMem()) {
duke@0 349 MergeMemNode* mmem = mem->as_MergeMem();
duke@0 350 const TypePtr *tp = t_adr->is_ptr();
kvn@64 351
kvn@64 352 mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty);
duke@0 353 }
duke@0 354
duke@0 355 if (mem != old_mem) {
duke@0 356 set_req(MemNode::Memory, mem);
roland@4222 357 if (can_reshape && old_mem->outcnt() == 0) {
roland@4222 358 igvn->_worklist.push(old_mem);
roland@4222 359 }
kvn@305 360 if (phase->type( mem ) == Type::TOP) return NodeSentinel;
duke@0 361 return this;
duke@0 362 }
duke@0 363
duke@0 364 // let the subclass continue analyzing...
duke@0 365 return NULL;
duke@0 366 }
duke@0 367
duke@0 368 // Helper function for proving some simple control dominations.
kvn@119 369 // Attempt to prove that all control inputs of 'dom' dominate 'sub'.
duke@0 370 // Already assumes that 'dom' is available at 'sub', and that 'sub'
duke@0 371 // is not a constant (dominated by the method's StartNode).
duke@0 372 // Used by MemNode::find_previous_store to prove that the
duke@0 373 // control input of a memory operation predates (dominates)
duke@0 374 // an allocation it wants to look past.
kvn@119 375 bool MemNode::all_controls_dominate(Node* dom, Node* sub) {
kvn@119 376 if (dom == NULL || dom->is_top() || sub == NULL || sub->is_top())
kvn@119 377 return false; // Conservative answer for dead code
kvn@119 378
kvn@193 379 // Check 'dom'. Skip Proj and CatchProj nodes.
kvn@119 380 dom = dom->find_exact_control(dom);
kvn@119 381 if (dom == NULL || dom->is_top())
kvn@119 382 return false; // Conservative answer for dead code
kvn@119 383
kvn@193 384 if (dom == sub) {
kvn@193 385 // For the case when, for example, 'sub' is Initialize and the original
kvn@193 386 // 'dom' is Proj node of the 'sub'.
kvn@193 387 return false;
kvn@193 388 }
kvn@193 389
kvn@155 390 if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub)
kvn@119 391 return true;
kvn@119 392
kvn@119 393 // 'dom' dominates 'sub' if its control edge and control edges
kvn@119 394 // of all its inputs dominate or equal to sub's control edge.
kvn@119 395
kvn@119 396 // Currently 'sub' is either Allocate, Initialize or Start nodes.
kvn@163 397 // Or Region for the check in LoadNode::Ideal();
kvn@163 398 // 'sub' should have sub->in(0) != NULL.
kvn@163 399 assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() ||
kvn@4675 400 sub->is_Region() || sub->is_Call(), "expecting only these nodes");
kvn@119 401
kvn@119 402 // Get control edge of 'sub'.
kvn@193 403 Node* orig_sub = sub;
kvn@119 404 sub = sub->find_exact_control(sub->in(0));
kvn@119 405 if (sub == NULL || sub->is_top())
kvn@119 406 return false; // Conservative answer for dead code
kvn@119 407
kvn@119 408 assert(sub->is_CFG(), "expecting control");
kvn@119 409
kvn@119 410 if (sub == dom)
kvn@119 411 return true;
kvn@119 412
kvn@119 413 if (sub->is_Start() || sub->is_Root())
kvn@119 414 return false;
kvn@119 415
kvn@119 416 {
kvn@119 417 // Check all control edges of 'dom'.
kvn@119 418
kvn@119 419 ResourceMark rm;
kvn@119 420 Arena* arena = Thread::current()->resource_area();
kvn@119 421 Node_List nlist(arena);
kvn@119 422 Unique_Node_List dom_list(arena);
kvn@119 423
kvn@119 424 dom_list.push(dom);
kvn@119 425 bool only_dominating_controls = false;
kvn@119 426
kvn@119 427 for (uint next = 0; next < dom_list.size(); next++) {
kvn@119 428 Node* n = dom_list.at(next);
kvn@193 429 if (n == orig_sub)
kvn@193 430 return false; // One of dom's inputs dominated by sub.
kvn@119 431 if (!n->is_CFG() && n->pinned()) {
kvn@119 432 // Check only own control edge for pinned non-control nodes.
kvn@119 433 n = n->find_exact_control(n->in(0));
kvn@119 434 if (n == NULL || n->is_top())
kvn@119 435 return false; // Conservative answer for dead code
kvn@119 436 assert(n->is_CFG(), "expecting control");
kvn@193 437 dom_list.push(n);
kvn@193 438 } else if (n->is_Con() || n->is_Start() || n->is_Root()) {
kvn@119 439 only_dominating_controls = true;
kvn@119 440 } else if (n->is_CFG()) {
kvn@119 441 if (n->dominates(sub, nlist))
kvn@119 442 only_dominating_controls = true;
kvn@119 443 else
kvn@119 444 return false;
kvn@119 445 } else {
kvn@119 446 // First, own control edge.
kvn@119 447 Node* m = n->find_exact_control(n->in(0));
kvn@155 448 if (m != NULL) {
kvn@155 449 if (m->is_top())
kvn@155 450 return false; // Conservative answer for dead code
kvn@155 451 dom_list.push(m);
kvn@155 452 }
kvn@119 453 // Now, the rest of edges.
kvn@119 454 uint cnt = n->req();
kvn@119 455 for (uint i = 1; i < cnt; i++) {
kvn@119 456 m = n->find_exact_control(n->in(i));
kvn@119 457 if (m == NULL || m->is_top())
kvn@119 458 continue;
kvn@119 459 dom_list.push(m);
duke@0 460 }
duke@0 461 }
duke@0 462 }
kvn@119 463 return only_dominating_controls;
duke@0 464 }
duke@0 465 }
duke@0 466
duke@0 467 //---------------------detect_ptr_independence---------------------------------
duke@0 468 // Used by MemNode::find_previous_store to prove that two base
duke@0 469 // pointers are never equal.
duke@0 470 // The pointers are accompanied by their associated allocations,
duke@0 471 // if any, which have been previously discovered by the caller.
duke@0 472 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1,
duke@0 473 Node* p2, AllocateNode* a2,
duke@0 474 PhaseTransform* phase) {
duke@0 475 // Attempt to prove that these two pointers cannot be aliased.
duke@0 476 // They may both manifestly be allocations, and they should differ.
duke@0 477 // Or, if they are not both allocations, they can be distinct constants.
duke@0 478 // Otherwise, one is an allocation and the other a pre-existing value.
duke@0 479 if (a1 == NULL && a2 == NULL) { // neither an allocation
duke@0 480 return (p1 != p2) && p1->is_Con() && p2->is_Con();
duke@0 481 } else if (a1 != NULL && a2 != NULL) { // both allocations
duke@0 482 return (a1 != a2);
duke@0 483 } else if (a1 != NULL) { // one allocation a1
duke@0 484 // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.)
kvn@119 485 return all_controls_dominate(p2, a1);
duke@0 486 } else { //(a2 != NULL) // one allocation a2
kvn@119 487 return all_controls_dominate(p1, a2);
duke@0 488 }
duke@0 489 return false;
duke@0 490 }
duke@0 491
duke@0 492
duke@0 493 // The logic for reordering loads and stores uses four steps:
duke@0 494 // (a) Walk carefully past stores and initializations which we
duke@0 495 // can prove are independent of this load.
duke@0 496 // (b) Observe that the next memory state makes an exact match
duke@0 497 // with self (load or store), and locate the relevant store.
duke@0 498 // (c) Ensure that, if we were to wire self directly to the store,
duke@0 499 // the optimizer would fold it up somehow.
duke@0 500 // (d) Do the rewiring, and return, depending on some other part of
duke@0 501 // the optimizer to fold up the load.
duke@0 502 // This routine handles steps (a) and (b). Steps (c) and (d) are
duke@0 503 // specific to loads and stores, so they are handled by the callers.
duke@0 504 // (Currently, only LoadNode::Ideal has steps (c), (d). More later.)
duke@0 505 //
duke@0 506 Node* MemNode::find_previous_store(PhaseTransform* phase) {
duke@0 507 Node* ctrl = in(MemNode::Control);
duke@0 508 Node* adr = in(MemNode::Address);
duke@0 509 intptr_t offset = 0;
duke@0 510 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
duke@0 511 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
duke@0 512
duke@0 513 if (offset == Type::OffsetBot)
duke@0 514 return NULL; // cannot unalias unless there are precise offsets
duke@0 515
kvn@74 516 const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr();
kvn@74 517
duke@0 518 intptr_t size_in_bytes = memory_size();
duke@0 519
duke@0 520 Node* mem = in(MemNode::Memory); // start searching here...
duke@0 521
duke@0 522 int cnt = 50; // Cycle limiter
duke@0 523 for (;;) { // While we can dance past unrelated stores...
duke@0 524 if (--cnt < 0) break; // Caught in cycle or a complicated dance?
duke@0 525
duke@0 526 if (mem->is_Store()) {
duke@0 527 Node* st_adr = mem->in(MemNode::Address);
duke@0 528 intptr_t st_offset = 0;
duke@0 529 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
duke@0 530 if (st_base == NULL)
duke@0 531 break; // inscrutable pointer
duke@0 532 if (st_offset != offset && st_offset != Type::OffsetBot) {
duke@0 533 const int MAX_STORE = BytesPerLong;
duke@0 534 if (st_offset >= offset + size_in_bytes ||
duke@0 535 st_offset <= offset - MAX_STORE ||
duke@0 536 st_offset <= offset - mem->as_Store()->memory_size()) {
duke@0 537 // Success: The offsets are provably independent.
duke@0 538 // (You may ask, why not just test st_offset != offset and be done?
duke@0 539 // The answer is that stores of different sizes can co-exist
duke@0 540 // in the same sequence of RawMem effects. We sometimes initialize
duke@0 541 // a whole 'tile' of array elements with a single jint or jlong.)
duke@0 542 mem = mem->in(MemNode::Memory);
duke@0 543 continue; // (a) advance through independent store memory
duke@0 544 }
duke@0 545 }
duke@0 546 if (st_base != base &&
duke@0 547 detect_ptr_independence(base, alloc,
duke@0 548 st_base,
duke@0 549 AllocateNode::Ideal_allocation(st_base, phase),
duke@0 550 phase)) {
duke@0 551 // Success: The bases are provably independent.
duke@0 552 mem = mem->in(MemNode::Memory);
duke@0 553 continue; // (a) advance through independent store memory
duke@0 554 }
duke@0 555
duke@0 556 // (b) At this point, if the bases or offsets do not agree, we lose,
duke@0 557 // since we have not managed to prove 'this' and 'mem' independent.
duke@0 558 if (st_base == base && st_offset == offset) {
duke@0 559 return mem; // let caller handle steps (c), (d)
duke@0 560 }
duke@0 561
duke@0 562 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
duke@0 563 InitializeNode* st_init = mem->in(0)->as_Initialize();
duke@0 564 AllocateNode* st_alloc = st_init->allocation();
duke@0 565 if (st_alloc == NULL)
duke@0 566 break; // something degenerated
duke@0 567 bool known_identical = false;
duke@0 568 bool known_independent = false;
duke@0 569 if (alloc == st_alloc)
duke@0 570 known_identical = true;
duke@0 571 else if (alloc != NULL)
duke@0 572 known_independent = true;
kvn@119 573 else if (all_controls_dominate(this, st_alloc))
duke@0 574 known_independent = true;
duke@0 575
duke@0 576 if (known_independent) {
duke@0 577 // The bases are provably independent: Either they are
duke@0 578 // manifestly distinct allocations, or else the control
duke@0 579 // of this load dominates the store's allocation.
duke@0 580 int alias_idx = phase->C->get_alias_index(adr_type());
duke@0 581 if (alias_idx == Compile::AliasIdxRaw) {
duke@0 582 mem = st_alloc->in(TypeFunc::Memory);
duke@0 583 } else {
duke@0 584 mem = st_init->memory(alias_idx);
duke@0 585 }
duke@0 586 continue; // (a) advance through independent store memory
duke@0 587 }
duke@0 588
duke@0 589 // (b) at this point, if we are not looking at a store initializing
duke@0 590 // the same allocation we are loading from, we lose.
duke@0 591 if (known_identical) {
duke@0 592 // From caller, can_see_stored_value will consult find_captured_store.
duke@0 593 return mem; // let caller handle steps (c), (d)
duke@0 594 }
duke@0 595
kvn@223 596 } else if (addr_t != NULL && addr_t->is_known_instance_field()) {
kvn@74 597 // Can't use optimize_simple_memory_chain() since it needs PhaseGVN.
kvn@74 598 if (mem->is_Proj() && mem->in(0)->is_Call()) {
kvn@74 599 CallNode *call = mem->in(0)->as_Call();
kvn@74 600 if (!call->may_modify(addr_t, phase)) {
kvn@74 601 mem = call->in(TypeFunc::Memory);
kvn@74 602 continue; // (a) advance through independent call memory
kvn@74 603 }
kvn@74 604 } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) {
kvn@74 605 mem = mem->in(0)->in(TypeFunc::Memory);
kvn@74 606 continue; // (a) advance through independent MemBar memory
kvn@1100 607 } else if (mem->is_ClearArray()) {
kvn@1100 608 if (ClearArrayNode::step_through(&mem, (uint)addr_t->instance_id(), phase)) {
kvn@1100 609 // (the call updated 'mem' value)
kvn@1100 610 continue; // (a) advance through independent allocation memory
kvn@1100 611 } else {
kvn@1100 612 // Can not bypass initialization of the instance
kvn@1100 613 // we are looking for.
kvn@1100 614 return mem;
kvn@1100 615 }
kvn@74 616 } else if (mem->is_MergeMem()) {
kvn@74 617 int alias_idx = phase->C->get_alias_index(adr_type());
kvn@74 618 mem = mem->as_MergeMem()->memory_at(alias_idx);
kvn@74 619 continue; // (a) advance through independent MergeMem memory
kvn@74 620 }
duke@0 621 }
duke@0 622
duke@0 623 // Unless there is an explicit 'continue', we must bail out here,
duke@0 624 // because 'mem' is an inscrutable memory state (e.g., a call).
duke@0 625 break;
duke@0 626 }
duke@0 627
duke@0 628 return NULL; // bail out
duke@0 629 }
duke@0 630
duke@0 631 //----------------------calculate_adr_type-------------------------------------
duke@0 632 // Helper function. Notices when the given type of address hits top or bottom.
duke@0 633 // Also, asserts a cross-check of the type against the expected address type.
duke@0 634 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) {
duke@0 635 if (t == Type::TOP) return NULL; // does not touch memory any more?
duke@0 636 #ifdef PRODUCT
duke@0 637 cross_check = NULL;
duke@0 638 #else
duke@0 639 if (!VerifyAliases || is_error_reported() || Node::in_dump()) cross_check = NULL;
duke@0 640 #endif
duke@0 641 const TypePtr* tp = t->isa_ptr();
duke@0 642 if (tp == NULL) {
duke@0 643 assert(cross_check == NULL || cross_check == TypePtr::BOTTOM, "expected memory type must be wide");
duke@0 644 return TypePtr::BOTTOM; // touches lots of memory
duke@0 645 } else {
duke@0 646 #ifdef ASSERT
duke@0 647 // %%%% [phh] We don't check the alias index if cross_check is
duke@0 648 // TypeRawPtr::BOTTOM. Needs to be investigated.
duke@0 649 if (cross_check != NULL &&
duke@0 650 cross_check != TypePtr::BOTTOM &&
duke@0 651 cross_check != TypeRawPtr::BOTTOM) {
duke@0 652 // Recheck the alias index, to see if it has changed (due to a bug).
duke@0 653 Compile* C = Compile::current();
duke@0 654 assert(C->get_alias_index(cross_check) == C->get_alias_index(tp),
duke@0 655 "must stay in the original alias category");
duke@0 656 // The type of the address must be contained in the adr_type,
duke@0 657 // disregarding "null"-ness.
duke@0 658 // (We make an exception for TypeRawPtr::BOTTOM, which is a bit bucket.)
duke@0 659 const TypePtr* tp_notnull = tp->join(TypePtr::NOTNULL)->is_ptr();
duke@0 660 assert(cross_check->meet(tp_notnull) == cross_check,
duke@0 661 "real address must not escape from expected memory type");
duke@0 662 }
duke@0 663 #endif
duke@0 664 return tp;
duke@0 665 }
duke@0 666 }
duke@0 667
duke@0 668 //------------------------adr_phi_is_loop_invariant----------------------------
duke@0 669 // A helper function for Ideal_DU_postCCP to check if a Phi in a counted
duke@0 670 // loop is loop invariant. Make a quick traversal of Phi and associated
duke@0 671 // CastPP nodes, looking to see if they are a closed group within the loop.
duke@0 672 bool MemNode::adr_phi_is_loop_invariant(Node* adr_phi, Node* cast) {
duke@0 673 // The idea is that the phi-nest must boil down to only CastPP nodes
duke@0 674 // with the same data. This implies that any path into the loop already
duke@0 675 // includes such a CastPP, and so the original cast, whatever its input,
duke@0 676 // must be covered by an equivalent cast, with an earlier control input.
duke@0 677 ResourceMark rm;
duke@0 678
duke@0 679 // The loop entry input of the phi should be the unique dominating
duke@0 680 // node for every Phi/CastPP in the loop.
duke@0 681 Unique_Node_List closure;
duke@0 682 closure.push(adr_phi->in(LoopNode::EntryControl));
duke@0 683
duke@0 684 // Add the phi node and the cast to the worklist.
duke@0 685 Unique_Node_List worklist;
duke@0 686 worklist.push(adr_phi);
duke@0 687 if( cast != NULL ){
duke@0 688 if( !cast->is_ConstraintCast() ) return false;
duke@0 689 worklist.push(cast);
duke@0 690 }
duke@0 691
duke@0 692 // Begin recursive walk of phi nodes.
duke@0 693 while( worklist.size() ){
duke@0 694 // Take a node off the worklist
duke@0 695 Node *n = worklist.pop();
duke@0 696 if( !closure.member(n) ){
duke@0 697 // Add it to the closure.
duke@0 698 closure.push(n);
duke@0 699 // Make a sanity check to ensure we don't waste too much time here.
duke@0 700 if( closure.size() > 20) return false;
duke@0 701 // This node is OK if:
duke@0 702 // - it is a cast of an identical value
duke@0 703 // - or it is a phi node (then we add its inputs to the worklist)
duke@0 704 // Otherwise, the node is not OK, and we presume the cast is not invariant
duke@0 705 if( n->is_ConstraintCast() ){
duke@0 706 worklist.push(n->in(1));
duke@0 707 } else if( n->is_Phi() ) {
duke@0 708 for( uint i = 1; i < n->req(); i++ ) {
duke@0 709 worklist.push(n->in(i));
duke@0 710 }
duke@0 711 } else {
duke@0 712 return false;
duke@0 713 }
duke@0 714 }
duke@0 715 }
duke@0 716
duke@0 717 // Quit when the worklist is empty, and we've found no offending nodes.
duke@0 718 return true;
duke@0 719 }
duke@0 720
duke@0 721 //------------------------------Ideal_DU_postCCP-------------------------------
duke@0 722 // Find any cast-away of null-ness and keep its control. Null cast-aways are
duke@0 723 // going away in this pass and we need to make this memory op depend on the
duke@0 724 // gating null check.
kvn@163 725 Node *MemNode::Ideal_DU_postCCP( PhaseCCP *ccp ) {
kvn@163 726 return Ideal_common_DU_postCCP(ccp, this, in(MemNode::Address));
kvn@163 727 }
duke@0 728
duke@0 729 // I tried to leave the CastPP's in. This makes the graph more accurate in
duke@0 730 // some sense; we get to keep around the knowledge that an oop is not-null
duke@0 731 // after some test. Alas, the CastPP's interfere with GVN (some values are
duke@0 732 // the regular oop, some are the CastPP of the oop, all merge at Phi's which
duke@0 733 // cannot collapse, etc). This cost us 10% on SpecJVM, even when I removed
duke@0 734 // some of the more trivial cases in the optimizer. Removing more useless
duke@0 735 // Phi's started allowing Loads to illegally float above null checks. I gave
duke@0 736 // up on this approach. CNC 10/20/2000
kvn@163 737 // This static method may be called not from MemNode (EncodePNode calls it).
kvn@163 738 // Only the control edge of the node 'n' might be updated.
kvn@163 739 Node *MemNode::Ideal_common_DU_postCCP( PhaseCCP *ccp, Node* n, Node* adr ) {
duke@0 740 Node *skipped_cast = NULL;
duke@0 741 // Need a null check? Regular static accesses do not because they are
duke@0 742 // from constant addresses. Array ops are gated by the range check (which
duke@0 743 // always includes a NULL check). Just check field ops.
kvn@163 744 if( n->in(MemNode::Control) == NULL ) {
duke@0 745 // Scan upwards for the highest location we can place this memory op.
duke@0 746 while( true ) {
duke@0 747 switch( adr->Opcode() ) {
duke@0 748
duke@0 749 case Op_AddP: // No change to NULL-ness, so peek thru AddP's
duke@0 750 adr = adr->in(AddPNode::Base);
duke@0 751 continue;
duke@0 752
coleenp@113 753 case Op_DecodeN: // No change to NULL-ness, so peek thru
roland@3724 754 case Op_DecodeNKlass:
coleenp@113 755 adr = adr->in(1);
coleenp@113 756 continue;
coleenp@113 757
kvn@3407 758 case Op_EncodeP:
roland@3724 759 case Op_EncodePKlass:
kvn@3407 760 // EncodeP node's control edge could be set by this method
kvn@3407 761 // when EncodeP node depends on CastPP node.
kvn@3407 762 //
kvn@3407 763 // Use its control edge for memory op because EncodeP may go away
kvn@3407 764 // later when it is folded with following or preceding DecodeN node.
kvn@3407 765 if (adr->in(0) == NULL) {
kvn@3407 766 // Keep looking for cast nodes.
kvn@3407 767 adr = adr->in(1);
kvn@3407 768 continue;
kvn@3407 769 }
kvn@3407 770 ccp->hash_delete(n);
kvn@3407 771 n->set_req(MemNode::Control, adr->in(0));
kvn@3407 772 ccp->hash_insert(n);
kvn@3407 773 return n;
kvn@3407 774
duke@0 775 case Op_CastPP:
duke@0 776 // If the CastPP is useless, just peek on through it.
duke@0 777 if( ccp->type(adr) == ccp->type(adr->in(1)) ) {
duke@0 778 // Remember the cast that we've peeked though. If we peek
duke@0 779 // through more than one, then we end up remembering the highest
duke@0 780 // one, that is, if in a loop, the one closest to the top.
duke@0 781 skipped_cast = adr;
duke@0 782 adr = adr->in(1);
duke@0 783 continue;
duke@0 784 }
duke@0 785 // CastPP is going away in this pass! We need this memory op to be
duke@0 786 // control-dependent on the test that is guarding the CastPP.
kvn@163 787 ccp->hash_delete(n);
kvn@163 788 n->set_req(MemNode::Control, adr->in(0));
kvn@163 789 ccp->hash_insert(n);
kvn@163 790 return n;
duke@0 791
duke@0 792 case Op_Phi:
duke@0 793 // Attempt to float above a Phi to some dominating point.
duke@0 794 if (adr->in(0) != NULL && adr->in(0)->is_CountedLoop()) {
duke@0 795 // If we've already peeked through a Cast (which could have set the
duke@0 796 // control), we can't float above a Phi, because the skipped Cast
duke@0 797 // may not be loop invariant.
duke@0 798 if (adr_phi_is_loop_invariant(adr, skipped_cast)) {
duke@0 799 adr = adr->in(1);
duke@0 800 continue;
duke@0 801 }
duke@0 802 }
duke@0 803
duke@0 804 // Intentional fallthrough!
duke@0 805
duke@0 806 // No obvious dominating point. The mem op is pinned below the Phi
duke@0 807 // by the Phi itself. If the Phi goes away (no true value is merged)
duke@0 808 // then the mem op can float, but not indefinitely. It must be pinned
duke@0 809 // behind the controls leading to the Phi.
duke@0 810 case Op_CheckCastPP:
duke@0 811 // These usually stick around to change address type, however a
duke@0 812 // useless one can be elided and we still need to pick up a control edge
duke@0 813 if (adr->in(0) == NULL) {
duke@0 814 // This CheckCastPP node has NO control and is likely useless. But we
duke@0 815 // need check further up the ancestor chain for a control input to keep
duke@0 816 // the node in place. 4959717.
duke@0 817 skipped_cast = adr;
duke@0 818 adr = adr->in(1);
duke@0 819 continue;
duke@0 820 }
kvn@163 821 ccp->hash_delete(n);
kvn@163 822 n->set_req(MemNode::Control, adr->in(0));
kvn@163 823 ccp->hash_insert(n);
kvn@163 824 return n;
duke@0 825
duke@0 826 // List of "safe" opcodes; those that implicitly block the memory
duke@0 827 // op below any null check.
duke@0 828 case Op_CastX2P: // no null checks on native pointers
duke@0 829 case Op_Parm: // 'this' pointer is not null
duke@0 830 case Op_LoadP: // Loading from within a klass
coleenp@113 831 case Op_LoadN: // Loading from within a klass
duke@0 832 case Op_LoadKlass: // Loading from within a klass
kvn@164 833 case Op_LoadNKlass: // Loading from within a klass
duke@0 834 case Op_ConP: // Loading from a klass
kvn@163 835 case Op_ConN: // Loading from a klass
roland@3724 836 case Op_ConNKlass: // Loading from a klass
duke@0 837 case Op_CreateEx: // Sucking up the guts of an exception oop
duke@0 838 case Op_Con: // Reading from TLS
duke@0 839 case Op_CMoveP: // CMoveP is pinned
kvn@164 840 case Op_CMoveN: // CMoveN is pinned
duke@0 841 break; // No progress
duke@0 842
duke@0 843 case Op_Proj: // Direct call to an allocation routine
duke@0 844 case Op_SCMemProj: // Memory state from store conditional ops
duke@0 845 #ifdef ASSERT
duke@0 846 {
duke@0 847 assert(adr->as_Proj()->_con == TypeFunc::Parms, "must be return value");
duke@0 848 const Node* call = adr->in(0);
kvn@163 849 if (call->is_CallJava()) {
kvn@163 850 const CallJavaNode* call_java = call->as_CallJava();
kvn@64 851 const TypeTuple *r = call_java->tf()->range();
kvn@64 852 assert(r->cnt() > TypeFunc::Parms, "must return value");
kvn@64 853 const Type* ret_type = r->field_at(TypeFunc::Parms);
kvn@64 854 assert(ret_type && ret_type->isa_ptr(), "must return pointer");
duke@0 855 // We further presume that this is one of
duke@0 856 // new_instance_Java, new_array_Java, or
duke@0 857 // the like, but do not assert for this.
duke@0 858 } else if (call->is_Allocate()) {
duke@0 859 // similar case to new_instance_Java, etc.
duke@0 860 } else if (!call->is_CallLeaf()) {
duke@0 861 // Projections from fetch_oop (OSR) are allowed as well.
duke@0 862 ShouldNotReachHere();
duke@0 863 }
duke@0 864 }
duke@0 865 #endif
duke@0 866 break;
duke@0 867 default:
duke@0 868 ShouldNotReachHere();
duke@0 869 }
duke@0 870 break;
duke@0 871 }
duke@0 872 }
duke@0 873
duke@0 874 return NULL; // No progress
duke@0 875 }
duke@0 876
duke@0 877
duke@0 878 //=============================================================================
duke@0 879 uint LoadNode::size_of() const { return sizeof(*this); }
duke@0 880 uint LoadNode::cmp( const Node &n ) const
duke@0 881 { return !Type::cmp( _type, ((LoadNode&)n)._type ); }
duke@0 882 const Type *LoadNode::bottom_type() const { return _type; }
duke@0 883 uint LoadNode::ideal_reg() const {
coleenp@3602 884 return _type->ideal_reg();
duke@0 885 }
duke@0 886
duke@0 887 #ifndef PRODUCT
duke@0 888 void LoadNode::dump_spec(outputStream *st) const {
duke@0 889 MemNode::dump_spec(st);
duke@0 890 if( !Verbose && !WizardMode ) {
duke@0 891 // standard dump does this in Verbose and WizardMode
duke@0 892 st->print(" #"); _type->dump_on(st);
duke@0 893 }
duke@0 894 }
duke@0 895 #endif
duke@0 896
kvn@1529 897 #ifdef ASSERT
kvn@1529 898 //----------------------------is_immutable_value-------------------------------
kvn@1529 899 // Helper function to allow a raw load without control edge for some cases
kvn@1529 900 bool LoadNode::is_immutable_value(Node* adr) {
kvn@1529 901 return (adr->is_AddP() && adr->in(AddPNode::Base)->is_top() &&
kvn@1529 902 adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
kvn@1529 903 (adr->in(AddPNode::Offset)->find_intptr_t_con(-1) ==
kvn@1529 904 in_bytes(JavaThread::osthread_offset())));
kvn@1529 905 }
kvn@1529 906 #endif
duke@0 907
duke@0 908 //----------------------------LoadNode::make-----------------------------------
duke@0 909 // Polymorphic factory method:
coleenp@113 910 Node *LoadNode::make( PhaseGVN& gvn, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt ) {
coleenp@113 911 Compile* C = gvn.C;
coleenp@113 912
duke@0 913 // sanity check the alias category against the created node type
duke@0 914 assert(!(adr_type->isa_oopptr() &&
duke@0 915 adr_type->offset() == oopDesc::klass_offset_in_bytes()),
duke@0 916 "use LoadKlassNode instead");
duke@0 917 assert(!(adr_type->isa_aryptr() &&
duke@0 918 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
duke@0 919 "use LoadRangeNode instead");
kvn@1529 920 // Check control edge of raw loads
kvn@1529 921 assert( ctl != NULL || C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
kvn@1529 922 // oop will be recorded in oop map if load crosses safepoint
kvn@1529 923 rt->isa_oopptr() || is_immutable_value(adr),
kvn@1529 924 "raw memory operations should have control edge");
duke@0 925 switch (bt) {
kvn@3680 926 case T_BOOLEAN: return new (C) LoadUBNode(ctl, mem, adr, adr_type, rt->is_int() );
kvn@3680 927 case T_BYTE: return new (C) LoadBNode (ctl, mem, adr, adr_type, rt->is_int() );
kvn@3680 928 case T_INT: return new (C) LoadINode (ctl, mem, adr, adr_type, rt->is_int() );
kvn@3680 929 case T_CHAR: return new (C) LoadUSNode(ctl, mem, adr, adr_type, rt->is_int() );
kvn@3680 930 case T_SHORT: return new (C) LoadSNode (ctl, mem, adr, adr_type, rt->is_int() );
kvn@3680 931 case T_LONG: return new (C) LoadLNode (ctl, mem, adr, adr_type, rt->is_long() );
kvn@3680 932 case T_FLOAT: return new (C) LoadFNode (ctl, mem, adr, adr_type, rt );
kvn@3680 933 case T_DOUBLE: return new (C) LoadDNode (ctl, mem, adr, adr_type, rt );
kvn@3680 934 case T_ADDRESS: return new (C) LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr() );
coleenp@113 935 case T_OBJECT:
coleenp@113 936 #ifdef _LP64
kvn@163 937 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
kvn@3680 938 Node* load = gvn.transform(new (C) LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop()));
kvn@3680 939 return new (C) DecodeNNode(load, load->bottom_type()->make_ptr());
coleenp@113 940 } else
coleenp@113 941 #endif
kvn@163 942 {
roland@3724 943 assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop");
kvn@3680 944 return new (C) LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr());
kvn@163 945 }
duke@0 946 }
duke@0 947 ShouldNotReachHere();
duke@0 948 return (LoadNode*)NULL;
duke@0 949 }
duke@0 950
duke@0 951 LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt) {
duke@0 952 bool require_atomic = true;
kvn@3680 953 return new (C) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), require_atomic);
duke@0 954 }
duke@0 955
duke@0 956
duke@0 957
duke@0 958
duke@0 959 //------------------------------hash-------------------------------------------
duke@0 960 uint LoadNode::hash() const {
duke@0 961 // unroll addition of interesting fields
duke@0 962 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
duke@0 963 }
duke@0 964
vlivanov@5223 965 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
vlivanov@5223 966 if ((atp != NULL) && (atp->index() >= Compile::AliasIdxRaw)) {
vlivanov@5223 967 bool non_volatile = (atp->field() != NULL) && !atp->field()->is_volatile();
vlivanov@5223 968 bool is_stable_ary = FoldStableValues &&
vlivanov@5223 969 (tp != NULL) && (tp->isa_aryptr() != NULL) &&
vlivanov@5223 970 tp->isa_aryptr()->is_stable();
vlivanov@5223 971
vlivanov@5223 972 return (eliminate_boxing && non_volatile) || is_stable_ary;
vlivanov@5223 973 }
vlivanov@5223 974
vlivanov@5223 975 return false;
vlivanov@5223 976 }
vlivanov@5223 977
duke@0 978 //---------------------------can_see_stored_value------------------------------
duke@0 979 // This routine exists to make sure this set of tests is done the same
duke@0 980 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
duke@0 981 // will change the graph shape in a way which makes memory alive twice at the
duke@0 982 // same time (uses the Oracle model of aliasing), then some
duke@0 983 // LoadXNode::Identity will fold things back to the equivalence-class model
duke@0 984 // of aliasing.
duke@0 985 Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const {
duke@0 986 Node* ld_adr = in(MemNode::Address);
kvn@4675 987 intptr_t ld_off = 0;
kvn@4675 988 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
never@17 989 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
kvn@4675 990 Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL;
kvn@4675 991 // This is more general than load from boxing objects.
vlivanov@5223 992 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
never@17 993 uint alias_idx = atp->index();
vlivanov@5223 994 bool final = !atp->is_rewritable();
never@17 995 Node* result = NULL;
never@17 996 Node* current = st;
never@17 997 // Skip through chains of MemBarNodes checking the MergeMems for
never@17 998 // new states for the slice of this load. Stop once any other
never@17 999 // kind of node is encountered. Loads from final memory can skip
never@17 1000 // through any kind of MemBar but normal loads shouldn't skip
never@17 1001 // through MemBarAcquire since the could allow them to move out of
never@17 1002 // a synchronized region.
never@17 1003 while (current->is_Proj()) {
never@17 1004 int opc = current->in(0)->Opcode();
roland@2612 1005 if ((final && (opc == Op_MemBarAcquire || opc == Op_MemBarAcquireLock)) ||
roland@2612 1006 opc == Op_MemBarRelease || opc == Op_MemBarCPUOrder ||
roland@2612 1007 opc == Op_MemBarReleaseLock) {
never@17 1008 Node* mem = current->in(0)->in(TypeFunc::Memory);
never@17 1009 if (mem->is_MergeMem()) {
never@17 1010 MergeMemNode* merge = mem->as_MergeMem();
never@17 1011 Node* new_st = merge->memory_at(alias_idx);
never@17 1012 if (new_st == merge->base_memory()) {
never@17 1013 // Keep searching
kvn@4675 1014 current = new_st;
never@17 1015 continue;
never@17 1016 }
never@17 1017 // Save the new memory state for the slice and fall through
never@17 1018 // to exit.
never@17 1019 result = new_st;
never@17 1020 }
never@17 1021 }
never@17 1022 break;
never@17 1023 }
never@17 1024 if (result != NULL) {
never@17 1025 st = result;
never@17 1026 }
never@17 1027 }
never@17 1028
duke@0 1029 // Loop around twice in the case Load -> Initialize -> Store.
duke@0 1030 // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.)
duke@0 1031 for (int trip = 0; trip <= 1; trip++) {
duke@0 1032
duke@0 1033 if (st->is_Store()) {
duke@0 1034 Node* st_adr = st->in(MemNode::Address);
duke@0 1035 if (!phase->eqv(st_adr, ld_adr)) {
duke@0 1036 // Try harder before giving up... Match raw and non-raw pointers.
duke@0 1037 intptr_t st_off = 0;
duke@0 1038 AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off);
duke@0 1039 if (alloc == NULL) return NULL;
kvn@4675 1040 if (alloc != ld_alloc) return NULL;
duke@0 1041 if (ld_off != st_off) return NULL;
duke@0 1042 // At this point we have proven something like this setup:
duke@0 1043 // A = Allocate(...)
duke@0 1044 // L = LoadQ(, AddP(CastPP(, A.Parm),, #Off))
duke@0 1045 // S = StoreQ(, AddP(, A.Parm , #Off), V)
duke@0 1046 // (Actually, we haven't yet proven the Q's are the same.)
duke@0 1047 // In other words, we are loading from a casted version of
duke@0 1048 // the same pointer-and-offset that we stored to.
duke@0 1049 // Thus, we are able to replace L by V.
duke@0 1050 }
duke@0 1051 // Now prove that we have a LoadQ matched to a StoreQ, for some Q.
duke@0 1052 if (store_Opcode() != st->Opcode())
duke@0 1053 return NULL;
duke@0 1054 return st->in(MemNode::ValueIn);
duke@0 1055 }
duke@0 1056
duke@0 1057 // A load from a freshly-created object always returns zero.
duke@0 1058 // (This can happen after LoadNode::Ideal resets the load's memory input
duke@0 1059 // to find_captured_store, which returned InitializeNode::zero_memory.)
duke@0 1060 if (st->is_Proj() && st->in(0)->is_Allocate() &&
kvn@4675 1061 (st->in(0) == ld_alloc) &&
kvn@4675 1062 (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) {
duke@0 1063 // return a zero value for the load's basic type
duke@0 1064 // (This is one of the few places where a generic PhaseTransform
duke@0 1065 // can create new nodes. Think of it as lazily manifesting
duke@0 1066 // virtually pre-existing constants.)
duke@0 1067 return phase->zerocon(memory_type());
duke@0 1068 }
duke@0 1069
duke@0 1070 // A load from an initialization barrier can match a captured store.
duke@0 1071 if (st->is_Proj() && st->in(0)->is_Initialize()) {
duke@0 1072 InitializeNode* init = st->in(0)->as_Initialize();
duke@0 1073 AllocateNode* alloc = init->allocation();
kvn@4675 1074 if ((alloc != NULL) && (alloc == ld_alloc)) {
duke@0 1075 // examine a captured store value
kvn@4675 1076 st = init->find_captured_store(ld_off, memory_size(), phase);
duke@0 1077 if (st != NULL)
duke@0 1078 continue; // take one more trip around
duke@0 1079 }
duke@0 1080 }
duke@0 1081
kvn@4675 1082 // Load boxed value from result of valueOf() call is input parameter.
kvn@4675 1083 if (this->is_Load() && ld_adr->is_AddP() &&
kvn@4675 1084 (tp != NULL) && tp->is_ptr_to_boxed_value()) {
kvn@4675 1085 intptr_t ignore = 0;
kvn@4675 1086 Node* base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ignore);
kvn@4675 1087 if (base != NULL && base->is_Proj() &&
kvn@4675 1088 base->as_Proj()->_con == TypeFunc::Parms &&
kvn@4675 1089 base->in(0)->is_CallStaticJava() &&
kvn@4675 1090 base->in(0)->as_CallStaticJava()->is_boxing_method()) {
kvn@4675 1091 return base->in(0)->in(TypeFunc::Parms);
kvn@4675 1092 }
kvn@4675 1093 }
kvn@4675 1094
duke@0 1095 break;
duke@0 1096 }
duke@0 1097
duke@0 1098 return NULL;
duke@0 1099 }
duke@0 1100
kvn@64 1101 //----------------------is_instance_field_load_with_local_phi------------------
kvn@64 1102 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
kvn@4675 1103 if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl &&
kvn@4675 1104 in(Address)->is_AddP() ) {
kvn@4675 1105 const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr();
kvn@4675 1106 // Only instances and boxed values.
kvn@4675 1107 if( t_oop != NULL &&
kvn@4675 1108 (t_oop->is_ptr_to_boxed_value() ||
kvn@4675 1109 t_oop->is_known_instance_field()) &&
kvn@64 1110 t_oop->offset() != Type::OffsetBot &&
kvn@64 1111 t_oop->offset() != Type::OffsetTop) {
kvn@64 1112 return true;
kvn@64 1113 }
kvn@64 1114 }
kvn@64 1115 return false;
kvn@64 1116 }
kvn@64 1117
duke@0 1118 //------------------------------Identity---------------------------------------
duke@0 1119 // Loads are identity if previous store is to same address
duke@0 1120 Node *LoadNode::Identity( PhaseTransform *phase ) {
duke@0 1121 // If the previous store-maker is the right kind of Store, and the store is
duke@0 1122 // to the same address, then we are equal to the value stored.
kvn@4675 1123 Node* mem = in(Memory);
duke@0 1124 Node* value = can_see_stored_value(mem, phase);
duke@0 1125 if( value ) {
duke@0 1126 // byte, short & char stores truncate naturally.
duke@0 1127 // A load has to load the truncated value which requires
duke@0 1128 // some sort of masking operation and that requires an
duke@0 1129 // Ideal call instead of an Identity call.
duke@0 1130 if (memory_size() < BytesPerInt) {
duke@0 1131 // If the input to the store does not fit with the load's result type,
duke@0 1132 // it must be truncated via an Ideal call.
duke@0 1133 if (!phase->type(value)->higher_equal(phase->type(this)))
duke@0 1134 return this;
duke@0 1135 }
duke@0 1136 // (This works even when value is a Con, but LoadNode::Value
duke@0 1137 // usually runs first, producing the singleton type of the Con.)
duke@0 1138 return value;
duke@0 1139 }
kvn@64 1140
kvn@64 1141 // Search for an existing data phi which was generated before for the same
twisti@605 1142 // instance's field to avoid infinite generation of phis in a loop.
kvn@64 1143 Node *region = mem->in(0);
kvn@64 1144 if (is_instance_field_load_with_local_phi(region)) {
kvn@4675 1145 const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr();
kvn@64 1146 int this_index = phase->C->get_alias_index(addr_t);
kvn@64 1147 int this_offset = addr_t->offset();
kvn@4675 1148 int this_iid = addr_t->instance_id();
kvn@4675 1149 if (!addr_t->is_known_instance() &&
kvn@4675 1150 addr_t->is_ptr_to_boxed_value()) {
kvn@4675 1151 // Use _idx of address base (could be Phi node) for boxed values.
kvn@4675 1152 intptr_t ignore = 0;
kvn@4675 1153 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
kvn@4675 1154 this_iid = base->_idx;
kvn@4675 1155 }
kvn@64 1156 const Type* this_type = bottom_type();
kvn@64 1157 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
kvn@64 1158 Node* phi = region->fast_out(i);
kvn@64 1159 if (phi->is_Phi() && phi != mem &&
kvn@4675 1160 phi->as_Phi()->is_same_inst_field(this_type, this_iid, this_index, this_offset)) {
kvn@64 1161 return phi;
kvn@64 1162 }
kvn@64 1163 }
kvn@64 1164 }
kvn@64 1165
duke@0 1166 return this;
duke@0 1167 }
duke@0 1168
never@17 1169 // We're loading from an object which has autobox behaviour.
never@17 1170 // If this object is result of a valueOf call we'll have a phi
never@17 1171 // merging a newly allocated object and a load from the cache.
never@17 1172 // We want to replace this load with the original incoming
never@17 1173 // argument to the valueOf call.
never@17 1174 Node* LoadNode::eliminate_autobox(PhaseGVN* phase) {
kvn@4675 1175 assert(phase->C->eliminate_boxing(), "sanity");
kvn@4675 1176 intptr_t ignore = 0;
kvn@4675 1177 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
kvn@4675 1178 if ((base == NULL) || base->is_Phi()) {
kvn@4675 1179 // Push the loads from the phi that comes from valueOf up
kvn@4675 1180 // through it to allow elimination of the loads and the recovery
kvn@4675 1181 // of the original value. It is done in split_through_phi().
kvn@4675 1182 return NULL;
kvn@583 1183 } else if (base->is_Load() ||
kvn@583 1184 base->is_DecodeN() && base->in(1)->is_Load()) {
kvn@4675 1185 // Eliminate the load of boxed value for integer types from the cache
kvn@4675 1186 // array by deriving the value from the index into the array.
kvn@4675 1187 // Capture the offset of the load and then reverse the computation.
kvn@4675 1188
kvn@4675 1189 // Get LoadN node which loads a boxing object from 'cache' array.
kvn@583 1190 if (base->is_DecodeN()) {
kvn@583 1191 base = base->in(1);
kvn@583 1192 }
kvn@4675 1193 if (!base->in(Address)->is_AddP()) {
kvn@4675 1194 return NULL; // Complex address
kvn@583 1195 }
kvn@4675 1196 AddPNode* address = base->in(Address)->as_AddP();
kvn@4675 1197 Node* cache_base = address->in(AddPNode::Base);
kvn@4675 1198 if ((cache_base != NULL) && cache_base->is_DecodeN()) {
kvn@4675 1199 // Get ConP node which is static 'cache' field.
kvn@4675 1200 cache_base = cache_base->in(1);
kvn@4675 1201 }
kvn@4675 1202 if ((cache_base != NULL) && cache_base->is_Con()) {
kvn@4675 1203 const TypeAryPtr* base_type = cache_base->bottom_type()->isa_aryptr();
kvn@4675 1204 if ((base_type != NULL) && base_type->is_autobox_cache()) {
never@17 1205 Node* elements[4];
kvn@4675 1206 int shift = exact_log2(type2aelembytes(T_OBJECT));
kvn@4675 1207 int count = address->unpack_offsets(elements, ARRAY_SIZE(elements));
kvn@4675 1208 if ((count > 0) && elements[0]->is_Con() &&
kvn@4675 1209 ((count == 1) ||
kvn@4675 1210 (count == 2) && elements[1]->Opcode() == Op_LShiftX &&
kvn@4675 1211 elements[1]->in(2) == phase->intcon(shift))) {
kvn@4675 1212 ciObjArray* array = base_type->const_oop()->as_obj_array();
kvn@4675 1213 // Fetch the box object cache[0] at the base of the array and get its value
kvn@4675 1214 ciInstance* box = array->obj_at(0)->as_instance();
kvn@4675 1215 ciInstanceKlass* ik = box->klass()->as_instance_klass();
kvn@4675 1216 assert(ik->is_box_klass(), "sanity");
kvn@4675 1217 assert(ik->nof_nonstatic_fields() == 1, "change following code");
kvn@4675 1218 if (ik->nof_nonstatic_fields() == 1) {
kvn@4675 1219 // This should be true nonstatic_field_at requires calling
kvn@4675 1220 // nof_nonstatic_fields so check it anyway
kvn@4675 1221 ciConstant c = box->field_value(ik->nonstatic_field_at(0));
kvn@4675 1222 BasicType bt = c.basic_type();
kvn@4675 1223 // Only integer types have boxing cache.
kvn@4675 1224 assert(bt == T_BOOLEAN || bt == T_CHAR ||
kvn@4675 1225 bt == T_BYTE || bt == T_SHORT ||
kvn@4675 1226 bt == T_INT || bt == T_LONG, err_msg_res("wrong type = %s", type2name(bt)));
kvn@4675 1227 jlong cache_low = (bt == T_LONG) ? c.as_long() : c.as_int();
kvn@4675 1228 if (cache_low != (int)cache_low) {
kvn@4675 1229 return NULL; // should not happen since cache is array indexed by value
kvn@4675 1230 }
kvn@4675 1231 jlong offset = arrayOopDesc::base_offset_in_bytes(T_OBJECT) - (cache_low << shift);
kvn@4675 1232 if (offset != (int)offset) {
kvn@4675 1233 return NULL; // should not happen since cache is array indexed by value
kvn@4675 1234 }
kvn@4675 1235 // Add up all the offsets making of the address of the load
kvn@4675 1236 Node* result = elements[0];
kvn@4675 1237 for (int i = 1; i < count; i++) {
kvn@4675 1238 result = phase->transform(new (phase->C) AddXNode(result, elements[i]));
kvn@4675 1239 }
kvn@4675 1240 // Remove the constant offset from the address and then
kvn@4675 1241 result = phase->transform(new (phase->C) AddXNode(result, phase->MakeConX(-(int)offset)));
kvn@4675 1242 // remove the scaling of the offset to recover the original index.
kvn@4675 1243 if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) {
kvn@4675 1244 // Peel the shift off directly but wrap it in a dummy node
kvn@4675 1245 // since Ideal can't return existing nodes
kvn@4675 1246 result = new (phase->C) RShiftXNode(result->in(1), phase->intcon(0));
kvn@4675 1247 } else if (result->is_Add() && result->in(2)->is_Con() &&
kvn@4675 1248 result->in(1)->Opcode() == Op_LShiftX &&
kvn@4675 1249 result->in(1)->in(2) == phase->intcon(shift)) {
kvn@4675 1250 // We can't do general optimization: ((X<<Z) + Y) >> Z ==> X + (Y>>Z)
kvn@4675 1251 // but for boxing cache access we know that X<<Z will not overflow
kvn@4675 1252 // (there is range check) so we do this optimizatrion by hand here.
kvn@4675 1253 Node* add_con = new (phase->C) RShiftXNode(result->in(2), phase->intcon(shift));
kvn@4675 1254 result = new (phase->C) AddXNode(result->in(1)->in(1), phase->transform(add_con));
kvn@4675 1255 } else {
kvn@4675 1256 result = new (phase->C) RShiftXNode(result, phase->intcon(shift));
kvn@4675 1257 }
kvn@4675 1258 #ifdef _LP64
kvn@4675 1259 if (bt != T_LONG) {
kvn@4675 1260 result = new (phase->C) ConvL2INode(phase->transform(result));
kvn@4675 1261 }
kvn@4675 1262 #else
kvn@4675 1263 if (bt == T_LONG) {
kvn@4675 1264 result = new (phase->C) ConvI2LNode(phase->transform(result));
kvn@4675 1265 }
kvn@4675 1266 #endif
kvn@4675 1267 return result;
never@17 1268 }
never@17 1269 }
never@17 1270 }
never@17 1271 }
never@17 1272 }
never@17 1273 return NULL;
never@17 1274 }
never@17 1275
kvn@4675 1276 static bool stable_phi(PhiNode* phi, PhaseGVN *phase) {
kvn@4675 1277 Node* region = phi->in(0);
kvn@163 1278 if (region == NULL) {
kvn@4675 1279 return false; // Wait stable graph
kvn@163 1280 }
kvn@4675 1281 uint cnt = phi->req();
kvn@2375 1282 for (uint i = 1; i < cnt; i++) {
kvn@2375 1283 Node* rc = region->in(i);
kvn@2375 1284 if (rc == NULL || phase->type(rc) == Type::TOP)
kvn@4675 1285 return false; // Wait stable graph
kvn@4675 1286 Node* in = phi->in(i);
kvn@4675 1287 if (in == NULL || phase->type(in) == Type::TOP)
kvn@4675 1288 return false; // Wait stable graph
kvn@4675 1289 }
kvn@4675 1290 return true;
kvn@4675 1291 }
kvn@4675 1292 //------------------------------split_through_phi------------------------------
kvn@4675 1293 // Split instance or boxed field load through Phi.
kvn@4675 1294 Node *LoadNode::split_through_phi(PhaseGVN *phase) {
kvn@4675 1295 Node* mem = in(Memory);
kvn@4675 1296 Node* address = in(Address);
kvn@4675 1297 const TypeOopPtr *t_oop = phase->type(address)->isa_oopptr();
kvn@4675 1298
kvn@4675 1299 assert((t_oop != NULL) &&
kvn@4675 1300 (t_oop->is_known_instance_field() ||
kvn@4675 1301 t_oop->is_ptr_to_boxed_value()), "invalide conditions");
kvn@4675 1302
kvn@4675 1303 Compile* C = phase->C;
kvn@4675 1304 intptr_t ignore = 0;
kvn@4675 1305 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
kvn@4675 1306 bool base_is_phi = (base != NULL) && base->is_Phi();
kvn@4675 1307 bool load_boxed_values = t_oop->is_ptr_to_boxed_value() && C->aggressive_unboxing() &&
kvn@4675 1308 (base != NULL) && (base == address->in(AddPNode::Base)) &&
kvn@4675 1309 phase->type(base)->higher_equal(TypePtr::NOTNULL);
kvn@4675 1310
kvn@4675 1311 if (!((mem->is_Phi() || base_is_phi) &&
kvn@4675 1312 (load_boxed_values || t_oop->is_known_instance_field()))) {
kvn@4675 1313 return NULL; // memory is not Phi
kvn@4675 1314 }
kvn@4675 1315
kvn@4675 1316 if (mem->is_Phi()) {
kvn@4675 1317 if (!stable_phi(mem->as_Phi(), phase)) {
kvn@163 1318 return NULL; // Wait stable graph
kvn@163 1319 }
kvn@4675 1320 uint cnt = mem->req();
kvn@4675 1321 // Check for loop invariant memory.
kvn@4675 1322 if (cnt == 3) {
kvn@4675 1323 for (uint i = 1; i < cnt; i++) {
kvn@4675 1324 Node* in = mem->in(i);
kvn@4675 1325 Node* m = optimize_memory_chain(in, t_oop, this, phase);
kvn@4675 1326 if (m == mem) {
kvn@4675 1327 set_req(Memory, mem->in(cnt - i));
kvn@4675 1328 return this; // made change
kvn@4675 1329 }
kvn@163 1330 }
kvn@163 1331 }
kvn@163 1332 }
kvn@4675 1333 if (base_is_phi) {
kvn@4675 1334 if (!stable_phi(base->as_Phi(), phase)) {
kvn@4675 1335 return NULL; // Wait stable graph
kvn@4675 1336 }
kvn@4675 1337 uint cnt = base->req();
kvn@4675 1338 // Check for loop invariant memory.
kvn@4675 1339 if (cnt == 3) {
kvn@4675 1340 for (uint i = 1; i < cnt; i++) {
kvn@4675 1341 if (base->in(i) == base) {
kvn@4675 1342 return NULL; // Wait stable graph
kvn@4675 1343 }
kvn@4675 1344 }
kvn@4675 1345 }
kvn@4675 1346 }
kvn@4675 1347
kvn@4675 1348 bool load_boxed_phi = load_boxed_values && base_is_phi && (base->in(0) == mem->in(0));
kvn@4675 1349
kvn@163 1350 // Split through Phi (see original code in loopopts.cpp).
kvn@4675 1351 assert(C->have_alias_type(t_oop), "instance should have alias type");
kvn@163 1352
kvn@163 1353 // Do nothing here if Identity will find a value
kvn@163 1354 // (to avoid infinite chain of value phis generation).
kvn@2375 1355 if (!phase->eqv(this, this->Identity(phase)))
kvn@163 1356 return NULL;
kvn@163 1357
kvn@4675 1358 // Select Region to split through.
kvn@4675 1359 Node* region;
kvn@4675 1360 if (!base_is_phi) {
kvn@4675 1361 assert(mem->is_Phi(), "sanity");
kvn@4675 1362 region = mem->in(0);
kvn@4675 1363 // Skip if the region dominates some control edge of the address.
kvn@4675 1364 if (!MemNode::all_controls_dominate(address, region))
kvn@4675 1365 return NULL;
kvn@4675 1366 } else if (!mem->is_Phi()) {
kvn@4675 1367 assert(base_is_phi, "sanity");
kvn@4675 1368 region = base->in(0);
kvn@4675 1369 // Skip if the region dominates some control edge of the memory.
kvn@4675 1370 if (!MemNode::all_controls_dominate(mem, region))
kvn@4675 1371 return NULL;
kvn@4675 1372 } else if (base->in(0) != mem->in(0)) {
kvn@4675 1373 assert(base_is_phi && mem->is_Phi(), "sanity");
kvn@4675 1374 if (MemNode::all_controls_dominate(mem, base->in(0))) {
kvn@4675 1375 region = base->in(0);
kvn@4675 1376 } else if (MemNode::all_controls_dominate(address, mem->in(0))) {
kvn@4675 1377 region = mem->in(0);
kvn@4675 1378 } else {
kvn@4675 1379 return NULL; // complex graph
kvn@4675 1380 }
kvn@4675 1381 } else {
kvn@4675 1382 assert(base->in(0) == mem->in(0), "sanity");
kvn@4675 1383 region = mem->in(0);
kvn@4675 1384 }
kvn@163 1385
kvn@163 1386 const Type* this_type = this->bottom_type();
kvn@4675 1387 int this_index = C->get_alias_index(t_oop);
kvn@4675 1388 int this_offset = t_oop->offset();
kvn@4675 1389 int this_iid = t_oop->instance_id();
kvn@4675 1390 if (!t_oop->is_known_instance() && load_boxed_values) {
kvn@4675 1391 // Use _idx of address base for boxed values.
kvn@4675 1392 this_iid = base->_idx;
kvn@4675 1393 }
kvn@4675 1394 PhaseIterGVN* igvn = phase->is_IterGVN();
kvn@4675 1395 Node* phi = new (C) PhiNode(region, this_type, NULL, this_iid, this_index, this_offset);
kvn@2375 1396 for (uint i = 1; i < region->req(); i++) {
kvn@4675 1397 Node* x;
kvn@163 1398 Node* the_clone = NULL;
kvn@4675 1399 if (region->in(i) == C->top()) {
kvn@4675 1400 x = C->top(); // Dead path? Use a dead data op
kvn@163 1401 } else {
kvn@163 1402 x = this->clone(); // Else clone up the data op
kvn@163 1403 the_clone = x; // Remember for possible deletion.
kvn@163 1404 // Alter data node to use pre-phi inputs
kvn@2375 1405 if (this->in(0) == region) {
kvn@2375 1406 x->set_req(0, region->in(i));
kvn@163 1407 } else {
kvn@2375 1408 x->set_req(0, NULL);
kvn@163 1409 }
kvn@4675 1410 if (mem->is_Phi() && (mem->in(0) == region)) {
kvn@4675 1411 x->set_req(Memory, mem->in(i)); // Use pre-Phi input for the clone.
kvn@4675 1412 }
kvn@4675 1413 if (address->is_Phi() && address->in(0) == region) {
kvn@4675 1414 x->set_req(Address, address->in(i)); // Use pre-Phi input for the clone
kvn@4675 1415 }
kvn@4675 1416 if (base_is_phi && (base->in(0) == region)) {
kvn@4675 1417 Node* base_x = base->in(i); // Clone address for loads from boxed objects.
kvn@4675 1418 Node* adr_x = phase->transform(new (C) AddPNode(base_x,base_x,address->in(AddPNode::Offset)));
kvn@4675 1419 x->set_req(Address, adr_x);
kvn@163 1420 }
kvn@163 1421 }
kvn@163 1422 // Check for a 'win' on some paths
kvn@163 1423 const Type *t = x->Value(igvn);
kvn@163 1424
kvn@163 1425 bool singleton = t->singleton();
kvn@163 1426
kvn@163 1427 // See comments in PhaseIdealLoop::split_thru_phi().
kvn@2375 1428 if (singleton && t == Type::TOP) {
kvn@163 1429 singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
kvn@163 1430 }
kvn@163 1431
kvn@2375 1432 if (singleton) {
kvn@163 1433 x = igvn->makecon(t);
kvn@163 1434 } else {
kvn@163 1435 // We now call Identity to try to simplify the cloned node.
kvn@163 1436 // Note that some Identity methods call phase->type(this).
kvn@163 1437 // Make sure that the type array is big enough for
kvn@163 1438 // our new node, even though we may throw the node away.
kvn@163 1439 // (This tweaking with igvn only works because x is a new node.)
kvn@163 1440 igvn->set_type(x, t);
kvn@293 1441 // If x is a TypeNode, capture any more-precise type permanently into Node
twisti@605 1442 // otherwise it will be not updated during igvn->transform since
kvn@293 1443 // igvn->type(x) is set to x->Value() already.
kvn@293 1444 x->raise_bottom_type(t);
kvn@163 1445 Node *y = x->Identity(igvn);
kvn@2375 1446 if (y != x) {
kvn@163 1447 x = y;
kvn@163 1448 } else {
kvn@4675 1449 y = igvn->hash_find_insert(x);
kvn@2375 1450 if (y) {
kvn@163 1451 x = y;
kvn@163 1452 } else {
kvn@163 1453 // Else x is a new node we are keeping
kvn@163 1454 // We do not need register_new_node_with_optimizer
kvn@163 1455 // because set_type has already been called.
kvn@163 1456 igvn->_worklist.push(x);
kvn@163 1457 }
kvn@163 1458 }
kvn@163 1459 }
kvn@4675 1460 if (x != the_clone && the_clone != NULL) {
kvn@163 1461 igvn->remove_dead_node(the_clone);
kvn@4675 1462 }
kvn@163 1463 phi->set_req(i, x);
kvn@163 1464 }
kvn@2375 1465 // Record Phi
kvn@2375 1466 igvn->register_new_node_with_optimizer(phi);
kvn@2375 1467 return phi;
kvn@163 1468 }
never@17 1469
duke@0 1470 //------------------------------Ideal------------------------------------------
duke@0 1471 // If the load is from Field memory and the pointer is non-null, we can
duke@0 1472 // zero out the control input.
duke@0 1473 // If the offset is constant and the base is an object allocation,
duke@0 1474 // try to hook me up to the exact initializing store.
duke@0 1475 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@0 1476 Node* p = MemNode::Ideal_common(phase, can_reshape);
duke@0 1477 if (p) return (p == NodeSentinel) ? NULL : p;
duke@0 1478
duke@0 1479 Node* ctrl = in(MemNode::Control);
duke@0 1480 Node* address = in(MemNode::Address);
duke@0 1481
duke@0 1482 // Skip up past a SafePoint control. Cannot do this for Stores because
duke@0 1483 // pointer stores & cardmarks must stay on the same side of a SafePoint.
duke@0 1484 if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint &&
duke@0 1485 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) {
duke@0 1486 ctrl = ctrl->in(0);
duke@0 1487 set_req(MemNode::Control,ctrl);
duke@0 1488 }
duke@0 1489
kvn@708 1490 intptr_t ignore = 0;
kvn@708 1491 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
kvn@708 1492 if (base != NULL
kvn@708 1493 && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) {
kvn@708 1494 // Check for useless control edge in some common special cases
kvn@708 1495 if (in(MemNode::Control) != NULL
duke@0 1496 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
kvn@119 1497 && all_controls_dominate(base, phase->C->start())) {
duke@0 1498 // A method-invariant, non-null address (constant or 'this' argument).
duke@0 1499 set_req(MemNode::Control, NULL);
duke@0 1500 }
never@17 1501 }
never@17 1502
kvn@74 1503 Node* mem = in(MemNode::Memory);
kvn@74 1504 const TypePtr *addr_t = phase->type(address)->isa_ptr();
kvn@74 1505
kvn@4675 1506 if (can_reshape && (addr_t != NULL)) {
kvn@74 1507 // try to optimize our memory input
kvn@4675 1508 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase);
kvn@74 1509 if (opt_mem != mem) {
kvn@74 1510 set_req(MemNode::Memory, opt_mem);
kvn@305 1511 if (phase->type( opt_mem ) == Type::TOP) return NULL;
kvn@74 1512 return this;
kvn@74 1513 }
kvn@74 1514 const TypeOopPtr *t_oop = addr_t->isa_oopptr();
kvn@4675 1515 if ((t_oop != NULL) &&
kvn@4675 1516 (t_oop->is_known_instance_field() ||
kvn@4675 1517 t_oop->is_ptr_to_boxed_value())) {
kvn@2825 1518 PhaseIterGVN *igvn = phase->is_IterGVN();
kvn@2825 1519 if (igvn != NULL && igvn->_worklist.member(opt_mem)) {
kvn@2825 1520 // Delay this transformation until memory Phi is processed.
kvn@2825 1521 phase->is_IterGVN()->_worklist.push(this);
kvn@2825 1522 return NULL;
kvn@2825 1523 }
kvn@163 1524 // Split instance field load through Phi.
kvn@163 1525 Node* result = split_through_phi(phase);
kvn@163 1526 if (result != NULL) return result;
kvn@4675 1527
kvn@4675 1528 if (t_oop->is_ptr_to_boxed_value()) {
kvn@4675 1529 Node* result = eliminate_autobox(phase);
kvn@4675 1530 if (result != NULL) return result;
kvn@4675 1531 }
kvn@74 1532 }
kvn@74 1533 }
kvn@74 1534
duke@0 1535 // Check for prior store with a different base or offset; make Load
duke@0 1536 // independent. Skip through any number of them. Bail out if the stores
duke@0 1537 // are in an endless dead cycle and report no progress. This is a key
duke@0 1538 // transform for Reflection. However, if after skipping through the Stores
duke@0 1539 // we can't then fold up against a prior store do NOT do the transform as
duke@0 1540 // this amounts to using the 'Oracle' model of aliasing. It leaves the same
duke@0 1541 // array memory alive twice: once for the hoisted Load and again after the
duke@0 1542 // bypassed Store. This situation only works if EVERYBODY who does
duke@0 1543 // anti-dependence work knows how to bypass. I.e. we need all
duke@0 1544 // anti-dependence checks to ask the same Oracle. Right now, that Oracle is
duke@0 1545 // the alias index stuff. So instead, peek through Stores and IFF we can
duke@0 1546 // fold up, do so.
duke@0 1547 Node* prev_mem = find_previous_store(phase);
duke@0 1548 // Steps (a), (b): Walk past independent stores to find an exact match.
duke@0 1549 if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) {
duke@0 1550 // (c) See if we can fold up on the spot, but don't fold up here.
twisti@558 1551 // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or
duke@0 1552 // just return a prior value, which is done by Identity calls.
duke@0 1553 if (can_see_stored_value(prev_mem, phase)) {
duke@0 1554 // Make ready for step (d):
duke@0 1555 set_req(MemNode::Memory, prev_mem);
duke@0 1556 return this;
duke@0 1557 }
duke@0 1558 }
duke@0 1559
duke@0 1560 return NULL; // No further progress
duke@0 1561 }
duke@0 1562
duke@0 1563 // Helper to recognize certain Klass fields which are invariant across
duke@0 1564 // some group of array types (e.g., int[] or all T[] where T < Object).
duke@0 1565 const Type*
duke@0 1566 LoadNode::load_array_final_field(const TypeKlassPtr *tkls,
duke@0 1567 ciKlass* klass) const {
stefank@2956 1568 if (tkls->offset() == in_bytes(Klass::modifier_flags_offset())) {
duke@0 1569 // The field is Klass::_modifier_flags. Return its (constant) value.
duke@0 1570 // (Folds up the 2nd indirection in aClassConstant.getModifiers().)
duke@0 1571 assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags");
duke@0 1572 return TypeInt::make(klass->modifier_flags());
duke@0 1573 }
stefank@2956 1574 if (tkls->offset() == in_bytes(Klass::access_flags_offset())) {
duke@0 1575 // The field is Klass::_access_flags. Return its (constant) value.
duke@0 1576 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).)
duke@0 1577 assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags");
duke@0 1578 return TypeInt::make(klass->access_flags());
duke@0 1579 }
stefank@2956 1580 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) {
duke@0 1581 // The field is Klass::_layout_helper. Return its constant value if known.
duke@0 1582 assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper");
duke@0 1583 return TypeInt::make(klass->layout_helper());
duke@0 1584 }
duke@0 1585
duke@0 1586 // No match.
duke@0 1587 return NULL;
duke@0 1588 }
duke@0 1589
vlivanov@5223 1590 // Try to constant-fold a stable array element.
vlivanov@5223 1591 static const Type* fold_stable_ary_elem(const TypeAryPtr* ary, int off, BasicType loadbt) {
vlivanov@5223 1592 assert(ary->is_stable(), "array should be stable");
vlivanov@5223 1593
vlivanov@5223 1594 if (ary->const_oop() != NULL) {
vlivanov@5223 1595 // Decode the results of GraphKit::array_element_address.
vlivanov@5223 1596 ciArray* aobj = ary->const_oop()->as_array();
vlivanov@5223 1597 ciConstant con = aobj->element_value_by_offset(off);
vlivanov@5223 1598
vlivanov@5223 1599 if (con.basic_type() != T_ILLEGAL && !con.is_null_or_zero()) {
vlivanov@5223 1600 const Type* con_type = Type::make_from_constant(con);
vlivanov@5223 1601 if (con_type != NULL) {
vlivanov@5223 1602 if (con_type->isa_aryptr()) {
vlivanov@5223 1603 // Join with the array element type, in case it is also stable.
vlivanov@5223 1604 int dim = ary->stable_dimension();
vlivanov@5223 1605 con_type = con_type->is_aryptr()->cast_to_stable(true, dim-1);
vlivanov@5223 1606 }
vlivanov@5223 1607 if (loadbt == T_NARROWOOP && con_type->isa_oopptr()) {
vlivanov@5223 1608 con_type = con_type->make_narrowoop();
vlivanov@5223 1609 }
vlivanov@5223 1610 #ifndef PRODUCT
vlivanov@5223 1611 if (TraceIterativeGVN) {
vlivanov@5223 1612 tty->print("FoldStableValues: array element [off=%d]: con_type=", off);
vlivanov@5223 1613 con_type->dump(); tty->cr();
vlivanov@5223 1614 }
vlivanov@5223 1615 #endif //PRODUCT
vlivanov@5223 1616 return con_type;
vlivanov@5223 1617 }
vlivanov@5223 1618 }
vlivanov@5223 1619 }
vlivanov@5223 1620
vlivanov@5223 1621 return NULL;
vlivanov@5223 1622 }
vlivanov@5223 1623
duke@0 1624 //------------------------------Value-----------------------------------------
duke@0 1625 const Type *LoadNode::Value( PhaseTransform *phase ) const {
duke@0 1626 // Either input is TOP ==> the result is TOP
duke@0 1627 Node* mem = in(MemNode::Memory);
duke@0 1628 const Type *t1 = phase->type(mem);
duke@0 1629 if (t1 == Type::TOP) return Type::TOP;
duke@0 1630 Node* adr = in(MemNode::Address);
duke@0 1631 const TypePtr* tp = phase->type(adr)->isa_ptr();
duke@0 1632 if (tp == NULL || tp->empty()) return Type::TOP;
duke@0 1633 int off = tp->offset();
duke@0 1634 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
twisti@2667 1635 Compile* C = phase->C;
duke@0 1636
duke@0 1637 // Try to guess loaded type from pointer type
vlivanov@5223 1638 if (tp->isa_aryptr()) {
vlivanov@5223 1639 const TypeAryPtr* ary = tp->is_aryptr();
vlivanov@5223 1640 const Type *t = ary->elem();
vlivanov@5223 1641
vlivanov@5223 1642 // Determine whether the reference is beyond the header or not, by comparing
vlivanov@5223 1643 // the offset against the offset of the start of the array's data.
vlivanov@5223 1644 // Different array types begin at slightly different offsets (12 vs. 16).
vlivanov@5223 1645 // We choose T_BYTE as an example base type that is least restrictive
vlivanov@5223 1646 // as to alignment, which will therefore produce the smallest
vlivanov@5223 1647 // possible base offset.
vlivanov@5223 1648 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
vlivanov@5223 1649 const bool off_beyond_header = ((uint)off >= (uint)min_base_off);
vlivanov@5223 1650
vlivanov@5223 1651 // Try to constant-fold a stable array element.
vlivanov@5223 1652 if (FoldStableValues && ary->is_stable()) {
vlivanov@5223 1653 // Make sure the reference is not into the header
vlivanov@5223 1654 if (off_beyond_header && off != Type::OffsetBot) {
vlivanov@5223 1655 assert(adr->is_AddP() && adr->in(AddPNode::Offset)->is_Con(), "offset is a constant");
vlivanov@5223 1656 const Type* con_type = fold_stable_ary_elem(ary, off, memory_type());
vlivanov@5223 1657 if (con_type != NULL) {
vlivanov@5223 1658 return con_type;
vlivanov@5223 1659 }
vlivanov@5223 1660 }
vlivanov@5223 1661 }
vlivanov@5223 1662
duke@0 1663 // Don't do this for integer types. There is only potential profit if
duke@0 1664 // the element type t is lower than _type; that is, for int types, if _type is
duke@0 1665 // more restrictive than t. This only happens here if one is short and the other
duke@0 1666 // char (both 16 bits), and in those cases we've made an intentional decision
duke@0 1667 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
duke@0 1668 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
duke@0 1669 //
duke@0 1670 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
duke@0 1671 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
duke@0 1672 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
duke@0 1673 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
duke@0 1674 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
duke@0 1675 // In fact, that could have been the original type of p1, and p1 could have
duke@0 1676 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
duke@0 1677 // expression (LShiftL quux 3) independently optimized to the constant 8.
duke@0 1678 if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
kvn@3447 1679 && (_type->isa_vect() == NULL)
kvn@293 1680 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
duke@0 1681 // t might actually be lower than _type, if _type is a unique
duke@0 1682 // concrete subclass of abstract class t.
vlivanov@5223 1683 if (off_beyond_header) { // is the offset beyond the header?
duke@0 1684 const Type* jt = t->join(_type);
duke@0 1685 // In any case, do not allow the join, per se, to empty out the type.
duke@0 1686 if (jt->empty() && !t->empty()) {
duke@0 1687 // This can happen if a interface-typed array narrows to a class type.
duke@0 1688 jt = _type;
duke@0 1689 }
kvn@4675 1690 #ifdef ASSERT
kvn@4675 1691 if (phase->C->eliminate_boxing() && adr->is_AddP()) {
never@17 1692 // The pointers in the autobox arrays are always non-null
kvn@708 1693 Node* base = adr->in(AddPNode::Base);
kvn@4675 1694 if ((base != NULL) && base->is_DecodeN()) {
kvn@4675 1695 // Get LoadN node which loads IntegerCache.cache field
kvn@4675 1696 base = base->in(1);
kvn@4675 1697 }
kvn@4675 1698 if ((base != NULL) && base->is_Con()) {
kvn@4675 1699 const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr();
kvn@4675 1700 if ((base_type != NULL) && base_type->is_autobox_cache()) {
kvn@4675 1701 // It could be narrow oop
kvn@4675 1702 assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity");
never@17 1703 }
never@17 1704 }
never@17 1705 }
kvn@4675 1706 #endif
duke@0 1707 return jt;
duke@0 1708 }
duke@0 1709 }
duke@0 1710 } else if (tp->base() == Type::InstPtr) {
twisti@2667 1711 ciEnv* env = C->env();
never@1080 1712 const TypeInstPtr* tinst = tp->is_instptr();
never@1080 1713 ciKlass* klass = tinst->klass();
duke@0 1714 assert( off != Type::OffsetBot ||
duke@0 1715 // arrays can be cast to Objects
duke@0 1716 tp->is_oopptr()->klass()->is_java_lang_Object() ||
duke@0 1717 // unsafe field access may not have a constant offset
twisti@2667 1718 C->has_unsafe_access(),
duke@0 1719 "Field accesses must be precise" );
duke@0 1720 // For oop loads, we expect the _type to be precise
twisti@2667 1721 if (klass == env->String_klass() &&
never@1080 1722 adr->is_AddP() && off != Type::OffsetBot) {
kvn@2167 1723 // For constant Strings treat the final fields as compile time constants.
never@1080 1724 Node* base = adr->in(AddPNode::Base);
never@1686 1725 const TypeOopPtr* t = phase->type(base)->isa_oopptr();
never@1686 1726 if (t != NULL && t->singleton()) {
twisti@2667 1727 ciField* field = env->String_klass()->get_field_by_offset(off, false);
kvn@2167 1728 if (field != NULL && field->is_final()) {
kvn@2167 1729 ciObject* string = t->const_oop();
kvn@2167 1730 ciConstant constant = string->as_instance()->field_value(field);
kvn@2167 1731 if (constant.basic_type() == T_INT) {
kvn@2167 1732 return TypeInt::make(constant.as_int());
kvn@2167 1733 } else if (constant.basic_type() == T_ARRAY) {
kvn@2167 1734 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
jcoomes@2226 1735 return TypeNarrowOop::make_from_constant(constant.as_object(), true);
kvn@2167 1736 } else {
jcoomes@2226 1737 return TypeOopPtr::make_from_constant(constant.as_object(), true);
kvn@2167 1738 }
never@1080 1739 }
never@1080 1740 }
never@1080 1741 }
never@1080 1742 }
twisti@2667 1743 // Optimizations for constant objects
twisti@2667 1744 ciObject* const_oop = tinst->const_oop();
twisti@2667 1745 if (const_oop != NULL) {
kvn@4675 1746 // For constant Boxed value treat the target field as a compile time constant.
kvn@4675 1747 if (tinst->is_ptr_to_boxed_value()) {
kvn@4675 1748 return tinst->get_const_boxed_value();
kvn@4675 1749 } else
twisti@2667 1750 // For constant CallSites treat the target field as a compile time constant.
twisti@2667 1751 if (const_oop->is_call_site()) {
twisti@2667 1752 ciCallSite* call_site = const_oop->as_call_site();
twisti@2667 1753 ciField* field = call_site->klass()->as_instance_klass()->get_field_by_offset(off, /*is_static=*/ false);
twisti@2667 1754 if (field != NULL && field->is_call_site_target()) {
twisti@2667 1755 ciMethodHandle* target = call_site->get_target();
twisti@2667 1756 if (target != NULL) { // just in case
twisti@2667 1757 ciConstant constant(T_OBJECT, target);
twisti@2667 1758 const Type* t;
twisti@2667 1759 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
twisti@2667 1760 t = TypeNarrowOop::make_from_constant(constant.as_object(), true);
twisti@2667 1761 } else {
twisti@2667 1762 t = TypeOopPtr::make_from_constant(constant.as_object(), true);
twisti@2667 1763 }
twisti@2667 1764 // Add a dependence for invalidation of the optimization.
twisti@2667 1765 if (!call_site->is_constant_call_site()) {
twisti@2667 1766 C->dependencies()->assert_call_site_target_value(call_site, target);
twisti@2667 1767 }
twisti@2667 1768 return t;
twisti@2667 1769 }
twisti@2667 1770 }
twisti@2667 1771 }
twisti@2667 1772 }
duke@0 1773 } else if (tp->base() == Type::KlassPtr) {
duke@0 1774 assert( off != Type::OffsetBot ||
duke@0 1775 // arrays can be cast to Objects
duke@0 1776 tp->is_klassptr()->klass()->is_java_lang_Object() ||
duke@0 1777 // also allow array-loading from the primary supertype
duke@0 1778 // array during subtype checks
duke@0 1779 Opcode() == Op_LoadKlass,
duke@0 1780 "Field accesses must be precise" );
duke@0 1781 // For klass/static loads, we expect the _type to be precise
duke@0 1782 }
duke@0 1783
duke@0 1784 const TypeKlassPtr *tkls = tp->isa_klassptr();
duke@0 1785 if (tkls != NULL && !StressReflectiveCode) {
duke@0 1786 ciKlass* klass = tkls->klass();
duke@0 1787 if (klass->is_loaded() && tkls->klass_is_exact()) {
duke@0 1788 // We are loading a field from a Klass metaobject whose identity
duke@0 1789 // is known at compile time (the type is "exact" or "precise").
duke@0 1790 // Check for fields we know are maintained as constants by the VM.
stefank@2956 1791 if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) {
duke@0 1792 // The field is Klass::_super_check_offset. Return its (constant) value.
duke@0 1793 // (Folds up type checking code.)
duke@0 1794 assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset");
duke@0 1795 return TypeInt::make(klass->super_check_offset());
duke@0 1796 }
duke@0 1797 // Compute index into primary_supers array
coleenp@3602 1798 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
duke@0 1799 // Check for overflowing; use unsigned compare to handle the negative case.
duke@0 1800 if( depth < ciKlass::primary_super_limit() ) {
duke@0 1801 // The field is an element of Klass::_primary_supers. Return its (constant) value.
duke@0 1802 // (Folds up type checking code.)
duke@0 1803 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
duke@0 1804 ciKlass *ss = klass->super_of_depth(depth);
duke@0 1805 return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
duke@0 1806 }
duke@0 1807 const Type* aift = load_array_final_field(tkls, klass);
duke@0 1808 if (aift != NULL) return aift;
coleenp@3707 1809 if (tkls->offset() == in_bytes(ArrayKlass::component_mirror_offset())
duke@0 1810 && klass->is_array_klass()) {
coleenp@3707 1811 // The field is ArrayKlass::_component_mirror. Return its (constant) value.
duke@0 1812 // (Folds up aClassConstant.getComponentType, common in Arrays.copyOf.)
duke@0 1813 assert(Opcode() == Op_LoadP, "must load an oop from _component_mirror");
duke@0 1814 return TypeInstPtr::make(klass->as_array_klass()->component_mirror());
duke@0 1815 }
stefank@2956 1816 if (tkls->offset() == in_bytes(Klass::java_mirror_offset())) {
duke@0 1817 // The field is Klass::_java_mirror. Return its (constant) value.
duke@0 1818 // (Folds up the 2nd indirection in anObjConstant.getClass().)
duke@0 1819 assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror");
duke@0 1820 return TypeInstPtr::make(klass->java_mirror());
duke@0 1821 }
duke@0 1822 }
duke@0 1823
duke@0 1824 // We can still check if we are loading from the primary_supers array at a
duke@0 1825 // shallow enough depth. Even though the klass is not exact, entries less
duke@0 1826 // than or equal to its super depth are correct.
duke@0 1827 if (klass->is_loaded() ) {
coleenp@3602 1828 ciType *inner = klass;
duke@0 1829 while( inner->is_obj_array_klass() )
duke@0 1830 inner = inner->as_obj_array_klass()->base_element_type();
duke@0 1831 if( inner->is_instance_klass() &&
duke@0 1832 !inner->as_instance_klass()->flags().is_interface() ) {
duke@0 1833 // Compute index into primary_supers array
coleenp@3602 1834 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
duke@0 1835 // Check for overflowing; use unsigned compare to handle the negative case.
duke@0 1836 if( depth < ciKlass::primary_super_limit() &&
duke@0 1837 depth <= klass->super_depth() ) { // allow self-depth checks to handle self-check case
duke@0 1838 // The field is an element of Klass::_primary_supers. Return its (constant) value.
duke@0 1839 // (Folds up type checking code.)
duke@0 1840 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
duke@0 1841 ciKlass *ss = klass->super_of_depth(depth);
duke@0 1842 return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
duke@0 1843 }
duke@0 1844 }
duke@0 1845 }
duke@0 1846
duke@0 1847 // If the type is enough to determine that the thing is not an array,
duke@0 1848 // we can give the layout_helper a positive interval type.
duke@0 1849 // This will help short-circuit some reflective code.
stefank@2956 1850 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())
duke@0 1851 && !klass->is_array_klass() // not directly typed as an array
duke@0 1852 && !klass->is_interface() // specifically not Serializable & Cloneable
duke@0 1853 && !klass->is_java_lang_Object() // not the supertype of all T[]
duke@0 1854 ) {
duke@0 1855 // Note: When interfaces are reliable, we can narrow the interface
duke@0 1856 // test to (klass != Serializable && klass != Cloneable).
duke@0 1857 assert(Opcode() == Op_LoadI, "must load an int from _layout_helper");
duke@0 1858 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
duke@0 1859 // The key property of this type is that it folds up tests
duke@0 1860 // for array-ness, since it proves that the layout_helper is positive.
duke@0 1861 // Thus, a generic value like the basic object layout helper works fine.
duke@0 1862 return TypeInt::make(min_size, max_jint, Type::WidenMin);
duke@0 1863 }
duke@0 1864 }
duke@0 1865
duke@0 1866 // If we are loading from a freshly-allocated object, produce a zero,
duke@0 1867 // if the load is provably beyond the header of the object.
duke@0 1868 // (Also allow a variable load from a fresh array to produce zero.)
kvn@2375 1869 const TypeOopPtr *tinst = tp->isa_oopptr();
kvn@2375 1870 bool is_instance = (tinst != NULL) && tinst->is_known_instance_field();
kvn@4675 1871 bool is_boxed_value = (tinst != NULL) && tinst->is_ptr_to_boxed_value();
kvn@4675 1872 if (ReduceFieldZeroing || is_instance || is_boxed_value) {
duke@0 1873 Node* value = can_see_stored_value(mem,phase);
kvn@3007 1874 if (value != NULL && value->is_Con()) {
kvn@3007 1875 assert(value->bottom_type()->higher_equal(_type),"sanity");
duke@0 1876 return value->bottom_type();
kvn@3007 1877 }
duke@0 1878 }
duke@0 1879
kvn@2375 1880 if (is_instance) {
kvn@64 1881 // If we have an instance type and our memory input is the
kvn@64 1882 // programs's initial memory state, there is no matching store,
kvn@64 1883 // so just return a zero of the appropriate type
kvn@64 1884 Node *mem = in(MemNode::Memory);
kvn@64 1885 if (mem->is_Parm() && mem->in(0)->is_Start()) {
kvn@64 1886 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
kvn@64 1887 return Type::get_zero_type(_type->basic_type());
kvn@64 1888 }
kvn@64 1889 }
duke@0 1890 return _type;
duke@0 1891 }
duke@0 1892
duke@0 1893 //------------------------------match_edge-------------------------------------
duke@0 1894 // Do we Match on this edge index or not? Match only the address.
duke@0 1895 uint LoadNode::match_edge(uint idx) const {
duke@0 1896 return idx == MemNode::Address;
duke@0 1897 }
duke@0 1898
duke@0 1899 //--------------------------LoadBNode::Ideal--------------------------------------
duke@0 1900 //
duke@0 1901 // If the previous store is to the same address as this load,
duke@0 1902 // and the value stored was larger than a byte, replace this load
duke@0 1903 // with the value stored truncated to a byte. If no truncation is
duke@0 1904 // needed, the replacement is done in LoadNode::Identity().
duke@0 1905 //
duke@0 1906 Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@0 1907 Node* mem = in(MemNode::Memory);
duke@0 1908 Node* value = can_see_stored_value(mem,phase);
duke@0 1909 if( value && !phase->type(value)->higher_equal( _type ) ) {
kvn@3680 1910 Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(24)) );
kvn@3680 1911 return new (phase->C) RShiftINode(result, phase->intcon(24));
duke@0 1912 }
duke@0 1913 // Identity call will handle the case where truncation is not needed.
duke@0 1914 return LoadNode::Ideal(phase, can_reshape);
duke@0 1915 }
duke@0 1916
kvn@3007 1917 const Type* LoadBNode::Value(PhaseTransform *phase) const {
kvn@3007 1918 Node* mem = in(MemNode::Memory);
kvn@3007 1919 Node* value = can_see_stored_value(mem,phase);
kvn@3013 1920 if (value != NULL && value->is_Con() &&
kvn@3013 1921 !value->bottom_type()->higher_equal(_type)) {
kvn@3007 1922 // If the input to the store does not fit with the load's result type,
kvn@3007 1923 // it must be truncated. We can't delay until Ideal call since
kvn@3007 1924 // a singleton Value is needed for split_thru_phi optimization.
kvn@3007 1925 int con = value->get_int();
kvn@3007 1926 return TypeInt::make((con << 24) >> 24);
kvn@3007 1927 }
kvn@3007 1928 return LoadNode::Value(phase);
kvn@3007 1929 }
kvn@3007 1930
twisti@624 1931 //--------------------------LoadUBNode::Ideal-------------------------------------
twisti@624 1932 //
twisti@624 1933 // If the previous store is to the same address as this load,
twisti@624 1934 // and the value stored was larger than a byte, replace this load
twisti@624 1935 // with the value stored truncated to a byte. If no truncation is
twisti@624 1936 // needed, the replacement is done in LoadNode::Identity().
twisti@624 1937 //
twisti@624 1938 Node* LoadUBNode::Ideal(PhaseGVN* phase, bool can_reshape) {
twisti@624 1939 Node* mem = in(MemNode::Memory);
twisti@624 1940 Node* value = can_see_stored_value(mem, phase);
twisti@624 1941 if (value && !phase->type(value)->higher_equal(_type))
kvn@3680 1942 return new (phase->C) AndINode(value, phase->intcon(0xFF));
twisti@624 1943 // Identity call will handle the case where truncation is not needed.
twisti@624 1944 return LoadNode::Ideal(phase, can_reshape);
twisti@624 1945 }
twisti@624 1946
kvn@3007 1947 const Type* LoadUBNode::Value(PhaseTransform *phase) const {
kvn@3007 1948 Node* mem = in(MemNode::Memory);
kvn@3007 1949 Node* value = can_see_stored_value(mem,phase);
kvn@3013 1950 if (value != NULL && value->is_Con() &&
kvn@3013 1951 !value->bottom_type()->higher_equal(_type)) {
kvn@3007 1952 // If the input to the store does not fit with the load's result type,
kvn@3007 1953 // it must be truncated. We can't delay until Ideal call since
kvn@3007 1954 // a singleton Value is needed for split_thru_phi optimization.
kvn@3007 1955 int con = value->get_int();
kvn@3007 1956 return TypeInt::make(con & 0xFF);
kvn@3007 1957 }
kvn@3007 1958 return LoadNode::Value(phase);
kvn@3007 1959 }
kvn@3007 1960
twisti@558 1961 //--------------------------LoadUSNode::Ideal-------------------------------------
duke@0 1962 //
duke@0 1963 // If the previous store is to the same address as this load,
duke@0 1964 // and the value stored was larger than a char, replace this load
duke@0 1965 // with the value stored truncated to a char. If no truncation is
duke@0 1966 // needed, the replacement is done in LoadNode::Identity().
duke@0 1967 //
twisti@558 1968 Node *LoadUSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@0 1969 Node* mem = in(MemNode::Memory);
duke@0 1970 Node* value = can_see_stored_value(mem,phase);
duke@0 1971 if( value && !phase->type(value)->higher_equal( _type ) )
kvn@3680 1972 return new (phase->C) AndINode(value,phase->intcon(0xFFFF));
duke@0 1973 // Identity call will handle the case where truncation is not needed.
duke@0 1974 return LoadNode::Ideal(phase, can_reshape);
duke@0 1975 }
duke@0 1976
kvn@3007 1977 const Type* LoadUSNode::Value(PhaseTransform *phase) const {
kvn@3007 1978 Node* mem = in(MemNode::Memory);
kvn@3007 1979 Node* value = can_see_stored_value(mem,phase);
kvn@3013 1980 if (value != NULL && value->is_Con() &&
kvn@3013 1981 !value->bottom_type()->higher_equal(_type)) {
kvn@3007 1982 // If the input to the store does not fit with the load's result type,
kvn@3007 1983 // it must be truncated. We can't delay until Ideal call since
kvn@3007 1984 // a singleton Value is needed for split_thru_phi optimization.
kvn@3007 1985 int con = value->get_int();
kvn@3007 1986 return TypeInt::make(con & 0xFFFF);
kvn@3007 1987 }
kvn@3007 1988 return LoadNode::Value(phase);
kvn@3007 1989 }
kvn@3007 1990
duke@0 1991 //--------------------------LoadSNode::Ideal--------------------------------------
duke@0 1992 //
duke@0 1993 // If the previous store is to the same address as this load,
duke@0 1994 // and the value stored was larger than a short, replace this load
duke@0 1995 // with the value stored truncated to a short. If no truncation is
duke@0 1996 // needed, the replacement is done in LoadNode::Identity().
duke@0 1997 //
duke@0 1998 Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@0 1999 Node* mem = in(MemNode::Memory);
duke@0 2000 Node* value = can_see_stored_value(mem,phase);
duke@0 2001 if( value && !phase->type(value)->higher_equal( _type ) ) {
kvn@3680 2002 Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(16)) );
kvn@3680 2003 return new (phase->C) RShiftINode(result, phase->intcon(16));
duke@0 2004 }
duke@0 2005 // Identity call will handle the case where truncation is not needed.
duke@0 2006 return LoadNode::Ideal(phase, can_reshape);
duke@0 2007 }
duke@0 2008
kvn@3007 2009 const Type* LoadSNode::Value(PhaseTransform *phase) const {
kvn@3007 2010 Node* mem = in(MemNode::Memory);
kvn@3007 2011 Node* value = can_see_stored_value(mem,phase);
kvn@3013 2012 if (value != NULL && value->is_Con() &&
kvn@3013 2013 !value->bottom_type()->higher_equal(_type)) {
kvn@3007 2014 // If the input to the store does not fit with the load's result type,
kvn@3007 2015 // it must be truncated. We can't delay until Ideal call since
kvn@3007 2016 // a singleton Value is needed for split_thru_phi optimization.
kvn@3007 2017 int con = value->get_int();
kvn@3007 2018 return TypeInt::make((con << 16) >> 16);
kvn@3007 2019 }
kvn@3007 2020 return LoadNode::Value(phase);
kvn@3007 2021 }
kvn@3007 2022
duke@0 2023 //=============================================================================
kvn@164 2024 //----------------------------LoadKlassNode::make------------------------------
kvn@164 2025 // Polymorphic factory method:
kvn@164 2026 Node *LoadKlassNode::make( PhaseGVN& gvn, Node *mem, Node *adr, const TypePtr* at, const TypeKlassPtr *tk ) {
kvn@164 2027 Compile* C = gvn.C;
kvn@164 2028 Node *ctl = NULL;
kvn@164 2029 // sanity check the alias category against the created node type
coleenp@3602 2030 const TypePtr *adr_type = adr->bottom_type()->isa_ptr();
coleenp@3602 2031 assert(adr_type != NULL, "expecting TypeKlassPtr");
kvn@164 2032 #ifdef _LP64
roland@3724 2033 if (adr_type->is_ptr_to_narrowklass()) {
ehelin@5259 2034 assert(UseCompressedClassPointers, "no compressed klasses");
roland@3724 2035 Node* load_klass = gvn.transform(new (C) LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowklass()));
roland@3724 2036 return new (C) DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr());
kvn@168 2037 }
kvn@164 2038 #endif
roland@3724 2039 assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
kvn@3680 2040 return new (C) LoadKlassNode(ctl, mem, adr, at, tk);
kvn@164 2041 }
kvn@164 2042
duke@0 2043 //------------------------------Value------------------------------------------
duke@0 2044 const Type *LoadKlassNode::Value( PhaseTransform *phase ) const {
kvn@164 2045 return klass_value_common(phase);
kvn@164 2046 }
kvn@164 2047
kvn@164 2048 const Type *LoadNode::klass_value_common( PhaseTransform *phase ) const {
duke@0 2049 // Either input is TOP ==> the result is TOP
duke@0 2050 const Type *t1 = phase->type( in(MemNode::Memory) );
duke@0 2051 if (t1 == Type::TOP) return Type::TOP;
duke@0 2052 Node *adr = in(MemNode::Address);
duke@0 2053 const Type *t2 = phase->type( adr );
duke@0 2054 if (t2 == Type::TOP) return Type::TOP;
duke@0 2055 const TypePtr *tp = t2->is_ptr();
duke@0 2056 if (TypePtr::above_centerline(tp->ptr()) ||
duke@0 2057 tp->ptr() == TypePtr::Null) return Type::TOP;
duke@0 2058
duke@0 2059 // Return a more precise klass, if possible
duke@0 2060 const TypeInstPtr *tinst = tp->isa_instptr();
duke@0 2061 if (tinst != NULL) {
duke@0 2062 ciInstanceKlass* ik = tinst->klass()->as_instance_klass();
duke@0 2063 int offset = tinst->offset();
duke@0 2064 if (ik == phase->C->env()->Class_klass()
duke@0 2065 && (offset == java_lang_Class::klass_offset_in_bytes() ||
duke@0 2066 offset == java_lang_Class::array_klass_offset_in_bytes())) {
duke@0 2067 // We are loading a special hidden field from a Class mirror object,
duke@0 2068 // the field which points to the VM's Klass metaobject.
duke@0 2069 ciType* t = tinst->java_mirror_type();
duke@0 2070 // java_mirror_type returns non-null for compile-time Class constants.
duke@0 2071 if (t != NULL) {
duke@0 2072 // constant oop => constant klass
duke@0 2073 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
twisti@5738 2074 if (t->is_void()) {
twisti@5738 2075 // We cannot create a void array. Since void is a primitive type return null
twisti@5738 2076 // klass. Users of this result need to do a null check on the returned klass.
twisti@5738 2077 return TypePtr::NULL_PTR;
twisti@5738 2078 }
duke@0 2079 return TypeKlassPtr::make(ciArrayKlass::make(t));
duke@0 2080 }
duke@0 2081 if (!t->is_klass()) {
duke@0 2082 // a primitive Class (e.g., int.class) has NULL for a klass field
duke@0 2083 return TypePtr::NULL_PTR;
duke@0 2084 }
duke@0 2085 // (Folds up the 1st indirection in aClassConstant.getModifiers().)
duke@0 2086 return TypeKlassPtr::make(t->as_klass());
duke@0 2087 }
duke@0 2088 // non-constant mirror, so we can't tell what's going on
duke@0 2089 }
duke@0 2090 if( !ik->is_loaded() )
duke@0 2091 return _type; // Bail out if not loaded
duke@0 2092 if (offset == oopDesc::klass_offset_in_bytes()) {
duke@0 2093 if (tinst->klass_is_exact()) {
duke@0 2094 return TypeKlassPtr::make(ik);
duke@0 2095 }
duke@0 2096 // See if we can become precise: no subklasses and no interface
duke@0 2097 // (Note: We need to support verified interfaces.)
duke@0 2098 if (!ik->is_interface() && !ik->has_subklass()) {
duke@0 2099 //assert(!UseExactTypes, "this code should be useless with exact types");
duke@0 2100 // Add a dependence; if any subclass added we need to recompile
duke@0 2101 if (!ik->is_final()) {
duke@0 2102 // %%% should use stronger assert_unique_concrete_subtype instead
duke@0 2103 phase->C->dependencies()->assert_leaf_type(ik);
duke@0 2104 }
duke@0 2105 // Return precise klass
duke@0 2106 return TypeKlassPtr::make(ik);
duke@0 2107 }
duke@0 2108
duke@0 2109 // Return root of possible klass
duke@0 2110 return TypeKlassPtr::make(TypePtr::NotNull, ik, 0/*offset*/);
duke@0 2111 }
duke@0 2112 }
duke@0 2113
duke@0 2114 // Check for loading klass from an array
duke@0 2115 const TypeAryPtr *tary = tp->isa_aryptr();
duke@0 2116 if( tary != NULL ) {
duke@0 2117 ciKlass *tary_klass = tary->klass();
duke@0 2118 if (tary_klass != NULL // can be NULL when at BOTTOM or TOP
duke@0 2119 && tary->offset() == oopDesc::klass_offset_in_bytes()) {
duke@0 2120 if (tary->klass_is_exact()) {
duke@0 2121 return TypeKlassPtr::make(tary_klass);
duke@0 2122 }
duke@0 2123 ciArrayKlass *ak = tary->klass()->as_array_klass();
duke@0 2124 // If the klass is an object array, we defer the question to the
duke@0 2125 // array component klass.
duke@0 2126 if( ak->is_obj_array_klass() ) {
duke@0 2127 assert( ak->is_loaded(), "" );
duke@0 2128 ciKlass *base_k = ak->as_obj_array_klass()->base_element_klass();
duke@0 2129 if( base_k->is_loaded() && base_k->is_instance_klass() ) {
duke@0 2130 ciInstanceKlass* ik = base_k->as_instance_klass();
duke@0 2131 // See if we can become precise: no subklasses and no interface
duke@0 2132 if (!ik->is_interface() && !ik->has_subklass()) {
duke@0 2133 //assert(!UseExactTypes, "this code should be useless with exact types");
duke@0 2134 // Add a dependence; if any subclass added we need to recompile
duke@0 2135 if (!ik->is_final()) {
duke@0 2136 phase->C->dependencies()->assert_leaf_type(ik);
duke@0 2137 }
duke@0 2138 // Return precise array klass
duke@0 2139 return TypeKlassPtr::make(ak);
duke@0 2140 }
duke@0 2141 }
duke@0 2142 return TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
duke@0 2143 } else { // Found a type-array?
duke@0 2144 //assert(!UseExactTypes, "this code should be useless with exact types");
duke@0 2145 assert( ak->is_type_array_klass(), "" );
duke@0 2146 return TypeKlassPtr::make(ak); // These are always precise
duke@0 2147 }
duke@0 2148 }
duke@0 2149 }
duke@0 2150
duke@0 2151 // Check for loading klass from an array klass
duke@0 2152 const TypeKlassPtr *tkls = tp->isa_klassptr();
duke@0 2153 if (tkls != NULL && !StressReflectiveCode) {
duke@0 2154 ciKlass* klass = tkls->klass();
duke@0 2155 if( !klass->is_loaded() )
duke@0 2156 return _type; // Bail out if not loaded
duke@0 2157 if( klass->is_obj_array_klass() &&
coleenp@3707 2158 tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) {
duke@0 2159 ciKlass* elem = klass->as_obj_array_klass()->element_klass();
duke@0 2160 // // Always returning precise element type is incorrect,
duke@0 2161 // // e.g., element type could be object and array may contain strings
duke@0 2162 // return TypeKlassPtr::make(TypePtr::Constant, elem, 0);
duke@0 2163
duke@0 2164 // The array's TypeKlassPtr was declared 'precise' or 'not precise'
duke@0 2165 // according to the element type's subclassing.
duke@0 2166 return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/);
duke@0 2167 }
duke@0 2168 if( klass->is_instance_klass() && tkls->klass_is_exact() &&
stefank@2956 2169 tkls->offset() == in_bytes(Klass::super_offset())) {
duke@0 2170 ciKlass* sup = klass->as_instance_klass()->super();
duke@0 2171 // The field is Klass::_super. Return its (constant) value.
duke@0 2172 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
duke@0 2173 return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR;
duke@0 2174 }
duke@0 2175 }
duke@0 2176
duke@0 2177 // Bailout case
duke@0 2178 return LoadNode::Value(phase);
duke@0 2179 }
duke@0 2180
duke@0 2181 //------------------------------Identity---------------------------------------
duke@0 2182 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k.
duke@0 2183 // Also feed through the klass in Allocate(...klass...)._klass.
duke@0 2184 Node* LoadKlassNode::Identity( PhaseTransform *phase ) {
kvn@164 2185 return klass_identity_common(phase);
kvn@164 2186 }
kvn@164 2187
kvn@164 2188 Node* LoadNode::klass_identity_common(PhaseTransform *phase ) {
duke@0 2189 Node* x = LoadNode::Identity(phase);
duke@0 2190 if (x != this) return x;
duke@0 2191
duke@0 2192 // Take apart the address into an oop and and offset.
duke@0 2193 // Return 'this' if we cannot.
duke@0 2194 Node* adr = in(MemNode::Address);
duke@0 2195 intptr_t offset = 0;
duke@0 2196 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
duke@0 2197 if (base == NULL) return this;
duke@0 2198 const TypeOopPtr* toop = phase->type(adr)->isa_oopptr();
duke@0 2199 if (toop == NULL) return this;
duke@0 2200
duke@0 2201 // We can fetch the klass directly through an AllocateNode.
duke@0 2202 // This works even if the klass is not constant (clone or newArray).
duke@0 2203 if (offset == oopDesc::klass_offset_in_bytes()) {
duke@0 2204 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
duke@0 2205 if (allocated_klass != NULL) {
duke@0 2206 return allocated_klass;
duke@0 2207 }
duke@0 2208 }
duke@0 2209
coleenp@3602 2210 // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*.
coleenp@3707 2211 // Simplify ak.component_mirror.array_klass to plain ak, ak an ArrayKlass.
duke@0 2212 // See inline_native_Class_query for occurrences of these patterns.
duke@0 2213 // Java Example: x.getClass().isAssignableFrom(y)
duke@0 2214 // Java Example: Array.newInstance(x.getClass().getComponentType(), n)
duke@0 2215 //
duke@0 2216 // This improves reflective code, often making the Class
duke@0 2217 // mirror go completely dead. (Current exception: Class
duke@0 2218 // mirrors may appear in debug info, but we could clean them out by
coleenp@3602 2219 // introducing a new debug info operator for Klass*.java_mirror).
duke@0 2220 if (toop->isa_instptr() && toop->klass() == phase->C->env()->Class_klass()
duke@0 2221 && (offset == java_lang_Class::klass_offset_in_bytes() ||
duke@0 2222 offset == java_lang_Class::array_klass_offset_in_bytes())) {
duke@0 2223 // We are loading a special hidden field from a Class mirror,
coleenp@3707 2224 // the field which points to its Klass or ArrayKlass metaobject.
duke@0 2225 if (base->is_Load()) {
duke@0 2226 Node* adr2 = base->in(MemNode::Address);
duke@0 2227 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
duke@0 2228 if (tkls != NULL && !tkls->empty()
duke@0 2229 && (tkls->klass()->is_instance_klass() ||
duke@0 2230 tkls->klass()->is_array_klass())
duke@0 2231 && adr2->is_AddP()
duke@0 2232 ) {
stefank@2956 2233 int mirror_field = in_bytes(Klass::java_mirror_offset());
duke@0 2234 if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
coleenp@3707 2235 mirror_field = in_bytes(ArrayKlass::component_mirror_offset());
duke@0 2236 }
stefank@2956 2237 if (tkls->offset() == mirror_field) {
duke@0 2238 return adr2->in(AddPNode::Base);
duke@0 2239 }
duke@0 2240 }
duke@0 2241 }
duke@0 2242 }
duke@0 2243
duke@0 2244 return this;
duke@0 2245 }
duke@0 2246
kvn@164 2247
kvn@164 2248 //------------------------------Value------------------------------------------
kvn@164 2249 const Type *LoadNKlassNode::Value( PhaseTransform *phase ) const {
kvn@164 2250 const Type *t = klass_value_common(phase);
kvn@221 2251 if (t == Type::TOP)
kvn@221 2252 return t;
kvn@221 2253
roland@3724 2254 return t->make_narrowklass();
kvn@164 2255 }
kvn@164 2256
kvn@164 2257 //------------------------------Identity---------------------------------------
kvn@164 2258 // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k.
kvn@164 2259 // Also feed through the klass in Allocate(...klass...)._klass.
kvn@164 2260 Node* LoadNKlassNode::Identity( PhaseTransform *phase ) {
kvn@164 2261 Node *x = klass_identity_common(phase);
kvn@164 2262
kvn@164 2263 const Type *t = phase->type( x );
kvn@164 2264 if( t == Type::TOP ) return x;
roland@3724 2265 if( t->isa_narrowklass()) return x;
roland@3724 2266 assert (!t->isa_narrowoop(), "no narrow oop here");
roland@3724 2267
roland@3724 2268 return phase->transform(new (phase->C) EncodePKlassNode(x, t->make_narrowklass()));
kvn@164 2269 }
kvn@164 2270
duke@0 2271 //------------------------------Value-----------------------------------------
duke@0 2272 const Type *LoadRangeNode::Value( PhaseTransform *phase ) const {
duke@0 2273 // Either input is TOP ==> the result is TOP
duke@0 2274 const Type *t1 = phase->type( in(MemNode::Memory) );
duke@0 2275 if( t1 == Type::TOP ) return Type::TOP;
duke@0 2276 Node *adr = in(MemNode::Address);
duke@0 2277 const Type *t2 = phase->type( adr );
duke@0 2278 if( t2 == Type::TOP ) return Type::TOP;
duke@0 2279 const TypePtr *tp = t2->is_ptr();
duke@0 2280 if (TypePtr::above_centerline(tp->ptr())) return Type::TOP;
duke@0 2281 const TypeAryPtr *tap = tp->isa_aryptr();
duke@0 2282 if( !tap ) return _type;
duke@0 2283 return tap->size();
duke@0 2284 }
duke@0 2285
rasbold@366 2286 //-------------------------------Ideal---------------------------------------
rasbold@366 2287 // Feed through the length in AllocateArray(...length...)._length.
rasbold@366 2288 Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) {
rasbold@366 2289 Node* p = MemNode::Ideal_common(phase, can_reshape);
rasbold@366 2290 if (p) return (p == NodeSentinel) ? NULL : p;
rasbold@366 2291
rasbold@366 2292 // Take apart the address into an oop and and offset.
rasbold@366 2293 // Return 'this' if we cannot.
rasbold@366 2294 Node* adr = in(MemNode::Address);
rasbold@366 2295 intptr_t offset = 0;
rasbold@366 2296 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
rasbold@366 2297 if (base == NULL) return NULL;
rasbold@366 2298 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
rasbold@366 2299 if (tary == NULL) return NULL;
rasbold@366 2300
rasbold@366 2301 // We can fetch the length directly through an AllocateArrayNode.
rasbold@366 2302 // This works even if the length is not constant (clone or newArray).
rasbold@366 2303 if (offset == arrayOopDesc::length_offset_in_bytes()) {
rasbold@366 2304 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
rasbold@366 2305 if (alloc != NULL) {
rasbold@366 2306 Node* allocated_length = alloc->Ideal_length();
rasbold@366 2307 Node* len = alloc->make_ideal_length(tary, phase);
rasbold@366 2308 if (allocated_length != len) {
rasbold@366 2309 // New CastII improves on this.
rasbold@366 2310 return len;
rasbold@366 2311 }
rasbold@366 2312 }
rasbold@366 2313 }
rasbold@366 2314
rasbold@366 2315 return NULL;
rasbold@366 2316 }
rasbold@366 2317
duke@0 2318 //------------------------------Identity---------------------------------------
duke@0 2319 // Feed through the length in AllocateArray(...length...)._length.
duke@0 2320 Node* LoadRangeNode::Identity( PhaseTransform *phase ) {
duke@0 2321 Node* x = LoadINode::Identity(phase);
duke@0 2322 if (x != this) return x;
duke@0 2323
duke@0 2324 // Take apart the address into an oop and and offset.
duke@0 2325 // Return 'this' if we cannot.
duke@0 2326 Node* adr = in(MemNode::Address);
duke@0 2327 intptr_t offset = 0;
duke@0 2328 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
duke@0 2329 if (base == NULL) return this;
duke@0 2330 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
duke@0 2331 if (tary == NULL) return this;
duke@0 2332
duke@0 2333 // We can fetch the length directly through an AllocateArrayNode.
duke@0 2334 // This works even if the length is not constant (clone or newArray).
duke@0 2335 if (offset == arrayOopDesc::length_offset_in_bytes()) {
rasbold@366 2336 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
rasbold@366 2337 if (alloc != NULL) {
rasbold@366 2338 Node* allocated_length = alloc->Ideal_length();
rasbold@366 2339 // Do not allow make_ideal_length to allocate a CastII node.
rasbold@366 2340 Node* len = alloc->make_ideal_length(tary, phase, false);
rasbold@366 2341 if (allocated_length == len) {
rasbold@366 2342 // Return allocated_length only if it would not be improved by a CastII.
rasbold@366 2343 return allocated_length;
rasbold@366 2344 }
duke@0 2345 }
duke@0 2346 }
duke@0 2347
duke@0 2348 return this;
duke@0 2349
duke@0 2350 }
rasbold@366 2351
duke@0 2352 //=============================================================================
duke@0 2353 //---------------------------StoreNode::make-----------------------------------
duke@0 2354 // Polymorphic factory method:
coleenp@113 2355 StoreNode* StoreNode::make( PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt ) {
coleenp@113 2356 Compile* C = gvn.C;
kvn@1529 2357 assert( C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
kvn@1529 2358 ctl != NULL, "raw memory operations should have control edge");
coleenp@113 2359
duke@0 2360 switch (bt) {
duke@0 2361 case T_BOOLEAN:
kvn@3680 2362 case T_BYTE: return new (C) StoreBNode(ctl, mem, adr, adr_type, val);
kvn@3680 2363 case T_INT: return new (C) StoreINode(ctl, mem, adr, adr_type, val);
duke@0 2364 case T_CHAR:
kvn@3680 2365 case T_SHORT: return new (C) StoreCNode(ctl, mem, adr, adr_type, val);
kvn@3680 2366 case T_LONG: return new (C) StoreLNode(ctl, mem, adr, adr_type, val);
kvn@3680 2367 case T_FLOAT: return new (C) StoreFNode(ctl, mem, adr, adr_type, val);
kvn@3680 2368 case T_DOUBLE: return new (C) StoreDNode(ctl, mem, adr, adr_type, val);
coleenp@3602 2369 case T_METADATA:
duke@0 2370 case T_ADDRESS:
coleenp@113 2371 case T_OBJECT:
coleenp@113 2372 #ifdef _LP64
roland@3724 2373 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
kvn@3680 2374 val = gvn.transform(new (C) EncodePNode(val, val->bottom_type()->make_narrowoop()));
kvn@3680 2375 return new (C) StoreNNode(ctl, mem, adr, adr_type, val);
roland@3724 2376 } else if (adr->bottom_type()->is_ptr_to_narrowklass() ||
ehelin@5259 2377 (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() &&
roland@3724 2378 adr->bottom_type()->isa_rawptr())) {
roland@3724 2379 val = gvn.transform(new (C) EncodePKlassNode(val, val->bottom_type()->make_narrowklass()));
roland@3724 2380 return new (C) StoreNKlassNode(ctl, mem, adr, adr_type, val);
roland@3724 2381 }
coleenp@113 2382 #endif
kvn@221 2383 {
kvn@3680 2384 return new (C) StorePNode(ctl, mem, adr, adr_type, val);
kvn@221 2385 }
duke@0 2386 }
duke@0 2387 ShouldNotReachHere();
duke@0 2388 return (StoreNode*)NULL;
duke@0 2389 }
duke@0 2390
duke@0 2391 StoreLNode* StoreLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val) {
duke@0 2392 bool require_atomic = true;
kvn@3680 2393 return new (C) StoreLNode(ctl, mem, adr, adr_type, val, require_atomic);
duke@0 2394 }
duke@0 2395
duke@0 2396
duke@0 2397 //--------------------------bottom_type----------------------------------------
duke@0 2398 const Type *StoreNode::bottom_type() const {
duke@0 2399 return Type::MEMORY;
duke@0 2400 }
duke@0 2401
duke@0 2402 //------------------------------hash-------------------------------------------
duke@0 2403 uint StoreNode::hash() const {
duke@0 2404 // unroll addition of interesting fields
duke@0 2405 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
duke@0 2406
duke@0 2407 // Since they are not commoned, do not hash them:
duke@0 2408 return NO_HASH;
duke@0 2409 }
duke@0 2410
duke@0 2411 //------------------------------Ideal------------------------------------------
duke@0 2412 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x).
duke@0 2413 // When a store immediately follows a relevant allocation/initialization,
duke@0 2414 // try to capture it into the initialization, or hoist it above.
duke@0 2415 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) {
duke@0 2416 Node* p = MemNode::Ideal_common(phase, can_reshape);
duke@0 2417 if (p) return (p == NodeSentinel) ? NULL : p;
duke@0 2418
duke@0 2419 Node* mem = in(MemNode::Memory);
duke@0 2420 Node* address = in(MemNode::Address);
duke@0 2421
never@2345 2422 // Back-to-back stores to same address? Fold em up. Generally
never@2345 2423 // unsafe if I have intervening uses... Also disallowed for StoreCM
never@2345 2424 // since they must follow each StoreP operation. Redundant StoreCMs
never@2345 2425 // are eliminated just before matching in final_graph_reshape.
kvn@2972 2426 if (mem->is_Store() && mem->in(MemNode::Address)->eqv_uncast(address) &&
never@2345 2427 mem->Opcode() != Op_StoreCM) {
duke@0 2428 // Looking at a dead closed cycle of memory?
duke@0 2429 assert(mem != mem->in(MemNode::Memory), "dead loop in StoreNode::Ideal");
duke@0 2430
duke@0 2431 assert(Opcode() == mem->Opcode() ||
duke@0 2432 phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw,
duke@0 2433 "no mismatched stores, except on raw memory");
duke@0 2434
duke@0 2435 if (mem->outcnt() == 1 && // check for intervening uses
duke@0 2436 mem->as_Store()->memory_size() <= this->memory_size()) {
duke@0 2437 // If anybody other than 'this' uses 'mem', we cannot fold 'mem' away.
duke@0 2438 // For example, 'mem' might be the final state at a conditional return.
duke@0 2439 // Or, 'mem' might be used by some node which is live at the same time
duke@0 2440 // 'this' is live, which might be unschedulable. So, require exactly
duke@0 2441 // ONE user, the 'this' store, until such time as we clone 'mem' for
duke@0 2442 // each of 'mem's uses (thus making the exactly-1-user-rule hold true).
duke@0 2443 if (can_reshape) { // (%%% is this an anachronism?)
duke@0 2444 set_req_X(MemNode::Memory, mem->in(MemNode::Memory),
duke@0 2445 phase->is_IterGVN());
duke@0 2446 } else {
duke@0 2447 // It's OK to do this in the parser, since DU info is always accurate,
duke@0 2448 // and the parser always refers to nodes via SafePointNode maps.
duke@0 2449 set_req(MemNode::Memory, mem->in(MemNode::Memory));
duke@0 2450 }
duke@0 2451 return this;
duke@0 2452 }
duke@0 2453 }
duke@0 2454
duke@0 2455 // Capture an unaliased, unconditional, simple store into an initializer.
duke@0 2456 // Or, if it is independent of the allocation, hoist it above the allocation.
duke@0 2457 if (ReduceFieldZeroing && /*can_reshape &&*/
duke@0 2458 mem->is_Proj() && mem->in(0)->is_Initialize()) {
duke@0 2459 InitializeNode* init = mem->in(0)->as_Initialize();
roland@4222 2460 intptr_t offset = init->can_capture_store(this, phase, can_reshape);
duke@0 2461 if (offset > 0) {
roland@4222 2462 Node* moved = init->capture_store(this, offset, phase, can_reshape);
duke@0 2463 // If the InitializeNode captured me, it made a raw copy of me,
duke@0 2464 // and I need to disappear.
duke@0 2465 if (moved != NULL) {
duke@0 2466 // %%% hack to ensure that Ideal returns a new node:
duke@0 2467 mem = MergeMemNode::make(phase->C, mem);
duke@0 2468 return mem; // fold me away
duke@0 2469 }
duke@0 2470 }
duke@0 2471 }
duke@0 2472
duke@0 2473 return NULL; // No further progress
duke@0 2474 }
duke@0 2475
duke@0 2476 //------------------------------Value-----------------------------------------
duke@0 2477 const Type *StoreNode::Value( PhaseTransform *phase ) const {
duke@0 2478 // Either input is TOP ==> the result is TOP
duke@0 2479 const Type *t1 = phase->type( in(MemNode::Memory) );
duke@0 2480 if( t1 == Type::TOP ) return Type::TOP;
duke@0 2481 const Type *t2 = phase->type( in(MemNode::Address) );
duke@0 2482 if( t2 == Type::TOP ) return Type::TOP;
duke@0 2483 const Type *t3 = phase->type( in(MemNode::ValueIn) );
duke@0 2484 if( t3 == Type::TOP ) return Type::TOP;
duke@0 2485 return Type::MEMORY;
duke@0 2486 }
duke@0 2487
duke@0 2488 //------------------------------Identity---------------------------------------
duke@0 2489 // Remove redundant stores:
duke@0 2490 // Store(m, p, Load(m, p)) changes to m.
duke@0 2491 // Store(, p, x) -> Store(m, p, x) changes to Store(m, p, x).
duke@0 2492 Node *StoreNode::Identity( PhaseTransform *phase ) {
duke@0 2493 Node* mem = in(MemNode::Memory);
duke@0 2494 Node* adr = in(MemNode::Address);
duke@0 2495 Node* val = in(MemNode::ValueIn);
duke@0 2496
duke@0 2497 // Load then Store? Then the Store is useless
duke@0 2498 if (val->is_Load() &&
kvn@2972 2499 val->in(MemNode::Address)->eqv_uncast(adr) &&
kvn@2972 2500 val->in(MemNode::Memory )->eqv_uncast(mem) &&
duke@0 2501 val->as_Load()->store_Opcode() == Opcode()) {
duke@0 2502 return mem;
duke@0 2503 }
duke@0 2504
duke@0 2505 // Two stores in a row of the same value?
duke@0 2506 if (mem->is_Store() &&
kvn@2972 2507 mem->in(MemNode::Address)->eqv_uncast(adr) &&
kvn@2972 2508 mem->in(MemNode::ValueIn)->eqv_uncast(val) &&
duke@0 2509 mem->Opcode() == Opcode()) {
duke@0 2510 return mem;
duke@0 2511 }
duke@0 2512
duke@0 2513 // Store of zero anywhere into a freshly-allocated object?
duke@0 2514 // Then the store is useless.
duke@0 2515 // (It must already have been captured by the InitializeNode.)
duke@0 2516 if (ReduceFieldZeroing && phase->type(val)->is_zero_type()) {
duke@0 2517 // a newly allocated object is already all-zeroes everywhere
duke@0 2518 if (mem->is_Proj() && mem->in(0)->is_Allocate()) {
duke@0 2519 return mem;
duke@0 2520 }
duke@0 2521
duke@0 2522 // the store may also apply to zero-bits in an earlier object
duke@0 2523 Node* prev_mem = find_previous_store(phase);
duke@0 2524 // Steps (a), (b): Walk past independent stores to find an exact match.
duke@0 2525 if (prev_mem != NULL) {
duke@0 2526 Node* prev_val = can_see_stored_value(prev_mem, phase);
duke@0 2527 if (prev_val != NULL && phase->eqv(prev_val, val)) {
duke@0 2528 // prev_val and val might differ by a cast; it would be good
duke@0 2529 // to keep the more informative of the two.
duke@0 2530 return mem;
duke@0 2531 }
duke@0 2532 }
duke@0 2533 }
duke@0 2534
duke@0 2535 return this;
duke@0 2536 }
duke@0 2537
duke@0 2538 //------------------------------match_edge-------------------------------------
duke@0 2539 // Do we Match on this edge index or not? Match only memory & value
duke@0 2540 uint StoreNode::match_edge(uint idx) const {
duke@0 2541 return idx == MemNode::Address || idx == MemNode::ValueIn;
duke@0 2542 }
duke@0 2543
duke@0 2544 //------------------------------cmp--------------------------------------------
duke@0 2545 // Do not common stores up together. They generally have to be split
duke@0 2546 // back up anyways, so do not bother.
duke@0 2547 uint StoreNode::cmp( const Node &n ) const {
duke@0 2548 return (&n == this); // Always fail except on self
duke@0 2549 }
duke@0 2550
duke@0 2551 //------------------------------Ideal_masked_input-----------------------------
duke@0 2552 // Check for a useless mask before a partial-word store
duke@0 2553 // (StoreB ... (AndI valIn conIa) )
duke@0 2554 // If (conIa & mask == mask) this simplifies to
duke@0 2555 // (StoreB ... (valIn) )
duke@0 2556 Node *StoreNode::Ideal_masked_input(PhaseGVN *phase, uint mask) {
duke@0 2557 Node *val = in(MemNode::ValueIn);
duke@0 2558 if( val->Opcode() == Op_AndI ) {
duke@0 2559 const TypeInt *t = phase->type( val->in(2) )->isa_int();
duke@0 2560 if( t && t->is_con() && (t->get_con() & mask) == mask ) {
duke@0 2561 set_req(MemNode::ValueIn, val->in(1));
duke@0 2562 return this;
duke@0 2563 }
duke@0 2564 }
duke@0 2565 return NULL;
duke@0 2566 }
duke@0 2567
duke@0 2568
duke@0 2569 //------------------------------Ideal_sign_extended_input----------------------
duke@0 2570 // Check for useless sign-extension before a partial-word store
duke@0 2571 // (StoreB ... (RShiftI _ (LShiftI _ valIn conIL ) conIR) )
duke@0 2572 // If (conIL == conIR && conIR <= num_bits) this simplifies to
duke@0 2573 // (StoreB ... (valIn) )
duke@0 2574 Node *StoreNode::Ideal_sign_extended_input(PhaseGVN *phase, int num_bits) {
duke@0 2575 Node *val = in(MemNode::ValueIn);
duke@0 2576 if( val->Opcode() == Op_RShiftI ) {
duke@0 2577 const TypeInt *t = phase->type( val->in(2) )->isa_int();
duke@0 2578 if( t && t->is_con() && (t->get_con() <= num_bits) ) {
duke@0 2579 Node *shl = val->in(1);
duke@0 2580 if( shl->Opcode() == Op_LShiftI ) {
duke@0 2581 const TypeInt *t2 = phase->type( shl->in(2) )->isa_int();
duke@0 2582 if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) {
duke@0 2583 set_req(MemNode::ValueIn, shl->in(1));
duke@0 2584 return this;
duke@0 2585 }
duke@0 2586 }
duke@0 2587 }
duke@0 2588 }
duke@0 2589 return NULL;
duke@0 2590 }
duke@0 2591
duke@0 2592 //------------------------------value_never_loaded-----------------------------------
duke@0 2593 // Determine whether there are any possible loads of the value stored.
duke@0 2594 // For simplicity, we actually check if there are any loads from the
duke@0 2595 // address stored to, not just for loads of the value stored by this node.
duke@0 2596 //
duke@0 2597 bool StoreNode::value_never_loaded( PhaseTransform *phase) const {
duke@0 2598 Node *adr = in(Address);
duke@0 2599 const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr();
duke@0 2600 if (adr_oop == NULL)
duke@0 2601 return false;
kvn@223 2602 if (!adr_oop->is_known_instance_field())
duke@0 2603 return false; // if not a distinct instance, there may be aliases of the address
duke@0 2604 for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) {
duke@0 2605 Node *use = adr->fast_out(i);
duke@0 2606 int opc = use->Opcode();
duke@0 2607 if (use->is_Load() || use->is_LoadStore()) {
duke@0 2608 return false;
duke@0 2609 }
duke@0 2610 }
duke@0 2611 return true;
duke@0 2612 }
duke@0 2613
duke@0 2614 //=============================================================================
duke@0 2615 //------------------------------Ideal------------------------------------------
duke@0 2616 // If the store is from an AND mask that leaves the low bits untouched, then
duke@0 2617 // we can skip the AND operation. If the store is from a sign-extension
duke@0 2618 // (a left shift, then right shift) we can skip both.
duke@0 2619 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@0 2620 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF);
duke@0 2621 if( progress != NULL ) return progress;
duke@0 2622
duke@0 2623 progress = StoreNode::Ideal_sign_extended_input(phase, 24);
duke@0 2624 if( progress != NULL ) return progress;
duke@0 2625
duke@0 2626 // Finally check the default case
duke@0 2627 return StoreNode::Ideal(phase, can_reshape);
duke@0 2628 }
duke@0 2629
duke@0 2630 //=============================================================================
duke@0 2631 //------------------------------Ideal------------------------------------------
duke@0 2632 // If the store is from an AND mask that leaves the low bits untouched, then
duke@0 2633 // we can skip the AND operation
duke@0 2634 Node *StoreCNode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@0 2635 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFFFF);
duke@0 2636 if( progress != NULL ) return progress;
duke@0 2637
duke@0 2638 progress = StoreNode::Ideal_sign_extended_input(phase, 16);
duke@0 2639 if( progress != NULL ) return progress;
duke@0 2640
duke@0 2641 // Finally check the default case
duke@0 2642 return StoreNode::Ideal(phase, can_reshape);
duke@0 2643 }
duke@0 2644
duke@0 2645 //=============================================================================
duke@0 2646 //------------------------------Identity---------------------------------------
duke@0 2647 Node *StoreCMNode::Identity( PhaseTransform *phase ) {
duke@0 2648 // No need to card mark when storing a null ptr
duke@0 2649 Node* my_store = in(MemNode::OopStore);
duke@0 2650 if (my_store->is_Store()) {
duke@0 2651 const Type *t1 = phase->type( my_store->in(MemNode::ValueIn) );
duke@0 2652 if( t1 == TypePtr::NULL_PTR ) {
duke@0 2653 return in(MemNode::Memory);
duke@0 2654 }
duke@0 2655 }
duke@0 2656 return this;
duke@0 2657 }
duke@0 2658
cfang@985 2659 //=============================================================================
cfang@985 2660 //------------------------------Ideal---------------------------------------
cfang@985 2661 Node *StoreCMNode::Ideal(PhaseGVN *phase, bool can_reshape){
cfang@985 2662 Node* progress = StoreNode::Ideal(phase, can_reshape);
cfang@985 2663 if (progress != NULL) return progress;
cfang@985 2664
cfang@985 2665 Node* my_store = in(MemNode::OopStore);
cfang@985 2666 if (my_store->is_MergeMem()) {
cfang@985 2667 Node* mem = my_store->as_MergeMem()->memory_at(oop_alias_idx());
cfang@985 2668 set_req(MemNode::OopStore, mem);
cfang@985 2669 return this;
cfang@985 2670 }
cfang@985 2671
cfang@985 2672 return NULL;
cfang@985 2673 }
cfang@985 2674
duke@0 2675 //------------------------------Value-----------------------------------------
duke@0 2676 const Type *StoreCMNode::Value( PhaseTransform *phase ) const {
kvn@43 2677 // Either input is TOP ==> the result is TOP
kvn@43 2678 const Type *t = phase->type( in(MemNode::Memory) );
kvn@43 2679 if( t == Type::TOP ) return Type::TOP;
kvn@43 2680 t = phase->type( in(MemNode::Address) );
kvn@43 2681 if( t == Type::TOP ) return Type::TOP;
kvn@43 2682 t = phase->type( in(MemNode::ValueIn) );
kvn@43 2683 if( t == Type::TOP ) return Type::TOP;
duke@0 2684 // If extra input is TOP ==> the result is TOP
kvn@43 2685 t = phase->type( in(MemNode::OopStore) );
kvn@43 2686 if( t == Type::TOP ) return Type::TOP;
duke@0 2687
duke@0 2688 return StoreNode::Value( phase );
duke@0 2689 }
duke@0 2690
duke@0 2691
duke@0 2692 //=============================================================================
duke@0 2693 //----------------------------------SCMemProjNode------------------------------
duke@0 2694 const Type * SCMemProjNode::Value( PhaseTransform *phase ) const
duke@0 2695 {
duke@0 2696 return bottom_type();
duke@0 2697 }
duke@0 2698
duke@0 2699 //=============================================================================
roland@3671 2700 //----------------------------------LoadStoreNode------------------------------
roland@3671 2701 LoadStoreNode::LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required )
roland@3671 2702 : Node(required),
roland@3671 2703 _type(rt),
roland@3671 2704 _adr_type(at)
roland@3671 2705 {
duke@0 2706 init_req(MemNode::Control, c );
duke@0 2707 init_req(MemNode::Memory , mem);
duke@0 2708 init_req(MemNode::Address, adr);
duke@0 2709 init_req(MemNode::ValueIn, val);
duke@0 2710 init_class_id(Class_LoadStore);
roland@3671 2711 }
roland@3671 2712
roland@3671 2713 uint LoadStoreNode::ideal_reg() const {
roland@3671 2714 return _type->ideal_reg();
roland@3671 2715 }
roland@3671 2716
roland@3671 2717 bool LoadStoreNode::result_not_used() const {
roland@3671 2718 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
roland@3671 2719 Node *x = fast_out(i);
roland@3671 2720 if (x->Opcode() == Op_SCMemProj) continue;
roland@3671 2721 return false;
roland@3671 2722 }
roland@3671 2723 return true;
roland@3671 2724 }
roland@3671 2725
roland@3671 2726 uint LoadStoreNode::size_of() const { return sizeof(*this); }
roland@3671 2727
roland@3671 2728 //=============================================================================
roland@3671 2729 //----------------------------------LoadStoreConditionalNode--------------------
roland@3671 2730 LoadStoreConditionalNode::LoadStoreConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex ) : LoadStoreNode(c, mem, adr, val, NULL, TypeInt::BOOL, 5) {
roland@3671 2731 init_req(ExpectedIn, ex );
duke@0 2732 }
duke@0 2733
duke@0 2734 //=============================================================================
duke@0 2735 //-------------------------------adr_type--------------------------------------
duke@0 2736 // Do we Match on this edge index or not? Do not match memory
duke@0 2737 const TypePtr* ClearArrayNode::adr_type() const {
duke@0 2738 Node *adr = in(3);
duke@0 2739 return MemNode::calculate_adr_type(adr->bottom_type());
duke@0 2740 }
duke@0 2741
duke@0 2742 //------------------------------match_edge-------------------------------------
duke@0 2743 // Do we Match on this edge index or not? Do not match memory
duke@0 2744 uint ClearArrayNode::match_edge(uint idx) const {
duke@0 2745 return idx > 1;
duke@0 2746 }
duke@0 2747
duke@0 2748 //------------------------------Identity---------------------------------------
duke@0 2749 // Clearing a zero length array does nothing
duke@0 2750 Node *ClearArrayNode::Identity( PhaseTransform *phase ) {
never@68 2751 return phase->type(in(2))->higher_equal(TypeX::ZERO) ? in(1) : this;
duke@0 2752 }
duke@0 2753
duke@0 2754 //------------------------------Idealize---------------------------------------
duke@0 2755 // Clearing a short array is faster with stores
duke@0 2756 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){
duke@0 2757 const int unit = BytesPerLong;
duke@0 2758 const TypeX* t = phase->type(in(2))->isa_intptr_t();
duke@0 2759 if (!t) return NULL;
duke@0 2760 if (!t->is_con()) return NULL;
duke@0 2761 intptr_t raw_count = t->get_con();
duke@0 2762 intptr_t size = raw_count;
duke@0 2763 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
duke@0 2764 // Clearing nothing uses the Identity call.
duke@0 2765 // Negative clears are possible on dead ClearArrays
duke@0 2766 // (see jck test stmt114.stmt11402.val).
duke@0 2767 if (size <= 0 || size % unit != 0) return NULL;
duke@0 2768 intptr_t count = size / unit;
duke@0 2769 // Length too long; use fast hardware clear
duke@0 2770 if (size > Matcher::init_array_short_size) return NULL;
duke@0 2771 Node *mem = in(1);
duke@0 2772 if( phase->type(mem)==Type::TOP ) return NULL;
duke@0 2773 Node *adr = in(3);
duke@0 2774 const Type* at = phase->type(adr);
duke@0 2775 if( at==Type::TOP ) return NULL;
duke@0 2776 const TypePtr* atp = at->isa_ptr();
duke@0 2777 // adjust atp to be the correct array element address type
duke@0 2778 if (atp == NULL) atp = TypePtr::BOTTOM;
duke@0 2779 else atp = atp->add_offset(Type::OffsetBot);
duke@0 2780 // Get base for derived pointer purposes
duke@0 2781 if( adr->Opcode() != Op_AddP ) Unimplemented();
duke@0 2782 Node *base = adr->in(1);
duke@0 2783
duke@0 2784 Node *zero = phase->makecon(TypeLong::ZERO);
duke@0 2785 Node *off = phase->MakeConX(BytesPerLong);
kvn@3680 2786 mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero);
duke@0 2787 count--;
duke@0 2788 while( count-- ) {
duke@0 2789 mem = phase->transform(mem);
kvn@3680 2790 adr = phase->transform(new (phase->C) AddPNode(base,adr,off));
kvn@3680 2791 mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero);
duke@0 2792 }
duke@0 2793 return mem;
duke@0 2794 }
duke@0 2795
kvn@1100 2796 //----------------------------step_through----------------------------------
kvn@1100 2797 // Return allocation input memory edge if it is different instance
kvn@1100 2798 // or itself if it is the one we are looking for.
kvn@1100 2799 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseTransform* phase) {
kvn@1100 2800 Node* n = *np;
kvn@1100 2801 assert(n->is_ClearArray(), "sanity");
kvn@1100 2802 intptr_t offset;
kvn@1100 2803 AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset);
kvn@1100 2804 // This method is called only before Allocate nodes are expanded during
kvn@1100 2805 // macro nodes expansion. Before that ClearArray nodes are only generated
kvn@1100 2806 // in LibraryCallKit::generate_arraycopy() which follows allocations.
kvn@1100 2807 assert(alloc != NULL, "should have allocation");
kvn@1100 2808 if (alloc->_idx == instance_id) {
kvn@1100 2809 // Can not bypass initialization of the instance we are looking for.
kvn@1100 2810 return false;
kvn@1100 2811 }
kvn@1100 2812 // Otherwise skip it.
kvn@1100 2813 InitializeNode* init = alloc->initialization();
kvn@1100 2814 if (init != NULL)
kvn@1100 2815 *np = init->in(TypeFunc::Memory);
kvn@1100 2816 else
kvn@1100 2817 *np = alloc->in(TypeFunc::Memory);
kvn@1100 2818 return true;
kvn@1100 2819 }
kvn@1100 2820
duke@0 2821 //----------------------------clear_memory-------------------------------------
duke@0 2822 // Generate code to initialize object storage to zero.
duke@0 2823 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
duke@0 2824 intptr_t start_offset,
duke@0 2825 Node* end_offset,
duke@0 2826 PhaseGVN* phase) {
duke@0 2827 Compile* C = phase->C;
duke@0 2828 intptr_t offset = start_offset;
duke@0 2829
duke@0 2830 int unit = BytesPerLong;
duke@0 2831 if ((offset % unit) != 0) {
kvn@3680 2832 Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(offset));
duke@0 2833 adr = phase->transform(adr);
duke@0 2834 const TypePtr* atp = TypeRawPtr::BOTTOM;
coleenp@113 2835 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
duke@0 2836 mem = phase->transform(mem);
duke@0 2837 offset += BytesPerInt;
duke@0 2838 }
duke@0 2839 assert((offset % unit) == 0, "");
duke@0 2840
duke@0 2841 // Initialize the remaining stuff, if any, with a ClearArray.
duke@0 2842 return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
duke@0 2843 }
duke@0 2844
duke@0 2845 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
duke@0 2846 Node* start_offset,
duke@0 2847 Node* end_offset,
duke@0 2848 PhaseGVN* phase) {
never@68 2849 if (start_offset == end_offset) {
never@68 2850 // nothing to do
never@68 2851 return mem;
never@68 2852 }
never@68 2853
duke@0 2854 Compile* C = phase->C;
duke@0 2855 int unit = BytesPerLong;
duke@0 2856 Node* zbase = start_offset;
duke@0 2857 Node* zend = end_offset;
duke@0 2858
duke@0 2859 // Scale to the unit required by the CPU:
duke@0 2860 if (!Matcher::init_array_count_is_in_bytes) {
duke@0 2861 Node* shift = phase->intcon(exact_log2(unit));
kvn@3680 2862 zbase = phase->transform( new(C) URShiftXNode(zbase, shift) );
kvn@3680 2863 zend = phase->transform( new(C) URShiftXNode(zend, shift) );
duke@0 2864 }
duke@0 2865
kvn@3975 2866 // Bulk clear double-words
kvn@3680 2867 Node* zsize = phase->transform( new(C) SubXNode(zend, zbase) );
kvn@3680 2868 Node* adr = phase->transform( new(C) AddPNode(dest, dest, start_offset) );
kvn@3680 2869 mem = new (C) ClearArrayNode(ctl, mem, zsize, adr);
duke@0 2870 return phase->transform(mem);
duke@0 2871 }
duke@0 2872
duke@0 2873 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
duke@0 2874 intptr_t start_offset,
duke@0 2875 intptr_t end_offset,
duke@0 2876 PhaseGVN* phase) {
never@68 2877 if (start_offset == end_offset) {
never@68 2878 // nothing to do
never@68 2879 return mem;
never@68 2880 }
never@68 2881
duke@0 2882 Compile* C = phase->C;
duke@0 2883 assert((end_offset % BytesPerInt) == 0, "odd end offset");
duke@0 2884 intptr_t done_offset = end_offset;
duke@0 2885 if ((done_offset % BytesPerLong) != 0) {
duke@0 2886 done_offset -= BytesPerInt;
duke@0 2887 }
duke@0 2888 if (done_offset > start_offset) {
duke@0 2889 mem = clear_memory(ctl, mem, dest,
duke@0 2890 start_offset, phase->MakeConX(done_offset), phase);
duke@0 2891 }
duke@0 2892 if (done_offset < end_offset) { // emit the final 32-bit store
kvn@3680 2893 Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(done_offset));
duke@0 2894 adr = phase->transform(adr);
duke@0 2895 const TypePtr* atp = TypeRawPtr::BOTTOM;
coleenp@113 2896 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT);
duke@0 2897 mem = phase->transform(mem);
duke@0 2898 done_offset += BytesPerInt;
duke@0 2899 }
duke@0 2900 assert(done_offset == end_offset, "");
duke@0 2901 return mem;
duke@0 2902 }
duke@0 2903
duke@0 2904 //=============================================================================
kvn@2259 2905 // Do not match memory edge.
kvn@2259 2906 uint StrIntrinsicNode::match_edge(uint idx) const {
kvn@2259 2907 return idx == 2 || idx == 3;
duke@0 2908 }
duke@0 2909
duke@0 2910 //------------------------------Ideal------------------------------------------
duke@0 2911 // Return a node which is more "ideal" than the current node. Strip out
duke@0 2912 // control copies
kvn@2259 2913 Node *StrIntrinsicNode::Ideal(PhaseGVN *phase, bool can_reshape) {
kvn@2259 2914 if (remove_dead_region(phase, can_reshape)) return this;
kvn@2876 2915 // Don't bother trying to transform a dead node
kvn@2876 2916 if (in(0) && in(0)->is_top()) return NULL;
kvn@2259 2917
kvn@2264 2918 if (can_reshape) {
kvn@2264 2919 Node* mem = phase->transform(in(MemNode::Memory));
kvn@2264 2920 // If transformed to a MergeMem, get the desired slice
kvn@2264 2921 uint alias_idx = phase->C->get_alias_index(adr_type());
kvn@2264 2922 mem = mem->is_MergeMem() ? mem->as_MergeMem()->memory_at(alias_idx) : mem;
kvn@2264 2923 if (mem != in(MemNode::Memory)) {
kvn@2264 2924 set_req(MemNode::Memory, mem);
kvn@2264 2925 return this;
kvn@2264 2926 }
kvn@2264 2927 }
kvn@2259 2928 return NULL;
rasbold@169 2929 }
rasbold@169 2930
kvn@2876 2931 //------------------------------Value------------------------------------------
kvn@2876 2932 const Type *StrIntrinsicNode::Value( PhaseTransform *phase ) const {
kvn@2876 2933 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
kvn@2876 2934 return bottom_type();
kvn@2876 2935 }
kvn@2876 2936
duke@0 2937 //=============================================================================
kvn@4044 2938 //------------------------------match_edge-------------------------------------
kvn@4044 2939 // Do not match memory edge
kvn@4044 2940 uint EncodeISOArrayNode::match_edge(uint idx) const {
kvn@4044 2941 return idx == 2 || idx == 3; // EncodeISOArray src (Binary dst len)
kvn@4044 2942 }
kvn@4044 2943
kvn@4044 2944 //------------------------------Ideal------------------------------------------
kvn@4044 2945 // Return a node which is more "ideal" than the current node. Strip out
kvn@4044 2946 // control copies
kvn@4044 2947 Node *EncodeISOArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
kvn@4044 2948 return remove_dead_region(phase, can_reshape) ? this : NULL;
kvn@4044 2949 }
kvn@4044 2950
kvn@4044 2951 //------------------------------Value------------------------------------------
kvn@4044 2952 const Type *EncodeISOArrayNode::Value(PhaseTransform *phase) const {
kvn@4044 2953 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
kvn@4044 2954 return bottom_type();
kvn@4044 2955 }
kvn@4044 2956
kvn@4044 2957 //=============================================================================
duke@0 2958 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
duke@0 2959 : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)),
duke@0 2960 _adr_type(C->get_adr_type(alias_idx))
duke@0 2961 {
duke@0 2962 init_class_id(Class_MemBar);
duke@0 2963 Node* top = C->top();
duke@0 2964 init_req(TypeFunc::I_O,top);
duke@0 2965 init_req(TypeFunc::FramePtr,top);
duke@0 2966 init_req(TypeFunc::ReturnAdr,top);
duke@0 2967 if (precedent != NULL)
duke@0 2968 init_req(TypeFunc::Parms, precedent);
duke@0 2969 }
duke@0 2970
duke@0 2971 //------------------------------cmp--------------------------------------------
duke@0 2972 uint MemBarNode::hash() const { return NO_HASH; }
duke@0 2973 uint MemBarNode::cmp( const Node &n ) const {
duke@0 2974 return (&n == this); // Always fail except on self
duke@0 2975 }
duke@0 2976
duke@0 2977 //------------------------------make-------------------------------------------
duke@0 2978 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
duke@0 2979 switch (opcode) {
kvn@3680 2980 case Op_MemBarAcquire: return new(C) MemBarAcquireNode(C, atp, pn);
kvn@3680 2981 case Op_MemBarRelease: return new(C) MemBarReleaseNode(C, atp, pn);
kvn@3680 2982 case Op_MemBarAcquireLock: return new(C) MemBarAcquireLockNode(C, atp, pn);
kvn@3680 2983 case Op_MemBarReleaseLock: return new(C) MemBarReleaseLockNode(C, atp, pn);
kvn@3680 2984 case Op_MemBarVolatile: return new(C) MemBarVolatileNode(C, atp, pn);
kvn@3680 2985 case Op_MemBarCPUOrder: return new(C) MemBarCPUOrderNode(C, atp, pn);
kvn@3680 2986 case Op_Initialize: return new(C) InitializeNode(C, atp, pn);
kvn@3680 2987 case Op_MemBarStoreStore: return new(C) MemBarStoreStoreNode(C, atp, pn);
duke@0 2988 default: ShouldNotReachHere(); return NULL;
duke@0 2989 }
duke@0 2990 }
duke@0 2991
duke@0 2992 //------------------------------Ideal------------------------------------------
duke@0 2993 // Return a node which is more "ideal" than the current node. Strip out
duke@0 2994 // control copies
duke@0 2995 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) {
kvn@1100 2996 if (remove_dead_region(phase, can_reshape)) return this;
kvn@2876 2997 // Don't bother trying to transform a dead node
adlertz@4856 2998 if (in(0) && in(0)->is_top()) {
adlertz@4856 2999 return NULL;
adlertz@4856 3000 }
kvn@1100 3001
kvn@1100 3002 // Eliminate volatile MemBars for scalar replaced objects.
kvn@4675 3003 if (can_reshape && req() == (Precedent+1)) {
kvn@4675 3004 bool eliminate = false;
kvn@4675 3005 int opc = Opcode();
kvn@4675 3006 if ((opc == Op_MemBarAcquire || opc == Op_MemBarVolatile)) {
kvn@4675 3007 // Volatile field loads and stores.
kvn@4675 3008 Node* my_mem = in(MemBarNode::Precedent);
adlertz@4856 3009 // The MembarAquire may keep an unused LoadNode alive through the Precedent edge
adlertz@4856 3010 if ((my_mem != NULL) && (opc == Op_MemBarAcquire) && (my_mem->outcnt() == 1)) {
adlertz@4882 3011 // if the Precedent is a decodeN and its input (a Load) is used at more than one place,
adlertz@4882 3012 // replace this Precedent (decodeN) with the Load instead.
adlertz@4882 3013 if ((my_mem->Opcode() == Op_DecodeN) && (my_mem->in(1)->outcnt() > 1)) {
adlertz@4882 3014 Node* load_node = my_mem->in(1);
adlertz@4882 3015 set_req(MemBarNode::Precedent, load_node);
adlertz@4882 3016 phase->is_IterGVN()->_worklist.push(my_mem);
adlertz@4882 3017 my_mem = load_node;
adlertz@4882 3018 } else {
adlertz@4882 3019 assert(my_mem->unique_out() == this, "sanity");
adlertz@4882 3020 del_req(Precedent);
adlertz@4882 3021 phase->is_IterGVN()->_worklist.push(my_mem); // remove dead node later
adlertz@4882 3022 my_mem = NULL;
adlertz@4882 3023 }
adlertz@4856 3024 }
kvn@4675 3025 if (my_mem != NULL && my_mem->is_Mem()) {
kvn@4675 3026 const TypeOopPtr* t_oop = my_mem->in(MemNode::Address)->bottom_type()->isa_oopptr();
kvn@4675 3027 // Check for scalar replaced object reference.
kvn@4675 3028 if( t_oop != NULL && t_oop->is_known_instance_field() &&
kvn@4675 3029 t_oop->offset() != Type::OffsetBot &&
kvn@4675 3030 t_oop->offset() != Type::OffsetTop) {
kvn@4675 3031 eliminate = true;
kvn@4675 3032 }
kvn@1100 3033 }
kvn@4675 3034 } else if (opc == Op_MemBarRelease) {
kvn@4675 3035 // Final field stores.
kvn@4675 3036 Node* alloc = AllocateNode::Ideal_allocation(in(MemBarNode::Precedent), phase);
kvn@4675 3037 if ((alloc != NULL) && alloc->is_Allocate() &&
kvn@4675 3038 alloc->as_Allocate()->_is_non_escaping) {
kvn@4675 3039 // The allocated object does not escape.
kvn@4675 3040 eliminate = true;
kvn@4675 3041 }
kvn@4675 3042 }
kvn@4675 3043 if (eliminate) {
kvn@4675 3044 // Replace MemBar projections by its inputs.
kvn@4675 3045 PhaseIterGVN* igvn = phase->is_IterGVN();
kvn@4675 3046 igvn->replace_node(proj_out(TypeFunc::Memory), in(TypeFunc::Memory));
kvn@4675 3047 igvn->replace_node(proj_out(TypeFunc::Control), in(TypeFunc::Control));
kvn@4675 3048 // Must return either the original node (now dead) or a new node
kvn@4675 3049 // (Do not return a top here, since that would break the uniqueness of top.)
kvn@4675 3050 return new (phase->C) ConINode(TypeInt::ZERO);
kvn@1100 3051 }
kvn@1100 3052 }
kvn@1100 3053 return NULL;
duke@0 3054 }
duke@0 3055
duke@0 3056 //------------------------------Value------------------------------------------
duke@0 3057 const Type *MemBarNode::Value( PhaseTransform *phase ) const {
duke@0 3058 if( !in(0) ) return Type::TOP;
duke@0 3059 if( phase->type(in(0)) == Type::TOP )
duke@0 3060 return Type::TOP;
duke@0 3061 return TypeTuple::MEMBAR;
duke@0 3062 }
duke@0 3063
duke@0 3064 //------------------------------match------------------------------------------
duke@0 3065 // Construct projections for memory.
duke@0 3066 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
duke@0 3067 switch (proj->_con) {
duke@0 3068 case TypeFunc::Control:
duke@0 3069 case TypeFunc::Memory:
kvn@3680 3070 return new (m->C) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
duke@0 3071 }
duke@0 3072 ShouldNotReachHere();
duke@0 3073 return NULL;
duke@0 3074 }
duke@0 3075
duke@0 3076 //===========================InitializeNode====================================
duke@0 3077 // SUMMARY:
duke@0 3078 // This node acts as a memory barrier on raw memory, after some raw stores.
duke@0 3079 // The 'cooked' oop value feeds from the Initialize, not the Allocation.
duke@0 3080 // The Initialize can 'capture' suitably constrained stores as raw inits.
duke@0 3081 // It can coalesce related raw stores into larger units (called 'tiles').
duke@0 3082 // It can avoid zeroing new storage for memory units which have raw inits.
duke@0 3083 // At macro-expansion, it is marked 'complete', and does not optimize further.
duke@0 3084 //
duke@0 3085 // EXAMPLE:
duke@0 3086 // The object 'new short[2]' occupies 16 bytes in a 32-bit machine.
duke@0 3087 // ctl = incoming control; mem* = incoming memory
duke@0 3088 // (Note: A star * on a memory edge denotes I/O and other standard edges.)
duke@0 3089 // First allocate uninitialized memory and fill in the header:
duke@0 3090 // alloc = (Allocate ctl mem* 16 #short[].klass ...)
duke@0 3091 // ctl := alloc.Control; mem* := alloc.Memory*
duke@0 3092 // rawmem = alloc.Memory; rawoop = alloc.RawAddress
duke@0 3093 // Then initialize to zero the non-header parts of the raw memory block:
duke@0 3094 // init = (Initialize alloc.Control alloc.Memory* alloc.RawAddress)
duke@0 3095 // ctl := init.Control; mem.SLICE(#short[*]) := init.Memory
duke@0 3096 // After the initialize node executes, the object is ready for service:
duke@0 3097 // oop := (CheckCastPP init.Control alloc.RawAddress #short[])
duke@0 3098 // Suppose its body is immediately initialized as {1,2}:
duke@0 3099 // store1 = (StoreC init.Control init.Memory (+ oop 12) 1)
duke@0 3100 // store2 = (StoreC init.Control store1 (+ oop 14) 2)
duke@0 3101 // mem.SLICE(#short[*]) := store2
duke@0 3102 //
duke@0 3103 // DETAILS:
duke@0 3104 // An InitializeNode collects and isolates object initialization after
duke@0 3105 // an AllocateNode and before the next possible safepoint. As a
duke@0 3106 // memory barrier (MemBarNode), it keeps critical stores from drifting
duke@0 3107 // down past any safepoint or any publication of the allocation.
duke@0 3108 // Before this barrier, a newly-allocated object may have uninitialized bits.
duke@0 3109 // After this barrier, it may be treated as a real oop, and GC is allowed.
duke@0 3110 //
duke@0 3111 // The semantics of the InitializeNode include an implicit zeroing of
duke@0 3112 // the new object from object header to the end of the object.
duke@0 3113 // (The object header and end are determined by the AllocateNode.)
duke@0 3114 //
duke@0 3115 // Certain stores may be added as direct inputs to the InitializeNode.
duke@0 3116 // These stores must update raw memory, and they must be to addresses
duke@0 3117 // derived from the raw address produced by AllocateNode, and with
duke@0 3118 // a constant offset. They must be ordered by increasing offset.
duke@0 3119 // The first one is at in(RawStores), the last at in(req()-1).
duke@0 3120 // Unlike most memory operations, they are not linked in a chain,
duke@0 3121 // but are displayed in parallel as users of the rawmem output of
duke@0 3122 // the allocation.
duke@0 3123 //
duke@0 3124 // (See comments in InitializeNode::capture_store, which continue
duke@0 3125 // the example given above.)
duke@0 3126 //
duke@0 3127 // When the associated Allocate is macro-expanded, the InitializeNode
duke@0 3128 // may be rewritten to optimize collected stores. A ClearArrayNode
duke@0 3129 // may also be created at that point to represent any required zeroing.
duke@0 3130 // The InitializeNode is then marked 'complete', prohibiting further
duke@0 3131 // capturing of nearby memory operations.
duke@0 3132 //
duke@0 3133 // During macro-expansion, all captured initializations which store
twisti@605 3134 // constant values of 32 bits or smaller are coalesced (if advantageous)
duke@0 3135 // into larger 'tiles' 32 or 64 bits. This allows an object to be
duke@0 3136 // initialized in fewer memory operations. Memory words which are
duke@0 3137 // covered by neither tiles nor non-constant stores are pre-zeroed
duke@0 3138 // by explicit stores of zero. (The code shape happens to do all
duke@0 3139 // zeroing first, then all other stores, with both sequences occurring
duke@0 3140 // in order of ascending offsets.)
duke@0 3141 //
duke@0 3142 // Alternatively, code may be inserted between an AllocateNode and its
duke@0 3143 // InitializeNode, to perform arbitrary initialization of the new object.
duke@0 3144 // E.g., the object copying intrinsics insert complex data transfers here.
duke@0 3145 // The initialization must then be marked as 'complete' disable the
duke@0 3146 // built-in zeroing semantics and the collection of initializing stores.
duke@0 3147 //
duke@0 3148 // While an InitializeNode is incomplete, reads from the memory state
duke@0 3149 // produced by it are optimizable if they match the control edge and
duke@0 3150 // new oop address associated with the allocation/initialization.
duke@0 3151 // They return a stored value (if the offset matches) or else zero.
duke@0 3152 // A write to the memory state, if it matches control and address,
duke@0 3153 // and if it is to a constant offset, may be 'captured' by the
duke@0 3154 // InitializeNode. It is cloned as a raw memory operation and rewired
duke@0 3155 // inside the initialization, to the raw oop produced by the allocation.
duke@0 3156 // Operations on addresses which are provably distinct (e.g., to
duke@0 3157 // other AllocateNodes) are allowed to bypass the initialization.
duke@0 3158 //
duke@0 3159 // The effect of all this is to consolidate object initialization
duke@0 3160 // (both arrays and non-arrays, both piecewise and bulk) into a
duke@0 3161 // single location, where it can be optimized as a unit.
duke@0 3162 //
duke@0 3163 // Only stores with an offset less than TrackedInitializationLimit words
duke@0 3164 // will be considered for capture by an InitializeNode. This puts a
duke@0 3165 // reasonable limit on the complexity of optimized initializations.
duke@0 3166
duke@0 3167 //---------------------------InitializeNode------------------------------------
duke@0 3168 InitializeNode::InitializeNode(Compile* C, int adr_type, Node* rawoop)
roland@2957 3169 : _is_complete(Incomplete), _does_not_escape(false),
duke@0 3170 MemBarNode(C, adr_type, rawoop)
duke@0 3171 {
duke@0 3172 init_class_id(Class_Initialize);
duke@0 3173
duke@0 3174 assert(adr_type == Compile::AliasIdxRaw, "only valid atp");
duke@0 3175 assert(in(RawAddress) == rawoop, "proper init");
duke@0 3176 // Note: allocation() can be NULL, for secondary initialization barriers
duke@0 3177 }
duke@0 3178
duke@0 3179 // Since this node is not matched, it will be processed by the
duke@0 3180 // register allocator. Declare that there are no constraints
duke@0 3181 // on the allocation of the RawAddress edge.
duke@0 3182 const RegMask &InitializeNode::in_RegMask(uint idx) const {
duke@0 3183 // This edge should be set to top, by the set_complete. But be conservative.
duke@0 3184 if (idx == InitializeNode::RawAddress)
duke@0 3185 return *(Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()]);
duke@0 3186 return RegMask::Empty;
duke@0 3187 }
duke@0 3188
duke@0 3189 Node* InitializeNode::memory(uint alias_idx) {
duke@0 3190 Node* mem = in(Memory);
duke@0 3191 if (mem->is_MergeMem()) {
duke@0 3192 return mem->as_MergeMem()->memory_at(alias_idx);
duke@0 3193 } else {
duke@0 3194 // incoming raw memory is not split
duke@0 3195 return mem;
duke@0 3196 }
duke@0 3197 }
duke@0 3198
duke@0 3199 bool InitializeNode::is_non_zero() {
duke@0 3200 if (is_complete()) return false;
duke@0 3201 remove_extra_zeroes();
duke@0 3202 return (req() > RawStores);
duke@0 3203 }
duke@0 3204
duke@0 3205 void InitializeNode::set_complete(PhaseGVN* phase) {
duke@0 3206 assert(!is_complete(), "caller responsibility");
kvn@2722 3207 _is_complete = Complete;
duke@0 3208
duke@0 3209 // After this node is complete, it contains a bunch of
duke@0 3210 // raw-memory initializations. There is no need for
duke@0 3211 // it to have anything to do with non-raw memory effects.
duke@0 3212 // Therefore, tell all non-raw users to re-optimize themselves,
duke@0 3213 // after skipping the memory effects of this initialization.
duke@0 3214 PhaseIterGVN* igvn = phase->is_IterGVN();
duke@0 3215 if (igvn) igvn->add_users_to_worklist(this);
duke@0 3216 }
duke@0 3217
duke@0 3218 // convenience function
duke@0 3219 // return false if the init contains any stores already
duke@0 3220 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) {
duke@0 3221 InitializeNode* init = initialization();
duke@0 3222 if (init == NULL || init->is_complete()) return false;
duke@0 3223 init->remove_extra_zeroes();
duke@0 3224 // for now, if this allocation has already collected any inits, bail:
duke@0 3225 if (init->is_non_zero()) return false;
duke@0 3226 init->set_complete(phase);
duke@0 3227 return true;
duke@0 3228 }
duke@0 3229
duke@0 3230 void InitializeNode::remove_extra_zeroes() {
duke@0 3231 if (req() == RawStores) return;
duke@0 3232 Node* zmem = zero_memory();
duke@0 3233 uint fill = RawStores;
duke@0 3234 for (uint i = fill; i < req(); i++) {
duke@0 3235 Node* n = in(i);
duke@0 3236 if (n->is_top() || n == zmem) continue; // skip
duke@0 3237 if (fill < i) set_req(fill, n); // compact
duke@0 3238 ++fill;
duke@0 3239 }
duke@0 3240 // delete any empty spaces created:
duke@0 3241 while (fill < req()) {
duke@0 3242 del_req(fill);
duke@0 3243 }
duke@0 3244 }
duke@0 3245
duke@0 3246 // Helper for remembering which stores go with which offsets.
duke@0 3247 intptr_t InitializeNode::get_store_offset(Node* st, PhaseTransform* phase) {
duke@0 3248 if (!st->is_Store()) return -1; // can happen to dead code via subsume_node
duke@0 3249 intptr_t offset = -1;
duke@0 3250 Node* base = AddPNode::Ideal_base_and_offset(st->in(MemNode::Address),
duke@0 3251 phase, offset);
duke@0 3252 if (base == NULL) return -1; // something is dead,
duke@0 3253 if (offset < 0) return -1; // dead, dead
duke@0 3254 return offset;
duke@0 3255 }
duke@0 3256
duke@0 3257 // Helper for proving that an initialization expression is
duke@0 3258 // "simple enough" to be folded into an object initialization.
duke@0 3259 // Attempts to prove that a store's initial value 'n' can be captured
duke@0 3260 // within the initialization without creating a vicious cycle, such as:
duke@0 3261 // { Foo p = new Foo(); p.next = p; }
duke@0 3262 // True for constants and parameters and small combinations thereof.
kvn@4675 3263 bool InitializeNode::detect_init_independence(Node* n, int& count) {
duke@0 3264 if (n == NULL) return true; // (can this really happen?)
duke@0 3265 if (n->is_Proj()) n = n->in(0);
duke@0 3266 if (n == this) return false; // found a cycle
duke@0 3267 if (n->is_Con()) return true;
duke@0 3268 if (n->is_Start()) return true; // params, etc., are OK
duke@0 3269 if (n->is_Root()) return true; // even better
duke@0 3270
duke@0 3271 Node* ctl = n->in(0);
duke@0 3272 if (ctl != NULL && !ctl->is_top()) {
duke@0 3273 if (ctl->is_Proj()) ctl = ctl->in(0);
duke@0 3274 if (ctl == this) return false;
duke@0 3275
duke@0 3276 // If we already know that the enclosing memory op is pinned right after
duke@0 3277 // the init, then any control flow that the store has picked up
duke@0 3278 // must have preceded the init, or else be equal to the init.
duke@0 3279 // Even after loop optimizations (which might change control edges)
duke@0 3280 // a store is never pinned *before* the availability of its inputs.
kvn@119 3281 if (!MemNode::all_controls_dominate(n, this))
duke@0 3282 return false; // failed to prove a good control
duke@0 3283 }
duke@0 3284
duke@0 3285 // Check data edges for possible dependencies on 'this'.
duke@0 3286 if ((count += 1) > 20) return false; // complexity limit
duke@0 3287 for (uint i = 1; i < n->req(); i++) {
duke@0 3288 Node* m = n->in(i);
duke@0 3289 if (m == NULL || m == n || m->is_top()) continue;
duke@0 3290 uint first_i = n->find_edge(m);
duke@0 3291 if (i != first_i) continue; // process duplicate edge just once
kvn@4675 3292 if (!detect_init_independence(m, count)) {
duke@0 3293 return false;
duke@0 3294 }
duke@0 3295 }
duke@0 3296
duke@0 3297 return true;
duke@0 3298 }
duke@0 3299
duke@0 3300 // Here are all the checks a Store must pass before it can be moved into
duke@0 3301 // an initialization. Returns zero if a check fails.
duke@0 3302 // On success, returns the (constant) offset to which the store applies,
duke@0 3303 // within the initialized memory.
roland@4222 3304 intptr_t InitializeNode::can_capture_store(StoreNode* st, PhaseTransform* phase, bool can_reshape) {
duke@0 3305 const int FAIL = 0;
duke@0 3306 if (st->req() != MemNode::ValueIn + 1)
duke@0 3307 return FAIL; // an inscrutable StoreNode (card mark?)
duke@0 3308 Node* ctl = st->in(MemNode::Control);
duke@0 3309 if (!(ctl != NULL && ctl->is_Proj() && ctl->in(0) == this))
duke@0 3310 return FAIL; // must be unconditional after the initialization
duke@0 3311 Node* mem = st->in(MemNode::Memory);
duke@0 3312 if (!(mem->is_Proj() && mem->in(0) == this))
duke@0 3313 return FAIL; // must not be preceded by other stores
duke@0 3314 Node* adr = st->in(MemNode::Address);
duke@0 3315 intptr_t offset;
duke@0 3316 AllocateNode* alloc = AllocateNode::Ideal_allocation(adr, phase, offset);
duke@0 3317 if (alloc == NULL)
duke@0 3318 return FAIL; // inscrutable address
duke@0 3319 if (alloc != allocation())
duke@0 3320 return FAIL; // wrong allocation! (store needs to float up)
duke@0 3321 Node* val = st->in(MemNode::ValueIn);
duke@0 3322 int complexity_count = 0;
kvn@4675 3323 if (!detect_init_independence(val, complexity_count))
duke@0 3324 return FAIL; // stored value must be 'simple enough'
duke@0 3325
roland@4222 3326 // The Store can be captured only if nothing after the allocation
roland@4222 3327 // and before the Store is using the memory location that the store
roland@4222 3328 // overwrites.
roland@4222 3329 bool failed = false;
roland@4222 3330 // If is_complete_with_arraycopy() is true the shape of the graph is
roland@4222 3331 // well defined and is safe so no need for extra checks.
roland@4222 3332 if (!is_complete_with_arraycopy()) {
roland@4222 3333 // We are going to look at each use of the memory state following
roland@4222 3334 // the allocation to make sure nothing reads the memory that the
roland@4222 3335 // Store writes.
roland@4222 3336 const TypePtr* t_adr = phase->type(adr)->isa_ptr();
roland@4222 3337 int alias_idx = phase->C->get_alias_index(t_adr);
roland@4222 3338 ResourceMark rm;
roland@4222 3339 Unique_Node_List mems;
roland@4222 3340 mems.push(mem);
roland@4222 3341 Node* unique_merge = NULL;
roland@4222 3342 for (uint next = 0; next < mems.size(); ++next) {
roland@4222 3343 Node *m = mems.at(next);
roland@4222 3344 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
roland@4222 3345 Node *n = m->fast_out(j);
roland@4222 3346 if (n->outcnt() == 0) {
roland@4222 3347 continue;
roland@4222 3348 }
roland@4222 3349 if (n == st) {
roland@4222 3350 continue;
roland@4222 3351 } else if (n->in(0) != NULL && n->in(0) != ctl) {
roland@4222 3352 // If the control of this use is different from the control
roland@4222 3353 // of the Store which is right after the InitializeNode then
roland@4222 3354 // this node cannot be between the InitializeNode and the
roland@4222 3355 // Store.
roland@4222 3356 continue;
roland@4222 3357 } else if (n->is_MergeMem()) {
roland@4222 3358 if (n->as_MergeMem()->memory_at(alias_idx) == m) {
roland@4222 3359 // We can hit a MergeMemNode (that will likely go away
roland@4222 3360 // later) that is a direct use of the memory state
roland@4222 3361 // following the InitializeNode on the same slice as the
roland@4222 3362 // store node that we'd like to capture. We need to check
roland@4222 3363 // the uses of the MergeMemNode.
roland@4222 3364 mems.push(n);
roland@4222 3365 }
roland@4222 3366 } else if (n->is_Mem()) {
roland@4222 3367 Node* other_adr = n->in(MemNode::Address);
roland@4222 3368 if (other_adr == adr) {
roland@4222 3369 failed = true;
roland@4222 3370 break;
roland@4222 3371 } else {
roland@4222 3372 const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr();
roland@4222 3373 if (other_t_adr != NULL) {
roland@4222 3374 int other_alias_idx = phase->C->get_alias_index(other_t_adr);
roland@4222 3375 if (other_alias_idx == alias_idx) {
roland@4222 3376 // A load from the same memory slice as the store right
roland@4222 3377 // after the InitializeNode. We check the control of the
roland@4222 3378 // object/array that is loaded from. If it's the same as
roland@4222 3379 // the store control then we cannot capture the store.
roland@4222 3380 assert(!n->is_Store(), "2 stores to same slice on same control?");
roland@4222 3381 Node* base = other_adr;
roland@4222 3382 assert(base->is_AddP(), err_msg_res("should be addp but is %s", base->Name()));
roland@4222 3383 base = base->in(AddPNode::Base);
roland@4222 3384 if (base != NULL) {
roland@4222 3385 base = base->uncast();
roland@4222 3386 if (base->is_Proj() && base->in(0) == alloc) {
roland@4222 3387 failed = true;
roland@4222 3388 break;
roland@4222 3389 }
roland@4222 3390 }
roland@4222 3391 }
roland@4222 3392 }
roland@4222 3393 }
roland@4222 3394 } else {
roland@4222 3395 failed = true;
roland@4222 3396 break;
roland@4222 3397 }
roland@4222 3398 }
roland@4222 3399 }
roland@4222 3400 }
roland@4222 3401 if (failed) {
roland@4222 3402 if (!can_reshape) {
roland@4222 3403 // We decided we couldn't capture the store during parsing. We
roland@4222 3404 // should try again during the next IGVN once the graph is
roland@4222 3405 // cleaner.
roland@4222 3406 phase->C->record_for_igvn(st);
roland@4222 3407 }
roland@4222 3408 return FAIL;
roland@4222 3409 }
roland@4222 3410
duke@0 3411 return offset; // success
duke@0 3412 }
duke@0 3413
duke@0 3414 // Find the captured store in