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