annotate hotspot/src/share/vm/ci/ciField.cpp @ 12982:7d7a3e02610e

7152811: Issues in client compiler Reviewed-by: kvn, jrose
author never
date Wed, 04 Apr 2012 20:44:38 -0700
parents d84de97ad847
children 8fc2453a8ec4
rev   line source
duke@1 1 /*
never@12982 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
stefank@7397 26 #include "ci/ciField.hpp"
stefank@7397 27 #include "ci/ciInstanceKlass.hpp"
stefank@7397 28 #include "ci/ciUtilities.hpp"
stefank@7397 29 #include "classfile/systemDictionary.hpp"
stefank@7397 30 #include "gc_interface/collectedHeap.inline.hpp"
stefank@7397 31 #include "interpreter/linkResolver.hpp"
stefank@7397 32 #include "memory/universe.inline.hpp"
stefank@7397 33 #include "oops/oop.inline.hpp"
stefank@7397 34 #include "oops/oop.inline2.hpp"
stefank@7397 35 #include "runtime/fieldDescriptor.hpp"
duke@1 36
duke@1 37 // ciField
duke@1 38 //
duke@1 39 // This class represents the result of a field lookup in the VM.
duke@1 40 // The lookup may not succeed, in which case the information in
duke@1 41 // the ciField will be incomplete.
duke@1 42
duke@1 43 // The ciObjectFactory cannot create circular data structures in one query.
duke@1 44 // To avoid vicious circularities, we initialize ciField::_type to NULL
duke@1 45 // for reference types and derive it lazily from the ciField::_signature.
duke@1 46 // Primitive types are eagerly initialized, and basic layout queries
duke@1 47 // can succeed without initialization, using only the BasicType of the field.
duke@1 48
duke@1 49 // Notes on bootstrapping and shared CI objects: A field is shared if and
duke@1 50 // only if it is (a) non-static and (b) declared by a shared instance klass.
duke@1 51 // This allows non-static field lists to be cached on shared types.
duke@1 52 // Because the _type field is lazily initialized, however, there is a
duke@1 53 // special restriction that a shared field cannot cache an unshared type.
duke@1 54 // This puts a small performance penalty on shared fields with unshared
duke@1 55 // types, such as StackTraceElement[] Throwable.stackTrace.
duke@1 56 // (Throwable is shared because ClassCastException is shared, but
duke@1 57 // StackTraceElement is not presently shared.)
duke@1 58
duke@1 59 // It is not a vicious circularity for a ciField to recursively create
duke@1 60 // the ciSymbols necessary to represent its name and signature.
duke@1 61 // Therefore, these items are created eagerly, and the name and signature
duke@1 62 // of a shared field are themselves shared symbols. This somewhat
duke@1 63 // pollutes the set of shared CI objects: It grows from 50 to 93 items,
duke@1 64 // with all of the additional 43 being uninteresting shared ciSymbols.
duke@1 65 // This adds at most one step to the binary search, an amount which
duke@1 66 // decreases for complex compilation tasks.
duke@1 67
duke@1 68 // ------------------------------------------------------------------
duke@1 69 // ciField::ciField
never@12982 70 ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
duke@1 71 ASSERT_IN_VM;
duke@1 72 CompilerThread *thread = CompilerThread::current();
duke@1 73
duke@1 74 assert(ciObjectFactory::is_initialized(), "not a shared field");
duke@1 75
duke@1 76 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");
duke@1 77
duke@1 78 _cp_index = index;
duke@1 79 constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
duke@1 80
duke@1 81 // Get the field's name, signature, and type.
coleenp@8076 82 Symbol* name = cpool->name_ref_at(index);
coleenp@8076 83 _name = ciEnv::current(thread)->get_symbol(name);
duke@1 84
duke@1 85 int nt_index = cpool->name_and_type_ref_index_at(index);
duke@1 86 int sig_index = cpool->signature_ref_index_at(nt_index);
coleenp@8076 87 Symbol* signature = cpool->symbol_at(sig_index);
coleenp@8076 88 _signature = ciEnv::current(thread)->get_symbol(signature);
duke@1 89
coleenp@8076 90 BasicType field_type = FieldType::basic_type(signature);
duke@1 91
duke@1 92 // If the field is a pointer type, get the klass of the
duke@1 93 // field.
duke@1 94 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@1 95 bool ignore;
duke@1 96 // This is not really a class reference; the index always refers to the
duke@1 97 // field's type signature, as a symbol. Linkage checks do not apply.
twisti@4567 98 _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);
duke@1 99 } else {
duke@1 100 _type = ciType::make(field_type);
duke@1 101 }
duke@1 102
coleenp@8076 103 _name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name);
duke@1 104
duke@1 105 // Get the field's declared holder.
duke@1 106 //
duke@1 107 // Note: we actually create a ciInstanceKlass for this klass,
duke@1 108 // even though we may not need to.
duke@1 109 int holder_index = cpool->klass_ref_index_at(index);
duke@1 110 bool holder_is_accessible;
duke@1 111 ciInstanceKlass* declared_holder =
twisti@4567 112 ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,
twisti@4567 113 holder_is_accessible,
twisti@4567 114 klass)->as_instance_klass();
duke@1 115
duke@1 116 // The declared holder of this field may not have been loaded.
duke@1 117 // Bail out with partial field information.
duke@1 118 if (!holder_is_accessible) {
duke@1 119 // _cp_index and _type have already been set.
duke@1 120 // The default values for _flags and _constant_value will suffice.
duke@1 121 // We need values for _holder, _offset, and _is_constant,
duke@1 122 _holder = declared_holder;
duke@1 123 _offset = -1;
duke@1 124 _is_constant = false;
duke@1 125 return;
duke@1 126 }
duke@1 127
duke@1 128 instanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();
duke@1 129
duke@1 130 // Perform the field lookup.
duke@1 131 fieldDescriptor field_desc;
duke@1 132 klassOop canonical_holder =
coleenp@8076 133 loaded_decl_holder->find_field(name, signature, &field_desc);
duke@1 134 if (canonical_holder == NULL) {
duke@1 135 // Field lookup failed. Will be detected by will_link.
duke@1 136 _holder = declared_holder;
duke@1 137 _offset = -1;
duke@1 138 _is_constant = false;
duke@1 139 return;
duke@1 140 }
duke@1 141
duke@1 142 assert(canonical_holder == field_desc.field_holder(), "just checking");
duke@1 143 initialize_from(&field_desc);
duke@1 144 }
duke@1 145
never@12982 146 ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
duke@1 147 ASSERT_IN_VM;
duke@1 148
duke@1 149 _cp_index = -1;
duke@1 150
duke@1 151 // Get the field's name, signature, and type.
duke@1 152 ciEnv* env = CURRENT_ENV;
coleenp@8076 153 _name = env->get_symbol(fd->name());
coleenp@8076 154 _signature = env->get_symbol(fd->signature());
duke@1 155
duke@1 156 BasicType field_type = fd->field_type();
duke@1 157
duke@1 158 // If the field is a pointer type, get the klass of the
duke@1 159 // field.
duke@1 160 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@1 161 _type = NULL; // must call compute_type on first access
duke@1 162 } else {
duke@1 163 _type = ciType::make(field_type);
duke@1 164 }
duke@1 165
duke@1 166 initialize_from(fd);
duke@1 167
duke@1 168 // Either (a) it is marked shared, or else (b) we are done bootstrapping.
duke@1 169 assert(is_shared() || ciObjectFactory::is_initialized(),
duke@1 170 "bootstrap classes must not create & cache unshared fields");
duke@1 171 }
duke@1 172
jrose@4644 173 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
jrose@4644 174 if (holder == NULL)
jrose@4644 175 return false;
jrose@4644 176 if (holder->name() == ciSymbol::java_lang_System())
jrose@4644 177 // Never trust strangely unstable finals: System.out, etc.
jrose@4644 178 return false;
jrose@4644 179 // Even if general trusting is disabled, trust system-built closures in these packages.
jrose@8676 180 if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
jrose@4644 181 return true;
jrose@4644 182 return TrustFinalNonStaticFields;
jrose@4644 183 }
jrose@4644 184
duke@1 185 void ciField::initialize_from(fieldDescriptor* fd) {
duke@1 186 // Get the flags, offset, and canonical holder of the field.
duke@1 187 _flags = ciFlags(fd->access_flags());
duke@1 188 _offset = fd->offset();
duke@1 189 _holder = CURRENT_ENV->get_object(fd->field_holder())->as_instance_klass();
duke@1 190
duke@1 191 // Check to see if the field is constant.
twisti@4567 192 if (_holder->is_initialized() && this->is_final()) {
twisti@4567 193 if (!this->is_static()) {
jrose@8676 194 // A field can be constant if it's a final static field or if
jrose@8676 195 // it's a final non-static field of a trusted class (classes in
jrose@8676 196 // java.lang.invoke and sun.invoke packages and subpackages).
jrose@4644 197 if (trust_final_non_static_fields(_holder)) {
twisti@4567 198 _is_constant = true;
twisti@4567 199 return;
twisti@4567 200 }
twisti@4567 201 _is_constant = false;
twisti@4567 202 return;
twisti@4567 203 }
twisti@4567 204
duke@1 205 // This field just may be constant. The only cases where it will
duke@1 206 // not be constant are:
duke@1 207 //
duke@1 208 // 1. The field holds a non-perm-space oop. The field is, strictly
duke@1 209 // speaking, constant but we cannot embed non-perm-space oops into
duke@1 210 // generated code. For the time being we need to consider the
duke@1 211 // field to be not constant.
duke@1 212 // 2. The field is a *special* static&final field whose value
duke@1 213 // may change. The three examples are java.lang.System.in,
duke@1 214 // java.lang.System.out, and java.lang.System.err.
duke@1 215
never@8725 216 KlassHandle k = _holder->get_klassOop();
never@4571 217 assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
never@8331 218 if( k() == SystemDictionary::System_klass() ) {
duke@1 219 // Check offsets for case 2: System.in, System.out, or System.err
duke@1 220 if( _offset == java_lang_System::in_offset_in_bytes() ||
duke@1 221 _offset == java_lang_System::out_offset_in_bytes() ||
duke@1 222 _offset == java_lang_System::err_offset_in_bytes() ) {
duke@1 223 _is_constant = false;
duke@1 224 return;
duke@1 225 }
duke@1 226 }
duke@1 227
never@8725 228 Handle mirror = k->java_mirror();
never@8725 229
duke@1 230 _is_constant = true;
duke@1 231 switch(type()->basic_type()) {
duke@1 232 case T_BYTE:
never@8725 233 _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
duke@1 234 break;
duke@1 235 case T_CHAR:
never@8725 236 _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
duke@1 237 break;
duke@1 238 case T_SHORT:
never@8725 239 _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
duke@1 240 break;
duke@1 241 case T_BOOLEAN:
never@8725 242 _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
duke@1 243 break;
duke@1 244 case T_INT:
never@8725 245 _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
duke@1 246 break;
duke@1 247 case T_FLOAT:
never@8725 248 _constant_value = ciConstant(mirror->float_field(_offset));
duke@1 249 break;
duke@1 250 case T_DOUBLE:
never@8725 251 _constant_value = ciConstant(mirror->double_field(_offset));
duke@1 252 break;
duke@1 253 case T_LONG:
never@8725 254 _constant_value = ciConstant(mirror->long_field(_offset));
duke@1 255 break;
duke@1 256 case T_OBJECT:
duke@1 257 case T_ARRAY:
duke@1 258 {
never@8725 259 oop o = mirror->obj_field(_offset);
duke@1 260
duke@1 261 // A field will be "constant" if it is known always to be
duke@1 262 // a non-null reference to an instance of a particular class,
duke@1 263 // or to a particular array. This can happen even if the instance
duke@1 264 // or array is not perm. In such a case, an "unloaded" ciArray
duke@1 265 // or ciInstance is created. The compiler may be able to use
duke@1 266 // information about the object's class (which is exact) or length.
duke@1 267
duke@1 268 if (o == NULL) {
duke@1 269 _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
duke@1 270 } else {
duke@1 271 _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
duke@1 272 assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
duke@1 273 }
duke@1 274 }
duke@1 275 }
duke@1 276 } else {
duke@1 277 _is_constant = false;
duke@1 278 }
duke@1 279 }
duke@1 280
duke@1 281 // ------------------------------------------------------------------
duke@1 282 // ciField::compute_type
duke@1 283 //
duke@1 284 // Lazily compute the type, if it is an instance klass.
duke@1 285 ciType* ciField::compute_type() {
duke@1 286 GUARDED_VM_ENTRY(return compute_type_impl();)
duke@1 287 }
duke@1 288
duke@1 289 ciType* ciField::compute_type_impl() {
jrose@10008 290 ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
duke@1 291 if (!type->is_primitive_type() && is_shared()) {
duke@1 292 // We must not cache a pointer to an unshared type, in a shared field.
duke@1 293 bool type_is_also_shared = false;
duke@1 294 if (type->is_type_array_klass()) {
duke@1 295 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
duke@1 296 } else if (type->is_instance_klass()) {
duke@1 297 type_is_also_shared = type->as_instance_klass()->is_shared();
duke@1 298 } else {
duke@1 299 // Currently there is no 'shared' query for array types.
duke@1 300 type_is_also_shared = !ciObjectFactory::is_initialized();
duke@1 301 }
duke@1 302 if (!type_is_also_shared)
duke@1 303 return type; // Bummer.
duke@1 304 }
duke@1 305 _type = type;
duke@1 306 return type;
duke@1 307 }
duke@1 308
duke@1 309
duke@1 310 // ------------------------------------------------------------------
duke@1 311 // ciField::will_link
duke@1 312 //
duke@1 313 // Can a specific access to this field be made without causing
duke@1 314 // link errors?
duke@1 315 bool ciField::will_link(ciInstanceKlass* accessing_klass,
duke@1 316 Bytecodes::Code bc) {
duke@1 317 VM_ENTRY_MARK;
never@12982 318 assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
never@12982 319 bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,
never@12982 320 "unexpected bytecode");
never@12982 321
duke@1 322 if (_offset == -1) {
duke@1 323 // at creation we couldn't link to our holder so we need to
duke@1 324 // maintain that stance, otherwise there's no safe way to use this
duke@1 325 // ciField.
duke@1 326 return false;
duke@1 327 }
duke@1 328
never@12982 329 // Check for static/nonstatic mismatch
never@12982 330 bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
never@12982 331 if (is_static != this->is_static()) {
never@12982 332 return false;
never@12982 333 }
never@12982 334
never@12982 335 // Get and put can have different accessibility rules
never@12982 336 bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);
never@12982 337 if (is_put) {
never@12982 338 if (_known_to_link_with_put == accessing_klass) {
never@12982 339 return true;
never@12982 340 }
never@12982 341 if (_known_to_link_with_get == accessing_klass) {
never@12982 342 return true;
never@12982 343 }
duke@1 344 }
duke@1 345
duke@1 346 FieldAccessInfo result;
duke@1 347 constantPoolHandle c_pool(THREAD,
duke@1 348 accessing_klass->get_instanceKlass()->constants());
duke@1 349 LinkResolver::resolve_field(result, c_pool, _cp_index,
duke@1 350 Bytecodes::java_code(bc),
duke@1 351 true, false, KILL_COMPILE_ON_FATAL_(false));
duke@1 352
duke@1 353 // update the hit-cache, unless there is a problem with memory scoping:
never@12982 354 if (accessing_klass->is_shared() || !is_shared()) {
never@12982 355 if (is_put) {
never@12982 356 _known_to_link_with_put = accessing_klass;
never@12982 357 } else {
never@12982 358 _known_to_link_with_get = accessing_klass;
never@12982 359 }
never@12982 360 }
duke@1 361
duke@1 362 return true;
duke@1 363 }
duke@1 364
duke@1 365 // ------------------------------------------------------------------
duke@1 366 // ciField::print
duke@1 367 void ciField::print() {
duke@1 368 tty->print("<ciField ");
duke@1 369 _holder->print_name();
duke@1 370 tty->print(".");
duke@1 371 _name->print_symbol();
duke@1 372 tty->print(" offset=%d type=", _offset);
duke@1 373 if (_type != NULL) _type->print_name();
duke@1 374 else tty->print("(reference)");
duke@1 375 tty->print(" is_constant=%s", bool_to_str(_is_constant));
kvn@6177 376 if (_is_constant && is_static()) {
duke@1 377 tty->print(" constant_value=");
duke@1 378 _constant_value.print();
duke@1 379 }
duke@1 380 tty->print(">");
duke@1 381 }
duke@1 382
duke@1 383 // ------------------------------------------------------------------
duke@1 384 // ciField::print_name_on
duke@1 385 //
duke@1 386 // Print the name of this field
duke@1 387 void ciField::print_name_on(outputStream* st) {
duke@1 388 name()->print_symbol_on(st);
duke@1 389 }