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