src/share/vm/opto/parse3.cpp

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

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 7994
04ff2f6cd0eb
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "compiler/compileLog.hpp"
aoqi@0 27 #include "interpreter/linkResolver.hpp"
aoqi@0 28 #include "memory/universe.inline.hpp"
aoqi@0 29 #include "oops/objArrayKlass.hpp"
aoqi@0 30 #include "opto/addnode.hpp"
aoqi@0 31 #include "opto/memnode.hpp"
aoqi@0 32 #include "opto/parse.hpp"
aoqi@0 33 #include "opto/rootnode.hpp"
aoqi@0 34 #include "opto/runtime.hpp"
aoqi@0 35 #include "opto/subnode.hpp"
aoqi@0 36 #include "runtime/deoptimization.hpp"
aoqi@0 37 #include "runtime/handles.inline.hpp"
aoqi@0 38
aoqi@0 39 //=============================================================================
aoqi@0 40 // Helper methods for _get* and _put* bytecodes
aoqi@0 41 //=============================================================================
aoqi@0 42 bool Parse::static_field_ok_in_clinit(ciField *field, ciMethod *method) {
aoqi@0 43 // Could be the field_holder's <clinit> method, or <clinit> for a subklass.
aoqi@0 44 // Better to check now than to Deoptimize as soon as we execute
aoqi@0 45 assert( field->is_static(), "Only check if field is static");
aoqi@0 46 // is_being_initialized() is too generous. It allows access to statics
aoqi@0 47 // by threads that are not running the <clinit> before the <clinit> finishes.
aoqi@0 48 // return field->holder()->is_being_initialized();
aoqi@0 49
aoqi@0 50 // The following restriction is correct but conservative.
aoqi@0 51 // It is also desirable to allow compilation of methods called from <clinit>
aoqi@0 52 // but this generated code will need to be made safe for execution by
aoqi@0 53 // other threads, or the transition from interpreted to compiled code would
aoqi@0 54 // need to be guarded.
aoqi@0 55 ciInstanceKlass *field_holder = field->holder();
aoqi@0 56
aoqi@0 57 bool access_OK = false;
aoqi@0 58 if (method->holder()->is_subclass_of(field_holder)) {
aoqi@0 59 if (method->is_static()) {
aoqi@0 60 if (method->name() == ciSymbol::class_initializer_name()) {
aoqi@0 61 // OK to access static fields inside initializer
aoqi@0 62 access_OK = true;
aoqi@0 63 }
aoqi@0 64 } else {
aoqi@0 65 if (method->name() == ciSymbol::object_initializer_name()) {
aoqi@0 66 // It's also OK to access static fields inside a constructor,
aoqi@0 67 // because any thread calling the constructor must first have
aoqi@0 68 // synchronized on the class by executing a '_new' bytecode.
aoqi@0 69 access_OK = true;
aoqi@0 70 }
aoqi@0 71 }
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 return access_OK;
aoqi@0 75
aoqi@0 76 }
aoqi@0 77
aoqi@0 78
aoqi@0 79 void Parse::do_field_access(bool is_get, bool is_field) {
aoqi@0 80 bool will_link;
aoqi@0 81 ciField* field = iter().get_field(will_link);
aoqi@0 82 assert(will_link, "getfield: typeflow responsibility");
aoqi@0 83
aoqi@0 84 ciInstanceKlass* field_holder = field->holder();
aoqi@0 85
aoqi@0 86 if (is_field == field->is_static()) {
aoqi@0 87 // Interpreter will throw java_lang_IncompatibleClassChangeError
aoqi@0 88 // Check this before allowing <clinit> methods to access static fields
aoqi@0 89 uncommon_trap(Deoptimization::Reason_unhandled,
aoqi@0 90 Deoptimization::Action_none);
aoqi@0 91 return;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 if (!is_field && !field_holder->is_initialized()) {
aoqi@0 95 if (!static_field_ok_in_clinit(field, method())) {
aoqi@0 96 uncommon_trap(Deoptimization::Reason_uninitialized,
aoqi@0 97 Deoptimization::Action_reinterpret,
aoqi@0 98 NULL, "!static_field_ok_in_clinit");
aoqi@0 99 return;
aoqi@0 100 }
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 // Deoptimize on putfield writes to call site target field.
aoqi@0 104 if (!is_get && field->is_call_site_target()) {
aoqi@0 105 uncommon_trap(Deoptimization::Reason_unhandled,
aoqi@0 106 Deoptimization::Action_reinterpret,
aoqi@0 107 NULL, "put to call site target field");
aoqi@0 108 return;
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 assert(field->will_link(method()->holder(), bc()), "getfield: typeflow responsibility");
aoqi@0 112
aoqi@0 113 // Note: We do not check for an unloaded field type here any more.
aoqi@0 114
aoqi@0 115 // Generate code for the object pointer.
aoqi@0 116 Node* obj;
aoqi@0 117 if (is_field) {
aoqi@0 118 int obj_depth = is_get ? 0 : field->type()->size();
aoqi@0 119 obj = null_check(peek(obj_depth));
aoqi@0 120 // Compile-time detect of null-exception?
aoqi@0 121 if (stopped()) return;
aoqi@0 122
aoqi@0 123 #ifdef ASSERT
aoqi@0 124 const TypeInstPtr *tjp = TypeInstPtr::make(TypePtr::NotNull, iter().get_declared_field_holder());
aoqi@0 125 assert(_gvn.type(obj)->higher_equal(tjp), "cast_up is no longer needed");
aoqi@0 126 #endif
aoqi@0 127
aoqi@0 128 if (is_get) {
aoqi@0 129 (void) pop(); // pop receiver before getting
aoqi@0 130 do_get_xxx(obj, field, is_field);
aoqi@0 131 } else {
aoqi@0 132 do_put_xxx(obj, field, is_field);
aoqi@0 133 (void) pop(); // pop receiver after putting
aoqi@0 134 }
aoqi@0 135 } else {
aoqi@0 136 const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror());
aoqi@0 137 obj = _gvn.makecon(tip);
aoqi@0 138 if (is_get) {
aoqi@0 139 do_get_xxx(obj, field, is_field);
aoqi@0 140 } else {
aoqi@0 141 do_put_xxx(obj, field, is_field);
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146
aoqi@0 147 void Parse::do_get_xxx(Node* obj, ciField* field, bool is_field) {
aoqi@0 148 // Does this field have a constant value? If so, just push the value.
aoqi@0 149 if (field->is_constant()) {
aoqi@0 150 // final or stable field
aoqi@0 151 const Type* stable_type = NULL;
aoqi@0 152 if (FoldStableValues && field->is_stable()) {
aoqi@0 153 stable_type = Type::get_const_type(field->type());
aoqi@0 154 if (field->type()->is_array_klass()) {
aoqi@0 155 int stable_dimension = field->type()->as_array_klass()->dimension();
aoqi@0 156 stable_type = stable_type->is_aryptr()->cast_to_stable(true, stable_dimension);
aoqi@0 157 }
aoqi@0 158 }
aoqi@0 159 if (field->is_static()) {
aoqi@0 160 // final static field
aoqi@0 161 if (C->eliminate_boxing()) {
aoqi@0 162 // The pointers in the autobox arrays are always non-null.
aoqi@0 163 ciSymbol* klass_name = field->holder()->name();
aoqi@0 164 if (field->name() == ciSymbol::cache_field_name() &&
aoqi@0 165 field->holder()->uses_default_loader() &&
aoqi@0 166 (klass_name == ciSymbol::java_lang_Character_CharacterCache() ||
aoqi@0 167 klass_name == ciSymbol::java_lang_Byte_ByteCache() ||
aoqi@0 168 klass_name == ciSymbol::java_lang_Short_ShortCache() ||
aoqi@0 169 klass_name == ciSymbol::java_lang_Integer_IntegerCache() ||
aoqi@0 170 klass_name == ciSymbol::java_lang_Long_LongCache())) {
aoqi@0 171 bool require_const = true;
aoqi@0 172 bool autobox_cache = true;
aoqi@0 173 if (push_constant(field->constant_value(), require_const, autobox_cache)) {
aoqi@0 174 return;
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177 }
aoqi@0 178 if (push_constant(field->constant_value(), false, false, stable_type))
aoqi@0 179 return;
aoqi@0 180 } else {
aoqi@0 181 // final or stable non-static field
aoqi@0 182 // Treat final non-static fields of trusted classes (classes in
aoqi@0 183 // java.lang.invoke and sun.invoke packages and subpackages) as
aoqi@0 184 // compile time constants.
aoqi@0 185 if (obj->is_Con()) {
aoqi@0 186 const TypeOopPtr* oop_ptr = obj->bottom_type()->isa_oopptr();
aoqi@0 187 ciObject* constant_oop = oop_ptr->const_oop();
aoqi@0 188 ciConstant constant = field->constant_value_of(constant_oop);
aoqi@0 189 if (FoldStableValues && field->is_stable() && constant.is_null_or_zero()) {
aoqi@0 190 // fall through to field load; the field is not yet initialized
aoqi@0 191 } else {
aoqi@0 192 if (push_constant(constant, true, false, stable_type))
aoqi@0 193 return;
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 ciType* field_klass = field->type();
aoqi@0 200 bool is_vol = field->is_volatile();
aoqi@0 201
aoqi@0 202 // Compute address and memory type.
aoqi@0 203 int offset = field->offset_in_bytes();
aoqi@0 204 const TypePtr* adr_type = C->alias_type(field)->adr_type();
aoqi@0 205 Node *adr = basic_plus_adr(obj, obj, offset);
aoqi@0 206 BasicType bt = field->layout_type();
aoqi@0 207
aoqi@0 208 // Build the resultant type of the load
aoqi@0 209 const Type *type;
aoqi@0 210
aoqi@0 211 bool must_assert_null = false;
aoqi@0 212
aoqi@0 213 if( bt == T_OBJECT ) {
aoqi@0 214 if (!field->type()->is_loaded()) {
aoqi@0 215 type = TypeInstPtr::BOTTOM;
aoqi@0 216 must_assert_null = true;
aoqi@0 217 } else if (field->is_constant() && field->is_static()) {
aoqi@0 218 // This can happen if the constant oop is non-perm.
aoqi@0 219 ciObject* con = field->constant_value().as_object();
aoqi@0 220 // Do not "join" in the previous type; it doesn't add value,
aoqi@0 221 // and may yield a vacuous result if the field is of interface type.
aoqi@0 222 type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
aoqi@0 223 assert(type != NULL, "field singleton type must be consistent");
aoqi@0 224 } else {
aoqi@0 225 type = TypeOopPtr::make_from_klass(field_klass->as_klass());
aoqi@0 226 }
aoqi@0 227 } else {
aoqi@0 228 type = Type::get_const_basic_type(bt);
aoqi@0 229 }
aoqi@0 230 if (support_IRIW_for_not_multiple_copy_atomic_cpu && field->is_volatile()) {
aoqi@0 231 insert_mem_bar(Op_MemBarVolatile); // StoreLoad barrier
aoqi@0 232 }
aoqi@0 233 // Build the load.
aoqi@0 234 //
aoqi@0 235 MemNode::MemOrd mo = is_vol ? MemNode::acquire : MemNode::unordered;
roland@7859 236 Node* ld = make_load(NULL, adr, type, bt, adr_type, mo, LoadNode::DependsOnlyOnTest, is_vol);
aoqi@0 237
aoqi@0 238 // Adjust Java stack
aoqi@0 239 if (type2size[bt] == 1)
aoqi@0 240 push(ld);
aoqi@0 241 else
aoqi@0 242 push_pair(ld);
aoqi@0 243
aoqi@0 244 if (must_assert_null) {
aoqi@0 245 // Do not take a trap here. It's possible that the program
aoqi@0 246 // will never load the field's class, and will happily see
aoqi@0 247 // null values in this field forever. Don't stumble into a
aoqi@0 248 // trap for such a program, or we might get a long series
aoqi@0 249 // of useless recompilations. (Or, we might load a class
aoqi@0 250 // which should not be loaded.) If we ever see a non-null
aoqi@0 251 // value, we will then trap and recompile. (The trap will
aoqi@0 252 // not need to mention the class index, since the class will
aoqi@0 253 // already have been loaded if we ever see a non-null value.)
aoqi@0 254 // uncommon_trap(iter().get_field_signature_index());
aoqi@0 255 #ifndef PRODUCT
aoqi@0 256 if (PrintOpto && (Verbose || WizardMode)) {
aoqi@0 257 method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci());
aoqi@0 258 }
aoqi@0 259 #endif
aoqi@0 260 if (C->log() != NULL) {
aoqi@0 261 C->log()->elem("assert_null reason='field' klass='%d'",
aoqi@0 262 C->log()->identify(field->type()));
aoqi@0 263 }
aoqi@0 264 // If there is going to be a trap, put it at the next bytecode:
aoqi@0 265 set_bci(iter().next_bci());
aoqi@0 266 null_assert(peek());
aoqi@0 267 set_bci(iter().cur_bci()); // put it back
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 // If reference is volatile, prevent following memory ops from
aoqi@0 271 // floating up past the volatile read. Also prevents commoning
aoqi@0 272 // another volatile read.
aoqi@0 273 if (field->is_volatile()) {
aoqi@0 274 // Memory barrier includes bogus read of value to force load BEFORE membar
aoqi@0 275 insert_mem_bar(Op_MemBarAcquire, ld);
aoqi@0 276 }
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) {
aoqi@0 280 bool is_vol = field->is_volatile();
aoqi@0 281 // If reference is volatile, prevent following memory ops from
aoqi@0 282 // floating down past the volatile write. Also prevents commoning
aoqi@0 283 // another volatile read.
aoqi@0 284 if (is_vol) insert_mem_bar(Op_MemBarRelease);
aoqi@0 285
aoqi@0 286 // Compute address and memory type.
aoqi@0 287 int offset = field->offset_in_bytes();
aoqi@0 288 const TypePtr* adr_type = C->alias_type(field)->adr_type();
aoqi@0 289 Node* adr = basic_plus_adr(obj, obj, offset);
aoqi@0 290 BasicType bt = field->layout_type();
aoqi@0 291 // Value to be stored
aoqi@0 292 Node* val = type2size[bt] == 1 ? pop() : pop_pair();
aoqi@0 293 // Round doubles before storing
aoqi@0 294 if (bt == T_DOUBLE) val = dstore_rounding(val);
aoqi@0 295
aoqi@0 296 // Conservatively release stores of object references.
aoqi@0 297 const MemNode::MemOrd mo =
aoqi@0 298 is_vol ?
aoqi@0 299 // Volatile fields need releasing stores.
aoqi@0 300 MemNode::release :
aoqi@0 301 // Non-volatile fields also need releasing stores if they hold an
aoqi@0 302 // object reference, because the object reference might point to
aoqi@0 303 // a freshly created object.
aoqi@0 304 StoreNode::release_if_reference(bt);
aoqi@0 305
aoqi@0 306 // Store the value.
aoqi@0 307 Node* store;
aoqi@0 308 if (bt == T_OBJECT) {
aoqi@0 309 const TypeOopPtr* field_type;
aoqi@0 310 if (!field->type()->is_loaded()) {
aoqi@0 311 field_type = TypeInstPtr::BOTTOM;
aoqi@0 312 } else {
aoqi@0 313 field_type = TypeOopPtr::make_from_klass(field->type()->as_klass());
aoqi@0 314 }
aoqi@0 315 store = store_oop_to_object(control(), obj, adr, adr_type, val, field_type, bt, mo);
aoqi@0 316 } else {
aoqi@0 317 store = store_to_memory(control(), adr, val, bt, adr_type, mo, is_vol);
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 // If reference is volatile, prevent following volatiles ops from
aoqi@0 321 // floating up before the volatile write.
aoqi@0 322 if (is_vol) {
aoqi@0 323 // If not multiple copy atomic, we do the MemBarVolatile before the load.
aoqi@0 324 if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
aoqi@0 325 insert_mem_bar(Op_MemBarVolatile); // Use fat membar
aoqi@0 326 }
aoqi@0 327 // Remember we wrote a volatile field.
aoqi@0 328 // For not multiple copy atomic cpu (ppc64) a barrier should be issued
aoqi@0 329 // in constructors which have such stores. See do_exits() in parse1.cpp.
aoqi@0 330 if (is_field) {
aoqi@0 331 set_wrote_volatile(true);
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 // If the field is final, the rules of Java say we are in <init> or <clinit>.
aoqi@0 336 // Note the presence of writes to final non-static fields, so that we
aoqi@0 337 // can insert a memory barrier later on to keep the writes from floating
aoqi@0 338 // out of the constructor.
aoqi@0 339 // Any method can write a @Stable field; insert memory barriers after those also.
aoqi@0 340 if (is_field && (field->is_final() || field->is_stable())) {
aoqi@0 341 set_wrote_final(true);
aoqi@0 342 // Preserve allocation ptr to create precedent edge to it in membar
aoqi@0 343 // generated on exit from constructor.
aoqi@0 344 if (C->eliminate_boxing() &&
aoqi@0 345 adr_type->isa_oopptr() && adr_type->is_oopptr()->is_ptr_to_boxed_value() &&
aoqi@0 346 AllocateNode::Ideal_allocation(obj, &_gvn) != NULL) {
aoqi@0 347 set_alloc_with_final(obj);
aoqi@0 348 }
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351
aoqi@0 352
aoqi@0 353
aoqi@0 354 bool Parse::push_constant(ciConstant constant, bool require_constant, bool is_autobox_cache, const Type* stable_type) {
aoqi@0 355 const Type* con_type = Type::make_from_constant(constant, require_constant, is_autobox_cache);
aoqi@0 356 switch (constant.basic_type()) {
aoqi@0 357 case T_ARRAY:
aoqi@0 358 case T_OBJECT:
aoqi@0 359 // cases:
aoqi@0 360 // can_be_constant = (oop not scavengable || ScavengeRootsInCode != 0)
aoqi@0 361 // should_be_constant = (oop not scavengable || ScavengeRootsInCode >= 2)
aoqi@0 362 // An oop is not scavengable if it is in the perm gen.
aoqi@0 363 if (stable_type != NULL && con_type != NULL && con_type->isa_oopptr())
aoqi@0 364 con_type = con_type->join_speculative(stable_type);
aoqi@0 365 break;
aoqi@0 366
aoqi@0 367 case T_ILLEGAL:
aoqi@0 368 // Invalid ciConstant returned due to OutOfMemoryError in the CI
aoqi@0 369 assert(C->env()->failing(), "otherwise should not see this");
aoqi@0 370 // These always occur because of object types; we are going to
aoqi@0 371 // bail out anyway, so make the stack depths match up
aoqi@0 372 push( zerocon(T_OBJECT) );
aoqi@0 373 return false;
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 if (con_type == NULL)
aoqi@0 377 // we cannot inline the oop, but we can use it later to narrow a type
aoqi@0 378 return false;
aoqi@0 379
aoqi@0 380 push_node(constant.basic_type(), makecon(con_type));
aoqi@0 381 return true;
aoqi@0 382 }
aoqi@0 383
aoqi@0 384
aoqi@0 385 //=============================================================================
aoqi@0 386 void Parse::do_anewarray() {
aoqi@0 387 bool will_link;
aoqi@0 388 ciKlass* klass = iter().get_klass(will_link);
aoqi@0 389
aoqi@0 390 // Uncommon Trap when class that array contains is not loaded
aoqi@0 391 // we need the loaded class for the rest of graph; do not
aoqi@0 392 // initialize the container class (see Java spec)!!!
aoqi@0 393 assert(will_link, "anewarray: typeflow responsibility");
aoqi@0 394
aoqi@0 395 ciObjArrayKlass* array_klass = ciObjArrayKlass::make(klass);
aoqi@0 396 // Check that array_klass object is loaded
aoqi@0 397 if (!array_klass->is_loaded()) {
aoqi@0 398 // Generate uncommon_trap for unloaded array_class
aoqi@0 399 uncommon_trap(Deoptimization::Reason_unloaded,
aoqi@0 400 Deoptimization::Action_reinterpret,
aoqi@0 401 array_klass);
aoqi@0 402 return;
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 kill_dead_locals();
aoqi@0 406
aoqi@0 407 const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass);
aoqi@0 408 Node* count_val = pop();
aoqi@0 409 Node* obj = new_array(makecon(array_klass_type), count_val, 1);
aoqi@0 410 push(obj);
aoqi@0 411 }
aoqi@0 412
aoqi@0 413
aoqi@0 414 void Parse::do_newarray(BasicType elem_type) {
aoqi@0 415 kill_dead_locals();
aoqi@0 416
aoqi@0 417 Node* count_val = pop();
aoqi@0 418 const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type));
aoqi@0 419 Node* obj = new_array(makecon(array_klass), count_val, 1);
aoqi@0 420 // Push resultant oop onto stack
aoqi@0 421 push(obj);
aoqi@0 422 }
aoqi@0 423
aoqi@0 424 // Expand simple expressions like new int[3][5] and new Object[2][nonConLen].
aoqi@0 425 // Also handle the degenerate 1-dimensional case of anewarray.
aoqi@0 426 Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) {
aoqi@0 427 Node* length = lengths[0];
aoqi@0 428 assert(length != NULL, "");
aoqi@0 429 Node* array = new_array(makecon(TypeKlassPtr::make(array_klass)), length, nargs);
aoqi@0 430 if (ndimensions > 1) {
aoqi@0 431 jint length_con = find_int_con(length, -1);
aoqi@0 432 guarantee(length_con >= 0, "non-constant multianewarray");
aoqi@0 433 ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass();
aoqi@0 434 const TypePtr* adr_type = TypeAryPtr::OOPS;
aoqi@0 435 const TypeOopPtr* elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr();
aoqi@0 436 const intptr_t header = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
aoqi@0 437 for (jint i = 0; i < length_con; i++) {
aoqi@0 438 Node* elem = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs);
aoqi@0 439 intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop);
aoqi@0 440 Node* eaddr = basic_plus_adr(array, offset);
aoqi@0 441 store_oop_to_array(control(), array, eaddr, adr_type, elem, elemtype, T_OBJECT, MemNode::unordered);
aoqi@0 442 }
aoqi@0 443 }
aoqi@0 444 return array;
aoqi@0 445 }
aoqi@0 446
aoqi@0 447 void Parse::do_multianewarray() {
aoqi@0 448 int ndimensions = iter().get_dimensions();
aoqi@0 449
aoqi@0 450 // the m-dimensional array
aoqi@0 451 bool will_link;
aoqi@0 452 ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass();
aoqi@0 453 assert(will_link, "multianewarray: typeflow responsibility");
aoqi@0 454
aoqi@0 455 // Note: Array classes are always initialized; no is_initialized check.
aoqi@0 456
aoqi@0 457 kill_dead_locals();
aoqi@0 458
aoqi@0 459 // get the lengths from the stack (first dimension is on top)
aoqi@0 460 Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1);
aoqi@0 461 length[ndimensions] = NULL; // terminating null for make_runtime_call
aoqi@0 462 int j;
aoqi@0 463 for (j = ndimensions-1; j >= 0 ; j--) length[j] = pop();
aoqi@0 464
aoqi@0 465 // The original expression was of this form: new T[length0][length1]...
aoqi@0 466 // It is often the case that the lengths are small (except the last).
aoqi@0 467 // If that happens, use the fast 1-d creator a constant number of times.
aoqi@0 468 const jint expand_limit = MIN2((juint)MultiArrayExpandLimit, (juint)100);
aoqi@0 469 jint expand_count = 1; // count of allocations in the expansion
aoqi@0 470 jint expand_fanout = 1; // running total fanout
aoqi@0 471 for (j = 0; j < ndimensions-1; j++) {
aoqi@0 472 jint dim_con = find_int_con(length[j], -1);
aoqi@0 473 expand_fanout *= dim_con;
aoqi@0 474 expand_count += expand_fanout; // count the level-J sub-arrays
aoqi@0 475 if (dim_con <= 0
aoqi@0 476 || dim_con > expand_limit
aoqi@0 477 || expand_count > expand_limit) {
aoqi@0 478 expand_count = 0;
aoqi@0 479 break;
aoqi@0 480 }
aoqi@0 481 }
aoqi@0 482
aoqi@0 483 // Can use multianewarray instead of [a]newarray if only one dimension,
aoqi@0 484 // or if all non-final dimensions are small constants.
aoqi@0 485 if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) {
aoqi@0 486 Node* obj = NULL;
aoqi@0 487 // Set the original stack and the reexecute bit for the interpreter
aoqi@0 488 // to reexecute the multianewarray bytecode if deoptimization happens.
aoqi@0 489 // Do it unconditionally even for one dimension multianewarray.
aoqi@0 490 // Note: the reexecute bit will be set in GraphKit::add_safepoint_edges()
aoqi@0 491 // when AllocateArray node for newarray is created.
aoqi@0 492 { PreserveReexecuteState preexecs(this);
aoqi@0 493 inc_sp(ndimensions);
aoqi@0 494 // Pass 0 as nargs since uncommon trap code does not need to restore stack.
aoqi@0 495 obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0);
aoqi@0 496 } //original reexecute and sp are set back here
aoqi@0 497 push(obj);
aoqi@0 498 return;
aoqi@0 499 }
aoqi@0 500
aoqi@0 501 address fun = NULL;
aoqi@0 502 switch (ndimensions) {
aoqi@0 503 case 1: ShouldNotReachHere(); break;
aoqi@0 504 case 2: fun = OptoRuntime::multianewarray2_Java(); break;
aoqi@0 505 case 3: fun = OptoRuntime::multianewarray3_Java(); break;
aoqi@0 506 case 4: fun = OptoRuntime::multianewarray4_Java(); break;
aoqi@0 507 case 5: fun = OptoRuntime::multianewarray5_Java(); break;
aoqi@0 508 };
aoqi@0 509 Node* c = NULL;
aoqi@0 510
aoqi@0 511 if (fun != NULL) {
aoqi@0 512 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
aoqi@0 513 OptoRuntime::multianewarray_Type(ndimensions),
aoqi@0 514 fun, NULL, TypeRawPtr::BOTTOM,
aoqi@0 515 makecon(TypeKlassPtr::make(array_klass)),
aoqi@0 516 length[0], length[1], length[2],
aoqi@0 517 (ndimensions > 2) ? length[3] : NULL,
aoqi@0 518 (ndimensions > 3) ? length[4] : NULL);
aoqi@0 519 } else {
aoqi@0 520 // Create a java array for dimension sizes
aoqi@0 521 Node* dims = NULL;
aoqi@0 522 { PreserveReexecuteState preexecs(this);
aoqi@0 523 inc_sp(ndimensions);
aoqi@0 524 Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT))));
aoqi@0 525 dims = new_array(dims_array_klass, intcon(ndimensions), 0);
aoqi@0 526
aoqi@0 527 // Fill-in it with values
aoqi@0 528 for (j = 0; j < ndimensions; j++) {
aoqi@0 529 Node *dims_elem = array_element_address(dims, intcon(j), T_INT);
aoqi@0 530 store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered);
aoqi@0 531 }
aoqi@0 532 }
aoqi@0 533
aoqi@0 534 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
aoqi@0 535 OptoRuntime::multianewarrayN_Type(),
aoqi@0 536 OptoRuntime::multianewarrayN_Java(), NULL, TypeRawPtr::BOTTOM,
aoqi@0 537 makecon(TypeKlassPtr::make(array_klass)),
aoqi@0 538 dims);
aoqi@0 539 }
aoqi@0 540 make_slow_call_ex(c, env()->Throwable_klass(), false);
aoqi@0 541
aoqi@0 542 Node* res = _gvn.transform(new (C) ProjNode(c, TypeFunc::Parms));
aoqi@0 543
aoqi@0 544 const Type* type = TypeOopPtr::make_from_klass_raw(array_klass);
aoqi@0 545
aoqi@0 546 // Improve the type: We know it's not null, exact, and of a given length.
aoqi@0 547 type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull);
aoqi@0 548 type = type->is_aryptr()->cast_to_exactness(true);
aoqi@0 549
aoqi@0 550 const TypeInt* ltype = _gvn.find_int_type(length[0]);
aoqi@0 551 if (ltype != NULL)
aoqi@0 552 type = type->is_aryptr()->cast_to_size(ltype);
aoqi@0 553
aoqi@0 554 // We cannot sharpen the nested sub-arrays, since the top level is mutable.
aoqi@0 555
aoqi@0 556 Node* cast = _gvn.transform( new (C) CheckCastPPNode(control(), res, type) );
aoqi@0 557 push(cast);
aoqi@0 558
aoqi@0 559 // Possible improvements:
aoqi@0 560 // - Make a fast path for small multi-arrays. (W/ implicit init. loops.)
aoqi@0 561 // - Issue CastII against length[*] values, to TypeInt::POS.
aoqi@0 562 }

mercurial