1 /*
2  * Copyright (c) 2000, 2013, 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 #include "precompiled.hpp"
26 #include "ci/ciConstant.hpp"
27 #include "ci/ciField.hpp"
28 #include "ci/ciMethod.hpp"
29 #include "ci/ciMethodData.hpp"
30 #include "ci/ciObjArrayKlass.hpp"
31 #include "ci/ciStreams.hpp"
32 #include "ci/ciTypeArrayKlass.hpp"
33 #include "ci/ciTypeFlow.hpp"
34 #include "compiler/compileLog.hpp"
35 #include "interpreter/bytecode.hpp"
36 #include "interpreter/bytecodes.hpp"
37 #include "memory/allocation.inline.hpp"
38 #include "opto/compile.hpp"
39 #include "opto/node.hpp"
40 #include "runtime/deoptimization.hpp"
41 #include "utilities/growableArray.hpp"
42 
43 // ciTypeFlow::JsrSet
44 //
45 // A JsrSet represents some set of JsrRecords.  This class
46 // is used to record a set of all jsr routines which we permit
47 // execution to return (ret) from.
48 //
49 // During abstract interpretation, JsrSets are used to determine
50 // whether two paths which reach a given block are unique, and
51 // should be cloned apart, or are compatible, and should merge
52 // together.
53 
54 // ------------------------------------------------------------------
55 // ciTypeFlow::JsrSet::JsrSet
JsrSet(Arena * arena,int default_len)56 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) {
57   if (arena != NULL) {
58     // Allocate growable array in Arena.
59     _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL);
60   } else {
61     // Allocate growable array in current ResourceArea.
62     _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false);
63   }
64 }
65 
66 // ------------------------------------------------------------------
67 // ciTypeFlow::JsrSet::copy_into
copy_into(JsrSet * jsrs)68 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
69   int len = size();
70   jsrs->_set->clear();
71   for (int i = 0; i < len; i++) {
72     jsrs->_set->append(_set->at(i));
73   }
74 }
75 
76 // ------------------------------------------------------------------
77 // ciTypeFlow::JsrSet::is_compatible_with
78 //
79 // !!!! MISGIVINGS ABOUT THIS... disregard
80 //
81 // Is this JsrSet compatible with some other JsrSet?
82 //
83 // In set-theoretic terms, a JsrSet can be viewed as a partial function
84 // from entry addresses to return addresses.  Two JsrSets A and B are
85 // compatible iff
86 //
87 //   For any x,
88 //   A(x) defined and B(x) defined implies A(x) == B(x)
89 //
90 // Less formally, two JsrSets are compatible when they have identical
91 // return addresses for any entry addresses they share in common.
is_compatible_with(JsrSet * other)92 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
93   // Walk through both sets in parallel.  If the same entry address
94   // appears in both sets, then the return address must match for
95   // the sets to be compatible.
96   int size1 = size();
97   int size2 = other->size();
98 
99   // Special case.  If nothing is on the jsr stack, then there can
100   // be no ret.
101   if (size2 == 0) {
102     return true;
103   } else if (size1 != size2) {
104     return false;
105   } else {
106     for (int i = 0; i < size1; i++) {
107       JsrRecord* record1 = record_at(i);
108       JsrRecord* record2 = other->record_at(i);
109       if (record1->entry_address() != record2->entry_address() ||
110           record1->return_address() != record2->return_address()) {
111         return false;
112       }
113     }
114     return true;
115   }
116 
117 #if 0
118   int pos1 = 0;
119   int pos2 = 0;
120   int size1 = size();
121   int size2 = other->size();
122   while (pos1 < size1 && pos2 < size2) {
123     JsrRecord* record1 = record_at(pos1);
124     JsrRecord* record2 = other->record_at(pos2);
125     int entry1 = record1->entry_address();
126     int entry2 = record2->entry_address();
127     if (entry1 < entry2) {
128       pos1++;
129     } else if (entry1 > entry2) {
130       pos2++;
131     } else {
132       if (record1->return_address() == record2->return_address()) {
133         pos1++;
134         pos2++;
135       } else {
136         // These two JsrSets are incompatible.
137         return false;
138       }
139     }
140   }
141   // The two JsrSets agree.
142   return true;
143 #endif
144 }
145 
146 // ------------------------------------------------------------------
147 // ciTypeFlow::JsrSet::insert_jsr_record
148 //
149 // Insert the given JsrRecord into the JsrSet, maintaining the order
150 // of the set and replacing any element with the same entry address.
insert_jsr_record(JsrRecord * record)151 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
152   int len = size();
153   int entry = record->entry_address();
154   int pos = 0;
155   for ( ; pos < len; pos++) {
156     JsrRecord* current = record_at(pos);
157     if (entry == current->entry_address()) {
158       // Stomp over this entry.
159       _set->at_put(pos, record);
160       assert(size() == len, "must be same size");
161       return;
162     } else if (entry < current->entry_address()) {
163       break;
164     }
165   }
166 
167   // Insert the record into the list.
168   JsrRecord* swap = record;
169   JsrRecord* temp = NULL;
170   for ( ; pos < len; pos++) {
171     temp = _set->at(pos);
172     _set->at_put(pos, swap);
173     swap = temp;
174   }
175   _set->append(swap);
176   assert(size() == len+1, "must be larger");
177 }
178 
179 // ------------------------------------------------------------------
180 // ciTypeFlow::JsrSet::remove_jsr_record
181 //
182 // Remove the JsrRecord with the given return address from the JsrSet.
remove_jsr_record(int return_address)183 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
184   int len = size();
185   for (int i = 0; i < len; i++) {
186     if (record_at(i)->return_address() == return_address) {
187       // We have found the proper entry.  Remove it from the
188       // JsrSet and exit.
189       for (int j = i+1; j < len ; j++) {
190         _set->at_put(j-1, _set->at(j));
191       }
192       _set->trunc_to(len-1);
193       assert(size() == len-1, "must be smaller");
194       return;
195     }
196   }
197   assert(false, "verify: returning from invalid subroutine");
198 }
199 
200 // ------------------------------------------------------------------
201 // ciTypeFlow::JsrSet::apply_control
202 //
203 // Apply the effect of a control-flow bytecode on the JsrSet.  The
204 // only bytecodes that modify the JsrSet are jsr and ret.
apply_control(ciTypeFlow * analyzer,ciBytecodeStream * str,ciTypeFlow::StateVector * state)205 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
206                                        ciBytecodeStream* str,
207                                        ciTypeFlow::StateVector* state) {
208   Bytecodes::Code code = str->cur_bc();
209   if (code == Bytecodes::_jsr) {
210     JsrRecord* record =
211       analyzer->make_jsr_record(str->get_dest(), str->next_bci());
212     insert_jsr_record(record);
213   } else if (code == Bytecodes::_jsr_w) {
214     JsrRecord* record =
215       analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
216     insert_jsr_record(record);
217   } else if (code == Bytecodes::_ret) {
218     Cell local = state->local(str->get_index());
219     ciType* return_address = state->type_at(local);
220     assert(return_address->is_return_address(), "verify: wrong type");
221     if (size() == 0) {
222       // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
223       // This can happen when a loop is inside a finally clause (4614060).
224       analyzer->record_failure("OSR in finally clause");
225       return;
226     }
227     remove_jsr_record(return_address->as_return_address()->bci());
228   }
229 }
230 
231 #ifndef PRODUCT
232 // ------------------------------------------------------------------
233 // ciTypeFlow::JsrSet::print_on
print_on(outputStream * st) const234 void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
235   st->print("{ ");
236   int num_elements = size();
237   if (num_elements > 0) {
238     int i = 0;
239     for( ; i < num_elements - 1; i++) {
240       _set->at(i)->print_on(st);
241       st->print(", ");
242     }
243     _set->at(i)->print_on(st);
244     st->print(" ");
245   }
246   st->print("}");
247 }
248 #endif
249 
250 // ciTypeFlow::StateVector
251 //
252 // A StateVector summarizes the type information at some point in
253 // the program.
254 
255 // ------------------------------------------------------------------
256 // ciTypeFlow::StateVector::type_meet
257 //
258 // Meet two types.
259 //
260 // The semi-lattice of types use by this analysis are modeled on those
261 // of the verifier.  The lattice is as follows:
262 //
263 //        top_type() >= all non-extremal types >= bottom_type
264 //                             and
265 //   Every primitive type is comparable only with itself.  The meet of
266 //   reference types is determined by their kind: instance class,
267 //   interface, or array class.  The meet of two types of the same
268 //   kind is their least common ancestor.  The meet of two types of
269 //   different kinds is always java.lang.Object.
type_meet_internal(ciType * t1,ciType * t2,ciTypeFlow * analyzer)270 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
271   assert(t1 != t2, "checked in caller");
272   if (t1->equals(top_type())) {
273     return t2;
274   } else if (t2->equals(top_type())) {
275     return t1;
276   } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
277     // Special case null_type.  null_type meet any reference type T
278     // is T.  null_type meet null_type is null_type.
279     if (t1->equals(null_type())) {
280       if (!t2->is_primitive_type() || t2->equals(null_type())) {
281         return t2;
282       }
283     } else if (t2->equals(null_type())) {
284       if (!t1->is_primitive_type()) {
285         return t1;
286       }
287     }
288 
289     // At least one of the two types is a non-top primitive type.
290     // The other type is not equal to it.  Fall to bottom.
291     return bottom_type();
292   } else {
293     // Both types are non-top non-primitive types.  That is,
294     // both types are either instanceKlasses or arrayKlasses.
295     ciKlass* object_klass = analyzer->env()->Object_klass();
296     ciKlass* k1 = t1->as_klass();
297     ciKlass* k2 = t2->as_klass();
298     if (k1->equals(object_klass) || k2->equals(object_klass)) {
299       return object_klass;
300     } else if (!k1->is_loaded() || !k2->is_loaded()) {
301       // Unloaded classes fall to java.lang.Object at a merge.
302       return object_klass;
303     } else if (k1->is_interface() != k2->is_interface()) {
304       // When an interface meets a non-interface, we get Object;
305       // This is what the verifier does.
306       return object_klass;
307     } else if (k1->is_array_klass() || k2->is_array_klass()) {
308       // When an array meets a non-array, we get Object.
309       // When objArray meets typeArray, we also get Object.
310       // And when typeArray meets different typeArray, we again get Object.
311       // But when objArray meets objArray, we look carefully at element types.
312       if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
313         // Meet the element types, then construct the corresponding array type.
314         ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
315         ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
316         ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
317         // Do an easy shortcut if one type is a super of the other.
318         if (elem == elem1) {
319           assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
320           return k1;
321         } else if (elem == elem2) {
322           assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
323           return k2;
324         } else {
325           return ciObjArrayKlass::make(elem);
326         }
327       } else {
328         return object_klass;
329       }
330     } else {
331       // Must be two plain old instance klasses.
332       assert(k1->is_instance_klass(), "previous cases handle non-instances");
333       assert(k2->is_instance_klass(), "previous cases handle non-instances");
334       return k1->least_common_ancestor(k2);
335     }
336   }
337 }
338 
339 
340 // ------------------------------------------------------------------
341 // ciTypeFlow::StateVector::StateVector
342 //
343 // Build a new state vector
StateVector(ciTypeFlow * analyzer)344 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
345   _outer = analyzer;
346   _stack_size = -1;
347   _monitor_count = -1;
348   // Allocate the _types array
349   int max_cells = analyzer->max_cells();
350   _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
351   for (int i=0; i<max_cells; i++) {
352     _types[i] = top_type();
353   }
354   _trap_bci = -1;
355   _trap_index = 0;
356   _def_locals.clear();
357 }
358 
359 
360 // ------------------------------------------------------------------
361 // ciTypeFlow::get_start_state
362 //
363 // Set this vector to the method entry state.
get_start_state()364 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
365   StateVector* state = new StateVector(this);
366   if (is_osr_flow()) {
367     ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
368     if (non_osr_flow->failing()) {
369       record_failure(non_osr_flow->failure_reason());
370       return NULL;
371     }
372     JsrSet* jsrs = new JsrSet(NULL, 16);
373     Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
374     if (non_osr_block == NULL) {
375       record_failure("cannot reach OSR point");
376       return NULL;
377     }
378     // load up the non-OSR state at this point
379     non_osr_block->copy_state_into(state);
380     int non_osr_start = non_osr_block->start();
381     if (non_osr_start != start_bci()) {
382       // must flow forward from it
383       if (CITraceTypeFlow) {
384         tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
385       }
386       Block* block = block_at(non_osr_start, jsrs);
387       assert(block->limit() == start_bci(), "must flow forward to start");
388       flow_block(block, state, jsrs);
389     }
390     return state;
391     // Note:  The code below would be an incorrect for an OSR flow,
392     // even if it were possible for an OSR entry point to be at bci zero.
393   }
394   // "Push" the method signature into the first few locals.
395   state->set_stack_size(-max_locals());
396   if (!method()->is_static()) {
397     state->push(method()->holder());
398     assert(state->tos() == state->local(0), "");
399   }
400   for (ciSignatureStream str(method()->signature());
401        !str.at_return_type();
402        str.next()) {
403     state->push_translate(str.type());
404   }
405   // Set the rest of the locals to bottom.
406   Cell cell = state->next_cell(state->tos());
407   state->set_stack_size(0);
408   int limit = state->limit_cell();
409   for (; cell < limit; cell = state->next_cell(cell)) {
410     state->set_type_at(cell, state->bottom_type());
411   }
412   // Lock an object, if necessary.
413   state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
414   return state;
415 }
416 
417 // ------------------------------------------------------------------
418 // ciTypeFlow::StateVector::copy_into
419 //
420 // Copy our value into some other StateVector
copy_into(ciTypeFlow::StateVector * copy) const421 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
422 const {
423   copy->set_stack_size(stack_size());
424   copy->set_monitor_count(monitor_count());
425   Cell limit = limit_cell();
426   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
427     copy->set_type_at(c, type_at(c));
428   }
429 }
430 
431 // ------------------------------------------------------------------
432 // ciTypeFlow::StateVector::meet
433 //
434 // Meets this StateVector with another, destructively modifying this
435 // one.  Returns true if any modification takes place.
meet(const ciTypeFlow::StateVector * incoming)436 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
437   if (monitor_count() == -1) {
438     set_monitor_count(incoming->monitor_count());
439   }
440   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
441 
442   if (stack_size() == -1) {
443     set_stack_size(incoming->stack_size());
444     Cell limit = limit_cell();
445     #ifdef ASSERT
446     { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
447         assert(type_at(c) == top_type(), "");
448     } }
449     #endif
450     // Make a simple copy of the incoming state.
451     for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
452       set_type_at(c, incoming->type_at(c));
453     }
454     return true;  // it is always different the first time
455   }
456 #ifdef ASSERT
457   if (stack_size() != incoming->stack_size()) {
458     _outer->method()->print_codes();
459     tty->print_cr("!!!! Stack size conflict");
460     tty->print_cr("Current state:");
461     print_on(tty);
462     tty->print_cr("Incoming state:");
463     ((StateVector*)incoming)->print_on(tty);
464   }
465 #endif
466   assert(stack_size() == incoming->stack_size(), "sanity");
467 
468   bool different = false;
469   Cell limit = limit_cell();
470   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
471     ciType* t1 = type_at(c);
472     ciType* t2 = incoming->type_at(c);
473     if (!t1->equals(t2)) {
474       ciType* new_type = type_meet(t1, t2);
475       if (!t1->equals(new_type)) {
476         set_type_at(c, new_type);
477         different = true;
478       }
479     }
480   }
481   return different;
482 }
483 
484 // ------------------------------------------------------------------
485 // ciTypeFlow::StateVector::meet_exception
486 //
487 // Meets this StateVector with another, destructively modifying this
488 // one.  The incoming state is coming via an exception.  Returns true
489 // if any modification takes place.
meet_exception(ciInstanceKlass * exc,const ciTypeFlow::StateVector * incoming)490 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
491                                      const ciTypeFlow::StateVector* incoming) {
492   if (monitor_count() == -1) {
493     set_monitor_count(incoming->monitor_count());
494   }
495   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
496 
497   if (stack_size() == -1) {
498     set_stack_size(1);
499   }
500 
501   assert(stack_size() ==  1, "must have one-element stack");
502 
503   bool different = false;
504 
505   // Meet locals from incoming array.
506   Cell limit = local(_outer->max_locals()-1);
507   for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
508     ciType* t1 = type_at(c);
509     ciType* t2 = incoming->type_at(c);
510     if (!t1->equals(t2)) {
511       ciType* new_type = type_meet(t1, t2);
512       if (!t1->equals(new_type)) {
513         set_type_at(c, new_type);
514         different = true;
515       }
516     }
517   }
518 
519   // Handle stack separately.  When an exception occurs, the
520   // only stack entry is the exception instance.
521   ciType* tos_type = type_at_tos();
522   if (!tos_type->equals(exc)) {
523     ciType* new_type = type_meet(tos_type, exc);
524     if (!tos_type->equals(new_type)) {
525       set_type_at_tos(new_type);
526       different = true;
527     }
528   }
529 
530   return different;
531 }
532 
533 // ------------------------------------------------------------------
534 // ciTypeFlow::StateVector::push_translate
push_translate(ciType * type)535 void ciTypeFlow::StateVector::push_translate(ciType* type) {
536   BasicType basic_type = type->basic_type();
537   if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
538       basic_type == T_BYTE    || basic_type == T_SHORT) {
539     push_int();
540   } else {
541     push(type);
542     if (type->is_two_word()) {
543       push(half_type(type));
544     }
545   }
546 }
547 
548 // ------------------------------------------------------------------
549 // ciTypeFlow::StateVector::do_aaload
do_aaload(ciBytecodeStream * str)550 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
551   pop_int();
552   ciObjArrayKlass* array_klass = pop_objArray();
553   if (array_klass == NULL) {
554     // Did aaload on a null reference; push a null and ignore the exception.
555     // This instruction will never continue normally.  All we have to do
556     // is report a value that will meet correctly with any downstream
557     // reference types on paths that will truly be executed.  This null type
558     // meets with any reference type to yield that same reference type.
559     // (The compiler will generate an unconditional exception here.)
560     push(null_type());
561     return;
562   }
563   if (!array_klass->is_loaded()) {
564     // Only fails for some -Xcomp runs
565     trap(str, array_klass,
566          Deoptimization::make_trap_request
567          (Deoptimization::Reason_unloaded,
568           Deoptimization::Action_reinterpret));
569     return;
570   }
571   ciKlass* element_klass = array_klass->element_klass();
572   if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
573     Untested("unloaded array element class in ciTypeFlow");
574     trap(str, element_klass,
575          Deoptimization::make_trap_request
576          (Deoptimization::Reason_unloaded,
577           Deoptimization::Action_reinterpret));
578   } else {
579     push_object(element_klass);
580   }
581 }
582 
583 
584 // ------------------------------------------------------------------
585 // ciTypeFlow::StateVector::do_checkcast
do_checkcast(ciBytecodeStream * str)586 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
587   bool will_link;
588   ciKlass* klass = str->get_klass(will_link);
589   if (!will_link) {
590     // VM's interpreter will not load 'klass' if object is NULL.
591     // Type flow after this block may still be needed in two situations:
592     // 1) C2 uses do_null_assert() and continues compilation for later blocks
593     // 2) C2 does an OSR compile in a later block (see bug 4778368).
594     pop_object();
595     do_null_assert(klass);
596   } else {
597     pop_object();
598     push_object(klass);
599   }
600 }
601 
602 // ------------------------------------------------------------------
603 // ciTypeFlow::StateVector::do_getfield
do_getfield(ciBytecodeStream * str)604 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
605   // could add assert here for type of object.
606   pop_object();
607   do_getstatic(str);
608 }
609 
610 // ------------------------------------------------------------------
611 // ciTypeFlow::StateVector::do_getstatic
do_getstatic(ciBytecodeStream * str)612 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
613   bool will_link;
614   ciField* field = str->get_field(will_link);
615   if (!will_link) {
616     trap(str, field->holder(), str->get_field_holder_index());
617   } else {
618     ciType* field_type = field->type();
619     if (!field_type->is_loaded()) {
620       // Normally, we need the field's type to be loaded if we are to
621       // do anything interesting with its value.
622       // We used to do this:  trap(str, str->get_field_signature_index());
623       //
624       // There is one good reason not to trap here.  Execution can
625       // get past this "getfield" or "getstatic" if the value of
626       // the field is null.  As long as the value is null, the class
627       // does not need to be loaded!  The compiler must assume that
628       // the value of the unloaded class reference is null; if the code
629       // ever sees a non-null value, loading has occurred.
630       //
631       // This actually happens often enough to be annoying.  If the
632       // compiler throws an uncommon trap at this bytecode, you can
633       // get an endless loop of recompilations, when all the code
634       // needs to do is load a series of null values.  Also, a trap
635       // here can make an OSR entry point unreachable, triggering the
636       // assert on non_osr_block in ciTypeFlow::get_start_state.
637       // (See bug 4379915.)
638       do_null_assert(field_type->as_klass());
639     } else {
640       push_translate(field_type);
641     }
642   }
643 }
644 
645 // ------------------------------------------------------------------
646 // ciTypeFlow::StateVector::do_invoke
do_invoke(ciBytecodeStream * str,bool has_receiver)647 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
648                                         bool has_receiver) {
649   bool will_link;
650   ciSignature* declared_signature = NULL;
651   ciMethod* callee = str->get_method(will_link, &declared_signature);
652   assert(declared_signature != NULL, "cannot be null");
653   if (!will_link) {
654     // We weren't able to find the method.
655     if (str->cur_bc() == Bytecodes::_invokedynamic) {
656       trap(str, NULL,
657            Deoptimization::make_trap_request
658            (Deoptimization::Reason_uninitialized,
659             Deoptimization::Action_reinterpret));
660     } else {
661       ciKlass* unloaded_holder = callee->holder();
662       trap(str, unloaded_holder, str->get_method_holder_index());
663     }
664   } else {
665     // We are using the declared signature here because it might be
666     // different from the callee signature (Cf. invokedynamic and
667     // invokehandle).
668     ciSignatureStream sigstr(declared_signature);
669     const int arg_size = declared_signature->size();
670     const int stack_base = stack_size() - arg_size;
671     int i = 0;
672     for( ; !sigstr.at_return_type(); sigstr.next()) {
673       ciType* type = sigstr.type();
674       ciType* stack_type = type_at(stack(stack_base + i++));
675       // Do I want to check this type?
676       // assert(stack_type->is_subtype_of(type), "bad type for field value");
677       if (type->is_two_word()) {
678         ciType* stack_type2 = type_at(stack(stack_base + i++));
679         assert(stack_type2->equals(half_type(type)), "must be 2nd half");
680       }
681     }
682     assert(arg_size == i, "must match");
683     for (int j = 0; j < arg_size; j++) {
684       pop();
685     }
686     if (has_receiver) {
687       // Check this?
688       pop_object();
689     }
690     assert(!sigstr.is_done(), "must have return type");
691     ciType* return_type = sigstr.type();
692     if (!return_type->is_void()) {
693       if (!return_type->is_loaded()) {
694         // As in do_getstatic(), generally speaking, we need the return type to
695         // be loaded if we are to do anything interesting with its value.
696         // We used to do this:  trap(str, str->get_method_signature_index());
697         //
698         // We do not trap here since execution can get past this invoke if
699         // the return value is null.  As long as the value is null, the class
700         // does not need to be loaded!  The compiler must assume that
701         // the value of the unloaded class reference is null; if the code
702         // ever sees a non-null value, loading has occurred.
703         //
704         // See do_getstatic() for similar explanation, as well as bug 4684993.
705         do_null_assert(return_type->as_klass());
706       } else {
707         push_translate(return_type);
708       }
709     }
710   }
711 }
712 
713 // ------------------------------------------------------------------
714 // ciTypeFlow::StateVector::do_jsr
do_jsr(ciBytecodeStream * str)715 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
716   push(ciReturnAddress::make(str->next_bci()));
717 }
718 
719 // ------------------------------------------------------------------
720 // ciTypeFlow::StateVector::do_ldc
do_ldc(ciBytecodeStream * str)721 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
722   ciConstant con = str->get_constant();
723   BasicType basic_type = con.basic_type();
724   if (basic_type == T_ILLEGAL) {
725     // OutOfMemoryError in the CI while loading constant
726     push_null();
727     outer()->record_failure("ldc did not link");
728     return;
729   }
730   if (basic_type == T_OBJECT || basic_type == T_ARRAY) {
731     ciObject* obj = con.as_object();
732     if (obj->is_null_object()) {
733       push_null();
734     } else {
735       assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
736       push_object(obj->klass());
737     }
738   } else {
739     push_translate(ciType::make(basic_type));
740   }
741 }
742 
743 // ------------------------------------------------------------------
744 // ciTypeFlow::StateVector::do_multianewarray
do_multianewarray(ciBytecodeStream * str)745 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
746   int dimensions = str->get_dimensions();
747   bool will_link;
748   ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
749   if (!will_link) {
750     trap(str, array_klass, str->get_klass_index());
751   } else {
752     for (int i = 0; i < dimensions; i++) {
753       pop_int();
754     }
755     push_object(array_klass);
756   }
757 }
758 
759 // ------------------------------------------------------------------
760 // ciTypeFlow::StateVector::do_new
do_new(ciBytecodeStream * str)761 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
762   bool will_link;
763   ciKlass* klass = str->get_klass(will_link);
764   if (!will_link || str->is_unresolved_klass()) {
765     trap(str, klass, str->get_klass_index());
766   } else {
767     push_object(klass);
768   }
769 }
770 
771 // ------------------------------------------------------------------
772 // ciTypeFlow::StateVector::do_newarray
do_newarray(ciBytecodeStream * str)773 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
774   pop_int();
775   ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
776   push_object(klass);
777 }
778 
779 // ------------------------------------------------------------------
780 // ciTypeFlow::StateVector::do_putfield
do_putfield(ciBytecodeStream * str)781 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
782   do_putstatic(str);
783   if (_trap_bci != -1)  return;  // unloaded field holder, etc.
784   // could add assert here for type of object.
785   pop_object();
786 }
787 
788 // ------------------------------------------------------------------
789 // ciTypeFlow::StateVector::do_putstatic
do_putstatic(ciBytecodeStream * str)790 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
791   bool will_link;
792   ciField* field = str->get_field(will_link);
793   if (!will_link) {
794     trap(str, field->holder(), str->get_field_holder_index());
795   } else {
796     ciType* field_type = field->type();
797     ciType* type = pop_value();
798     // Do I want to check this type?
799     //      assert(type->is_subtype_of(field_type), "bad type for field value");
800     if (field_type->is_two_word()) {
801       ciType* type2 = pop_value();
802       assert(type2->is_two_word(), "must be 2nd half");
803       assert(type == half_type(type2), "must be 2nd half");
804     }
805   }
806 }
807 
808 // ------------------------------------------------------------------
809 // ciTypeFlow::StateVector::do_ret
do_ret(ciBytecodeStream * str)810 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
811   Cell index = local(str->get_index());
812 
813   ciType* address = type_at(index);
814   assert(address->is_return_address(), "bad return address");
815   set_type_at(index, bottom_type());
816 }
817 
818 // ------------------------------------------------------------------
819 // ciTypeFlow::StateVector::trap
820 //
821 // Stop interpretation of this path with a trap.
trap(ciBytecodeStream * str,ciKlass * klass,int index)822 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
823   _trap_bci = str->cur_bci();
824   _trap_index = index;
825 
826   // Log information about this trap:
827   CompileLog* log = outer()->env()->log();
828   if (log != NULL) {
829     int mid = log->identify(outer()->method());
830     int kid = (klass == NULL)? -1: log->identify(klass);
831     log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
832     char buf[100];
833     log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
834                                                           index));
835     if (kid >= 0)
836       log->print(" klass='%d'", kid);
837     log->end_elem();
838   }
839 }
840 
841 // ------------------------------------------------------------------
842 // ciTypeFlow::StateVector::do_null_assert
843 // Corresponds to graphKit::do_null_assert.
do_null_assert(ciKlass * unloaded_klass)844 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
845   if (unloaded_klass->is_loaded()) {
846     // We failed to link, but we can still compute with this class,
847     // since it is loaded somewhere.  The compiler will uncommon_trap
848     // if the object is not null, but the typeflow pass can not assume
849     // that the object will be null, otherwise it may incorrectly tell
850     // the parser that an object is known to be null. 4761344, 4807707
851     push_object(unloaded_klass);
852   } else {
853     // The class is not loaded anywhere.  It is safe to model the
854     // null in the typestates, because we can compile in a null check
855     // which will deoptimize us if someone manages to load the
856     // class later.
857     push_null();
858   }
859 }
860 
861 
862 // ------------------------------------------------------------------
863 // ciTypeFlow::StateVector::apply_one_bytecode
864 //
865 // Apply the effect of one bytecode to this StateVector
apply_one_bytecode(ciBytecodeStream * str)866 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
867   _trap_bci = -1;
868   _trap_index = 0;
869 
870   if (CITraceTypeFlow) {
871     tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
872                   Bytecodes::name(str->cur_bc()));
873   }
874 
875   switch(str->cur_bc()) {
876   case Bytecodes::_aaload: do_aaload(str);                       break;
877 
878   case Bytecodes::_aastore:
879     {
880       pop_object();
881       pop_int();
882       pop_objArray();
883       break;
884     }
885   case Bytecodes::_aconst_null:
886     {
887       push_null();
888       break;
889     }
890   case Bytecodes::_aload:   load_local_object(str->get_index());    break;
891   case Bytecodes::_aload_0: load_local_object(0);                   break;
892   case Bytecodes::_aload_1: load_local_object(1);                   break;
893   case Bytecodes::_aload_2: load_local_object(2);                   break;
894   case Bytecodes::_aload_3: load_local_object(3);                   break;
895 
896   case Bytecodes::_anewarray:
897     {
898       pop_int();
899       bool will_link;
900       ciKlass* element_klass = str->get_klass(will_link);
901       if (!will_link) {
902         trap(str, element_klass, str->get_klass_index());
903       } else {
904         push_object(ciObjArrayKlass::make(element_klass));
905       }
906       break;
907     }
908   case Bytecodes::_areturn:
909   case Bytecodes::_ifnonnull:
910   case Bytecodes::_ifnull:
911     {
912       pop_object();
913       break;
914     }
915   case Bytecodes::_monitorenter:
916     {
917       pop_object();
918       set_monitor_count(monitor_count() + 1);
919       break;
920     }
921   case Bytecodes::_monitorexit:
922     {
923       pop_object();
924       assert(monitor_count() > 0, "must be a monitor to exit from");
925       set_monitor_count(monitor_count() - 1);
926       break;
927     }
928   case Bytecodes::_arraylength:
929     {
930       pop_array();
931       push_int();
932       break;
933     }
934   case Bytecodes::_astore:   store_local_object(str->get_index());  break;
935   case Bytecodes::_astore_0: store_local_object(0);                 break;
936   case Bytecodes::_astore_1: store_local_object(1);                 break;
937   case Bytecodes::_astore_2: store_local_object(2);                 break;
938   case Bytecodes::_astore_3: store_local_object(3);                 break;
939 
940   case Bytecodes::_athrow:
941     {
942       NEEDS_CLEANUP;
943       pop_object();
944       break;
945     }
946   case Bytecodes::_baload:
947   case Bytecodes::_caload:
948   case Bytecodes::_iaload:
949   case Bytecodes::_saload:
950     {
951       pop_int();
952       ciTypeArrayKlass* array_klass = pop_typeArray();
953       // Put assert here for right type?
954       push_int();
955       break;
956     }
957   case Bytecodes::_bastore:
958   case Bytecodes::_castore:
959   case Bytecodes::_iastore:
960   case Bytecodes::_sastore:
961     {
962       pop_int();
963       pop_int();
964       pop_typeArray();
965       // assert here?
966       break;
967     }
968   case Bytecodes::_bipush:
969   case Bytecodes::_iconst_m1:
970   case Bytecodes::_iconst_0:
971   case Bytecodes::_iconst_1:
972   case Bytecodes::_iconst_2:
973   case Bytecodes::_iconst_3:
974   case Bytecodes::_iconst_4:
975   case Bytecodes::_iconst_5:
976   case Bytecodes::_sipush:
977     {
978       push_int();
979       break;
980     }
981   case Bytecodes::_checkcast: do_checkcast(str);                  break;
982 
983   case Bytecodes::_d2f:
984     {
985       pop_double();
986       push_float();
987       break;
988     }
989   case Bytecodes::_d2i:
990     {
991       pop_double();
992       push_int();
993       break;
994     }
995   case Bytecodes::_d2l:
996     {
997       pop_double();
998       push_long();
999       break;
1000     }
1001   case Bytecodes::_dadd:
1002   case Bytecodes::_ddiv:
1003   case Bytecodes::_dmul:
1004   case Bytecodes::_drem:
1005   case Bytecodes::_dsub:
1006     {
1007       pop_double();
1008       pop_double();
1009       push_double();
1010       break;
1011     }
1012   case Bytecodes::_daload:
1013     {
1014       pop_int();
1015       ciTypeArrayKlass* array_klass = pop_typeArray();
1016       // Put assert here for right type?
1017       push_double();
1018       break;
1019     }
1020   case Bytecodes::_dastore:
1021     {
1022       pop_double();
1023       pop_int();
1024       pop_typeArray();
1025       // assert here?
1026       break;
1027     }
1028   case Bytecodes::_dcmpg:
1029   case Bytecodes::_dcmpl:
1030     {
1031       pop_double();
1032       pop_double();
1033       push_int();
1034       break;
1035     }
1036   case Bytecodes::_dconst_0:
1037   case Bytecodes::_dconst_1:
1038     {
1039       push_double();
1040       break;
1041     }
1042   case Bytecodes::_dload:   load_local_double(str->get_index());    break;
1043   case Bytecodes::_dload_0: load_local_double(0);                   break;
1044   case Bytecodes::_dload_1: load_local_double(1);                   break;
1045   case Bytecodes::_dload_2: load_local_double(2);                   break;
1046   case Bytecodes::_dload_3: load_local_double(3);                   break;
1047 
1048   case Bytecodes::_dneg:
1049     {
1050       pop_double();
1051       push_double();
1052       break;
1053     }
1054   case Bytecodes::_dreturn:
1055     {
1056       pop_double();
1057       break;
1058     }
1059   case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
1060   case Bytecodes::_dstore_0: store_local_double(0);                 break;
1061   case Bytecodes::_dstore_1: store_local_double(1);                 break;
1062   case Bytecodes::_dstore_2: store_local_double(2);                 break;
1063   case Bytecodes::_dstore_3: store_local_double(3);                 break;
1064 
1065   case Bytecodes::_dup:
1066     {
1067       push(type_at_tos());
1068       break;
1069     }
1070   case Bytecodes::_dup_x1:
1071     {
1072       ciType* value1 = pop_value();
1073       ciType* value2 = pop_value();
1074       push(value1);
1075       push(value2);
1076       push(value1);
1077       break;
1078     }
1079   case Bytecodes::_dup_x2:
1080     {
1081       ciType* value1 = pop_value();
1082       ciType* value2 = pop_value();
1083       ciType* value3 = pop_value();
1084       push(value1);
1085       push(value3);
1086       push(value2);
1087       push(value1);
1088       break;
1089     }
1090   case Bytecodes::_dup2:
1091     {
1092       ciType* value1 = pop_value();
1093       ciType* value2 = pop_value();
1094       push(value2);
1095       push(value1);
1096       push(value2);
1097       push(value1);
1098       break;
1099     }
1100   case Bytecodes::_dup2_x1:
1101     {
1102       ciType* value1 = pop_value();
1103       ciType* value2 = pop_value();
1104       ciType* value3 = pop_value();
1105       push(value2);
1106       push(value1);
1107       push(value3);
1108       push(value2);
1109       push(value1);
1110       break;
1111     }
1112   case Bytecodes::_dup2_x2:
1113     {
1114       ciType* value1 = pop_value();
1115       ciType* value2 = pop_value();
1116       ciType* value3 = pop_value();
1117       ciType* value4 = pop_value();
1118       push(value2);
1119       push(value1);
1120       push(value4);
1121       push(value3);
1122       push(value2);
1123       push(value1);
1124       break;
1125     }
1126   case Bytecodes::_f2d:
1127     {
1128       pop_float();
1129       push_double();
1130       break;
1131     }
1132   case Bytecodes::_f2i:
1133     {
1134       pop_float();
1135       push_int();
1136       break;
1137     }
1138   case Bytecodes::_f2l:
1139     {
1140       pop_float();
1141       push_long();
1142       break;
1143     }
1144   case Bytecodes::_fadd:
1145   case Bytecodes::_fdiv:
1146   case Bytecodes::_fmul:
1147   case Bytecodes::_frem:
1148   case Bytecodes::_fsub:
1149     {
1150       pop_float();
1151       pop_float();
1152       push_float();
1153       break;
1154     }
1155   case Bytecodes::_faload:
1156     {
1157       pop_int();
1158       ciTypeArrayKlass* array_klass = pop_typeArray();
1159       // Put assert here.
1160       push_float();
1161       break;
1162     }
1163   case Bytecodes::_fastore:
1164     {
1165       pop_float();
1166       pop_int();
1167       ciTypeArrayKlass* array_klass = pop_typeArray();
1168       // Put assert here.
1169       break;
1170     }
1171   case Bytecodes::_fcmpg:
1172   case Bytecodes::_fcmpl:
1173     {
1174       pop_float();
1175       pop_float();
1176       push_int();
1177       break;
1178     }
1179   case Bytecodes::_fconst_0:
1180   case Bytecodes::_fconst_1:
1181   case Bytecodes::_fconst_2:
1182     {
1183       push_float();
1184       break;
1185     }
1186   case Bytecodes::_fload:   load_local_float(str->get_index());     break;
1187   case Bytecodes::_fload_0: load_local_float(0);                    break;
1188   case Bytecodes::_fload_1: load_local_float(1);                    break;
1189   case Bytecodes::_fload_2: load_local_float(2);                    break;
1190   case Bytecodes::_fload_3: load_local_float(3);                    break;
1191 
1192   case Bytecodes::_fneg:
1193     {
1194       pop_float();
1195       push_float();
1196       break;
1197     }
1198   case Bytecodes::_freturn:
1199     {
1200       pop_float();
1201       break;
1202     }
1203   case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
1204   case Bytecodes::_fstore_0:  store_local_float(0);                  break;
1205   case Bytecodes::_fstore_1:  store_local_float(1);                  break;
1206   case Bytecodes::_fstore_2:  store_local_float(2);                  break;
1207   case Bytecodes::_fstore_3:  store_local_float(3);                  break;
1208 
1209   case Bytecodes::_getfield:  do_getfield(str);                      break;
1210   case Bytecodes::_getstatic: do_getstatic(str);                     break;
1211 
1212   case Bytecodes::_goto:
1213   case Bytecodes::_goto_w:
1214   case Bytecodes::_nop:
1215   case Bytecodes::_return:
1216     {
1217       // do nothing.
1218       break;
1219     }
1220   case Bytecodes::_i2b:
1221   case Bytecodes::_i2c:
1222   case Bytecodes::_i2s:
1223   case Bytecodes::_ineg:
1224     {
1225       pop_int();
1226       push_int();
1227       break;
1228     }
1229   case Bytecodes::_i2d:
1230     {
1231       pop_int();
1232       push_double();
1233       break;
1234     }
1235   case Bytecodes::_i2f:
1236     {
1237       pop_int();
1238       push_float();
1239       break;
1240     }
1241   case Bytecodes::_i2l:
1242     {
1243       pop_int();
1244       push_long();
1245       break;
1246     }
1247   case Bytecodes::_iadd:
1248   case Bytecodes::_iand:
1249   case Bytecodes::_idiv:
1250   case Bytecodes::_imul:
1251   case Bytecodes::_ior:
1252   case Bytecodes::_irem:
1253   case Bytecodes::_ishl:
1254   case Bytecodes::_ishr:
1255   case Bytecodes::_isub:
1256   case Bytecodes::_iushr:
1257   case Bytecodes::_ixor:
1258     {
1259       pop_int();
1260       pop_int();
1261       push_int();
1262       break;
1263     }
1264   case Bytecodes::_if_acmpeq:
1265   case Bytecodes::_if_acmpne:
1266     {
1267       pop_object();
1268       pop_object();
1269       break;
1270     }
1271   case Bytecodes::_if_icmpeq:
1272   case Bytecodes::_if_icmpge:
1273   case Bytecodes::_if_icmpgt:
1274   case Bytecodes::_if_icmple:
1275   case Bytecodes::_if_icmplt:
1276   case Bytecodes::_if_icmpne:
1277     {
1278       pop_int();
1279       pop_int();
1280       break;
1281     }
1282   case Bytecodes::_ifeq:
1283   case Bytecodes::_ifle:
1284   case Bytecodes::_iflt:
1285   case Bytecodes::_ifge:
1286   case Bytecodes::_ifgt:
1287   case Bytecodes::_ifne:
1288   case Bytecodes::_ireturn:
1289   case Bytecodes::_lookupswitch:
1290   case Bytecodes::_tableswitch:
1291     {
1292       pop_int();
1293       break;
1294     }
1295   case Bytecodes::_iinc:
1296     {
1297       int lnum = str->get_index();
1298       check_int(local(lnum));
1299       store_to_local(lnum);
1300       break;
1301     }
1302   case Bytecodes::_iload:   load_local_int(str->get_index()); break;
1303   case Bytecodes::_iload_0: load_local_int(0);                      break;
1304   case Bytecodes::_iload_1: load_local_int(1);                      break;
1305   case Bytecodes::_iload_2: load_local_int(2);                      break;
1306   case Bytecodes::_iload_3: load_local_int(3);                      break;
1307 
1308   case Bytecodes::_instanceof:
1309     {
1310       // Check for uncommon trap:
1311       do_checkcast(str);
1312       pop_object();
1313       push_int();
1314       break;
1315     }
1316   case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
1317   case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
1318   case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
1319   case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
1320   case Bytecodes::_invokedynamic:   do_invoke(str, false);          break;
1321 
1322   case Bytecodes::_istore:   store_local_int(str->get_index());     break;
1323   case Bytecodes::_istore_0: store_local_int(0);                    break;
1324   case Bytecodes::_istore_1: store_local_int(1);                    break;
1325   case Bytecodes::_istore_2: store_local_int(2);                    break;
1326   case Bytecodes::_istore_3: store_local_int(3);                    break;
1327 
1328   case Bytecodes::_jsr:
1329   case Bytecodes::_jsr_w: do_jsr(str);                              break;
1330 
1331   case Bytecodes::_l2d:
1332     {
1333       pop_long();
1334       push_double();
1335       break;
1336     }
1337   case Bytecodes::_l2f:
1338     {
1339       pop_long();
1340       push_float();
1341       break;
1342     }
1343   case Bytecodes::_l2i:
1344     {
1345       pop_long();
1346       push_int();
1347       break;
1348     }
1349   case Bytecodes::_ladd:
1350   case Bytecodes::_land:
1351   case Bytecodes::_ldiv:
1352   case Bytecodes::_lmul:
1353   case Bytecodes::_lor:
1354   case Bytecodes::_lrem:
1355   case Bytecodes::_lsub:
1356   case Bytecodes::_lxor:
1357     {
1358       pop_long();
1359       pop_long();
1360       push_long();
1361       break;
1362     }
1363   case Bytecodes::_laload:
1364     {
1365       pop_int();
1366       ciTypeArrayKlass* array_klass = pop_typeArray();
1367       // Put assert here for right type?
1368       push_long();
1369       break;
1370     }
1371   case Bytecodes::_lastore:
1372     {
1373       pop_long();
1374       pop_int();
1375       pop_typeArray();
1376       // assert here?
1377       break;
1378     }
1379   case Bytecodes::_lcmp:
1380     {
1381       pop_long();
1382       pop_long();
1383       push_int();
1384       break;
1385     }
1386   case Bytecodes::_lconst_0:
1387   case Bytecodes::_lconst_1:
1388     {
1389       push_long();
1390       break;
1391     }
1392   case Bytecodes::_ldc:
1393   case Bytecodes::_ldc_w:
1394   case Bytecodes::_ldc2_w:
1395     {
1396       do_ldc(str);
1397       break;
1398     }
1399 
1400   case Bytecodes::_lload:   load_local_long(str->get_index());      break;
1401   case Bytecodes::_lload_0: load_local_long(0);                     break;
1402   case Bytecodes::_lload_1: load_local_long(1);                     break;
1403   case Bytecodes::_lload_2: load_local_long(2);                     break;
1404   case Bytecodes::_lload_3: load_local_long(3);                     break;
1405 
1406   case Bytecodes::_lneg:
1407     {
1408       pop_long();
1409       push_long();
1410       break;
1411     }
1412   case Bytecodes::_lreturn:
1413     {
1414       pop_long();
1415       break;
1416     }
1417   case Bytecodes::_lshl:
1418   case Bytecodes::_lshr:
1419   case Bytecodes::_lushr:
1420     {
1421       pop_int();
1422       pop_long();
1423       push_long();
1424       break;
1425     }
1426   case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
1427   case Bytecodes::_lstore_0: store_local_long(0);                   break;
1428   case Bytecodes::_lstore_1: store_local_long(1);                   break;
1429   case Bytecodes::_lstore_2: store_local_long(2);                   break;
1430   case Bytecodes::_lstore_3: store_local_long(3);                   break;
1431 
1432   case Bytecodes::_multianewarray: do_multianewarray(str);          break;
1433 
1434   case Bytecodes::_new:      do_new(str);                           break;
1435 
1436   case Bytecodes::_newarray: do_newarray(str);                      break;
1437 
1438   case Bytecodes::_pop:
1439     {
1440       pop();
1441       break;
1442     }
1443   case Bytecodes::_pop2:
1444     {
1445       pop();
1446       pop();
1447       break;
1448     }
1449 
1450   case Bytecodes::_putfield:       do_putfield(str);                 break;
1451   case Bytecodes::_putstatic:      do_putstatic(str);                break;
1452 
1453   case Bytecodes::_ret: do_ret(str);                                 break;
1454 
1455   case Bytecodes::_swap:
1456     {
1457       ciType* value1 = pop_value();
1458       ciType* value2 = pop_value();
1459       push(value1);
1460       push(value2);
1461       break;
1462     }
1463   case Bytecodes::_wide:
1464   default:
1465     {
1466       // The iterator should skip this.
1467       ShouldNotReachHere();
1468       break;
1469     }
1470   }
1471 
1472   if (CITraceTypeFlow) {
1473     print_on(tty);
1474   }
1475 
1476   return (_trap_bci != -1);
1477 }
1478 
1479 #ifndef PRODUCT
1480 // ------------------------------------------------------------------
1481 // ciTypeFlow::StateVector::print_cell_on
print_cell_on(outputStream * st,Cell c) const1482 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1483   ciType* type = type_at(c);
1484   if (type == top_type()) {
1485     st->print("top");
1486   } else if (type == bottom_type()) {
1487     st->print("bottom");
1488   } else if (type == null_type()) {
1489     st->print("null");
1490   } else if (type == long2_type()) {
1491     st->print("long2");
1492   } else if (type == double2_type()) {
1493     st->print("double2");
1494   } else if (is_int(type)) {
1495     st->print("int");
1496   } else if (is_long(type)) {
1497     st->print("long");
1498   } else if (is_float(type)) {
1499     st->print("float");
1500   } else if (is_double(type)) {
1501     st->print("double");
1502   } else if (type->is_return_address()) {
1503     st->print("address(%d)", type->as_return_address()->bci());
1504   } else {
1505     if (type->is_klass()) {
1506       type->as_klass()->name()->print_symbol_on(st);
1507     } else {
1508       st->print("UNEXPECTED TYPE");
1509       type->print();
1510     }
1511   }
1512 }
1513 
1514 // ------------------------------------------------------------------
1515 // ciTypeFlow::StateVector::print_on
print_on(outputStream * st) const1516 void ciTypeFlow::StateVector::print_on(outputStream* st) const {
1517   int num_locals   = _outer->max_locals();
1518   int num_stack    = stack_size();
1519   int num_monitors = monitor_count();
1520   st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
1521   if (num_stack >= 0) {
1522     int i;
1523     for (i = 0; i < num_locals; i++) {
1524       st->print("    local %2d : ", i);
1525       print_cell_on(st, local(i));
1526       st->cr();
1527     }
1528     for (i = 0; i < num_stack; i++) {
1529       st->print("    stack %2d : ", i);
1530       print_cell_on(st, stack(i));
1531       st->cr();
1532     }
1533   }
1534 }
1535 #endif
1536 
1537 
1538 // ------------------------------------------------------------------
1539 // ciTypeFlow::SuccIter::next
1540 //
next()1541 void ciTypeFlow::SuccIter::next() {
1542   int succ_ct = _pred->successors()->length();
1543   int next = _index + 1;
1544   if (next < succ_ct) {
1545     _index = next;
1546     _succ = _pred->successors()->at(next);
1547     return;
1548   }
1549   for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) {
1550     // Do not compile any code for unloaded exception types.
1551     // Following compiler passes are responsible for doing this also.
1552     ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i);
1553     if (exception_klass->is_loaded()) {
1554       _index = next;
1555       _succ = _pred->exceptions()->at(i);
1556       return;
1557     }
1558     next++;
1559   }
1560   _index = -1;
1561   _succ = NULL;
1562 }
1563 
1564 // ------------------------------------------------------------------
1565 // ciTypeFlow::SuccIter::set_succ
1566 //
set_succ(Block * succ)1567 void ciTypeFlow::SuccIter::set_succ(Block* succ) {
1568   int succ_ct = _pred->successors()->length();
1569   if (_index < succ_ct) {
1570     _pred->successors()->at_put(_index, succ);
1571   } else {
1572     int idx = _index - succ_ct;
1573     _pred->exceptions()->at_put(idx, succ);
1574   }
1575 }
1576 
1577 // ciTypeFlow::Block
1578 //
1579 // A basic block.
1580 
1581 // ------------------------------------------------------------------
1582 // ciTypeFlow::Block::Block
Block(ciTypeFlow * outer,ciBlock * ciblk,ciTypeFlow::JsrSet * jsrs)1583 ciTypeFlow::Block::Block(ciTypeFlow* outer,
1584                          ciBlock *ciblk,
1585                          ciTypeFlow::JsrSet* jsrs) {
1586   _ciblock = ciblk;
1587   _exceptions = NULL;
1588   _exc_klasses = NULL;
1589   _successors = NULL;
1590   _state = new (outer->arena()) StateVector(outer);
1591   JsrSet* new_jsrs =
1592     new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
1593   jsrs->copy_into(new_jsrs);
1594   _jsrs = new_jsrs;
1595   _next = NULL;
1596   _on_work_list = false;
1597   _backedge_copy = false;
1598   _has_monitorenter = false;
1599   _trap_bci = -1;
1600   _trap_index = 0;
1601   df_init();
1602 
1603   if (CITraceTypeFlow) {
1604     tty->print_cr(">> Created new block");
1605     print_on(tty);
1606   }
1607 
1608   assert(this->outer() == outer, "outer link set up");
1609   assert(!outer->have_block_count(), "must not have mapped blocks yet");
1610 }
1611 
1612 // ------------------------------------------------------------------
1613 // ciTypeFlow::Block::df_init
df_init()1614 void ciTypeFlow::Block::df_init() {
1615   _pre_order = -1; assert(!has_pre_order(), "");
1616   _post_order = -1; assert(!has_post_order(), "");
1617   _loop = NULL;
1618   _irreducible_entry = false;
1619   _rpo_next = NULL;
1620 }
1621 
1622 // ------------------------------------------------------------------
1623 // ciTypeFlow::Block::successors
1624 //
1625 // Get the successors for this Block.
1626 GrowableArray<ciTypeFlow::Block*>*
successors(ciBytecodeStream * str,ciTypeFlow::StateVector * state,ciTypeFlow::JsrSet * jsrs)1627 ciTypeFlow::Block::successors(ciBytecodeStream* str,
1628                               ciTypeFlow::StateVector* state,
1629                               ciTypeFlow::JsrSet* jsrs) {
1630   if (_successors == NULL) {
1631     if (CITraceTypeFlow) {
1632       tty->print(">> Computing successors for block ");
1633       print_value_on(tty);
1634       tty->cr();
1635     }
1636 
1637     ciTypeFlow* analyzer = outer();
1638     Arena* arena = analyzer->arena();
1639     Block* block = NULL;
1640     bool has_successor = !has_trap() &&
1641                          (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size());
1642     if (!has_successor) {
1643       _successors =
1644         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1645       // No successors
1646     } else if (control() == ciBlock::fall_through_bci) {
1647       assert(str->cur_bci() == limit(), "bad block end");
1648       // This block simply falls through to the next.
1649       _successors =
1650         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1651 
1652       Block* block = analyzer->block_at(limit(), _jsrs);
1653       assert(_successors->length() == FALL_THROUGH, "");
1654       _successors->append(block);
1655     } else {
1656       int current_bci = str->cur_bci();
1657       int next_bci = str->next_bci();
1658       int branch_bci = -1;
1659       Block* target = NULL;
1660       assert(str->next_bci() == limit(), "bad block end");
1661       // This block is not a simple fall-though.  Interpret
1662       // the current bytecode to find our successors.
1663       switch (str->cur_bc()) {
1664       case Bytecodes::_ifeq:         case Bytecodes::_ifne:
1665       case Bytecodes::_iflt:         case Bytecodes::_ifge:
1666       case Bytecodes::_ifgt:         case Bytecodes::_ifle:
1667       case Bytecodes::_if_icmpeq:    case Bytecodes::_if_icmpne:
1668       case Bytecodes::_if_icmplt:    case Bytecodes::_if_icmpge:
1669       case Bytecodes::_if_icmpgt:    case Bytecodes::_if_icmple:
1670       case Bytecodes::_if_acmpeq:    case Bytecodes::_if_acmpne:
1671       case Bytecodes::_ifnull:       case Bytecodes::_ifnonnull:
1672         // Our successors are the branch target and the next bci.
1673         branch_bci = str->get_dest();
1674         _successors =
1675           new (arena) GrowableArray<Block*>(arena, 2, 0, NULL);
1676         assert(_successors->length() == IF_NOT_TAKEN, "");
1677         _successors->append(analyzer->block_at(next_bci, jsrs));
1678         assert(_successors->length() == IF_TAKEN, "");
1679         _successors->append(analyzer->block_at(branch_bci, jsrs));
1680         break;
1681 
1682       case Bytecodes::_goto:
1683         branch_bci = str->get_dest();
1684         _successors =
1685           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1686         assert(_successors->length() == GOTO_TARGET, "");
1687         _successors->append(analyzer->block_at(branch_bci, jsrs));
1688         break;
1689 
1690       case Bytecodes::_jsr:
1691         branch_bci = str->get_dest();
1692         _successors =
1693           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1694         assert(_successors->length() == GOTO_TARGET, "");
1695         _successors->append(analyzer->block_at(branch_bci, jsrs));
1696         break;
1697 
1698       case Bytecodes::_goto_w:
1699       case Bytecodes::_jsr_w:
1700         _successors =
1701           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1702         assert(_successors->length() == GOTO_TARGET, "");
1703         _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
1704         break;
1705 
1706       case Bytecodes::_tableswitch:  {
1707         Bytecode_tableswitch tableswitch(str);
1708 
1709         int len = tableswitch.length();
1710         _successors =
1711           new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL);
1712         int bci = current_bci + tableswitch.default_offset();
1713         Block* block = analyzer->block_at(bci, jsrs);
1714         assert(_successors->length() == SWITCH_DEFAULT, "");
1715         _successors->append(block);
1716         while (--len >= 0) {
1717           int bci = current_bci + tableswitch.dest_offset_at(len);
1718           block = analyzer->block_at(bci, jsrs);
1719           assert(_successors->length() >= SWITCH_CASES, "");
1720           _successors->append_if_missing(block);
1721         }
1722         break;
1723       }
1724 
1725       case Bytecodes::_lookupswitch: {
1726         Bytecode_lookupswitch lookupswitch(str);
1727 
1728         int npairs = lookupswitch.number_of_pairs();
1729         _successors =
1730           new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL);
1731         int bci = current_bci + lookupswitch.default_offset();
1732         Block* block = analyzer->block_at(bci, jsrs);
1733         assert(_successors->length() == SWITCH_DEFAULT, "");
1734         _successors->append(block);
1735         while(--npairs >= 0) {
1736           LookupswitchPair pair = lookupswitch.pair_at(npairs);
1737           int bci = current_bci + pair.offset();
1738           Block* block = analyzer->block_at(bci, jsrs);
1739           assert(_successors->length() >= SWITCH_CASES, "");
1740           _successors->append_if_missing(block);
1741         }
1742         break;
1743       }
1744 
1745       case Bytecodes::_athrow:     case Bytecodes::_ireturn:
1746       case Bytecodes::_lreturn:    case Bytecodes::_freturn:
1747       case Bytecodes::_dreturn:    case Bytecodes::_areturn:
1748       case Bytecodes::_return:
1749         _successors =
1750           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1751         // No successors
1752         break;
1753 
1754       case Bytecodes::_ret: {
1755         _successors =
1756           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1757 
1758         Cell local = state->local(str->get_index());
1759         ciType* return_address = state->type_at(local);
1760         assert(return_address->is_return_address(), "verify: wrong type");
1761         int bci = return_address->as_return_address()->bci();
1762         assert(_successors->length() == GOTO_TARGET, "");
1763         _successors->append(analyzer->block_at(bci, jsrs));
1764         break;
1765       }
1766 
1767       case Bytecodes::_wide:
1768       default:
1769         ShouldNotReachHere();
1770         break;
1771       }
1772     }
1773   }
1774   return _successors;
1775 }
1776 
1777 // ------------------------------------------------------------------
1778 // ciTypeFlow::Block:compute_exceptions
1779 //
1780 // Compute the exceptional successors and types for this Block.
compute_exceptions()1781 void ciTypeFlow::Block::compute_exceptions() {
1782   assert(_exceptions == NULL && _exc_klasses == NULL, "repeat");
1783 
1784   if (CITraceTypeFlow) {
1785     tty->print(">> Computing exceptions for block ");
1786     print_value_on(tty);
1787     tty->cr();
1788   }
1789 
1790   ciTypeFlow* analyzer = outer();
1791   Arena* arena = analyzer->arena();
1792 
1793   // Any bci in the block will do.
1794   ciExceptionHandlerStream str(analyzer->method(), start());
1795 
1796   // Allocate our growable arrays.
1797   int exc_count = str.count();
1798   _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL);
1799   _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
1800                                                              0, NULL);
1801 
1802   for ( ; !str.is_done(); str.next()) {
1803     ciExceptionHandler* handler = str.handler();
1804     int bci = handler->handler_bci();
1805     ciInstanceKlass* klass = NULL;
1806     if (bci == -1) {
1807       // There is no catch all.  It is possible to exit the method.
1808       break;
1809     }
1810     if (handler->is_catch_all()) {
1811       klass = analyzer->env()->Throwable_klass();
1812     } else {
1813       klass = handler->catch_klass();
1814     }
1815     _exceptions->append(analyzer->block_at(bci, _jsrs));
1816     _exc_klasses->append(klass);
1817   }
1818 }
1819 
1820 // ------------------------------------------------------------------
1821 // ciTypeFlow::Block::set_backedge_copy
1822 // Use this only to make a pre-existing public block into a backedge copy.
set_backedge_copy(bool z)1823 void ciTypeFlow::Block::set_backedge_copy(bool z) {
1824   assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public");
1825   _backedge_copy = z;
1826 }
1827 
1828 // ------------------------------------------------------------------
1829 // ciTypeFlow::Block::is_clonable_exit
1830 //
1831 // At most 2 normal successors, one of which continues looping,
1832 // and all exceptional successors must exit.
is_clonable_exit(ciTypeFlow::Loop * lp)1833 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) {
1834   int normal_cnt  = 0;
1835   int in_loop_cnt = 0;
1836   for (SuccIter iter(this); !iter.done(); iter.next()) {
1837     Block* succ = iter.succ();
1838     if (iter.is_normal_ctrl()) {
1839       if (++normal_cnt > 2) return false;
1840       if (lp->contains(succ->loop())) {
1841         if (++in_loop_cnt > 1) return false;
1842       }
1843     } else {
1844       if (lp->contains(succ->loop())) return false;
1845     }
1846   }
1847   return in_loop_cnt == 1;
1848 }
1849 
1850 // ------------------------------------------------------------------
1851 // ciTypeFlow::Block::looping_succ
1852 //
looping_succ(ciTypeFlow::Loop * lp)1853 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) {
1854   assert(successors()->length() <= 2, "at most 2 normal successors");
1855   for (SuccIter iter(this); !iter.done(); iter.next()) {
1856     Block* succ = iter.succ();
1857     if (lp->contains(succ->loop())) {
1858       return succ;
1859     }
1860   }
1861   return NULL;
1862 }
1863 
1864 #ifndef PRODUCT
1865 // ------------------------------------------------------------------
1866 // ciTypeFlow::Block::print_value_on
print_value_on(outputStream * st) const1867 void ciTypeFlow::Block::print_value_on(outputStream* st) const {
1868   if (has_pre_order()) st->print("#%-2d ", pre_order());
1869   if (has_rpo())       st->print("rpo#%-2d ", rpo());
1870   st->print("[%d - %d)", start(), limit());
1871   if (is_loop_head()) st->print(" lphd");
1872   if (is_irreducible_entry()) st->print(" irred");
1873   if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
1874   if (is_backedge_copy())  st->print("/backedge_copy");
1875 }
1876 
1877 // ------------------------------------------------------------------
1878 // ciTypeFlow::Block::print_on
print_on(outputStream * st) const1879 void ciTypeFlow::Block::print_on(outputStream* st) const {
1880   if ((Verbose || WizardMode) && (limit() >= 0)) {
1881     // Don't print 'dummy' blocks (i.e. blocks with limit() '-1')
1882     outer()->method()->print_codes_on(start(), limit(), st);
1883   }
1884   st->print_cr("  ====================================================  ");
1885   st->print ("  ");
1886   print_value_on(st);
1887   st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr();
1888   if (loop() && loop()->parent() != NULL) {
1889     st->print(" loops:");
1890     Loop* lp = loop();
1891     do {
1892       st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order());
1893       if (lp->is_irreducible()) st->print("(ir)");
1894       lp = lp->parent();
1895     } while (lp->parent() != NULL);
1896   }
1897   st->cr();
1898   _state->print_on(st);
1899   if (_successors == NULL) {
1900     st->print_cr("  No successor information");
1901   } else {
1902     int num_successors = _successors->length();
1903     st->print_cr("  Successors : %d", num_successors);
1904     for (int i = 0; i < num_successors; i++) {
1905       Block* successor = _successors->at(i);
1906       st->print("    ");
1907       successor->print_value_on(st);
1908       st->cr();
1909     }
1910   }
1911   if (_exceptions == NULL) {
1912     st->print_cr("  No exception information");
1913   } else {
1914     int num_exceptions = _exceptions->length();
1915     st->print_cr("  Exceptions : %d", num_exceptions);
1916     for (int i = 0; i < num_exceptions; i++) {
1917       Block* exc_succ = _exceptions->at(i);
1918       ciInstanceKlass* exc_klass = _exc_klasses->at(i);
1919       st->print("    ");
1920       exc_succ->print_value_on(st);
1921       st->print(" -- ");
1922       exc_klass->name()->print_symbol_on(st);
1923       st->cr();
1924     }
1925   }
1926   if (has_trap()) {
1927     st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
1928   }
1929   st->print_cr("  ====================================================  ");
1930 }
1931 #endif
1932 
1933 #ifndef PRODUCT
1934 // ------------------------------------------------------------------
1935 // ciTypeFlow::LocalSet::print_on
print_on(outputStream * st,int limit) const1936 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const {
1937   st->print("{");
1938   for (int i = 0; i < max; i++) {
1939     if (test(i)) st->print(" %d", i);
1940   }
1941   if (limit > max) {
1942     st->print(" %d..%d ", max, limit);
1943   }
1944   st->print(" }");
1945 }
1946 #endif
1947 
1948 // ciTypeFlow
1949 //
1950 // This is a pass over the bytecodes which computes the following:
1951 //   basic block structure
1952 //   interpreter type-states (a la the verifier)
1953 
1954 // ------------------------------------------------------------------
1955 // ciTypeFlow::ciTypeFlow
ciTypeFlow(ciEnv * env,ciMethod * method,int osr_bci)1956 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
1957   _env = env;
1958   _method = method;
1959   _methodBlocks = method->get_method_blocks();
1960   _max_locals = method->max_locals();
1961   _max_stack = method->max_stack();
1962   _code_size = method->code_size();
1963   _has_irreducible_entry = false;
1964   _osr_bci = osr_bci;
1965   _failure_reason = NULL;
1966   assert(0 <= start_bci() && start_bci() < code_size() , err_msg("correct osr_bci argument: 0 <= %d < %d", start_bci(), code_size()));
1967   _work_list = NULL;
1968 
1969   _ciblock_count = _methodBlocks->num_blocks();
1970   _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count);
1971   for (int i = 0; i < _ciblock_count; i++) {
1972     _idx_to_blocklist[i] = NULL;
1973   }
1974   _block_map = NULL;  // until all blocks are seen
1975   _jsr_count = 0;
1976   _jsr_records = NULL;
1977 }
1978 
1979 // ------------------------------------------------------------------
1980 // ciTypeFlow::work_list_next
1981 //
1982 // Get the next basic block from our work list.
work_list_next()1983 ciTypeFlow::Block* ciTypeFlow::work_list_next() {
1984   assert(!work_list_empty(), "work list must not be empty");
1985   Block* next_block = _work_list;
1986   _work_list = next_block->next();
1987   next_block->set_next(NULL);
1988   next_block->set_on_work_list(false);
1989   return next_block;
1990 }
1991 
1992 // ------------------------------------------------------------------
1993 // ciTypeFlow::add_to_work_list
1994 //
1995 // Add a basic block to our work list.
1996 // List is sorted by decreasing postorder sort (same as increasing RPO)
add_to_work_list(ciTypeFlow::Block * block)1997 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
1998   assert(!block->is_on_work_list(), "must not already be on work list");
1999 
2000   if (CITraceTypeFlow) {
2001     tty->print(">> Adding block ");
2002     block->print_value_on(tty);
2003     tty->print_cr(" to the work list : ");
2004   }
2005 
2006   block->set_on_work_list(true);
2007 
2008   // decreasing post order sort
2009 
2010   Block* prev = NULL;
2011   Block* current = _work_list;
2012   int po = block->post_order();
2013   while (current != NULL) {
2014     if (!current->has_post_order() || po > current->post_order())
2015       break;
2016     prev = current;
2017     current = current->next();
2018   }
2019   if (prev == NULL) {
2020     block->set_next(_work_list);
2021     _work_list = block;
2022   } else {
2023     block->set_next(current);
2024     prev->set_next(block);
2025   }
2026 
2027   if (CITraceTypeFlow) {
2028     tty->cr();
2029   }
2030 }
2031 
2032 // ------------------------------------------------------------------
2033 // ciTypeFlow::block_at
2034 //
2035 // Return the block beginning at bci which has a JsrSet compatible
2036 // with jsrs.
block_at(int bci,ciTypeFlow::JsrSet * jsrs,CreateOption option)2037 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2038   // First find the right ciBlock.
2039   if (CITraceTypeFlow) {
2040     tty->print(">> Requesting block for %d/", bci);
2041     jsrs->print_on(tty);
2042     tty->cr();
2043   }
2044 
2045   ciBlock* ciblk = _methodBlocks->block_containing(bci);
2046   assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
2047   Block* block = get_block_for(ciblk->index(), jsrs, option);
2048 
2049   assert(block == NULL? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result");
2050 
2051   if (CITraceTypeFlow) {
2052     if (block != NULL) {
2053       tty->print(">> Found block ");
2054       block->print_value_on(tty);
2055       tty->cr();
2056     } else {
2057       tty->print_cr(">> No such block.");
2058     }
2059   }
2060 
2061   return block;
2062 }
2063 
2064 // ------------------------------------------------------------------
2065 // ciTypeFlow::make_jsr_record
2066 //
2067 // Make a JsrRecord for a given (entry, return) pair, if such a record
2068 // does not already exist.
make_jsr_record(int entry_address,int return_address)2069 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
2070                                                    int return_address) {
2071   if (_jsr_records == NULL) {
2072     _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
2073                                                            _jsr_count,
2074                                                            0,
2075                                                            NULL);
2076   }
2077   JsrRecord* record = NULL;
2078   int len = _jsr_records->length();
2079   for (int i = 0; i < len; i++) {
2080     JsrRecord* record = _jsr_records->at(i);
2081     if (record->entry_address() == entry_address &&
2082         record->return_address() == return_address) {
2083       return record;
2084     }
2085   }
2086 
2087   record = new (arena()) JsrRecord(entry_address, return_address);
2088   _jsr_records->append(record);
2089   return record;
2090 }
2091 
2092 // ------------------------------------------------------------------
2093 // ciTypeFlow::flow_exceptions
2094 //
2095 // Merge the current state into all exceptional successors at the
2096 // current point in the code.
flow_exceptions(GrowableArray<ciTypeFlow::Block * > * exceptions,GrowableArray<ciInstanceKlass * > * exc_klasses,ciTypeFlow::StateVector * state)2097 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
2098                                  GrowableArray<ciInstanceKlass*>* exc_klasses,
2099                                  ciTypeFlow::StateVector* state) {
2100   int len = exceptions->length();
2101   assert(exc_klasses->length() == len, "must have same length");
2102   for (int i = 0; i < len; i++) {
2103     Block* block = exceptions->at(i);
2104     ciInstanceKlass* exception_klass = exc_klasses->at(i);
2105 
2106     if (!exception_klass->is_loaded()) {
2107       // Do not compile any code for unloaded exception types.
2108       // Following compiler passes are responsible for doing this also.
2109       continue;
2110     }
2111 
2112     if (block->meet_exception(exception_klass, state)) {
2113       // Block was modified and has PO.  Add it to the work list.
2114       if (block->has_post_order() &&
2115           !block->is_on_work_list()) {
2116         add_to_work_list(block);
2117       }
2118     }
2119   }
2120 }
2121 
2122 // ------------------------------------------------------------------
2123 // ciTypeFlow::flow_successors
2124 //
2125 // Merge the current state into all successors at the current point
2126 // in the code.
flow_successors(GrowableArray<ciTypeFlow::Block * > * successors,ciTypeFlow::StateVector * state)2127 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
2128                                  ciTypeFlow::StateVector* state) {
2129   int len = successors->length();
2130   for (int i = 0; i < len; i++) {
2131     Block* block = successors->at(i);
2132     if (block->meet(state)) {
2133       // Block was modified and has PO.  Add it to the work list.
2134       if (block->has_post_order() &&
2135           !block->is_on_work_list()) {
2136         add_to_work_list(block);
2137       }
2138     }
2139   }
2140 }
2141 
2142 // ------------------------------------------------------------------
2143 // ciTypeFlow::can_trap
2144 //
2145 // Tells if a given instruction is able to generate an exception edge.
can_trap(ciBytecodeStream & str)2146 bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
2147   // Cf. GenerateOopMap::do_exception_edge.
2148   if (!Bytecodes::can_trap(str.cur_bc()))  return false;
2149 
2150   switch (str.cur_bc()) {
2151     // %%% FIXME: ldc of Class can generate an exception
2152     case Bytecodes::_ldc:
2153     case Bytecodes::_ldc_w:
2154     case Bytecodes::_ldc2_w:
2155     case Bytecodes::_aload_0:
2156       // These bytecodes can trap for rewriting.  We need to assume that
2157       // they do not throw exceptions to make the monitor analysis work.
2158       return false;
2159 
2160     case Bytecodes::_ireturn:
2161     case Bytecodes::_lreturn:
2162     case Bytecodes::_freturn:
2163     case Bytecodes::_dreturn:
2164     case Bytecodes::_areturn:
2165     case Bytecodes::_return:
2166       // We can assume the monitor stack is empty in this analysis.
2167       return false;
2168 
2169     case Bytecodes::_monitorexit:
2170       // We can assume monitors are matched in this analysis.
2171       return false;
2172   }
2173 
2174   return true;
2175 }
2176 
2177 // ------------------------------------------------------------------
2178 // ciTypeFlow::clone_loop_heads
2179 //
2180 // Clone the loop heads
clone_loop_heads(Loop * lp,StateVector * temp_vector,JsrSet * temp_set)2181 bool ciTypeFlow::clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
2182   bool rslt = false;
2183   for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) {
2184     lp = iter.current();
2185     Block* head = lp->head();
2186     if (lp == loop_tree_root() ||
2187         lp->is_irreducible() ||
2188         !head->is_clonable_exit(lp))
2189       continue;
2190 
2191     // Avoid BoxLock merge.
2192     if (EliminateNestedLocks && head->has_monitorenter())
2193       continue;
2194 
2195     // check not already cloned
2196     if (head->backedge_copy_count() != 0)
2197       continue;
2198 
2199     // Don't clone head of OSR loop to get correct types in start block.
2200     if (is_osr_flow() && head->start() == start_bci())
2201       continue;
2202 
2203     // check _no_ shared head below us
2204     Loop* ch;
2205     for (ch = lp->child(); ch != NULL && ch->head() != head; ch = ch->sibling());
2206     if (ch != NULL)
2207       continue;
2208 
2209     // Clone head
2210     Block* new_head = head->looping_succ(lp);
2211     Block* clone = clone_loop_head(lp, temp_vector, temp_set);
2212     // Update lp's info
2213     clone->set_loop(lp);
2214     lp->set_head(new_head);
2215     lp->set_tail(clone);
2216     // And move original head into outer loop
2217     head->set_loop(lp->parent());
2218 
2219     rslt = true;
2220   }
2221   return rslt;
2222 }
2223 
2224 // ------------------------------------------------------------------
2225 // ciTypeFlow::clone_loop_head
2226 //
2227 // Clone lp's head and replace tail's successors with clone.
2228 //
2229 //  |
2230 //  v
2231 // head <-> body
2232 //  |
2233 //  v
2234 // exit
2235 //
2236 // new_head
2237 //
2238 //  |
2239 //  v
2240 // head ----------\
2241 //  |             |
2242 //  |             v
2243 //  |  clone <-> body
2244 //  |    |
2245 //  | /--/
2246 //  | |
2247 //  v v
2248 // exit
2249 //
clone_loop_head(Loop * lp,StateVector * temp_vector,JsrSet * temp_set)2250 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
2251   Block* head = lp->head();
2252   Block* tail = lp->tail();
2253   if (CITraceTypeFlow) {
2254     tty->print(">> Requesting clone of loop head "); head->print_value_on(tty);
2255     tty->print("  for predecessor ");                tail->print_value_on(tty);
2256     tty->cr();
2257   }
2258   Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy);
2259   assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges");
2260 
2261   assert(!clone->has_pre_order(), "just created");
2262   clone->set_next_pre_order();
2263 
2264   // Insert clone after (orig) tail in reverse post order
2265   clone->set_rpo_next(tail->rpo_next());
2266   tail->set_rpo_next(clone);
2267 
2268   // tail->head becomes tail->clone
2269   for (SuccIter iter(tail); !iter.done(); iter.next()) {
2270     if (iter.succ() == head) {
2271       iter.set_succ(clone);
2272     }
2273   }
2274   flow_block(tail, temp_vector, temp_set);
2275   if (head == tail) {
2276     // For self-loops, clone->head becomes clone->clone
2277     flow_block(clone, temp_vector, temp_set);
2278     for (SuccIter iter(clone); !iter.done(); iter.next()) {
2279       if (iter.succ() == head) {
2280         iter.set_succ(clone);
2281         break;
2282       }
2283     }
2284   }
2285   flow_block(clone, temp_vector, temp_set);
2286 
2287   return clone;
2288 }
2289 
2290 // ------------------------------------------------------------------
2291 // ciTypeFlow::flow_block
2292 //
2293 // Interpret the effects of the bytecodes on the incoming state
2294 // vector of a basic block.  Push the changed state to succeeding
2295 // basic blocks.
flow_block(ciTypeFlow::Block * block,ciTypeFlow::StateVector * state,ciTypeFlow::JsrSet * jsrs)2296 void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
2297                             ciTypeFlow::StateVector* state,
2298                             ciTypeFlow::JsrSet* jsrs) {
2299   if (CITraceTypeFlow) {
2300     tty->print("\n>> ANALYZING BLOCK : ");
2301     tty->cr();
2302     block->print_on(tty);
2303   }
2304   assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
2305 
2306   int start = block->start();
2307   int limit = block->limit();
2308   int control = block->control();
2309   if (control != ciBlock::fall_through_bci) {
2310     limit = control;
2311   }
2312 
2313   // Grab the state from the current block.
2314   block->copy_state_into(state);
2315   state->def_locals()->clear();
2316 
2317   GrowableArray<Block*>*           exceptions = block->exceptions();
2318   GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
2319   bool has_exceptions = exceptions->length() > 0;
2320 
2321   bool exceptions_used = false;
2322 
2323   ciBytecodeStream str(method());
2324   str.reset_to_bci(start);
2325   Bytecodes::Code code;
2326   while ((code = str.next()) != ciBytecodeStream::EOBC() &&
2327          str.cur_bci() < limit) {
2328     // Check for exceptional control flow from this point.
2329     if (has_exceptions && can_trap(str)) {
2330       flow_exceptions(exceptions, exc_klasses, state);
2331       exceptions_used = true;
2332     }
2333     // Apply the effects of the current bytecode to our state.
2334     bool res = state->apply_one_bytecode(&str);
2335 
2336     // Watch for bailouts.
2337     if (failing())  return;
2338 
2339     if (str.cur_bc() == Bytecodes::_monitorenter) {
2340       block->set_has_monitorenter();
2341     }
2342 
2343     if (res) {
2344 
2345       // We have encountered a trap.  Record it in this block.
2346       block->set_trap(state->trap_bci(), state->trap_index());
2347 
2348       if (CITraceTypeFlow) {
2349         tty->print_cr(">> Found trap");
2350         block->print_on(tty);
2351       }
2352 
2353       // Save set of locals defined in this block
2354       block->def_locals()->add(state->def_locals());
2355 
2356       // Record (no) successors.
2357       block->successors(&str, state, jsrs);
2358 
2359       assert(!has_exceptions || exceptions_used, "Not removing exceptions");
2360 
2361       // Discontinue interpretation of this Block.
2362       return;
2363     }
2364   }
2365 
2366   GrowableArray<Block*>* successors = NULL;
2367   if (control != ciBlock::fall_through_bci) {
2368     // Check for exceptional control flow from this point.
2369     if (has_exceptions && can_trap(str)) {
2370       flow_exceptions(exceptions, exc_klasses, state);
2371       exceptions_used = true;
2372     }
2373 
2374     // Fix the JsrSet to reflect effect of the bytecode.
2375     block->copy_jsrs_into(jsrs);
2376     jsrs->apply_control(this, &str, state);
2377 
2378     // Find successor edges based on old state and new JsrSet.
2379     successors = block->successors(&str, state, jsrs);
2380 
2381     // Apply the control changes to the state.
2382     state->apply_one_bytecode(&str);
2383   } else {
2384     // Fall through control
2385     successors = block->successors(&str, NULL, NULL);
2386   }
2387 
2388   // Save set of locals defined in this block
2389   block->def_locals()->add(state->def_locals());
2390 
2391   // Remove untaken exception paths
2392   if (!exceptions_used)
2393     exceptions->clear();
2394 
2395   // Pass our state to successors.
2396   flow_successors(successors, state);
2397 }
2398 
2399 // ------------------------------------------------------------------
2400 // ciTypeFlow::PostOrderLoops::next
2401 //
2402 // Advance to next loop tree using a postorder, left-to-right traversal.
next()2403 void ciTypeFlow::PostorderLoops::next() {
2404   assert(!done(), "must not be done.");
2405   if (_current->sibling() != NULL) {
2406     _current = _current->sibling();
2407     while (_current->child() != NULL) {
2408       _current = _current->child();
2409     }
2410   } else {
2411     _current = _current->parent();
2412   }
2413 }
2414 
2415 // ------------------------------------------------------------------
2416 // ciTypeFlow::PreOrderLoops::next
2417 //
2418 // Advance to next loop tree using a preorder, left-to-right traversal.
next()2419 void ciTypeFlow::PreorderLoops::next() {
2420   assert(!done(), "must not be done.");
2421   if (_current->child() != NULL) {
2422     _current = _current->child();
2423   } else if (_current->sibling() != NULL) {
2424     _current = _current->sibling();
2425   } else {
2426     while (_current != _root && _current->sibling() == NULL) {
2427       _current = _current->parent();
2428     }
2429     if (_current == _root) {
2430       _current = NULL;
2431       assert(done(), "must be done.");
2432     } else {
2433       assert(_current->sibling() != NULL, "must be more to do");
2434       _current = _current->sibling();
2435     }
2436   }
2437 }
2438 
2439 // ------------------------------------------------------------------
2440 // ciTypeFlow::Loop::sorted_merge
2441 //
2442 // Merge the branch lp into this branch, sorting on the loop head
2443 // pre_orders. Returns the leaf of the merged branch.
2444 // Child and sibling pointers will be setup later.
2445 // Sort is (looking from leaf towards the root)
2446 //  descending on primary key: loop head's pre_order, and
2447 //  ascending  on secondary key: loop tail's pre_order.
sorted_merge(Loop * lp)2448 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) {
2449   Loop* leaf = this;
2450   Loop* prev = NULL;
2451   Loop* current = leaf;
2452   while (lp != NULL) {
2453     int lp_pre_order = lp->head()->pre_order();
2454     // Find insertion point for "lp"
2455     while (current != NULL) {
2456       if (current == lp)
2457         return leaf; // Already in list
2458       if (current->head()->pre_order() < lp_pre_order)
2459         break;
2460       if (current->head()->pre_order() == lp_pre_order &&
2461           current->tail()->pre_order() > lp->tail()->pre_order()) {
2462         break;
2463       }
2464       prev = current;
2465       current = current->parent();
2466     }
2467     Loop* next_lp = lp->parent(); // Save future list of items to insert
2468     // Insert lp before current
2469     lp->set_parent(current);
2470     if (prev != NULL) {
2471       prev->set_parent(lp);
2472     } else {
2473       leaf = lp;
2474     }
2475     prev = lp;     // Inserted item is new prev[ious]
2476     lp = next_lp;  // Next item to insert
2477   }
2478   return leaf;
2479 }
2480 
2481 // ------------------------------------------------------------------
2482 // ciTypeFlow::build_loop_tree
2483 //
2484 // Incrementally build loop tree.
build_loop_tree(Block * blk)2485 void ciTypeFlow::build_loop_tree(Block* blk) {
2486   assert(!blk->is_post_visited(), "precondition");
2487   Loop* innermost = NULL; // merge of loop tree branches over all successors
2488 
2489   for (SuccIter iter(blk); !iter.done(); iter.next()) {
2490     Loop*  lp   = NULL;
2491     Block* succ = iter.succ();
2492     if (!succ->is_post_visited()) {
2493       // Found backedge since predecessor post visited, but successor is not
2494       assert(succ->pre_order() <= blk->pre_order(), "should be backedge");
2495 
2496       // Create a LoopNode to mark this loop.
2497       lp = new (arena()) Loop(succ, blk);
2498       if (succ->loop() == NULL)
2499         succ->set_loop(lp);
2500       // succ->loop will be updated to innermost loop on a later call, when blk==succ
2501 
2502     } else {  // Nested loop
2503       lp = succ->loop();
2504 
2505       // If succ is loop head, find outer loop.
2506       while (lp != NULL && lp->head() == succ) {
2507         lp = lp->parent();
2508       }
2509       if (lp == NULL) {
2510         // Infinite loop, it's parent is the root
2511         lp = loop_tree_root();
2512       }
2513     }
2514 
2515     // Check for irreducible loop.
2516     // Successor has already been visited. If the successor's loop head
2517     // has already been post-visited, then this is another entry into the loop.
2518     while (lp->head()->is_post_visited() && lp != loop_tree_root()) {
2519       _has_irreducible_entry = true;
2520       lp->set_irreducible(succ);
2521       if (!succ->is_on_work_list()) {
2522         // Assume irreducible entries need more data flow
2523         add_to_work_list(succ);
2524       }
2525       Loop* plp = lp->parent();
2526       if (plp == NULL) {
2527         // This only happens for some irreducible cases.  The parent
2528         // will be updated during a later pass.
2529         break;
2530       }
2531       lp = plp;
2532     }
2533 
2534     // Merge loop tree branch for all successors.
2535     innermost = innermost == NULL ? lp : innermost->sorted_merge(lp);
2536 
2537   } // end loop
2538 
2539   if (innermost == NULL) {
2540     assert(blk->successors()->length() == 0, "CFG exit");
2541     blk->set_loop(loop_tree_root());
2542   } else if (innermost->head() == blk) {
2543     // If loop header, complete the tree pointers
2544     if (blk->loop() != innermost) {
2545 #ifdef ASSERT
2546       assert(blk->loop()->head() == innermost->head(), "same head");
2547       Loop* dl;
2548       for (dl = innermost; dl != NULL && dl != blk->loop(); dl = dl->parent());
2549       assert(dl == blk->loop(), "blk->loop() already in innermost list");
2550 #endif
2551       blk->set_loop(innermost);
2552     }
2553     innermost->def_locals()->add(blk->def_locals());
2554     Loop* l = innermost;
2555     Loop* p = l->parent();
2556     while (p && l->head() == blk) {
2557       l->set_sibling(p->child());  // Put self on parents 'next child'
2558       p->set_child(l);             // Make self the first child of parent
2559       p->def_locals()->add(l->def_locals());
2560       l = p;                       // Walk up the parent chain
2561       p = l->parent();
2562     }
2563   } else {
2564     blk->set_loop(innermost);
2565     innermost->def_locals()->add(blk->def_locals());
2566   }
2567 }
2568 
2569 // ------------------------------------------------------------------
2570 // ciTypeFlow::Loop::contains
2571 //
2572 // Returns true if lp is nested loop.
contains(ciTypeFlow::Loop * lp) const2573 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const {
2574   assert(lp != NULL, "");
2575   if (this == lp || head() == lp->head()) return true;
2576   int depth1 = depth();
2577   int depth2 = lp->depth();
2578   if (depth1 > depth2)
2579     return false;
2580   while (depth1 < depth2) {
2581     depth2--;
2582     lp = lp->parent();
2583   }
2584   return this == lp;
2585 }
2586 
2587 // ------------------------------------------------------------------
2588 // ciTypeFlow::Loop::depth
2589 //
2590 // Loop depth
depth() const2591 int ciTypeFlow::Loop::depth() const {
2592   int dp = 0;
2593   for (Loop* lp = this->parent(); lp != NULL; lp = lp->parent())
2594     dp++;
2595   return dp;
2596 }
2597 
2598 #ifndef PRODUCT
2599 // ------------------------------------------------------------------
2600 // ciTypeFlow::Loop::print
print(outputStream * st,int indent) const2601 void ciTypeFlow::Loop::print(outputStream* st, int indent) const {
2602   for (int i = 0; i < indent; i++) st->print(" ");
2603   st->print("%d<-%d %s",
2604             is_root() ? 0 : this->head()->pre_order(),
2605             is_root() ? 0 : this->tail()->pre_order(),
2606             is_irreducible()?" irr":"");
2607   st->print(" defs: ");
2608   def_locals()->print_on(st, _head->outer()->method()->max_locals());
2609   st->cr();
2610   for (Loop* ch = child(); ch != NULL; ch = ch->sibling())
2611     ch->print(st, indent+2);
2612 }
2613 #endif
2614 
2615 // ------------------------------------------------------------------
2616 // ciTypeFlow::df_flow_types
2617 //
2618 // Perform the depth first type flow analysis. Helper for flow_types.
df_flow_types(Block * start,bool do_flow,StateVector * temp_vector,JsrSet * temp_set)2619 void ciTypeFlow::df_flow_types(Block* start,
2620                                bool do_flow,
2621                                StateVector* temp_vector,
2622                                JsrSet* temp_set) {
2623   int dft_len = 100;
2624   GrowableArray<Block*> stk(dft_len);
2625 
2626   ciBlock* dummy = _methodBlocks->make_dummy_block();
2627   JsrSet* root_set = new JsrSet(NULL, 0);
2628   Block* root_head = new (arena()) Block(this, dummy, root_set);
2629   Block* root_tail = new (arena()) Block(this, dummy, root_set);
2630   root_head->set_pre_order(0);
2631   root_head->set_post_order(0);
2632   root_tail->set_pre_order(max_jint);
2633   root_tail->set_post_order(max_jint);
2634   set_loop_tree_root(new (arena()) Loop(root_head, root_tail));
2635 
2636   stk.push(start);
2637 
2638   _next_pre_order = 0;  // initialize pre_order counter
2639   _rpo_list = NULL;
2640   int next_po = 0;      // initialize post_order counter
2641 
2642   // Compute RPO and the control flow graph
2643   int size;
2644   while ((size = stk.length()) > 0) {
2645     Block* blk = stk.top(); // Leave node on stack
2646     if (!blk->is_visited()) {
2647       // forward arc in graph
2648       assert (!blk->has_pre_order(), "");
2649       blk->set_next_pre_order();
2650 
2651       if (_next_pre_order >= (int)Compile::current()->max_node_limit() / 2) {
2652         // Too many basic blocks.  Bail out.
2653         // This can happen when try/finally constructs are nested to depth N,
2654         // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
2655         // "MaxNodeLimit / 2" is used because probably the parser will
2656         // generate at least twice that many nodes and bail out.
2657         record_failure("too many basic blocks");
2658         return;
2659       }
2660       if (do_flow) {
2661         flow_block(blk, temp_vector, temp_set);
2662         if (failing()) return; // Watch for bailouts.
2663       }
2664     } else if (!blk->is_post_visited()) {
2665       // cross or back arc
2666       for (SuccIter iter(blk); !iter.done(); iter.next()) {
2667         Block* succ = iter.succ();
2668         if (!succ->is_visited()) {
2669           stk.push(succ);
2670         }
2671       }
2672       if (stk.length() == size) {
2673         // There were no additional children, post visit node now
2674         stk.pop(); // Remove node from stack
2675 
2676         build_loop_tree(blk);
2677         blk->set_post_order(next_po++);   // Assign post order
2678         prepend_to_rpo_list(blk);
2679         assert(blk->is_post_visited(), "");
2680 
2681         if (blk->is_loop_head() && !blk->is_on_work_list()) {
2682           // Assume loop heads need more data flow
2683           add_to_work_list(blk);
2684         }
2685       }
2686     } else {
2687       stk.pop(); // Remove post-visited node from stack
2688     }
2689   }
2690 }
2691 
2692 // ------------------------------------------------------------------
2693 // ciTypeFlow::flow_types
2694 //
2695 // Perform the type flow analysis, creating and cloning Blocks as
2696 // necessary.
flow_types()2697 void ciTypeFlow::flow_types() {
2698   ResourceMark rm;
2699   StateVector* temp_vector = new StateVector(this);
2700   JsrSet* temp_set = new JsrSet(NULL, 16);
2701 
2702   // Create the method entry block.
2703   Block* start = block_at(start_bci(), temp_set);
2704 
2705   // Load the initial state into it.
2706   const StateVector* start_state = get_start_state();
2707   if (failing())  return;
2708   start->meet(start_state);
2709 
2710   // Depth first visit
2711   df_flow_types(start, true /*do flow*/, temp_vector, temp_set);
2712 
2713   if (failing())  return;
2714   assert(_rpo_list == start, "must be start");
2715 
2716   // Any loops found?
2717   if (loop_tree_root()->child() != NULL &&
2718       env()->comp_level() >= CompLevel_full_optimization) {
2719       // Loop optimizations are not performed on Tier1 compiles.
2720 
2721     bool changed = clone_loop_heads(loop_tree_root(), temp_vector, temp_set);
2722 
2723     // If some loop heads were cloned, recompute postorder and loop tree
2724     if (changed) {
2725       loop_tree_root()->set_child(NULL);
2726       for (Block* blk = _rpo_list; blk != NULL;) {
2727         Block* next = blk->rpo_next();
2728         blk->df_init();
2729         blk = next;
2730       }
2731       df_flow_types(start, false /*no flow*/, temp_vector, temp_set);
2732     }
2733   }
2734 
2735   if (CITraceTypeFlow) {
2736     tty->print_cr("\nLoop tree");
2737     loop_tree_root()->print();
2738   }
2739 
2740   // Continue flow analysis until fixed point reached
2741 
2742   debug_only(int max_block = _next_pre_order;)
2743 
2744   while (!work_list_empty()) {
2745     Block* blk = work_list_next();
2746     assert (blk->has_post_order(), "post order assigned above");
2747 
2748     flow_block(blk, temp_vector, temp_set);
2749 
2750     assert (max_block == _next_pre_order, "no new blocks");
2751     assert (!failing(), "no more bailouts");
2752   }
2753 }
2754 
2755 // ------------------------------------------------------------------
2756 // ciTypeFlow::map_blocks
2757 //
2758 // Create the block map, which indexes blocks in reverse post-order.
map_blocks()2759 void ciTypeFlow::map_blocks() {
2760   assert(_block_map == NULL, "single initialization");
2761   int block_ct = _next_pre_order;
2762   _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct);
2763   assert(block_ct == block_count(), "");
2764 
2765   Block* blk = _rpo_list;
2766   for (int m = 0; m < block_ct; m++) {
2767     int rpo = blk->rpo();
2768     assert(rpo == m, "should be sequential");
2769     _block_map[rpo] = blk;
2770     blk = blk->rpo_next();
2771   }
2772   assert(blk == NULL, "should be done");
2773 
2774   for (int j = 0; j < block_ct; j++) {
2775     assert(_block_map[j] != NULL, "must not drop any blocks");
2776     Block* block = _block_map[j];
2777     // Remove dead blocks from successor lists:
2778     for (int e = 0; e <= 1; e++) {
2779       GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
2780       for (int k = 0; k < l->length(); k++) {
2781         Block* s = l->at(k);
2782         if (!s->has_post_order()) {
2783           if (CITraceTypeFlow) {
2784             tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
2785             s->print_value_on(tty);
2786             tty->cr();
2787           }
2788           l->remove(s);
2789           --k;
2790         }
2791       }
2792     }
2793   }
2794 }
2795 
2796 // ------------------------------------------------------------------
2797 // ciTypeFlow::get_block_for
2798 //
2799 // Find a block with this ciBlock which has a compatible JsrSet.
2800 // If no such block exists, create it, unless the option is no_create.
2801 // If the option is create_backedge_copy, always create a fresh backedge copy.
get_block_for(int ciBlockIndex,ciTypeFlow::JsrSet * jsrs,CreateOption option)2802 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2803   Arena* a = arena();
2804   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2805   if (blocks == NULL) {
2806     // Query only?
2807     if (option == no_create)  return NULL;
2808 
2809     // Allocate the growable array.
2810     blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL);
2811     _idx_to_blocklist[ciBlockIndex] = blocks;
2812   }
2813 
2814   if (option != create_backedge_copy) {
2815     int len = blocks->length();
2816     for (int i = 0; i < len; i++) {
2817       Block* block = blocks->at(i);
2818       if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
2819         return block;
2820       }
2821     }
2822   }
2823 
2824   // Query only?
2825   if (option == no_create)  return NULL;
2826 
2827   // We did not find a compatible block.  Create one.
2828   Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs);
2829   if (option == create_backedge_copy)  new_block->set_backedge_copy(true);
2830   blocks->append(new_block);
2831   return new_block;
2832 }
2833 
2834 // ------------------------------------------------------------------
2835 // ciTypeFlow::backedge_copy_count
2836 //
backedge_copy_count(int ciBlockIndex,ciTypeFlow::JsrSet * jsrs) const2837 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
2838   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2839 
2840   if (blocks == NULL) {
2841     return 0;
2842   }
2843 
2844   int count = 0;
2845   int len = blocks->length();
2846   for (int i = 0; i < len; i++) {
2847     Block* block = blocks->at(i);
2848     if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
2849       count++;
2850     }
2851   }
2852 
2853   return count;
2854 }
2855 
2856 // ------------------------------------------------------------------
2857 // ciTypeFlow::do_flow
2858 //
2859 // Perform type inference flow analysis.
do_flow()2860 void ciTypeFlow::do_flow() {
2861   if (CITraceTypeFlow) {
2862     tty->print_cr("\nPerforming flow analysis on method");
2863     method()->print();
2864     if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
2865     tty->cr();
2866     method()->print_codes();
2867   }
2868   if (CITraceTypeFlow) {
2869     tty->print_cr("Initial CI Blocks");
2870     print_on(tty);
2871   }
2872   flow_types();
2873   // Watch for bailouts.
2874   if (failing()) {
2875     return;
2876   }
2877 
2878   map_blocks();
2879 
2880   if (CIPrintTypeFlow || CITraceTypeFlow) {
2881     rpo_print_on(tty);
2882   }
2883 }
2884 
2885 // ------------------------------------------------------------------
2886 // ciTypeFlow::record_failure()
2887 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
2888 // This is required because there is not a 1-1 relation between the ciEnv and
2889 // the TypeFlow passes within a compilation task.  For example, if the compiler
2890 // is considering inlining a method, it will request a TypeFlow.  If that fails,
2891 // the compilation as a whole may continue without the inlining.  Some TypeFlow
2892 // requests are not optional; if they fail the requestor is responsible for
2893 // copying the failure reason up to the ciEnv.  (See Parse::Parse.)
record_failure(const char * reason)2894 void ciTypeFlow::record_failure(const char* reason) {
2895   if (env()->log() != NULL) {
2896     env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
2897   }
2898   if (_failure_reason == NULL) {
2899     // Record the first failure reason.
2900     _failure_reason = reason;
2901   }
2902 }
2903 
2904 #ifndef PRODUCT
2905 // ------------------------------------------------------------------
2906 // ciTypeFlow::print_on
print_on(outputStream * st) const2907 void ciTypeFlow::print_on(outputStream* st) const {
2908   // Walk through CI blocks
2909   st->print_cr("********************************************************");
2910   st->print   ("TypeFlow for ");
2911   method()->name()->print_symbol_on(st);
2912   int limit_bci = code_size();
2913   st->print_cr("  %d bytes", limit_bci);
2914   ciMethodBlocks  *mblks = _methodBlocks;
2915   ciBlock* current = NULL;
2916   for (int bci = 0; bci < limit_bci; bci++) {
2917     ciBlock* blk = mblks->block_containing(bci);
2918     if (blk != NULL && blk != current) {
2919       current = blk;
2920       current->print_on(st);
2921 
2922       GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
2923       int num_blocks = (blocks == NULL) ? 0 : blocks->length();
2924 
2925       if (num_blocks == 0) {
2926         st->print_cr("  No Blocks");
2927       } else {
2928         for (int i = 0; i < num_blocks; i++) {
2929           Block* block = blocks->at(i);
2930           block->print_on(st);
2931         }
2932       }
2933       st->print_cr("--------------------------------------------------------");
2934       st->cr();
2935     }
2936   }
2937   st->print_cr("********************************************************");
2938   st->cr();
2939 }
2940 
rpo_print_on(outputStream * st) const2941 void ciTypeFlow::rpo_print_on(outputStream* st) const {
2942   st->print_cr("********************************************************");
2943   st->print   ("TypeFlow for ");
2944   method()->name()->print_symbol_on(st);
2945   int limit_bci = code_size();
2946   st->print_cr("  %d bytes", limit_bci);
2947   for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) {
2948     blk->print_on(st);
2949     st->print_cr("--------------------------------------------------------");
2950     st->cr();
2951   }
2952   st->print_cr("********************************************************");
2953   st->cr();
2954 }
2955 #endif
2956