src/share/vm/ci/ciTypeFlow.hpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 1907
c18cbe5936b8
child 2729
e863062e521d
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

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

mercurial