src/share/vm/ci/ciTypeFlow.hpp

Wed, 06 Jan 2010 14:22:39 -0800

author
never
date
Wed, 06 Jan 2010 14:22:39 -0800
changeset 1577
4ce7240d622c
parent 815
eb28cf662f56
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6914300: ciEnv should export all well known classes
Reviewed-by: kvn, twisti

duke@435 1 /*
xdono@772 2 * Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25
duke@435 26 class ciTypeFlow : public ResourceObj {
duke@435 27 private:
duke@435 28 ciEnv* _env;
duke@435 29 ciMethod* _method;
duke@435 30 ciMethodBlocks* _methodBlocks;
duke@435 31 int _osr_bci;
duke@435 32
duke@435 33 // information cached from the method:
duke@435 34 int _max_locals;
duke@435 35 int _max_stack;
duke@435 36 int _code_size;
never@802 37 bool _has_irreducible_entry;
duke@435 38
duke@435 39 const char* _failure_reason;
duke@435 40
duke@435 41 public:
duke@435 42 class StateVector;
never@802 43 class Loop;
duke@435 44 class Block;
duke@435 45
duke@435 46 // Build a type flow analyzer
duke@435 47 // Do an OSR analysis if osr_bci >= 0.
duke@435 48 ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci = InvocationEntryBci);
duke@435 49
duke@435 50 // Accessors
duke@435 51 ciMethod* method() const { return _method; }
duke@435 52 ciEnv* env() { return _env; }
duke@435 53 Arena* arena() { return _env->arena(); }
duke@435 54 bool is_osr_flow() const{ return _osr_bci != InvocationEntryBci; }
duke@435 55 int start_bci() const { return is_osr_flow()? _osr_bci: 0; }
duke@435 56 int max_locals() const { return _max_locals; }
duke@435 57 int max_stack() const { return _max_stack; }
duke@435 58 int max_cells() const { return _max_locals + _max_stack; }
duke@435 59 int code_size() const { return _code_size; }
never@802 60 bool has_irreducible_entry() const { return _has_irreducible_entry; }
duke@435 61
duke@435 62 // Represents information about an "active" jsr call. This
duke@435 63 // class represents a call to the routine at some entry address
duke@435 64 // with some distinct return address.
duke@435 65 class JsrRecord : public ResourceObj {
duke@435 66 private:
duke@435 67 int _entry_address;
duke@435 68 int _return_address;
duke@435 69 public:
duke@435 70 JsrRecord(int entry_address, int return_address) {
duke@435 71 _entry_address = entry_address;
duke@435 72 _return_address = return_address;
duke@435 73 }
duke@435 74
duke@435 75 int entry_address() const { return _entry_address; }
duke@435 76 int return_address() const { return _return_address; }
duke@435 77
duke@435 78 void print_on(outputStream* st) const {
duke@435 79 #ifndef PRODUCT
duke@435 80 st->print("%d->%d", entry_address(), return_address());
duke@435 81 #endif
duke@435 82 }
duke@435 83 };
duke@435 84
duke@435 85 // A JsrSet represents some set of JsrRecords. This class
duke@435 86 // is used to record a set of all jsr routines which we permit
duke@435 87 // execution to return (ret) from.
duke@435 88 //
duke@435 89 // During abstract interpretation, JsrSets are used to determine
duke@435 90 // whether two paths which reach a given block are unique, and
duke@435 91 // should be cloned apart, or are compatible, and should merge
duke@435 92 // together.
duke@435 93 //
duke@435 94 // Note that different amounts of effort can be expended determining
duke@435 95 // if paths are compatible. <DISCUSSION>
duke@435 96 class JsrSet : public ResourceObj {
duke@435 97 private:
duke@435 98 GrowableArray<JsrRecord*>* _set;
duke@435 99
duke@435 100 JsrRecord* record_at(int i) {
duke@435 101 return _set->at(i);
duke@435 102 }
duke@435 103
duke@435 104 // Insert the given JsrRecord into the JsrSet, maintaining the order
duke@435 105 // of the set and replacing any element with the same entry address.
duke@435 106 void insert_jsr_record(JsrRecord* record);
duke@435 107
duke@435 108 // Remove the JsrRecord with the given return address from the JsrSet.
duke@435 109 void remove_jsr_record(int return_address);
duke@435 110
duke@435 111 public:
duke@435 112 JsrSet(Arena* arena, int default_len = 4);
duke@435 113
duke@435 114 // Copy this JsrSet.
duke@435 115 void copy_into(JsrSet* jsrs);
duke@435 116
duke@435 117 // Is this JsrSet compatible with some other JsrSet?
duke@435 118 bool is_compatible_with(JsrSet* other);
duke@435 119
duke@435 120 // Apply the effect of a single bytecode to the JsrSet.
duke@435 121 void apply_control(ciTypeFlow* analyzer,
duke@435 122 ciBytecodeStream* str,
duke@435 123 StateVector* state);
duke@435 124
duke@435 125 // What is the cardinality of this set?
duke@435 126 int size() const { return _set->length(); }
duke@435 127
duke@435 128 void print_on(outputStream* st) const PRODUCT_RETURN;
duke@435 129 };
duke@435 130
never@802 131 class LocalSet VALUE_OBJ_CLASS_SPEC {
never@802 132 private:
never@802 133 enum Constants { max = 63 };
never@802 134 uint64_t _bits;
never@802 135 public:
never@802 136 LocalSet() : _bits(0) {}
never@802 137 void add(uint32_t i) { if (i < (uint32_t)max) _bits |= (1LL << i); }
never@802 138 void add(LocalSet* ls) { _bits |= ls->_bits; }
never@802 139 bool test(uint32_t i) const { return i < (uint32_t)max ? (_bits>>i)&1U : true; }
never@802 140 void clear() { _bits = 0; }
never@802 141 void print_on(outputStream* st, int limit) const PRODUCT_RETURN;
never@802 142 };
never@802 143
duke@435 144 // Used as a combined index for locals and temps
duke@435 145 enum Cell {
never@738 146 Cell_0, Cell_max = INT_MAX
duke@435 147 };
duke@435 148
duke@435 149 // A StateVector summarizes the type information at some
duke@435 150 // point in the program
duke@435 151 class StateVector : public ResourceObj {
duke@435 152 private:
duke@435 153 ciType** _types;
duke@435 154 int _stack_size;
duke@435 155 int _monitor_count;
duke@435 156 ciTypeFlow* _outer;
duke@435 157
duke@435 158 int _trap_bci;
duke@435 159 int _trap_index;
duke@435 160
never@802 161 LocalSet _def_locals; // For entire block
never@802 162
duke@435 163 static ciType* type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer);
duke@435 164
duke@435 165 public:
duke@435 166 // Special elements in our type lattice.
duke@435 167 enum {
duke@435 168 T_TOP = T_VOID, // why not?
duke@435 169 T_BOTTOM = T_CONFLICT,
duke@435 170 T_LONG2 = T_SHORT, // 2nd word of T_LONG
duke@435 171 T_DOUBLE2 = T_CHAR, // 2nd word of T_DOUBLE
duke@435 172 T_NULL = T_BYTE // for now.
duke@435 173 };
duke@435 174 static ciType* top_type() { return ciType::make((BasicType)T_TOP); }
duke@435 175 static ciType* bottom_type() { return ciType::make((BasicType)T_BOTTOM); }
duke@435 176 static ciType* long2_type() { return ciType::make((BasicType)T_LONG2); }
duke@435 177 static ciType* double2_type(){ return ciType::make((BasicType)T_DOUBLE2); }
duke@435 178 static ciType* null_type() { return ciType::make((BasicType)T_NULL); }
duke@435 179
duke@435 180 static ciType* half_type(ciType* t) {
duke@435 181 switch (t->basic_type()) {
duke@435 182 case T_LONG: return long2_type();
duke@435 183 case T_DOUBLE: return double2_type();
duke@435 184 default: ShouldNotReachHere(); return NULL;
duke@435 185 }
duke@435 186 }
duke@435 187
duke@435 188 // The meet operation for our type lattice.
duke@435 189 ciType* type_meet(ciType* t1, ciType* t2) {
duke@435 190 return type_meet_internal(t1, t2, outer());
duke@435 191 }
duke@435 192
duke@435 193 // Accessors
duke@435 194 ciTypeFlow* outer() const { return _outer; }
duke@435 195
duke@435 196 int stack_size() const { return _stack_size; }
duke@435 197 void set_stack_size(int ss) { _stack_size = ss; }
duke@435 198
duke@435 199 int monitor_count() const { return _monitor_count; }
duke@435 200 void set_monitor_count(int mc) { _monitor_count = mc; }
duke@435 201
never@802 202 LocalSet* def_locals() { return &_def_locals; }
never@802 203 const LocalSet* def_locals() const { return &_def_locals; }
never@802 204
duke@435 205 static Cell start_cell() { return (Cell)0; }
duke@435 206 static Cell next_cell(Cell c) { return (Cell)(((int)c) + 1); }
duke@435 207 Cell limit_cell() const {
duke@435 208 return (Cell)(outer()->max_locals() + stack_size());
duke@435 209 }
duke@435 210
duke@435 211 // Cell creation
duke@435 212 Cell local(int lnum) const {
duke@435 213 assert(lnum < outer()->max_locals(), "index check");
duke@435 214 return (Cell)(lnum);
duke@435 215 }
duke@435 216
duke@435 217 Cell stack(int snum) const {
duke@435 218 assert(snum < stack_size(), "index check");
duke@435 219 return (Cell)(outer()->max_locals() + snum);
duke@435 220 }
duke@435 221
duke@435 222 Cell tos() const { return stack(stack_size()-1); }
duke@435 223
duke@435 224 // For external use only:
duke@435 225 ciType* local_type_at(int i) const { return type_at(local(i)); }
duke@435 226 ciType* stack_type_at(int i) const { return type_at(stack(i)); }
duke@435 227
duke@435 228 // Accessors for the type of some Cell c
duke@435 229 ciType* type_at(Cell c) const {
duke@435 230 assert(start_cell() <= c && c < limit_cell(), "out of bounds");
duke@435 231 return _types[c];
duke@435 232 }
duke@435 233
duke@435 234 void set_type_at(Cell c, ciType* type) {
duke@435 235 assert(start_cell() <= c && c < limit_cell(), "out of bounds");
duke@435 236 _types[c] = type;
duke@435 237 }
duke@435 238
duke@435 239 // Top-of-stack operations.
duke@435 240 void set_type_at_tos(ciType* type) { set_type_at(tos(), type); }
duke@435 241 ciType* type_at_tos() const { return type_at(tos()); }
duke@435 242
duke@435 243 void push(ciType* type) {
duke@435 244 _stack_size++;
duke@435 245 set_type_at_tos(type);
duke@435 246 }
duke@435 247 void pop() {
duke@435 248 debug_only(set_type_at_tos(bottom_type()));
duke@435 249 _stack_size--;
duke@435 250 }
duke@435 251 ciType* pop_value() {
duke@435 252 ciType* t = type_at_tos();
duke@435 253 pop();
duke@435 254 return t;
duke@435 255 }
duke@435 256
duke@435 257 // Convenience operations.
duke@435 258 bool is_reference(ciType* type) const {
duke@435 259 return type == null_type() || !type->is_primitive_type();
duke@435 260 }
duke@435 261 bool is_int(ciType* type) const {
duke@435 262 return type->basic_type() == T_INT;
duke@435 263 }
duke@435 264 bool is_long(ciType* type) const {
duke@435 265 return type->basic_type() == T_LONG;
duke@435 266 }
duke@435 267 bool is_float(ciType* type) const {
duke@435 268 return type->basic_type() == T_FLOAT;
duke@435 269 }
duke@435 270 bool is_double(ciType* type) const {
duke@435 271 return type->basic_type() == T_DOUBLE;
duke@435 272 }
duke@435 273
never@802 274 void store_to_local(int lnum) {
never@802 275 _def_locals.add((uint) lnum);
never@802 276 }
never@802 277
duke@435 278 void push_translate(ciType* type);
duke@435 279
duke@435 280 void push_int() {
duke@435 281 push(ciType::make(T_INT));
duke@435 282 }
duke@435 283 void pop_int() {
duke@435 284 assert(is_int(type_at_tos()), "must be integer");
duke@435 285 pop();
duke@435 286 }
duke@435 287 void check_int(Cell c) {
duke@435 288 assert(is_int(type_at(c)), "must be integer");
duke@435 289 }
duke@435 290 void push_double() {
duke@435 291 push(ciType::make(T_DOUBLE));
duke@435 292 push(double2_type());
duke@435 293 }
duke@435 294 void pop_double() {
duke@435 295 assert(type_at_tos() == double2_type(), "must be 2nd half");
duke@435 296 pop();
duke@435 297 assert(is_double(type_at_tos()), "must be double");
duke@435 298 pop();
duke@435 299 }
duke@435 300 void push_float() {
duke@435 301 push(ciType::make(T_FLOAT));
duke@435 302 }
duke@435 303 void pop_float() {
duke@435 304 assert(is_float(type_at_tos()), "must be float");
duke@435 305 pop();
duke@435 306 }
duke@435 307 void push_long() {
duke@435 308 push(ciType::make(T_LONG));
duke@435 309 push(long2_type());
duke@435 310 }
duke@435 311 void pop_long() {
duke@435 312 assert(type_at_tos() == long2_type(), "must be 2nd half");
duke@435 313 pop();
duke@435 314 assert(is_long(type_at_tos()), "must be long");
duke@435 315 pop();
duke@435 316 }
duke@435 317 void push_object(ciKlass* klass) {
duke@435 318 push(klass);
duke@435 319 }
duke@435 320 void pop_object() {
duke@435 321 assert(is_reference(type_at_tos()), "must be reference type");
duke@435 322 pop();
duke@435 323 }
duke@435 324 void pop_array() {
duke@435 325 assert(type_at_tos() == null_type() ||
duke@435 326 type_at_tos()->is_array_klass(), "must be array type");
duke@435 327 pop();
duke@435 328 }
duke@435 329 // pop_objArray and pop_typeArray narrow the tos to ciObjArrayKlass
duke@435 330 // or ciTypeArrayKlass (resp.). In the rare case that an explicit
duke@435 331 // null is popped from the stack, we return NULL. Caller beware.
duke@435 332 ciObjArrayKlass* pop_objArray() {
duke@435 333 ciType* array = pop_value();
duke@435 334 if (array == null_type()) return NULL;
duke@435 335 assert(array->is_obj_array_klass(), "must be object array type");
duke@435 336 return array->as_obj_array_klass();
duke@435 337 }
duke@435 338 ciTypeArrayKlass* pop_typeArray() {
duke@435 339 ciType* array = pop_value();
duke@435 340 if (array == null_type()) return NULL;
duke@435 341 assert(array->is_type_array_klass(), "must be prim array type");
duke@435 342 return array->as_type_array_klass();
duke@435 343 }
duke@435 344 void push_null() {
duke@435 345 push(null_type());
duke@435 346 }
duke@435 347 void do_null_assert(ciKlass* unloaded_klass);
duke@435 348
duke@435 349 // Helper convenience routines.
duke@435 350 void do_aaload(ciBytecodeStream* str);
duke@435 351 void do_checkcast(ciBytecodeStream* str);
duke@435 352 void do_getfield(ciBytecodeStream* str);
duke@435 353 void do_getstatic(ciBytecodeStream* str);
duke@435 354 void do_invoke(ciBytecodeStream* str, bool has_receiver);
duke@435 355 void do_jsr(ciBytecodeStream* str);
duke@435 356 void do_ldc(ciBytecodeStream* str);
duke@435 357 void do_multianewarray(ciBytecodeStream* str);
duke@435 358 void do_new(ciBytecodeStream* str);
duke@435 359 void do_newarray(ciBytecodeStream* str);
duke@435 360 void do_putfield(ciBytecodeStream* str);
duke@435 361 void do_putstatic(ciBytecodeStream* str);
duke@435 362 void do_ret(ciBytecodeStream* str);
duke@435 363
duke@435 364 void overwrite_local_double_long(int index) {
duke@435 365 // Invalidate the previous local if it contains first half of
duke@435 366 // a double or long value since it's seconf half is being overwritten.
duke@435 367 int prev_index = index - 1;
duke@435 368 if (prev_index >= 0 &&
duke@435 369 (is_double(type_at(local(prev_index))) ||
duke@435 370 is_long(type_at(local(prev_index))))) {
duke@435 371 set_type_at(local(prev_index), bottom_type());
duke@435 372 }
duke@435 373 }
duke@435 374
duke@435 375 void load_local_object(int index) {
duke@435 376 ciType* type = type_at(local(index));
duke@435 377 assert(is_reference(type), "must be reference type");
duke@435 378 push(type);
duke@435 379 }
duke@435 380 void store_local_object(int index) {
duke@435 381 ciType* type = pop_value();
duke@435 382 assert(is_reference(type) || type->is_return_address(),
duke@435 383 "must be reference type or return address");
duke@435 384 overwrite_local_double_long(index);
duke@435 385 set_type_at(local(index), type);
never@802 386 store_to_local(index);
duke@435 387 }
duke@435 388
duke@435 389 void load_local_double(int index) {
duke@435 390 ciType* type = type_at(local(index));
duke@435 391 ciType* type2 = type_at(local(index+1));
duke@435 392 assert(is_double(type), "must be double type");
duke@435 393 assert(type2 == double2_type(), "must be 2nd half");
duke@435 394 push(type);
duke@435 395 push(double2_type());
duke@435 396 }
duke@435 397 void store_local_double(int index) {
duke@435 398 ciType* type2 = pop_value();
duke@435 399 ciType* type = pop_value();
duke@435 400 assert(is_double(type), "must be double");
duke@435 401 assert(type2 == double2_type(), "must be 2nd half");
duke@435 402 overwrite_local_double_long(index);
duke@435 403 set_type_at(local(index), type);
duke@435 404 set_type_at(local(index+1), type2);
never@802 405 store_to_local(index);
never@802 406 store_to_local(index+1);
duke@435 407 }
duke@435 408
duke@435 409 void load_local_float(int index) {
duke@435 410 ciType* type = type_at(local(index));
duke@435 411 assert(is_float(type), "must be float type");
duke@435 412 push(type);
duke@435 413 }
duke@435 414 void store_local_float(int index) {
duke@435 415 ciType* type = pop_value();
duke@435 416 assert(is_float(type), "must be float type");
duke@435 417 overwrite_local_double_long(index);
duke@435 418 set_type_at(local(index), type);
never@802 419 store_to_local(index);
duke@435 420 }
duke@435 421
duke@435 422 void load_local_int(int index) {
duke@435 423 ciType* type = type_at(local(index));
duke@435 424 assert(is_int(type), "must be int type");
duke@435 425 push(type);
duke@435 426 }
duke@435 427 void store_local_int(int index) {
duke@435 428 ciType* type = pop_value();
duke@435 429 assert(is_int(type), "must be int type");
duke@435 430 overwrite_local_double_long(index);
duke@435 431 set_type_at(local(index), type);
never@802 432 store_to_local(index);
duke@435 433 }
duke@435 434
duke@435 435 void load_local_long(int index) {
duke@435 436 ciType* type = type_at(local(index));
duke@435 437 ciType* type2 = type_at(local(index+1));
duke@435 438 assert(is_long(type), "must be long type");
duke@435 439 assert(type2 == long2_type(), "must be 2nd half");
duke@435 440 push(type);
duke@435 441 push(long2_type());
duke@435 442 }
duke@435 443 void store_local_long(int index) {
duke@435 444 ciType* type2 = pop_value();
duke@435 445 ciType* type = pop_value();
duke@435 446 assert(is_long(type), "must be long");
duke@435 447 assert(type2 == long2_type(), "must be 2nd half");
duke@435 448 overwrite_local_double_long(index);
duke@435 449 set_type_at(local(index), type);
duke@435 450 set_type_at(local(index+1), type2);
never@802 451 store_to_local(index);
never@802 452 store_to_local(index+1);
duke@435 453 }
duke@435 454
duke@435 455 // Stop interpretation of this path with a trap.
duke@435 456 void trap(ciBytecodeStream* str, ciKlass* klass, int index);
duke@435 457
duke@435 458 public:
duke@435 459 StateVector(ciTypeFlow* outer);
duke@435 460
duke@435 461 // Copy our value into some other StateVector
duke@435 462 void copy_into(StateVector* copy) const;
duke@435 463
duke@435 464 // Meets this StateVector with another, destructively modifying this
duke@435 465 // one. Returns true if any modification takes place.
duke@435 466 bool meet(const StateVector* incoming);
duke@435 467
duke@435 468 // Ditto, except that the incoming state is coming from an exception.
duke@435 469 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming);
duke@435 470
duke@435 471 // Apply the effect of one bytecode to this StateVector
duke@435 472 bool apply_one_bytecode(ciBytecodeStream* stream);
duke@435 473
duke@435 474 // What is the bci of the trap?
duke@435 475 int trap_bci() { return _trap_bci; }
duke@435 476
duke@435 477 // What is the index associated with the trap?
duke@435 478 int trap_index() { return _trap_index; }
duke@435 479
duke@435 480 void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN;
duke@435 481 void print_on(outputStream* st) const PRODUCT_RETURN;
duke@435 482 };
duke@435 483
duke@435 484 // Parameter for "find_block" calls:
never@802 485 // Describes the difference between a public and backedge copy.
duke@435 486 enum CreateOption {
duke@435 487 create_public_copy,
never@802 488 create_backedge_copy,
duke@435 489 no_create
duke@435 490 };
duke@435 491
never@802 492 // Successor iterator
never@802 493 class SuccIter : public StackObj {
never@802 494 private:
never@802 495 Block* _pred;
never@802 496 int _index;
never@802 497 Block* _succ;
never@802 498 public:
never@802 499 SuccIter() : _pred(NULL), _index(-1), _succ(NULL) {}
never@802 500 SuccIter(Block* pred) : _pred(pred), _index(-1), _succ(NULL) { next(); }
never@802 501 int index() { return _index; }
never@802 502 Block* pred() { return _pred; } // Return predecessor
never@802 503 bool done() { return _index < 0; } // Finished?
never@802 504 Block* succ() { return _succ; } // Return current successor
never@802 505 void next(); // Advance
never@802 506 void set_succ(Block* succ); // Update current successor
never@802 507 bool is_normal_ctrl() { return index() < _pred->successors()->length(); }
never@802 508 };
never@802 509
duke@435 510 // A basic block
duke@435 511 class Block : public ResourceObj {
duke@435 512 private:
duke@435 513 ciBlock* _ciblock;
duke@435 514 GrowableArray<Block*>* _exceptions;
duke@435 515 GrowableArray<ciInstanceKlass*>* _exc_klasses;
duke@435 516 GrowableArray<Block*>* _successors;
duke@435 517 StateVector* _state;
duke@435 518 JsrSet* _jsrs;
duke@435 519
duke@435 520 int _trap_bci;
duke@435 521 int _trap_index;
duke@435 522
never@802 523 // pre_order, assigned at first visit. Used as block ID and "visited" tag
duke@435 524 int _pre_order;
duke@435 525
never@802 526 // A post-order, used to compute the reverse post order (RPO) provided to the client
never@802 527 int _post_order; // used to compute rpo
never@802 528
never@802 529 // Has this block been cloned for a loop backedge?
never@802 530 bool _backedge_copy;
duke@435 531
duke@435 532 // A pointer used for our internal work list
never@802 533 Block* _next;
never@802 534 bool _on_work_list; // on the work list
never@802 535 Block* _rpo_next; // Reverse post order list
never@802 536
never@802 537 // Loop info
never@802 538 Loop* _loop; // nearest loop
never@802 539 bool _irreducible_entry; // entry to irreducible loop
never@802 540 bool _exception_entry; // entry to exception handler
duke@435 541
duke@435 542 ciBlock* ciblock() const { return _ciblock; }
duke@435 543 StateVector* state() const { return _state; }
duke@435 544
duke@435 545 // Compute the exceptional successors and types for this Block.
duke@435 546 void compute_exceptions();
duke@435 547
duke@435 548 public:
duke@435 549 // constructors
duke@435 550 Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs);
duke@435 551
duke@435 552 void set_trap(int trap_bci, int trap_index) {
duke@435 553 _trap_bci = trap_bci;
duke@435 554 _trap_index = trap_index;
duke@435 555 assert(has_trap(), "");
duke@435 556 }
duke@435 557 bool has_trap() const { return _trap_bci != -1; }
duke@435 558 int trap_bci() const { assert(has_trap(), ""); return _trap_bci; }
duke@435 559 int trap_index() const { assert(has_trap(), ""); return _trap_index; }
duke@435 560
duke@435 561 // accessors
duke@435 562 ciTypeFlow* outer() const { return state()->outer(); }
duke@435 563 int start() const { return _ciblock->start_bci(); }
duke@435 564 int limit() const { return _ciblock->limit_bci(); }
duke@435 565 int control() const { return _ciblock->control_bci(); }
never@802 566 JsrSet* jsrs() const { return _jsrs; }
duke@435 567
never@802 568 bool is_backedge_copy() const { return _backedge_copy; }
never@802 569 void set_backedge_copy(bool z);
never@802 570 int backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); }
duke@435 571
duke@435 572 // access to entry state
duke@435 573 int stack_size() const { return _state->stack_size(); }
duke@435 574 int monitor_count() const { return _state->monitor_count(); }
duke@435 575 ciType* local_type_at(int i) const { return _state->local_type_at(i); }
duke@435 576 ciType* stack_type_at(int i) const { return _state->stack_type_at(i); }
duke@435 577
never@802 578 // Data flow on locals
never@802 579 bool is_invariant_local(uint v) const {
never@802 580 assert(is_loop_head(), "only loop heads");
never@802 581 // Find outermost loop with same loop head
never@802 582 Loop* lp = loop();
never@802 583 while (lp->parent() != NULL) {
never@802 584 if (lp->parent()->head() != lp->head()) break;
never@802 585 lp = lp->parent();
never@802 586 }
never@802 587 return !lp->def_locals()->test(v);
never@802 588 }
never@802 589 LocalSet* def_locals() { return _state->def_locals(); }
never@802 590 const LocalSet* def_locals() const { return _state->def_locals(); }
never@802 591
duke@435 592 // Get the successors for this Block.
duke@435 593 GrowableArray<Block*>* successors(ciBytecodeStream* str,
duke@435 594 StateVector* state,
duke@435 595 JsrSet* jsrs);
duke@435 596 GrowableArray<Block*>* successors() {
duke@435 597 assert(_successors != NULL, "must be filled in");
duke@435 598 return _successors;
duke@435 599 }
duke@435 600
duke@435 601 // Get the exceptional successors for this Block.
duke@435 602 GrowableArray<Block*>* exceptions() {
duke@435 603 if (_exceptions == NULL) {
duke@435 604 compute_exceptions();
duke@435 605 }
duke@435 606 return _exceptions;
duke@435 607 }
duke@435 608
duke@435 609 // Get the exception klasses corresponding to the
duke@435 610 // exceptional successors for this Block.
duke@435 611 GrowableArray<ciInstanceKlass*>* exc_klasses() {
duke@435 612 if (_exc_klasses == NULL) {
duke@435 613 compute_exceptions();
duke@435 614 }
duke@435 615 return _exc_klasses;
duke@435 616 }
duke@435 617
duke@435 618 // Is this Block compatible with a given JsrSet?
duke@435 619 bool is_compatible_with(JsrSet* other) {
duke@435 620 return _jsrs->is_compatible_with(other);
duke@435 621 }
duke@435 622
duke@435 623 // Copy the value of our state vector into another.
duke@435 624 void copy_state_into(StateVector* copy) const {
duke@435 625 _state->copy_into(copy);
duke@435 626 }
duke@435 627
duke@435 628 // Copy the value of our JsrSet into another
duke@435 629 void copy_jsrs_into(JsrSet* copy) const {
duke@435 630 _jsrs->copy_into(copy);
duke@435 631 }
duke@435 632
duke@435 633 // Meets the start state of this block with another state, destructively
duke@435 634 // modifying this one. Returns true if any modification takes place.
duke@435 635 bool meet(const StateVector* incoming) {
duke@435 636 return state()->meet(incoming);
duke@435 637 }
duke@435 638
duke@435 639 // Ditto, except that the incoming state is coming from an
duke@435 640 // exception path. This means the stack is replaced by the
duke@435 641 // appropriate exception type.
duke@435 642 bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) {
duke@435 643 return state()->meet_exception(exc, incoming);
duke@435 644 }
duke@435 645
duke@435 646 // Work list manipulation
duke@435 647 void set_next(Block* block) { _next = block; }
duke@435 648 Block* next() const { return _next; }
duke@435 649
duke@435 650 void set_on_work_list(bool c) { _on_work_list = c; }
duke@435 651 bool is_on_work_list() const { return _on_work_list; }
duke@435 652
duke@435 653 bool has_pre_order() const { return _pre_order >= 0; }
never@802 654 void set_pre_order(int po) { assert(!has_pre_order(), ""); _pre_order = po; }
duke@435 655 int pre_order() const { assert(has_pre_order(), ""); return _pre_order; }
never@802 656 void set_next_pre_order() { set_pre_order(outer()->inc_next_pre_order()); }
duke@435 657 bool is_start() const { return _pre_order == outer()->start_block_num(); }
duke@435 658
never@802 659 // Reverse post order
never@802 660 void df_init();
never@802 661 bool has_post_order() const { return _post_order >= 0; }
never@802 662 void set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; }
never@802 663 void reset_post_order(int o){ _post_order = o; }
never@802 664 int post_order() const { assert(has_post_order(), ""); return _post_order; }
never@802 665
never@802 666 bool has_rpo() const { return has_post_order() && outer()->have_block_count(); }
never@802 667 int rpo() const { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; }
never@802 668 void set_rpo_next(Block* b) { _rpo_next = b; }
never@802 669 Block* rpo_next() { return _rpo_next; }
never@802 670
never@802 671 // Loops
never@802 672 Loop* loop() const { return _loop; }
never@802 673 void set_loop(Loop* lp) { _loop = lp; }
never@802 674 bool is_loop_head() const { return _loop && _loop->head() == this; }
never@802 675 void set_irreducible_entry(bool c) { _irreducible_entry = c; }
never@802 676 bool is_irreducible_entry() const { return _irreducible_entry; }
never@802 677 bool is_visited() const { return has_pre_order(); }
never@802 678 bool is_post_visited() const { return has_post_order(); }
never@802 679 bool is_clonable_exit(Loop* lp);
never@802 680 Block* looping_succ(Loop* lp); // Successor inside of loop
never@802 681 bool is_single_entry_loop_head() const {
never@802 682 if (!is_loop_head()) return false;
never@802 683 for (Loop* lp = loop(); lp != NULL && lp->head() == this; lp = lp->parent())
never@802 684 if (lp->is_irreducible()) return false;
never@802 685 return true;
never@802 686 }
duke@435 687
duke@435 688 void print_value_on(outputStream* st) const PRODUCT_RETURN;
duke@435 689 void print_on(outputStream* st) const PRODUCT_RETURN;
duke@435 690 };
duke@435 691
never@802 692 // Loop
never@802 693 class Loop : public ResourceObj {
never@802 694 private:
never@802 695 Loop* _parent;
never@802 696 Loop* _sibling; // List of siblings, null terminated
never@802 697 Loop* _child; // Head of child list threaded thru sibling pointer
never@802 698 Block* _head; // Head of loop
never@802 699 Block* _tail; // Tail of loop
never@802 700 bool _irreducible;
never@802 701 LocalSet _def_locals;
never@802 702
never@802 703 public:
never@802 704 Loop(Block* head, Block* tail) :
never@802 705 _head(head), _tail(tail),
never@802 706 _parent(NULL), _sibling(NULL), _child(NULL),
never@802 707 _irreducible(false), _def_locals() {}
never@802 708
never@802 709 Loop* parent() const { return _parent; }
never@802 710 Loop* sibling() const { return _sibling; }
never@802 711 Loop* child() const { return _child; }
never@802 712 Block* head() const { return _head; }
never@802 713 Block* tail() const { return _tail; }
never@802 714 void set_parent(Loop* p) { _parent = p; }
never@802 715 void set_sibling(Loop* s) { _sibling = s; }
never@802 716 void set_child(Loop* c) { _child = c; }
never@802 717 void set_head(Block* hd) { _head = hd; }
never@802 718 void set_tail(Block* tl) { _tail = tl; }
never@802 719
never@802 720 int depth() const; // nesting depth
never@802 721
never@802 722 // Returns true if lp is a nested loop or us.
never@802 723 bool contains(Loop* lp) const;
never@802 724 bool contains(Block* blk) const { return contains(blk->loop()); }
never@802 725
never@802 726 // Data flow on locals
never@802 727 LocalSet* def_locals() { return &_def_locals; }
never@802 728 const LocalSet* def_locals() const { return &_def_locals; }
never@802 729
never@802 730 // Merge the branch lp into this branch, sorting on the loop head
never@802 731 // pre_orders. Returns the new branch.
never@802 732 Loop* sorted_merge(Loop* lp);
never@802 733
never@802 734 // Mark non-single entry to loop
never@802 735 void set_irreducible(Block* entry) {
never@802 736 _irreducible = true;
never@802 737 entry->set_irreducible_entry(true);
never@802 738 }
never@802 739 bool is_irreducible() const { return _irreducible; }
never@802 740
never@802 741 bool is_root() const { return _tail->pre_order() == max_jint; }
never@802 742
never@802 743 void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN;
never@802 744 };
never@802 745
never@802 746 // Postorder iteration over the loop tree.
never@802 747 class PostorderLoops : public StackObj {
never@802 748 private:
never@802 749 Loop* _root;
never@802 750 Loop* _current;
never@802 751 public:
never@802 752 PostorderLoops(Loop* root) : _root(root), _current(root) {
never@802 753 while (_current->child() != NULL) {
never@802 754 _current = _current->child();
never@802 755 }
never@802 756 }
never@802 757 bool done() { return _current == NULL; } // Finished iterating?
never@802 758 void next(); // Advance to next loop
never@802 759 Loop* current() { return _current; } // Return current loop.
never@802 760 };
never@802 761
never@802 762 // Preorder iteration over the loop tree.
never@802 763 class PreorderLoops : public StackObj {
never@802 764 private:
never@802 765 Loop* _root;
never@802 766 Loop* _current;
never@802 767 public:
never@802 768 PreorderLoops(Loop* root) : _root(root), _current(root) {}
never@802 769 bool done() { return _current == NULL; } // Finished iterating?
never@802 770 void next(); // Advance to next loop
never@802 771 Loop* current() { return _current; } // Return current loop.
never@802 772 };
never@802 773
duke@435 774 // Standard indexes of successors, for various bytecodes.
duke@435 775 enum {
duke@435 776 FALL_THROUGH = 0, // normal control
duke@435 777 IF_NOT_TAKEN = 0, // the not-taken branch of an if (i.e., fall-through)
duke@435 778 IF_TAKEN = 1, // the taken branch of an if
duke@435 779 GOTO_TARGET = 0, // unique successor for goto, jsr, or ret
duke@435 780 SWITCH_DEFAULT = 0, // default branch of a switch
duke@435 781 SWITCH_CASES = 1 // first index for any non-default switch branches
duke@435 782 // Unlike in other blocks, the successors of a switch are listed uniquely.
duke@435 783 };
duke@435 784
duke@435 785 private:
duke@435 786 // A mapping from pre_order to Blocks. This array is created
duke@435 787 // only at the end of the flow.
duke@435 788 Block** _block_map;
duke@435 789
duke@435 790 // For each ciBlock index, a list of Blocks which share this ciBlock.
duke@435 791 GrowableArray<Block*>** _idx_to_blocklist;
duke@435 792 // count of ciBlocks
duke@435 793 int _ciblock_count;
duke@435 794
duke@435 795 // Tells if a given instruction is able to generate an exception edge.
duke@435 796 bool can_trap(ciBytecodeStream& str);
duke@435 797
never@802 798 // Clone the loop heads. Returns true if any cloning occurred.
never@802 799 bool clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
never@802 800
never@802 801 // Clone lp's head and replace tail's successors with clone.
never@802 802 Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
never@802 803
duke@435 804 public:
duke@435 805 // Return the block beginning at bci which has a JsrSet compatible
duke@435 806 // with jsrs.
duke@435 807 Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy);
duke@435 808
duke@435 809 // block factory
duke@435 810 Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy);
duke@435 811
never@802 812 // How many of the blocks have the backedge_copy bit set?
never@802 813 int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const;
duke@435 814
duke@435 815 // Return an existing block containing bci which has a JsrSet compatible
duke@435 816 // with jsrs, or NULL if there is none.
duke@435 817 Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); }
duke@435 818
duke@435 819 // Tell whether the flow analysis has encountered an error of some sort.
duke@435 820 bool failing() { return env()->failing() || _failure_reason != NULL; }
duke@435 821
duke@435 822 // Reason this compilation is failing, such as "too many basic blocks".
duke@435 823 const char* failure_reason() { return _failure_reason; }
duke@435 824
duke@435 825 // Note a failure.
duke@435 826 void record_failure(const char* reason);
duke@435 827
duke@435 828 // Return the block of a given pre-order number.
duke@435 829 int have_block_count() const { return _block_map != NULL; }
duke@435 830 int block_count() const { assert(have_block_count(), "");
duke@435 831 return _next_pre_order; }
duke@435 832 Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds");
duke@435 833 return _block_map[po]; }
duke@435 834 Block* start_block() const { return pre_order_at(start_block_num()); }
duke@435 835 int start_block_num() const { return 0; }
never@802 836 Block* rpo_at(int rpo) const { assert(0 <= rpo && rpo < block_count(), "out of bounds");
never@802 837 return _block_map[rpo]; }
never@802 838 int next_pre_order() { return _next_pre_order; }
never@802 839 int inc_next_pre_order() { return _next_pre_order++; }
duke@435 840
duke@435 841 private:
duke@435 842 // A work list used during flow analysis.
duke@435 843 Block* _work_list;
duke@435 844
never@802 845 // List of blocks in reverse post order
never@802 846 Block* _rpo_list;
never@802 847
duke@435 848 // Next Block::_pre_order. After mapping, doubles as block_count.
duke@435 849 int _next_pre_order;
duke@435 850
duke@435 851 // Are there more blocks on the work list?
duke@435 852 bool work_list_empty() { return _work_list == NULL; }
duke@435 853
duke@435 854 // Get the next basic block from our work list.
duke@435 855 Block* work_list_next();
duke@435 856
duke@435 857 // Add a basic block to our work list.
duke@435 858 void add_to_work_list(Block* block);
duke@435 859
never@802 860 // Prepend a basic block to rpo list.
never@802 861 void prepend_to_rpo_list(Block* blk) {
never@802 862 blk->set_rpo_next(_rpo_list);
never@802 863 _rpo_list = blk;
never@802 864 }
never@802 865
never@802 866 // Root of the loop tree
never@802 867 Loop* _loop_tree_root;
never@802 868
duke@435 869 // State used for make_jsr_record
duke@435 870 int _jsr_count;
duke@435 871 GrowableArray<JsrRecord*>* _jsr_records;
duke@435 872
duke@435 873 public:
duke@435 874 // Make a JsrRecord for a given (entry, return) pair, if such a record
duke@435 875 // does not already exist.
duke@435 876 JsrRecord* make_jsr_record(int entry_address, int return_address);
duke@435 877
never@802 878 void set_loop_tree_root(Loop* ltr) { _loop_tree_root = ltr; }
never@802 879 Loop* loop_tree_root() { return _loop_tree_root; }
never@802 880
duke@435 881 private:
duke@435 882 // Get the initial state for start_bci:
duke@435 883 const StateVector* get_start_state();
duke@435 884
duke@435 885 // Merge the current state into all exceptional successors at the
duke@435 886 // current point in the code.
duke@435 887 void flow_exceptions(GrowableArray<Block*>* exceptions,
duke@435 888 GrowableArray<ciInstanceKlass*>* exc_klasses,
duke@435 889 StateVector* state);
duke@435 890
duke@435 891 // Merge the current state into all successors at the current point
duke@435 892 // in the code.
duke@435 893 void flow_successors(GrowableArray<Block*>* successors,
duke@435 894 StateVector* state);
duke@435 895
duke@435 896 // Interpret the effects of the bytecodes on the incoming state
duke@435 897 // vector of a basic block. Push the changed state to succeeding
duke@435 898 // basic blocks.
duke@435 899 void flow_block(Block* block,
duke@435 900 StateVector* scratch_state,
duke@435 901 JsrSet* scratch_jsrs);
duke@435 902
duke@435 903 // Perform the type flow analysis, creating and cloning Blocks as
duke@435 904 // necessary.
duke@435 905 void flow_types();
duke@435 906
never@802 907 // Perform the depth first type flow analysis. Helper for flow_types.
never@802 908 void df_flow_types(Block* start,
never@802 909 bool do_flow,
never@802 910 StateVector* temp_vector,
never@802 911 JsrSet* temp_set);
never@802 912
never@802 913 // Incrementally build loop tree.
never@802 914 void build_loop_tree(Block* blk);
never@802 915
duke@435 916 // Create the block map, which indexes blocks in pre_order.
duke@435 917 void map_blocks();
duke@435 918
duke@435 919 public:
duke@435 920 // Perform type inference flow analysis.
duke@435 921 void do_flow();
duke@435 922
duke@435 923 void print_on(outputStream* st) const PRODUCT_RETURN;
never@802 924
never@802 925 void rpo_print_on(outputStream* st) const PRODUCT_RETURN;
duke@435 926 };

mercurial