Thu, 07 Oct 2010 21:40:55 -0700
6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
Reviewed-by: kvn
1 /*
2 * Copyright (c) 1998, 2010, 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 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_parse2.cpp.incl"
28 extern int explicit_null_checks_inserted,
29 explicit_null_checks_elided;
31 //---------------------------------array_load----------------------------------
32 void Parse::array_load(BasicType elem_type) {
33 const Type* elem = Type::TOP;
34 Node* adr = array_addressing(elem_type, 0, &elem);
35 if (stopped()) return; // guaranteed null or range check
36 _sp -= 2; // Pop array and index
37 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type);
38 Node* ld = make_load(control(), adr, elem, elem_type, adr_type);
39 push(ld);
40 }
43 //--------------------------------array_store----------------------------------
44 void Parse::array_store(BasicType elem_type) {
45 Node* adr = array_addressing(elem_type, 1);
46 if (stopped()) return; // guaranteed null or range check
47 Node* val = pop();
48 _sp -= 2; // Pop array and index
49 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type);
50 store_to_memory(control(), adr, val, elem_type, adr_type);
51 }
54 //------------------------------array_addressing-------------------------------
55 // Pull array and index from the stack. Compute pointer-to-element.
56 Node* Parse::array_addressing(BasicType type, int vals, const Type* *result2) {
57 Node *idx = peek(0+vals); // Get from stack without popping
58 Node *ary = peek(1+vals); // in case of exception
60 // Null check the array base, with correct stack contents
61 ary = do_null_check(ary, T_ARRAY);
62 // Compile-time detect of null-exception?
63 if (stopped()) return top();
65 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
66 const TypeInt* sizetype = arytype->size();
67 const Type* elemtype = arytype->elem();
69 if (UseUniqueSubclasses && result2 != NULL) {
70 const Type* el = elemtype->make_ptr();
71 if (el && el->isa_instptr()) {
72 const TypeInstPtr* toop = el->is_instptr();
73 if (toop->klass()->as_instance_klass()->unique_concrete_subklass()) {
74 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
75 const Type* subklass = Type::get_const_type(toop->klass());
76 elemtype = subklass->join(el);
77 }
78 }
79 }
81 // Check for big class initializers with all constant offsets
82 // feeding into a known-size array.
83 const TypeInt* idxtype = _gvn.type(idx)->is_int();
84 // See if the highest idx value is less than the lowest array bound,
85 // and if the idx value cannot be negative:
86 bool need_range_check = true;
87 if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
88 need_range_check = false;
89 if (C->log() != NULL) C->log()->elem("observe that='!need_range_check'");
90 }
92 if (!arytype->klass()->is_loaded()) {
93 // Only fails for some -Xcomp runs
94 // The class is unloaded. We have to run this bytecode in the interpreter.
95 uncommon_trap(Deoptimization::Reason_unloaded,
96 Deoptimization::Action_reinterpret,
97 arytype->klass(), "!loaded array");
98 return top();
99 }
101 // Do the range check
102 if (GenerateRangeChecks && need_range_check) {
103 Node* tst;
104 if (sizetype->_hi <= 0) {
105 // The greatest array bound is negative, so we can conclude that we're
106 // compiling unreachable code, but the unsigned compare trick used below
107 // only works with non-negative lengths. Instead, hack "tst" to be zero so
108 // the uncommon_trap path will always be taken.
109 tst = _gvn.intcon(0);
110 } else {
111 // Range is constant in array-oop, so we can use the original state of mem
112 Node* len = load_array_length(ary);
114 // Test length vs index (standard trick using unsigned compare)
115 Node* chk = _gvn.transform( new (C, 3) CmpUNode(idx, len) );
116 BoolTest::mask btest = BoolTest::lt;
117 tst = _gvn.transform( new (C, 2) BoolNode(chk, btest) );
118 }
119 // Branch to failure if out of bounds
120 { BuildCutout unless(this, tst, PROB_MAX);
121 if (C->allow_range_check_smearing()) {
122 // Do not use builtin_throw, since range checks are sometimes
123 // made more stringent by an optimistic transformation.
124 // This creates "tentative" range checks at this point,
125 // which are not guaranteed to throw exceptions.
126 // See IfNode::Ideal, is_range_check, adjust_check.
127 uncommon_trap(Deoptimization::Reason_range_check,
128 Deoptimization::Action_make_not_entrant,
129 NULL, "range_check");
130 } else {
131 // If we have already recompiled with the range-check-widening
132 // heroic optimization turned off, then we must really be throwing
133 // range check exceptions.
134 builtin_throw(Deoptimization::Reason_range_check, idx);
135 }
136 }
137 }
138 // Check for always knowing you are throwing a range-check exception
139 if (stopped()) return top();
141 Node* ptr = array_element_address(ary, idx, type, sizetype);
143 if (result2 != NULL) *result2 = elemtype;
145 assert(ptr != top(), "top should go hand-in-hand with stopped");
147 return ptr;
148 }
151 // returns IfNode
152 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask) {
153 Node *cmp = _gvn.transform( new (C, 3) CmpINode( a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
154 Node *tst = _gvn.transform( new (C, 2) BoolNode( cmp, mask));
155 IfNode *iff = create_and_map_if( control(), tst, ((mask == BoolTest::eq) ? PROB_STATIC_INFREQUENT : PROB_FAIR), COUNT_UNKNOWN );
156 return iff;
157 }
159 // return Region node
160 Node* Parse::jump_if_join(Node* iffalse, Node* iftrue) {
161 Node *region = new (C, 3) RegionNode(3); // 2 results
162 record_for_igvn(region);
163 region->init_req(1, iffalse);
164 region->init_req(2, iftrue );
165 _gvn.set_type(region, Type::CONTROL);
166 region = _gvn.transform(region);
167 set_control (region);
168 return region;
169 }
172 //------------------------------helper for tableswitch-------------------------
173 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) {
174 // True branch, use existing map info
175 { PreserveJVMState pjvms(this);
176 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode (iff) );
177 set_control( iftrue );
178 profile_switch_case(prof_table_index);
179 merge_new_path(dest_bci_if_true);
180 }
182 // False branch
183 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) );
184 set_control( iffalse );
185 }
187 void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) {
188 // True branch, use existing map info
189 { PreserveJVMState pjvms(this);
190 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode (iff) );
191 set_control( iffalse );
192 profile_switch_case(prof_table_index);
193 merge_new_path(dest_bci_if_true);
194 }
196 // False branch
197 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff) );
198 set_control( iftrue );
199 }
201 void Parse::jump_if_always_fork(int dest_bci, int prof_table_index) {
202 // False branch, use existing map and control()
203 profile_switch_case(prof_table_index);
204 merge_new_path(dest_bci);
205 }
208 extern "C" {
209 static int jint_cmp(const void *i, const void *j) {
210 int a = *(jint *)i;
211 int b = *(jint *)j;
212 return a > b ? 1 : a < b ? -1 : 0;
213 }
214 }
217 // Default value for methodData switch indexing. Must be a negative value to avoid
218 // conflict with any legal switch index.
219 #define NullTableIndex -1
221 class SwitchRange : public StackObj {
222 // a range of integers coupled with a bci destination
223 jint _lo; // inclusive lower limit
224 jint _hi; // inclusive upper limit
225 int _dest;
226 int _table_index; // index into method data table
228 public:
229 jint lo() const { return _lo; }
230 jint hi() const { return _hi; }
231 int dest() const { return _dest; }
232 int table_index() const { return _table_index; }
233 bool is_singleton() const { return _lo == _hi; }
235 void setRange(jint lo, jint hi, int dest, int table_index) {
236 assert(lo <= hi, "must be a non-empty range");
237 _lo = lo, _hi = hi; _dest = dest; _table_index = table_index;
238 }
239 bool adjoinRange(jint lo, jint hi, int dest, int table_index) {
240 assert(lo <= hi, "must be a non-empty range");
241 if (lo == _hi+1 && dest == _dest && table_index == _table_index) {
242 _hi = hi;
243 return true;
244 }
245 return false;
246 }
248 void set (jint value, int dest, int table_index) {
249 setRange(value, value, dest, table_index);
250 }
251 bool adjoin(jint value, int dest, int table_index) {
252 return adjoinRange(value, value, dest, table_index);
253 }
255 void print(ciEnv* env) {
256 if (is_singleton())
257 tty->print(" {%d}=>%d", lo(), dest());
258 else if (lo() == min_jint)
259 tty->print(" {..%d}=>%d", hi(), dest());
260 else if (hi() == max_jint)
261 tty->print(" {%d..}=>%d", lo(), dest());
262 else
263 tty->print(" {%d..%d}=>%d", lo(), hi(), dest());
264 }
265 };
268 //-------------------------------do_tableswitch--------------------------------
269 void Parse::do_tableswitch() {
270 Node* lookup = pop();
272 // Get information about tableswitch
273 int default_dest = iter().get_dest_table(0);
274 int lo_index = iter().get_int_table(1);
275 int hi_index = iter().get_int_table(2);
276 int len = hi_index - lo_index + 1;
278 if (len < 1) {
279 // If this is a backward branch, add safepoint
280 maybe_add_safepoint(default_dest);
281 if (should_add_predicate(default_dest)){
282 _sp += 1; // set original stack for use by uncommon_trap
283 add_predicate();
284 _sp -= 1;
285 }
286 merge(default_dest);
287 return;
288 }
290 // generate decision tree, using trichotomy when possible
291 int rnum = len+2;
292 bool makes_backward_branch = false;
293 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
294 int rp = -1;
295 if (lo_index != min_jint) {
296 ranges[++rp].setRange(min_jint, lo_index-1, default_dest, NullTableIndex);
297 }
298 for (int j = 0; j < len; j++) {
299 jint match_int = lo_index+j;
300 int dest = iter().get_dest_table(j+3);
301 makes_backward_branch |= (dest <= bci());
302 int table_index = method_data_update() ? j : NullTableIndex;
303 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index)) {
304 ranges[++rp].set(match_int, dest, table_index);
305 }
306 }
307 jint highest = lo_index+(len-1);
308 assert(ranges[rp].hi() == highest, "");
309 if (highest != max_jint
310 && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex)) {
311 ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex);
312 }
313 assert(rp < len+2, "not too many ranges");
315 // Safepoint in case if backward branch observed
316 if( makes_backward_branch && UseLoopSafepoints )
317 add_safepoint();
319 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
320 }
323 //------------------------------do_lookupswitch--------------------------------
324 void Parse::do_lookupswitch() {
325 Node *lookup = pop(); // lookup value
326 // Get information about lookupswitch
327 int default_dest = iter().get_dest_table(0);
328 int len = iter().get_int_table(1);
330 if (len < 1) { // If this is a backward branch, add safepoint
331 maybe_add_safepoint(default_dest);
332 if (should_add_predicate(default_dest)){
333 _sp += 1; // set original stack for use by uncommon_trap
334 add_predicate();
335 _sp -= 1;
336 }
337 merge(default_dest);
338 return;
339 }
341 // generate decision tree, using trichotomy when possible
342 jint* table = NEW_RESOURCE_ARRAY(jint, len*2);
343 {
344 for( int j = 0; j < len; j++ ) {
345 table[j+j+0] = iter().get_int_table(2+j+j);
346 table[j+j+1] = iter().get_dest_table(2+j+j+1);
347 }
348 qsort( table, len, 2*sizeof(table[0]), jint_cmp );
349 }
351 int rnum = len*2+1;
352 bool makes_backward_branch = false;
353 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
354 int rp = -1;
355 for( int j = 0; j < len; j++ ) {
356 jint match_int = table[j+j+0];
357 int dest = table[j+j+1];
358 int next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1;
359 int table_index = method_data_update() ? j : NullTableIndex;
360 makes_backward_branch |= (dest <= bci());
361 if( match_int != next_lo ) {
362 ranges[++rp].setRange(next_lo, match_int-1, default_dest, NullTableIndex);
363 }
364 if( rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index) ) {
365 ranges[++rp].set(match_int, dest, table_index);
366 }
367 }
368 jint highest = table[2*(len-1)];
369 assert(ranges[rp].hi() == highest, "");
370 if( highest != max_jint
371 && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex) ) {
372 ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex);
373 }
374 assert(rp < rnum, "not too many ranges");
376 // Safepoint in case backward branch observed
377 if( makes_backward_branch && UseLoopSafepoints )
378 add_safepoint();
380 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
381 }
383 //----------------------------create_jump_tables-------------------------------
384 bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) {
385 // Are jumptables enabled
386 if (!UseJumpTables) return false;
388 // Are jumptables supported
389 if (!Matcher::has_match_rule(Op_Jump)) return false;
391 // Don't make jump table if profiling
392 if (method_data_update()) return false;
394 // Decide if a guard is needed to lop off big ranges at either (or
395 // both) end(s) of the input set. We'll call this the default target
396 // even though we can't be sure that it is the true "default".
398 bool needs_guard = false;
399 int default_dest;
400 int64 total_outlier_size = 0;
401 int64 hi_size = ((int64)hi->hi()) - ((int64)hi->lo()) + 1;
402 int64 lo_size = ((int64)lo->hi()) - ((int64)lo->lo()) + 1;
404 if (lo->dest() == hi->dest()) {
405 total_outlier_size = hi_size + lo_size;
406 default_dest = lo->dest();
407 } else if (lo_size > hi_size) {
408 total_outlier_size = lo_size;
409 default_dest = lo->dest();
410 } else {
411 total_outlier_size = hi_size;
412 default_dest = hi->dest();
413 }
415 // If a guard test will eliminate very sparse end ranges, then
416 // it is worth the cost of an extra jump.
417 if (total_outlier_size > (MaxJumpTableSparseness * 4)) {
418 needs_guard = true;
419 if (default_dest == lo->dest()) lo++;
420 if (default_dest == hi->dest()) hi--;
421 }
423 // Find the total number of cases and ranges
424 int64 num_cases = ((int64)hi->hi()) - ((int64)lo->lo()) + 1;
425 int num_range = hi - lo + 1;
427 // Don't create table if: too large, too small, or too sparse.
428 if (num_cases < MinJumpTableSize || num_cases > MaxJumpTableSize)
429 return false;
430 if (num_cases > (MaxJumpTableSparseness * num_range))
431 return false;
433 // Normalize table lookups to zero
434 int lowval = lo->lo();
435 key_val = _gvn.transform( new (C, 3) SubINode(key_val, _gvn.intcon(lowval)) );
437 // Generate a guard to protect against input keyvals that aren't
438 // in the switch domain.
439 if (needs_guard) {
440 Node* size = _gvn.intcon(num_cases);
441 Node* cmp = _gvn.transform( new (C, 3) CmpUNode(key_val, size) );
442 Node* tst = _gvn.transform( new (C, 2) BoolNode(cmp, BoolTest::ge) );
443 IfNode* iff = create_and_map_if( control(), tst, PROB_FAIR, COUNT_UNKNOWN);
444 jump_if_true_fork(iff, default_dest, NullTableIndex);
445 }
447 // Create an ideal node JumpTable that has projections
448 // of all possible ranges for a switch statement
449 // The key_val input must be converted to a pointer offset and scaled.
450 // Compare Parse::array_addressing above.
451 #ifdef _LP64
452 // Clean the 32-bit int into a real 64-bit offset.
453 // Otherwise, the jint value 0 might turn into an offset of 0x0800000000.
454 const TypeLong* lkeytype = TypeLong::make(CONST64(0), num_cases-1, Type::WidenMin);
455 key_val = _gvn.transform( new (C, 2) ConvI2LNode(key_val, lkeytype) );
456 #endif
457 // Shift the value by wordsize so we have an index into the table, rather
458 // than a switch value
459 Node *shiftWord = _gvn.MakeConX(wordSize);
460 key_val = _gvn.transform( new (C, 3) MulXNode( key_val, shiftWord));
462 // Create the JumpNode
463 Node* jtn = _gvn.transform( new (C, 2) JumpNode(control(), key_val, num_cases) );
465 // These are the switch destinations hanging off the jumpnode
466 int i = 0;
467 for (SwitchRange* r = lo; r <= hi; r++) {
468 for (int j = r->lo(); j <= r->hi(); j++, i++) {
469 Node* input = _gvn.transform(new (C, 1) JumpProjNode(jtn, i, r->dest(), j - lowval));
470 {
471 PreserveJVMState pjvms(this);
472 set_control(input);
473 jump_if_always_fork(r->dest(), r->table_index());
474 }
475 }
476 }
477 assert(i == num_cases, "miscount of cases");
478 stop_and_kill_map(); // no more uses for this JVMS
479 return true;
480 }
482 //----------------------------jump_switch_ranges-------------------------------
483 void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) {
484 Block* switch_block = block();
486 if (switch_depth == 0) {
487 // Do special processing for the top-level call.
488 assert(lo->lo() == min_jint, "initial range must exhaust Type::INT");
489 assert(hi->hi() == max_jint, "initial range must exhaust Type::INT");
491 // Decrement pred-numbers for the unique set of nodes.
492 #ifdef ASSERT
493 // Ensure that the block's successors are a (duplicate-free) set.
494 int successors_counted = 0; // block occurrences in [hi..lo]
495 int unique_successors = switch_block->num_successors();
496 for (int i = 0; i < unique_successors; i++) {
497 Block* target = switch_block->successor_at(i);
499 // Check that the set of successors is the same in both places.
500 int successors_found = 0;
501 for (SwitchRange* p = lo; p <= hi; p++) {
502 if (p->dest() == target->start()) successors_found++;
503 }
504 assert(successors_found > 0, "successor must be known");
505 successors_counted += successors_found;
506 }
507 assert(successors_counted == (hi-lo)+1, "no unexpected successors");
508 #endif
510 // Maybe prune the inputs, based on the type of key_val.
511 jint min_val = min_jint;
512 jint max_val = max_jint;
513 const TypeInt* ti = key_val->bottom_type()->isa_int();
514 if (ti != NULL) {
515 min_val = ti->_lo;
516 max_val = ti->_hi;
517 assert(min_val <= max_val, "invalid int type");
518 }
519 while (lo->hi() < min_val) lo++;
520 if (lo->lo() < min_val) lo->setRange(min_val, lo->hi(), lo->dest(), lo->table_index());
521 while (hi->lo() > max_val) hi--;
522 if (hi->hi() > max_val) hi->setRange(hi->lo(), max_val, hi->dest(), hi->table_index());
523 }
525 #ifndef PRODUCT
526 if (switch_depth == 0) {
527 _max_switch_depth = 0;
528 _est_switch_depth = log2_intptr((hi-lo+1)-1)+1;
529 }
530 #endif
532 assert(lo <= hi, "must be a non-empty set of ranges");
533 if (lo == hi) {
534 jump_if_always_fork(lo->dest(), lo->table_index());
535 } else {
536 assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges");
537 assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges");
539 if (create_jump_tables(key_val, lo, hi)) return;
541 int nr = hi - lo + 1;
543 SwitchRange* mid = lo + nr/2;
544 // if there is an easy choice, pivot at a singleton:
545 if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton()) mid--;
547 assert(lo < mid && mid <= hi, "good pivot choice");
548 assert(nr != 2 || mid == hi, "should pick higher of 2");
549 assert(nr != 3 || mid == hi-1, "should pick middle of 3");
551 Node *test_val = _gvn.intcon(mid->lo());
553 if (mid->is_singleton()) {
554 IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne);
555 jump_if_false_fork(iff_ne, mid->dest(), mid->table_index());
557 // Special Case: If there are exactly three ranges, and the high
558 // and low range each go to the same place, omit the "gt" test,
559 // since it will not discriminate anything.
560 bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest());
561 if (eq_test_only) {
562 assert(mid == hi-1, "");
563 }
565 // if there is a higher range, test for it and process it:
566 if (mid < hi && !eq_test_only) {
567 // two comparisons of same values--should enable 1 test for 2 branches
568 // Use BoolTest::le instead of BoolTest::gt
569 IfNode *iff_le = jump_if_fork_int(key_val, test_val, BoolTest::le);
570 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff_le) );
571 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff_le) );
572 { PreserveJVMState pjvms(this);
573 set_control(iffalse);
574 jump_switch_ranges(key_val, mid+1, hi, switch_depth+1);
575 }
576 set_control(iftrue);
577 }
579 } else {
580 // mid is a range, not a singleton, so treat mid..hi as a unit
581 IfNode *iff_ge = jump_if_fork_int(key_val, test_val, BoolTest::ge);
583 // if there is a higher range, test for it and process it:
584 if (mid == hi) {
585 jump_if_true_fork(iff_ge, mid->dest(), mid->table_index());
586 } else {
587 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff_ge) );
588 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff_ge) );
589 { PreserveJVMState pjvms(this);
590 set_control(iftrue);
591 jump_switch_ranges(key_val, mid, hi, switch_depth+1);
592 }
593 set_control(iffalse);
594 }
595 }
597 // in any case, process the lower range
598 jump_switch_ranges(key_val, lo, mid-1, switch_depth+1);
599 }
601 // Decrease pred_count for each successor after all is done.
602 if (switch_depth == 0) {
603 int unique_successors = switch_block->num_successors();
604 for (int i = 0; i < unique_successors; i++) {
605 Block* target = switch_block->successor_at(i);
606 // Throw away the pre-allocated path for each unique successor.
607 target->next_path_num();
608 }
609 }
611 #ifndef PRODUCT
612 _max_switch_depth = MAX2(switch_depth, _max_switch_depth);
613 if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) {
614 SwitchRange* r;
615 int nsing = 0;
616 for( r = lo; r <= hi; r++ ) {
617 if( r->is_singleton() ) nsing++;
618 }
619 tty->print(">>> ");
620 _method->print_short_name();
621 tty->print_cr(" switch decision tree");
622 tty->print_cr(" %d ranges (%d singletons), max_depth=%d, est_depth=%d",
623 hi-lo+1, nsing, _max_switch_depth, _est_switch_depth);
624 if (_max_switch_depth > _est_switch_depth) {
625 tty->print_cr("******** BAD SWITCH DEPTH ********");
626 }
627 tty->print(" ");
628 for( r = lo; r <= hi; r++ ) {
629 r->print(env());
630 }
631 tty->print_cr("");
632 }
633 #endif
634 }
636 void Parse::modf() {
637 Node *f2 = pop();
638 Node *f1 = pop();
639 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::modf_Type(),
640 CAST_FROM_FN_PTR(address, SharedRuntime::frem),
641 "frem", NULL, //no memory effects
642 f1, f2);
643 Node* res = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0));
645 push(res);
646 }
648 void Parse::modd() {
649 Node *d2 = pop_pair();
650 Node *d1 = pop_pair();
651 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(),
652 CAST_FROM_FN_PTR(address, SharedRuntime::drem),
653 "drem", NULL, //no memory effects
654 d1, top(), d2, top());
655 Node* res_d = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0));
657 #ifdef ASSERT
658 Node* res_top = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 1));
659 assert(res_top == top(), "second value must be top");
660 #endif
662 push_pair(res_d);
663 }
665 void Parse::l2f() {
666 Node* f2 = pop();
667 Node* f1 = pop();
668 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(),
669 CAST_FROM_FN_PTR(address, SharedRuntime::l2f),
670 "l2f", NULL, //no memory effects
671 f1, f2);
672 Node* res = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0));
674 push(res);
675 }
677 void Parse::do_irem() {
678 // Must keep both values on the expression-stack during null-check
679 do_null_check(peek(), T_INT);
680 // Compile-time detect of null-exception?
681 if (stopped()) return;
683 Node* b = pop();
684 Node* a = pop();
686 const Type *t = _gvn.type(b);
687 if (t != Type::TOP) {
688 const TypeInt *ti = t->is_int();
689 if (ti->is_con()) {
690 int divisor = ti->get_con();
691 // check for positive power of 2
692 if (divisor > 0 &&
693 (divisor & ~(divisor-1)) == divisor) {
694 // yes !
695 Node *mask = _gvn.intcon((divisor - 1));
696 // Sigh, must handle negative dividends
697 Node *zero = _gvn.intcon(0);
698 IfNode *ifff = jump_if_fork_int(a, zero, BoolTest::lt);
699 Node *iff = _gvn.transform( new (C, 1) IfFalseNode(ifff) );
700 Node *ift = _gvn.transform( new (C, 1) IfTrueNode (ifff) );
701 Node *reg = jump_if_join(ift, iff);
702 Node *phi = PhiNode::make(reg, NULL, TypeInt::INT);
703 // Negative path; negate/and/negate
704 Node *neg = _gvn.transform( new (C, 3) SubINode(zero, a) );
705 Node *andn= _gvn.transform( new (C, 3) AndINode(neg, mask) );
706 Node *negn= _gvn.transform( new (C, 3) SubINode(zero, andn) );
707 phi->init_req(1, negn);
708 // Fast positive case
709 Node *andx = _gvn.transform( new (C, 3) AndINode(a, mask) );
710 phi->init_req(2, andx);
711 // Push the merge
712 push( _gvn.transform(phi) );
713 return;
714 }
715 }
716 }
717 // Default case
718 push( _gvn.transform( new (C, 3) ModINode(control(),a,b) ) );
719 }
721 // Handle jsr and jsr_w bytecode
722 void Parse::do_jsr() {
723 assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode");
725 // Store information about current state, tagged with new _jsr_bci
726 int return_bci = iter().next_bci();
727 int jsr_bci = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest();
729 // Update method data
730 profile_taken_branch(jsr_bci);
732 // The way we do things now, there is only one successor block
733 // for the jsr, because the target code is cloned by ciTypeFlow.
734 Block* target = successor_for_bci(jsr_bci);
736 // What got pushed?
737 const Type* ret_addr = target->peek();
738 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
740 // Effect on jsr on stack
741 push(_gvn.makecon(ret_addr));
743 // Flow to the jsr.
744 if (should_add_predicate(jsr_bci)){
745 add_predicate();
746 }
747 merge(jsr_bci);
748 }
750 // Handle ret bytecode
751 void Parse::do_ret() {
752 // Find to whom we return.
753 #if 0 // %%%% MAKE THIS WORK
754 Node* con = local();
755 const TypePtr* tp = con->bottom_type()->isa_ptr();
756 assert(tp && tp->singleton(), "");
757 int return_bci = (int) tp->get_con();
758 merge(return_bci);
759 #else
760 assert(block()->num_successors() == 1, "a ret can only go one place now");
761 Block* target = block()->successor_at(0);
762 assert(!target->is_ready(), "our arrival must be expected");
763 profile_ret(target->flow()->start());
764 int pnum = target->next_path_num();
765 merge_common(target, pnum);
766 #endif
767 }
769 //--------------------------dynamic_branch_prediction--------------------------
770 // Try to gather dynamic branch prediction behavior. Return a probability
771 // of the branch being taken and set the "cnt" field. Returns a -1.0
772 // if we need to use static prediction for some reason.
773 float Parse::dynamic_branch_prediction(float &cnt) {
774 ResourceMark rm;
776 cnt = COUNT_UNKNOWN;
778 // Use MethodData information if it is available
779 // FIXME: free the ProfileData structure
780 ciMethodData* methodData = method()->method_data();
781 if (!methodData->is_mature()) return PROB_UNKNOWN;
782 ciProfileData* data = methodData->bci_to_data(bci());
783 if (!data->is_JumpData()) return PROB_UNKNOWN;
785 // get taken and not taken values
786 int taken = data->as_JumpData()->taken();
787 int not_taken = 0;
788 if (data->is_BranchData()) {
789 not_taken = data->as_BranchData()->not_taken();
790 }
792 // scale the counts to be commensurate with invocation counts:
793 taken = method()->scale_count(taken);
794 not_taken = method()->scale_count(not_taken);
796 // Give up if too few counts to be meaningful
797 if (taken + not_taken < 40) {
798 if (C->log() != NULL) {
799 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken);
800 }
801 return PROB_UNKNOWN;
802 }
804 // Compute frequency that we arrive here
805 int sum = taken + not_taken;
806 // Adjust, if this block is a cloned private block but the
807 // Jump counts are shared. Taken the private counts for
808 // just this path instead of the shared counts.
809 if( block()->count() > 0 )
810 sum = block()->count();
811 cnt = (float)sum / (float)FreqCountInvocations;
813 // Pin probability to sane limits
814 float prob;
815 if( !taken )
816 prob = (0+PROB_MIN) / 2;
817 else if( !not_taken )
818 prob = (1+PROB_MAX) / 2;
819 else { // Compute probability of true path
820 prob = (float)taken / (float)(taken + not_taken);
821 if (prob > PROB_MAX) prob = PROB_MAX;
822 if (prob < PROB_MIN) prob = PROB_MIN;
823 }
825 assert((cnt > 0.0f) && (prob > 0.0f),
826 "Bad frequency assignment in if");
828 if (C->log() != NULL) {
829 const char* prob_str = NULL;
830 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always";
831 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never";
832 char prob_str_buf[30];
833 if (prob_str == NULL) {
834 sprintf(prob_str_buf, "%g", prob);
835 prob_str = prob_str_buf;
836 }
837 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%g' prob='%s'",
838 iter().get_dest(), taken, not_taken, cnt, prob_str);
839 }
840 return prob;
841 }
843 //-----------------------------branch_prediction-------------------------------
844 float Parse::branch_prediction(float& cnt,
845 BoolTest::mask btest,
846 int target_bci) {
847 float prob = dynamic_branch_prediction(cnt);
848 // If prob is unknown, switch to static prediction
849 if (prob != PROB_UNKNOWN) return prob;
851 prob = PROB_FAIR; // Set default value
852 if (btest == BoolTest::eq) // Exactly equal test?
853 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent
854 else if (btest == BoolTest::ne)
855 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent
857 // If this is a conditional test guarding a backwards branch,
858 // assume its a loop-back edge. Make it a likely taken branch.
859 if (target_bci < bci()) {
860 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt
861 // Since it's an OSR, we probably have profile data, but since
862 // branch_prediction returned PROB_UNKNOWN, the counts are too small.
863 // Let's make a special check here for completely zero counts.
864 ciMethodData* methodData = method()->method_data();
865 if (!methodData->is_empty()) {
866 ciProfileData* data = methodData->bci_to_data(bci());
867 // Only stop for truly zero counts, which mean an unknown part
868 // of the OSR-ed method, and we want to deopt to gather more stats.
869 // If you have ANY counts, then this loop is simply 'cold' relative
870 // to the OSR loop.
871 if (data->as_BranchData()->taken() +
872 data->as_BranchData()->not_taken() == 0 ) {
873 // This is the only way to return PROB_UNKNOWN:
874 return PROB_UNKNOWN;
875 }
876 }
877 }
878 prob = PROB_STATIC_FREQUENT; // Likely to take backwards branch
879 }
881 assert(prob != PROB_UNKNOWN, "must have some guess at this point");
882 return prob;
883 }
885 // The magic constants are chosen so as to match the output of
886 // branch_prediction() when the profile reports a zero taken count.
887 // It is important to distinguish zero counts unambiguously, because
888 // some branches (e.g., _213_javac.Assembler.eliminate) validly produce
889 // very small but nonzero probabilities, which if confused with zero
890 // counts would keep the program recompiling indefinitely.
891 bool Parse::seems_never_taken(float prob) {
892 return prob < PROB_MIN;
893 }
895 // True if the comparison seems to be the kind that will not change its
896 // statistics from true to false. See comments in adjust_map_after_if.
897 // This question is only asked along paths which are already
898 // classifed as untaken (by seems_never_taken), so really,
899 // if a path is never taken, its controlling comparison is
900 // already acting in a stable fashion. If the comparison
901 // seems stable, we will put an expensive uncommon trap
902 // on the untaken path. To be conservative, and to allow
903 // partially executed counted loops to be compiled fully,
904 // we will plant uncommon traps only after pointer comparisons.
905 bool Parse::seems_stable_comparison(BoolTest::mask btest, Node* cmp) {
906 for (int depth = 4; depth > 0; depth--) {
907 // The following switch can find CmpP here over half the time for
908 // dynamic language code rich with type tests.
909 // Code using counted loops or array manipulations (typical
910 // of benchmarks) will have many (>80%) CmpI instructions.
911 switch (cmp->Opcode()) {
912 case Op_CmpP:
913 // A never-taken null check looks like CmpP/BoolTest::eq.
914 // These certainly should be closed off as uncommon traps.
915 if (btest == BoolTest::eq)
916 return true;
917 // A never-failed type check looks like CmpP/BoolTest::ne.
918 // Let's put traps on those, too, so that we don't have to compile
919 // unused paths with indeterminate dynamic type information.
920 if (ProfileDynamicTypes)
921 return true;
922 return false;
924 case Op_CmpI:
925 // A small minority (< 10%) of CmpP are masked as CmpI,
926 // as if by boolean conversion ((p == q? 1: 0) != 0).
927 // Detect that here, even if it hasn't optimized away yet.
928 // Specifically, this covers the 'instanceof' operator.
929 if (btest == BoolTest::ne || btest == BoolTest::eq) {
930 if (_gvn.type(cmp->in(2))->singleton() &&
931 cmp->in(1)->is_Phi()) {
932 PhiNode* phi = cmp->in(1)->as_Phi();
933 int true_path = phi->is_diamond_phi();
934 if (true_path > 0 &&
935 _gvn.type(phi->in(1))->singleton() &&
936 _gvn.type(phi->in(2))->singleton()) {
937 // phi->region->if_proj->ifnode->bool->cmp
938 BoolNode* bol = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
939 btest = bol->_test._test;
940 cmp = bol->in(1);
941 continue;
942 }
943 }
944 }
945 return false;
946 }
947 }
948 return false;
949 }
951 //-------------------------------repush_if_args--------------------------------
952 // Push arguments of an "if" bytecode back onto the stack by adjusting _sp.
953 inline int Parse::repush_if_args() {
954 #ifndef PRODUCT
955 if (PrintOpto && WizardMode) {
956 tty->print("defending against excessive implicit null exceptions on %s @%d in ",
957 Bytecodes::name(iter().cur_bc()), iter().cur_bci());
958 method()->print_name(); tty->cr();
959 }
960 #endif
961 int bc_depth = - Bytecodes::depth(iter().cur_bc());
962 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
963 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms
964 assert(argument(0) != NULL, "must exist");
965 assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
966 _sp += bc_depth;
967 return bc_depth;
968 }
970 //----------------------------------do_ifnull----------------------------------
971 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
972 int target_bci = iter().get_dest();
974 Block* branch_block = successor_for_bci(target_bci);
975 Block* next_block = successor_for_bci(iter().next_bci());
977 float cnt;
978 float prob = branch_prediction(cnt, btest, target_bci);
979 if (prob == PROB_UNKNOWN) {
980 // (An earlier version of do_ifnull omitted this trap for OSR methods.)
981 #ifndef PRODUCT
982 if (PrintOpto && Verbose)
983 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
984 #endif
985 repush_if_args(); // to gather stats on loop
986 // We need to mark this branch as taken so that if we recompile we will
987 // see that it is possible. In the tiered system the interpreter doesn't
988 // do profiling and by the time we get to the lower tier from the interpreter
989 // the path may be cold again. Make sure it doesn't look untaken
990 profile_taken_branch(target_bci, !ProfileInterpreter);
991 uncommon_trap(Deoptimization::Reason_unreached,
992 Deoptimization::Action_reinterpret,
993 NULL, "cold");
994 if (EliminateAutoBox) {
995 // Mark the successor blocks as parsed
996 branch_block->next_path_num();
997 next_block->next_path_num();
998 }
999 return;
1000 }
1002 explicit_null_checks_inserted++;
1004 // Generate real control flow
1005 Node *tst = _gvn.transform( new (C, 2) BoolNode( c, btest ) );
1007 // Sanity check the probability value
1008 assert(prob > 0.0f,"Bad probability in Parser");
1009 // Need xform to put node in hash table
1010 IfNode *iff = create_and_xform_if( control(), tst, prob, cnt );
1011 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1012 // True branch
1013 { PreserveJVMState pjvms(this);
1014 Node* iftrue = _gvn.transform( new (C, 1) IfTrueNode (iff) );
1015 set_control(iftrue);
1017 if (stopped()) { // Path is dead?
1018 explicit_null_checks_elided++;
1019 if (EliminateAutoBox) {
1020 // Mark the successor block as parsed
1021 branch_block->next_path_num();
1022 }
1023 } else { // Path is live.
1024 // Update method data
1025 profile_taken_branch(target_bci);
1026 adjust_map_after_if(btest, c, prob, branch_block, next_block);
1027 if (!stopped()) {
1028 if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop
1029 int nargs = repush_if_args(); // set original stack for uncommon_trap
1030 add_predicate();
1031 _sp -= nargs;
1032 }
1033 merge(target_bci);
1034 }
1035 }
1036 }
1038 // False branch
1039 Node* iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) );
1040 set_control(iffalse);
1042 if (stopped()) { // Path is dead?
1043 explicit_null_checks_elided++;
1044 if (EliminateAutoBox) {
1045 // Mark the successor block as parsed
1046 next_block->next_path_num();
1047 }
1048 } else { // Path is live.
1049 // Update method data
1050 profile_not_taken_branch();
1051 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob,
1052 next_block, branch_block);
1053 }
1054 }
1056 //------------------------------------do_if------------------------------------
1057 void Parse::do_if(BoolTest::mask btest, Node* c) {
1058 int target_bci = iter().get_dest();
1060 Block* branch_block = successor_for_bci(target_bci);
1061 Block* next_block = successor_for_bci(iter().next_bci());
1063 float cnt;
1064 float prob = branch_prediction(cnt, btest, target_bci);
1065 float untaken_prob = 1.0 - prob;
1067 if (prob == PROB_UNKNOWN) {
1068 #ifndef PRODUCT
1069 if (PrintOpto && Verbose)
1070 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
1071 #endif
1072 repush_if_args(); // to gather stats on loop
1073 // We need to mark this branch as taken so that if we recompile we will
1074 // see that it is possible. In the tiered system the interpreter doesn't
1075 // do profiling and by the time we get to the lower tier from the interpreter
1076 // the path may be cold again. Make sure it doesn't look untaken
1077 profile_taken_branch(target_bci, !ProfileInterpreter);
1078 uncommon_trap(Deoptimization::Reason_unreached,
1079 Deoptimization::Action_reinterpret,
1080 NULL, "cold");
1081 if (EliminateAutoBox) {
1082 // Mark the successor blocks as parsed
1083 branch_block->next_path_num();
1084 next_block->next_path_num();
1085 }
1086 return;
1087 }
1089 // Sanity check the probability value
1090 assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1092 bool taken_if_true = true;
1093 // Convert BoolTest to canonical form:
1094 if (!BoolTest(btest).is_canonical()) {
1095 btest = BoolTest(btest).negate();
1096 taken_if_true = false;
1097 // prob is NOT updated here; it remains the probability of the taken
1098 // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1099 }
1100 assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1102 Node* tst0 = new (C, 2) BoolNode(c, btest);
1103 Node* tst = _gvn.transform(tst0);
1104 BoolTest::mask taken_btest = BoolTest::illegal;
1105 BoolTest::mask untaken_btest = BoolTest::illegal;
1107 if (tst->is_Bool()) {
1108 // Refresh c from the transformed bool node, since it may be
1109 // simpler than the original c. Also re-canonicalize btest.
1110 // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)).
1111 // That can arise from statements like: if (x instanceof C) ...
1112 if (tst != tst0) {
1113 // Canonicalize one more time since transform can change it.
1114 btest = tst->as_Bool()->_test._test;
1115 if (!BoolTest(btest).is_canonical()) {
1116 // Reverse edges one more time...
1117 tst = _gvn.transform( tst->as_Bool()->negate(&_gvn) );
1118 btest = tst->as_Bool()->_test._test;
1119 assert(BoolTest(btest).is_canonical(), "sanity");
1120 taken_if_true = !taken_if_true;
1121 }
1122 c = tst->in(1);
1123 }
1124 BoolTest::mask neg_btest = BoolTest(btest).negate();
1125 taken_btest = taken_if_true ? btest : neg_btest;
1126 untaken_btest = taken_if_true ? neg_btest : btest;
1127 }
1129 // Generate real control flow
1130 float true_prob = (taken_if_true ? prob : untaken_prob);
1131 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1132 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1133 Node* taken_branch = new (C, 1) IfTrueNode(iff);
1134 Node* untaken_branch = new (C, 1) IfFalseNode(iff);
1135 if (!taken_if_true) { // Finish conversion to canonical form
1136 Node* tmp = taken_branch;
1137 taken_branch = untaken_branch;
1138 untaken_branch = tmp;
1139 }
1141 // Branch is taken:
1142 { PreserveJVMState pjvms(this);
1143 taken_branch = _gvn.transform(taken_branch);
1144 set_control(taken_branch);
1146 if (stopped()) {
1147 if (EliminateAutoBox) {
1148 // Mark the successor block as parsed
1149 branch_block->next_path_num();
1150 }
1151 } else {
1152 // Update method data
1153 profile_taken_branch(target_bci);
1154 adjust_map_after_if(taken_btest, c, prob, branch_block, next_block);
1155 if (!stopped()) {
1156 if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop
1157 int nargs = repush_if_args(); // set original stack for the uncommon_trap
1158 add_predicate();
1159 _sp -= nargs;
1160 }
1161 merge(target_bci);
1162 }
1163 }
1164 }
1166 untaken_branch = _gvn.transform(untaken_branch);
1167 set_control(untaken_branch);
1169 // Branch not taken.
1170 if (stopped()) {
1171 if (EliminateAutoBox) {
1172 // Mark the successor block as parsed
1173 next_block->next_path_num();
1174 }
1175 } else {
1176 // Update method data
1177 profile_not_taken_branch();
1178 adjust_map_after_if(untaken_btest, c, untaken_prob,
1179 next_block, branch_block);
1180 }
1181 }
1183 //----------------------------adjust_map_after_if------------------------------
1184 // Adjust the JVM state to reflect the result of taking this path.
1185 // Basically, it means inspecting the CmpNode controlling this
1186 // branch, seeing how it constrains a tested value, and then
1187 // deciding if it's worth our while to encode this constraint
1188 // as graph nodes in the current abstract interpretation map.
1189 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob,
1190 Block* path, Block* other_path) {
1191 if (stopped() || !c->is_Cmp() || btest == BoolTest::illegal)
1192 return; // nothing to do
1194 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1196 if (seems_never_taken(prob) && seems_stable_comparison(btest, c)) {
1197 // If this might possibly turn into an implicit null check,
1198 // and the null has never yet been seen, we need to generate
1199 // an uncommon trap, so as to recompile instead of suffering
1200 // with very slow branches. (We'll get the slow branches if
1201 // the program ever changes phase and starts seeing nulls here.)
1202 //
1203 // We do not inspect for a null constant, since a node may
1204 // optimize to 'null' later on.
1205 //
1206 // Null checks, and other tests which expect inequality,
1207 // show btest == BoolTest::eq along the non-taken branch.
1208 // On the other hand, type tests, must-be-null tests,
1209 // and other tests which expect pointer equality,
1210 // show btest == BoolTest::ne along the non-taken branch.
1211 // We prune both types of branches if they look unused.
1212 repush_if_args();
1213 // We need to mark this branch as taken so that if we recompile we will
1214 // see that it is possible. In the tiered system the interpreter doesn't
1215 // do profiling and by the time we get to the lower tier from the interpreter
1216 // the path may be cold again. Make sure it doesn't look untaken
1217 if (is_fallthrough) {
1218 profile_not_taken_branch(!ProfileInterpreter);
1219 } else {
1220 profile_taken_branch(iter().get_dest(), !ProfileInterpreter);
1221 }
1222 uncommon_trap(Deoptimization::Reason_unreached,
1223 Deoptimization::Action_reinterpret,
1224 NULL,
1225 (is_fallthrough ? "taken always" : "taken never"));
1226 return;
1227 }
1229 Node* val = c->in(1);
1230 Node* con = c->in(2);
1231 const Type* tcon = _gvn.type(con);
1232 const Type* tval = _gvn.type(val);
1233 bool have_con = tcon->singleton();
1234 if (tval->singleton()) {
1235 if (!have_con) {
1236 // Swap, so constant is in con.
1237 con = val;
1238 tcon = tval;
1239 val = c->in(2);
1240 tval = _gvn.type(val);
1241 btest = BoolTest(btest).commute();
1242 have_con = true;
1243 } else {
1244 // Do we have two constants? Then leave well enough alone.
1245 have_con = false;
1246 }
1247 }
1248 if (!have_con) // remaining adjustments need a con
1249 return;
1252 int val_in_map = map()->find_edge(val);
1253 if (val_in_map < 0) return; // replace_in_map would be useless
1254 {
1255 JVMState* jvms = this->jvms();
1256 if (!(jvms->is_loc(val_in_map) ||
1257 jvms->is_stk(val_in_map)))
1258 return; // again, it would be useless
1259 }
1261 // Check for a comparison to a constant, and "know" that the compared
1262 // value is constrained on this path.
1263 assert(tcon->singleton(), "");
1264 ConstraintCastNode* ccast = NULL;
1265 Node* cast = NULL;
1267 switch (btest) {
1268 case BoolTest::eq: // Constant test?
1269 {
1270 const Type* tboth = tcon->join(tval);
1271 if (tboth == tval) break; // Nothing to gain.
1272 if (tcon->isa_int()) {
1273 ccast = new (C, 2) CastIINode(val, tboth);
1274 } else if (tcon == TypePtr::NULL_PTR) {
1275 // Cast to null, but keep the pointer identity temporarily live.
1276 ccast = new (C, 2) CastPPNode(val, tboth);
1277 } else {
1278 const TypeF* tf = tcon->isa_float_constant();
1279 const TypeD* td = tcon->isa_double_constant();
1280 // Exclude tests vs float/double 0 as these could be
1281 // either +0 or -0. Just because you are equal to +0
1282 // doesn't mean you ARE +0!
1283 if ((!tf || tf->_f != 0.0) &&
1284 (!td || td->_d != 0.0))
1285 cast = con; // Replace non-constant val by con.
1286 }
1287 }
1288 break;
1290 case BoolTest::ne:
1291 if (tcon == TypePtr::NULL_PTR) {
1292 cast = cast_not_null(val, false);
1293 }
1294 break;
1296 default:
1297 // (At this point we could record int range types with CastII.)
1298 break;
1299 }
1301 if (ccast != NULL) {
1302 const Type* tcc = ccast->as_Type()->type();
1303 assert(tcc != tval && tcc->higher_equal(tval), "must improve");
1304 // Delay transform() call to allow recovery of pre-cast value
1305 // at the control merge.
1306 ccast->set_req(0, control());
1307 _gvn.set_type_bottom(ccast);
1308 record_for_igvn(ccast);
1309 cast = ccast;
1310 }
1312 if (cast != NULL) { // Here's the payoff.
1313 replace_in_map(val, cast);
1314 }
1315 }
1318 //------------------------------do_one_bytecode--------------------------------
1319 // Parse this bytecode, and alter the Parsers JVM->Node mapping
1320 void Parse::do_one_bytecode() {
1321 Node *a, *b, *c, *d; // Handy temps
1322 BoolTest::mask btest;
1323 int i;
1325 assert(!has_exceptions(), "bytecode entry state must be clear of throws");
1327 if (C->check_node_count(NodeLimitFudgeFactor * 5,
1328 "out of nodes parsing method")) {
1329 return;
1330 }
1332 #ifdef ASSERT
1333 // for setting breakpoints
1334 if (TraceOptoParse) {
1335 tty->print(" @");
1336 dump_bci(bci());
1337 }
1338 #endif
1340 switch (bc()) {
1341 case Bytecodes::_nop:
1342 // do nothing
1343 break;
1344 case Bytecodes::_lconst_0:
1345 push_pair(longcon(0));
1346 break;
1348 case Bytecodes::_lconst_1:
1349 push_pair(longcon(1));
1350 break;
1352 case Bytecodes::_fconst_0:
1353 push(zerocon(T_FLOAT));
1354 break;
1356 case Bytecodes::_fconst_1:
1357 push(makecon(TypeF::ONE));
1358 break;
1360 case Bytecodes::_fconst_2:
1361 push(makecon(TypeF::make(2.0f)));
1362 break;
1364 case Bytecodes::_dconst_0:
1365 push_pair(zerocon(T_DOUBLE));
1366 break;
1368 case Bytecodes::_dconst_1:
1369 push_pair(makecon(TypeD::ONE));
1370 break;
1372 case Bytecodes::_iconst_m1:push(intcon(-1)); break;
1373 case Bytecodes::_iconst_0: push(intcon( 0)); break;
1374 case Bytecodes::_iconst_1: push(intcon( 1)); break;
1375 case Bytecodes::_iconst_2: push(intcon( 2)); break;
1376 case Bytecodes::_iconst_3: push(intcon( 3)); break;
1377 case Bytecodes::_iconst_4: push(intcon( 4)); break;
1378 case Bytecodes::_iconst_5: push(intcon( 5)); break;
1379 case Bytecodes::_bipush: push(intcon(iter().get_constant_u1())); break;
1380 case Bytecodes::_sipush: push(intcon(iter().get_constant_u2())); break;
1381 case Bytecodes::_aconst_null: push(null()); break;
1382 case Bytecodes::_ldc:
1383 case Bytecodes::_ldc_w:
1384 case Bytecodes::_ldc2_w:
1385 // If the constant is unresolved, run this BC once in the interpreter.
1386 {
1387 ciConstant constant = iter().get_constant();
1388 if (constant.basic_type() == T_OBJECT &&
1389 !constant.as_object()->is_loaded()) {
1390 int index = iter().get_constant_pool_index();
1391 constantTag tag = iter().get_constant_pool_tag(index);
1392 uncommon_trap(Deoptimization::make_trap_request
1393 (Deoptimization::Reason_unloaded,
1394 Deoptimization::Action_reinterpret,
1395 index),
1396 NULL, tag.internal_name());
1397 break;
1398 }
1399 assert(constant.basic_type() != T_OBJECT || !constant.as_object()->is_klass(),
1400 "must be java_mirror of klass");
1401 bool pushed = push_constant(constant, true);
1402 guarantee(pushed, "must be possible to push this constant");
1403 }
1405 break;
1407 case Bytecodes::_aload_0:
1408 push( local(0) );
1409 break;
1410 case Bytecodes::_aload_1:
1411 push( local(1) );
1412 break;
1413 case Bytecodes::_aload_2:
1414 push( local(2) );
1415 break;
1416 case Bytecodes::_aload_3:
1417 push( local(3) );
1418 break;
1419 case Bytecodes::_aload:
1420 push( local(iter().get_index()) );
1421 break;
1423 case Bytecodes::_fload_0:
1424 case Bytecodes::_iload_0:
1425 push( local(0) );
1426 break;
1427 case Bytecodes::_fload_1:
1428 case Bytecodes::_iload_1:
1429 push( local(1) );
1430 break;
1431 case Bytecodes::_fload_2:
1432 case Bytecodes::_iload_2:
1433 push( local(2) );
1434 break;
1435 case Bytecodes::_fload_3:
1436 case Bytecodes::_iload_3:
1437 push( local(3) );
1438 break;
1439 case Bytecodes::_fload:
1440 case Bytecodes::_iload:
1441 push( local(iter().get_index()) );
1442 break;
1443 case Bytecodes::_lload_0:
1444 push_pair_local( 0 );
1445 break;
1446 case Bytecodes::_lload_1:
1447 push_pair_local( 1 );
1448 break;
1449 case Bytecodes::_lload_2:
1450 push_pair_local( 2 );
1451 break;
1452 case Bytecodes::_lload_3:
1453 push_pair_local( 3 );
1454 break;
1455 case Bytecodes::_lload:
1456 push_pair_local( iter().get_index() );
1457 break;
1459 case Bytecodes::_dload_0:
1460 push_pair_local(0);
1461 break;
1462 case Bytecodes::_dload_1:
1463 push_pair_local(1);
1464 break;
1465 case Bytecodes::_dload_2:
1466 push_pair_local(2);
1467 break;
1468 case Bytecodes::_dload_3:
1469 push_pair_local(3);
1470 break;
1471 case Bytecodes::_dload:
1472 push_pair_local(iter().get_index());
1473 break;
1474 case Bytecodes::_fstore_0:
1475 case Bytecodes::_istore_0:
1476 case Bytecodes::_astore_0:
1477 set_local( 0, pop() );
1478 break;
1479 case Bytecodes::_fstore_1:
1480 case Bytecodes::_istore_1:
1481 case Bytecodes::_astore_1:
1482 set_local( 1, pop() );
1483 break;
1484 case Bytecodes::_fstore_2:
1485 case Bytecodes::_istore_2:
1486 case Bytecodes::_astore_2:
1487 set_local( 2, pop() );
1488 break;
1489 case Bytecodes::_fstore_3:
1490 case Bytecodes::_istore_3:
1491 case Bytecodes::_astore_3:
1492 set_local( 3, pop() );
1493 break;
1494 case Bytecodes::_fstore:
1495 case Bytecodes::_istore:
1496 case Bytecodes::_astore:
1497 set_local( iter().get_index(), pop() );
1498 break;
1499 // long stores
1500 case Bytecodes::_lstore_0:
1501 set_pair_local( 0, pop_pair() );
1502 break;
1503 case Bytecodes::_lstore_1:
1504 set_pair_local( 1, pop_pair() );
1505 break;
1506 case Bytecodes::_lstore_2:
1507 set_pair_local( 2, pop_pair() );
1508 break;
1509 case Bytecodes::_lstore_3:
1510 set_pair_local( 3, pop_pair() );
1511 break;
1512 case Bytecodes::_lstore:
1513 set_pair_local( iter().get_index(), pop_pair() );
1514 break;
1516 // double stores
1517 case Bytecodes::_dstore_0:
1518 set_pair_local( 0, dstore_rounding(pop_pair()) );
1519 break;
1520 case Bytecodes::_dstore_1:
1521 set_pair_local( 1, dstore_rounding(pop_pair()) );
1522 break;
1523 case Bytecodes::_dstore_2:
1524 set_pair_local( 2, dstore_rounding(pop_pair()) );
1525 break;
1526 case Bytecodes::_dstore_3:
1527 set_pair_local( 3, dstore_rounding(pop_pair()) );
1528 break;
1529 case Bytecodes::_dstore:
1530 set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) );
1531 break;
1533 case Bytecodes::_pop: _sp -= 1; break;
1534 case Bytecodes::_pop2: _sp -= 2; break;
1535 case Bytecodes::_swap:
1536 a = pop();
1537 b = pop();
1538 push(a);
1539 push(b);
1540 break;
1541 case Bytecodes::_dup:
1542 a = pop();
1543 push(a);
1544 push(a);
1545 break;
1546 case Bytecodes::_dup_x1:
1547 a = pop();
1548 b = pop();
1549 push( a );
1550 push( b );
1551 push( a );
1552 break;
1553 case Bytecodes::_dup_x2:
1554 a = pop();
1555 b = pop();
1556 c = pop();
1557 push( a );
1558 push( c );
1559 push( b );
1560 push( a );
1561 break;
1562 case Bytecodes::_dup2:
1563 a = pop();
1564 b = pop();
1565 push( b );
1566 push( a );
1567 push( b );
1568 push( a );
1569 break;
1571 case Bytecodes::_dup2_x1:
1572 // before: .. c, b, a
1573 // after: .. b, a, c, b, a
1574 // not tested
1575 a = pop();
1576 b = pop();
1577 c = pop();
1578 push( b );
1579 push( a );
1580 push( c );
1581 push( b );
1582 push( a );
1583 break;
1584 case Bytecodes::_dup2_x2:
1585 // before: .. d, c, b, a
1586 // after: .. b, a, d, c, b, a
1587 // not tested
1588 a = pop();
1589 b = pop();
1590 c = pop();
1591 d = pop();
1592 push( b );
1593 push( a );
1594 push( d );
1595 push( c );
1596 push( b );
1597 push( a );
1598 break;
1600 case Bytecodes::_arraylength: {
1601 // Must do null-check with value on expression stack
1602 Node *ary = do_null_check(peek(), T_ARRAY);
1603 // Compile-time detect of null-exception?
1604 if (stopped()) return;
1605 a = pop();
1606 push(load_array_length(a));
1607 break;
1608 }
1610 case Bytecodes::_baload: array_load(T_BYTE); break;
1611 case Bytecodes::_caload: array_load(T_CHAR); break;
1612 case Bytecodes::_iaload: array_load(T_INT); break;
1613 case Bytecodes::_saload: array_load(T_SHORT); break;
1614 case Bytecodes::_faload: array_load(T_FLOAT); break;
1615 case Bytecodes::_aaload: array_load(T_OBJECT); break;
1616 case Bytecodes::_laload: {
1617 a = array_addressing(T_LONG, 0);
1618 if (stopped()) return; // guaranteed null or range check
1619 _sp -= 2; // Pop array and index
1620 push_pair( make_load(control(), a, TypeLong::LONG, T_LONG, TypeAryPtr::LONGS));
1621 break;
1622 }
1623 case Bytecodes::_daload: {
1624 a = array_addressing(T_DOUBLE, 0);
1625 if (stopped()) return; // guaranteed null or range check
1626 _sp -= 2; // Pop array and index
1627 push_pair( make_load(control(), a, Type::DOUBLE, T_DOUBLE, TypeAryPtr::DOUBLES));
1628 break;
1629 }
1630 case Bytecodes::_bastore: array_store(T_BYTE); break;
1631 case Bytecodes::_castore: array_store(T_CHAR); break;
1632 case Bytecodes::_iastore: array_store(T_INT); break;
1633 case Bytecodes::_sastore: array_store(T_SHORT); break;
1634 case Bytecodes::_fastore: array_store(T_FLOAT); break;
1635 case Bytecodes::_aastore: {
1636 d = array_addressing(T_OBJECT, 1);
1637 if (stopped()) return; // guaranteed null or range check
1638 array_store_check();
1639 c = pop(); // Oop to store
1640 b = pop(); // index (already used)
1641 a = pop(); // the array itself
1642 const TypeOopPtr* elemtype = _gvn.type(a)->is_aryptr()->elem()->make_oopptr();
1643 const TypeAryPtr* adr_type = TypeAryPtr::OOPS;
1644 Node* store = store_oop_to_array(control(), a, d, adr_type, c, elemtype, T_OBJECT);
1645 break;
1646 }
1647 case Bytecodes::_lastore: {
1648 a = array_addressing(T_LONG, 2);
1649 if (stopped()) return; // guaranteed null or range check
1650 c = pop_pair();
1651 _sp -= 2; // Pop array and index
1652 store_to_memory(control(), a, c, T_LONG, TypeAryPtr::LONGS);
1653 break;
1654 }
1655 case Bytecodes::_dastore: {
1656 a = array_addressing(T_DOUBLE, 2);
1657 if (stopped()) return; // guaranteed null or range check
1658 c = pop_pair();
1659 _sp -= 2; // Pop array and index
1660 c = dstore_rounding(c);
1661 store_to_memory(control(), a, c, T_DOUBLE, TypeAryPtr::DOUBLES);
1662 break;
1663 }
1664 case Bytecodes::_getfield:
1665 do_getfield();
1666 break;
1668 case Bytecodes::_getstatic:
1669 do_getstatic();
1670 break;
1672 case Bytecodes::_putfield:
1673 do_putfield();
1674 break;
1676 case Bytecodes::_putstatic:
1677 do_putstatic();
1678 break;
1680 case Bytecodes::_irem:
1681 do_irem();
1682 break;
1683 case Bytecodes::_idiv:
1684 // Must keep both values on the expression-stack during null-check
1685 do_null_check(peek(), T_INT);
1686 // Compile-time detect of null-exception?
1687 if (stopped()) return;
1688 b = pop();
1689 a = pop();
1690 push( _gvn.transform( new (C, 3) DivINode(control(),a,b) ) );
1691 break;
1692 case Bytecodes::_imul:
1693 b = pop(); a = pop();
1694 push( _gvn.transform( new (C, 3) MulINode(a,b) ) );
1695 break;
1696 case Bytecodes::_iadd:
1697 b = pop(); a = pop();
1698 push( _gvn.transform( new (C, 3) AddINode(a,b) ) );
1699 break;
1700 case Bytecodes::_ineg:
1701 a = pop();
1702 push( _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),a)) );
1703 break;
1704 case Bytecodes::_isub:
1705 b = pop(); a = pop();
1706 push( _gvn.transform( new (C, 3) SubINode(a,b) ) );
1707 break;
1708 case Bytecodes::_iand:
1709 b = pop(); a = pop();
1710 push( _gvn.transform( new (C, 3) AndINode(a,b) ) );
1711 break;
1712 case Bytecodes::_ior:
1713 b = pop(); a = pop();
1714 push( _gvn.transform( new (C, 3) OrINode(a,b) ) );
1715 break;
1716 case Bytecodes::_ixor:
1717 b = pop(); a = pop();
1718 push( _gvn.transform( new (C, 3) XorINode(a,b) ) );
1719 break;
1720 case Bytecodes::_ishl:
1721 b = pop(); a = pop();
1722 push( _gvn.transform( new (C, 3) LShiftINode(a,b) ) );
1723 break;
1724 case Bytecodes::_ishr:
1725 b = pop(); a = pop();
1726 push( _gvn.transform( new (C, 3) RShiftINode(a,b) ) );
1727 break;
1728 case Bytecodes::_iushr:
1729 b = pop(); a = pop();
1730 push( _gvn.transform( new (C, 3) URShiftINode(a,b) ) );
1731 break;
1733 case Bytecodes::_fneg:
1734 a = pop();
1735 b = _gvn.transform(new (C, 2) NegFNode (a));
1736 push(b);
1737 break;
1739 case Bytecodes::_fsub:
1740 b = pop();
1741 a = pop();
1742 c = _gvn.transform( new (C, 3) SubFNode(a,b) );
1743 d = precision_rounding(c);
1744 push( d );
1745 break;
1747 case Bytecodes::_fadd:
1748 b = pop();
1749 a = pop();
1750 c = _gvn.transform( new (C, 3) AddFNode(a,b) );
1751 d = precision_rounding(c);
1752 push( d );
1753 break;
1755 case Bytecodes::_fmul:
1756 b = pop();
1757 a = pop();
1758 c = _gvn.transform( new (C, 3) MulFNode(a,b) );
1759 d = precision_rounding(c);
1760 push( d );
1761 break;
1763 case Bytecodes::_fdiv:
1764 b = pop();
1765 a = pop();
1766 c = _gvn.transform( new (C, 3) DivFNode(0,a,b) );
1767 d = precision_rounding(c);
1768 push( d );
1769 break;
1771 case Bytecodes::_frem:
1772 if (Matcher::has_match_rule(Op_ModF)) {
1773 // Generate a ModF node.
1774 b = pop();
1775 a = pop();
1776 c = _gvn.transform( new (C, 3) ModFNode(0,a,b) );
1777 d = precision_rounding(c);
1778 push( d );
1779 }
1780 else {
1781 // Generate a call.
1782 modf();
1783 }
1784 break;
1786 case Bytecodes::_fcmpl:
1787 b = pop();
1788 a = pop();
1789 c = _gvn.transform( new (C, 3) CmpF3Node( a, b));
1790 push(c);
1791 break;
1792 case Bytecodes::_fcmpg:
1793 b = pop();
1794 a = pop();
1796 // Same as fcmpl but need to flip the unordered case. Swap the inputs,
1797 // which negates the result sign except for unordered. Flip the unordered
1798 // as well by using CmpF3 which implements unordered-lesser instead of
1799 // unordered-greater semantics. Finally, commute the result bits. Result
1800 // is same as using a CmpF3Greater except we did it with CmpF3 alone.
1801 c = _gvn.transform( new (C, 3) CmpF3Node( b, a));
1802 c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) );
1803 push(c);
1804 break;
1806 case Bytecodes::_f2i:
1807 a = pop();
1808 push(_gvn.transform(new (C, 2) ConvF2INode(a)));
1809 break;
1811 case Bytecodes::_d2i:
1812 a = pop_pair();
1813 b = _gvn.transform(new (C, 2) ConvD2INode(a));
1814 push( b );
1815 break;
1817 case Bytecodes::_f2d:
1818 a = pop();
1819 b = _gvn.transform( new (C, 2) ConvF2DNode(a));
1820 push_pair( b );
1821 break;
1823 case Bytecodes::_d2f:
1824 a = pop_pair();
1825 b = _gvn.transform( new (C, 2) ConvD2FNode(a));
1826 // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed)
1827 //b = _gvn.transform(new (C, 2) RoundFloatNode(0, b) );
1828 push( b );
1829 break;
1831 case Bytecodes::_l2f:
1832 if (Matcher::convL2FSupported()) {
1833 a = pop_pair();
1834 b = _gvn.transform( new (C, 2) ConvL2FNode(a));
1835 // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits.
1836 // Rather than storing the result into an FP register then pushing
1837 // out to memory to round, the machine instruction that implements
1838 // ConvL2D is responsible for rounding.
1839 // c = precision_rounding(b);
1840 c = _gvn.transform(b);
1841 push(c);
1842 } else {
1843 l2f();
1844 }
1845 break;
1847 case Bytecodes::_l2d:
1848 a = pop_pair();
1849 b = _gvn.transform( new (C, 2) ConvL2DNode(a));
1850 // For i486.ad, rounding is always necessary (see _l2f above).
1851 // c = dprecision_rounding(b);
1852 c = _gvn.transform(b);
1853 push_pair(c);
1854 break;
1856 case Bytecodes::_f2l:
1857 a = pop();
1858 b = _gvn.transform( new (C, 2) ConvF2LNode(a));
1859 push_pair(b);
1860 break;
1862 case Bytecodes::_d2l:
1863 a = pop_pair();
1864 b = _gvn.transform( new (C, 2) ConvD2LNode(a));
1865 push_pair(b);
1866 break;
1868 case Bytecodes::_dsub:
1869 b = pop_pair();
1870 a = pop_pair();
1871 c = _gvn.transform( new (C, 3) SubDNode(a,b) );
1872 d = dprecision_rounding(c);
1873 push_pair( d );
1874 break;
1876 case Bytecodes::_dadd:
1877 b = pop_pair();
1878 a = pop_pair();
1879 c = _gvn.transform( new (C, 3) AddDNode(a,b) );
1880 d = dprecision_rounding(c);
1881 push_pair( d );
1882 break;
1884 case Bytecodes::_dmul:
1885 b = pop_pair();
1886 a = pop_pair();
1887 c = _gvn.transform( new (C, 3) MulDNode(a,b) );
1888 d = dprecision_rounding(c);
1889 push_pair( d );
1890 break;
1892 case Bytecodes::_ddiv:
1893 b = pop_pair();
1894 a = pop_pair();
1895 c = _gvn.transform( new (C, 3) DivDNode(0,a,b) );
1896 d = dprecision_rounding(c);
1897 push_pair( d );
1898 break;
1900 case Bytecodes::_dneg:
1901 a = pop_pair();
1902 b = _gvn.transform(new (C, 2) NegDNode (a));
1903 push_pair(b);
1904 break;
1906 case Bytecodes::_drem:
1907 if (Matcher::has_match_rule(Op_ModD)) {
1908 // Generate a ModD node.
1909 b = pop_pair();
1910 a = pop_pair();
1911 // a % b
1913 c = _gvn.transform( new (C, 3) ModDNode(0,a,b) );
1914 d = dprecision_rounding(c);
1915 push_pair( d );
1916 }
1917 else {
1918 // Generate a call.
1919 modd();
1920 }
1921 break;
1923 case Bytecodes::_dcmpl:
1924 b = pop_pair();
1925 a = pop_pair();
1926 c = _gvn.transform( new (C, 3) CmpD3Node( a, b));
1927 push(c);
1928 break;
1930 case Bytecodes::_dcmpg:
1931 b = pop_pair();
1932 a = pop_pair();
1933 // Same as dcmpl but need to flip the unordered case.
1934 // Commute the inputs, which negates the result sign except for unordered.
1935 // Flip the unordered as well by using CmpD3 which implements
1936 // unordered-lesser instead of unordered-greater semantics.
1937 // Finally, negate the result bits. Result is same as using a
1938 // CmpD3Greater except we did it with CmpD3 alone.
1939 c = _gvn.transform( new (C, 3) CmpD3Node( b, a));
1940 c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) );
1941 push(c);
1942 break;
1945 // Note for longs -> lo word is on TOS, hi word is on TOS - 1
1946 case Bytecodes::_land:
1947 b = pop_pair();
1948 a = pop_pair();
1949 c = _gvn.transform( new (C, 3) AndLNode(a,b) );
1950 push_pair(c);
1951 break;
1952 case Bytecodes::_lor:
1953 b = pop_pair();
1954 a = pop_pair();
1955 c = _gvn.transform( new (C, 3) OrLNode(a,b) );
1956 push_pair(c);
1957 break;
1958 case Bytecodes::_lxor:
1959 b = pop_pair();
1960 a = pop_pair();
1961 c = _gvn.transform( new (C, 3) XorLNode(a,b) );
1962 push_pair(c);
1963 break;
1965 case Bytecodes::_lshl:
1966 b = pop(); // the shift count
1967 a = pop_pair(); // value to be shifted
1968 c = _gvn.transform( new (C, 3) LShiftLNode(a,b) );
1969 push_pair(c);
1970 break;
1971 case Bytecodes::_lshr:
1972 b = pop(); // the shift count
1973 a = pop_pair(); // value to be shifted
1974 c = _gvn.transform( new (C, 3) RShiftLNode(a,b) );
1975 push_pair(c);
1976 break;
1977 case Bytecodes::_lushr:
1978 b = pop(); // the shift count
1979 a = pop_pair(); // value to be shifted
1980 c = _gvn.transform( new (C, 3) URShiftLNode(a,b) );
1981 push_pair(c);
1982 break;
1983 case Bytecodes::_lmul:
1984 b = pop_pair();
1985 a = pop_pair();
1986 c = _gvn.transform( new (C, 3) MulLNode(a,b) );
1987 push_pair(c);
1988 break;
1990 case Bytecodes::_lrem:
1991 // Must keep both values on the expression-stack during null-check
1992 assert(peek(0) == top(), "long word order");
1993 do_null_check(peek(1), T_LONG);
1994 // Compile-time detect of null-exception?
1995 if (stopped()) return;
1996 b = pop_pair();
1997 a = pop_pair();
1998 c = _gvn.transform( new (C, 3) ModLNode(control(),a,b) );
1999 push_pair(c);
2000 break;
2002 case Bytecodes::_ldiv:
2003 // Must keep both values on the expression-stack during null-check
2004 assert(peek(0) == top(), "long word order");
2005 do_null_check(peek(1), T_LONG);
2006 // Compile-time detect of null-exception?
2007 if (stopped()) return;
2008 b = pop_pair();
2009 a = pop_pair();
2010 c = _gvn.transform( new (C, 3) DivLNode(control(),a,b) );
2011 push_pair(c);
2012 break;
2014 case Bytecodes::_ladd:
2015 b = pop_pair();
2016 a = pop_pair();
2017 c = _gvn.transform( new (C, 3) AddLNode(a,b) );
2018 push_pair(c);
2019 break;
2020 case Bytecodes::_lsub:
2021 b = pop_pair();
2022 a = pop_pair();
2023 c = _gvn.transform( new (C, 3) SubLNode(a,b) );
2024 push_pair(c);
2025 break;
2026 case Bytecodes::_lcmp:
2027 // Safepoints are now inserted _before_ branches. The long-compare
2028 // bytecode painfully produces a 3-way value (-1,0,+1) which requires a
2029 // slew of control flow. These are usually followed by a CmpI vs zero and
2030 // a branch; this pattern then optimizes to the obvious long-compare and
2031 // branch. However, if the branch is backwards there's a Safepoint
2032 // inserted. The inserted Safepoint captures the JVM state at the
2033 // pre-branch point, i.e. it captures the 3-way value. Thus if a
2034 // long-compare is used to control a loop the debug info will force
2035 // computation of the 3-way value, even though the generated code uses a
2036 // long-compare and branch. We try to rectify the situation by inserting
2037 // a SafePoint here and have it dominate and kill the safepoint added at a
2038 // following backwards branch. At this point the JVM state merely holds 2
2039 // longs but not the 3-way value.
2040 if( UseLoopSafepoints ) {
2041 switch( iter().next_bc() ) {
2042 case Bytecodes::_ifgt:
2043 case Bytecodes::_iflt:
2044 case Bytecodes::_ifge:
2045 case Bytecodes::_ifle:
2046 case Bytecodes::_ifne:
2047 case Bytecodes::_ifeq:
2048 // If this is a backwards branch in the bytecodes, add Safepoint
2049 maybe_add_safepoint(iter().next_get_dest());
2050 }
2051 }
2052 b = pop_pair();
2053 a = pop_pair();
2054 c = _gvn.transform( new (C, 3) CmpL3Node( a, b ));
2055 push(c);
2056 break;
2058 case Bytecodes::_lneg:
2059 a = pop_pair();
2060 b = _gvn.transform( new (C, 3) SubLNode(longcon(0),a));
2061 push_pair(b);
2062 break;
2063 case Bytecodes::_l2i:
2064 a = pop_pair();
2065 push( _gvn.transform( new (C, 2) ConvL2INode(a)));
2066 break;
2067 case Bytecodes::_i2l:
2068 a = pop();
2069 b = _gvn.transform( new (C, 2) ConvI2LNode(a));
2070 push_pair(b);
2071 break;
2072 case Bytecodes::_i2b:
2073 // Sign extend
2074 a = pop();
2075 a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(24)) );
2076 a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(24)) );
2077 push( a );
2078 break;
2079 case Bytecodes::_i2s:
2080 a = pop();
2081 a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(16)) );
2082 a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(16)) );
2083 push( a );
2084 break;
2085 case Bytecodes::_i2c:
2086 a = pop();
2087 push( _gvn.transform( new (C, 3) AndINode(a,_gvn.intcon(0xFFFF)) ) );
2088 break;
2090 case Bytecodes::_i2f:
2091 a = pop();
2092 b = _gvn.transform( new (C, 2) ConvI2FNode(a) ) ;
2093 c = precision_rounding(b);
2094 push (b);
2095 break;
2097 case Bytecodes::_i2d:
2098 a = pop();
2099 b = _gvn.transform( new (C, 2) ConvI2DNode(a));
2100 push_pair(b);
2101 break;
2103 case Bytecodes::_iinc: // Increment local
2104 i = iter().get_index(); // Get local index
2105 set_local( i, _gvn.transform( new (C, 3) AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2106 break;
2108 // Exit points of synchronized methods must have an unlock node
2109 case Bytecodes::_return:
2110 return_current(NULL);
2111 break;
2113 case Bytecodes::_ireturn:
2114 case Bytecodes::_areturn:
2115 case Bytecodes::_freturn:
2116 return_current(pop());
2117 break;
2118 case Bytecodes::_lreturn:
2119 return_current(pop_pair());
2120 break;
2121 case Bytecodes::_dreturn:
2122 return_current(pop_pair());
2123 break;
2125 case Bytecodes::_athrow:
2126 // null exception oop throws NULL pointer exception
2127 do_null_check(peek(), T_OBJECT);
2128 if (stopped()) return;
2129 // Hook the thrown exception directly to subsequent handlers.
2130 if (BailoutToInterpreterForThrows) {
2131 // Keep method interpreted from now on.
2132 uncommon_trap(Deoptimization::Reason_unhandled,
2133 Deoptimization::Action_make_not_compilable);
2134 return;
2135 }
2136 if (env()->jvmti_can_post_on_exceptions()) {
2137 // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2138 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2139 }
2140 // Here if either can_post_on_exceptions or should_post_on_exceptions is false
2141 add_exception_state(make_exception_state(peek()));
2142 break;
2144 case Bytecodes::_goto: // fall through
2145 case Bytecodes::_goto_w: {
2146 int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest();
2148 // If this is a backwards branch in the bytecodes, add Safepoint
2149 maybe_add_safepoint(target_bci);
2151 // Update method data
2152 profile_taken_branch(target_bci);
2154 // Add loop predicate if it goes to a loop
2155 if (should_add_predicate(target_bci)){
2156 add_predicate();
2157 }
2158 // Merge the current control into the target basic block
2159 merge(target_bci);
2161 // See if we can get some profile data and hand it off to the next block
2162 Block *target_block = block()->successor_for_bci(target_bci);
2163 if (target_block->pred_count() != 1) break;
2164 ciMethodData* methodData = method()->method_data();
2165 if (!methodData->is_mature()) break;
2166 ciProfileData* data = methodData->bci_to_data(bci());
2167 assert( data->is_JumpData(), "" );
2168 int taken = ((ciJumpData*)data)->taken();
2169 taken = method()->scale_count(taken);
2170 target_block->set_count(taken);
2171 break;
2172 }
2174 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
2175 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2176 handle_if_null:
2177 // If this is a backwards branch in the bytecodes, add Safepoint
2178 maybe_add_safepoint(iter().get_dest());
2179 a = null();
2180 b = pop();
2181 c = _gvn.transform( new (C, 3) CmpPNode(b, a) );
2182 do_ifnull(btest, c);
2183 break;
2185 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2186 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2187 handle_if_acmp:
2188 // If this is a backwards branch in the bytecodes, add Safepoint
2189 maybe_add_safepoint(iter().get_dest());
2190 a = pop();
2191 b = pop();
2192 c = _gvn.transform( new (C, 3) CmpPNode(b, a) );
2193 do_if(btest, c);
2194 break;
2196 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2197 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2198 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2199 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2200 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2201 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2202 handle_ifxx:
2203 // If this is a backwards branch in the bytecodes, add Safepoint
2204 maybe_add_safepoint(iter().get_dest());
2205 a = _gvn.intcon(0);
2206 b = pop();
2207 c = _gvn.transform( new (C, 3) CmpINode(b, a) );
2208 do_if(btest, c);
2209 break;
2211 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2212 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2213 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2214 case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp;
2215 case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp;
2216 case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp;
2217 handle_if_icmp:
2218 // If this is a backwards branch in the bytecodes, add Safepoint
2219 maybe_add_safepoint(iter().get_dest());
2220 a = pop();
2221 b = pop();
2222 c = _gvn.transform( new (C, 3) CmpINode( b, a ) );
2223 do_if(btest, c);
2224 break;
2226 case Bytecodes::_tableswitch:
2227 do_tableswitch();
2228 break;
2230 case Bytecodes::_lookupswitch:
2231 do_lookupswitch();
2232 break;
2234 case Bytecodes::_invokestatic:
2235 case Bytecodes::_invokedynamic:
2236 case Bytecodes::_invokespecial:
2237 case Bytecodes::_invokevirtual:
2238 case Bytecodes::_invokeinterface:
2239 do_call();
2240 break;
2241 case Bytecodes::_checkcast:
2242 do_checkcast();
2243 break;
2244 case Bytecodes::_instanceof:
2245 do_instanceof();
2246 break;
2247 case Bytecodes::_anewarray:
2248 do_anewarray();
2249 break;
2250 case Bytecodes::_newarray:
2251 do_newarray((BasicType)iter().get_index());
2252 break;
2253 case Bytecodes::_multianewarray:
2254 do_multianewarray();
2255 break;
2256 case Bytecodes::_new:
2257 do_new();
2258 break;
2260 case Bytecodes::_jsr:
2261 case Bytecodes::_jsr_w:
2262 do_jsr();
2263 break;
2265 case Bytecodes::_ret:
2266 do_ret();
2267 break;
2270 case Bytecodes::_monitorenter:
2271 do_monitor_enter();
2272 break;
2274 case Bytecodes::_monitorexit:
2275 do_monitor_exit();
2276 break;
2278 case Bytecodes::_breakpoint:
2279 // Breakpoint set concurrently to compile
2280 // %%% use an uncommon trap?
2281 C->record_failure("breakpoint in method");
2282 return;
2284 default:
2285 #ifndef PRODUCT
2286 map()->dump(99);
2287 #endif
2288 tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) );
2289 ShouldNotReachHere();
2290 }
2292 #ifndef PRODUCT
2293 IdealGraphPrinter *printer = IdealGraphPrinter::printer();
2294 if(printer) {
2295 char buffer[256];
2296 sprintf(buffer, "Bytecode %d: %s", bci(), Bytecodes::name(bc()));
2297 bool old = printer->traverse_outs();
2298 printer->set_traverse_outs(true);
2299 printer->print_method(C, buffer, 4);
2300 printer->set_traverse_outs(old);
2301 }
2302 #endif
2303 }