parse2.cpp revision 1522:136b78722a08
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 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_parse2.cpp.incl"
27
28extern int explicit_null_checks_inserted,
29           explicit_null_checks_elided;
30
31//---------------------------------array_load----------------------------------
32void 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}
41
42
43//--------------------------------array_store----------------------------------
44void 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}
52
53
54//------------------------------array_addressing-------------------------------
55// Pull array and index from the stack.  Compute pointer-to-element.
56Node* 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
59
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();
64
65  const TypeAryPtr* arytype  = _gvn.type(ary)->is_aryptr();
66  const TypeInt*    sizetype = arytype->size();
67  const Type*       elemtype = arytype->elem();
68
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  }
80
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  }
91
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  }
100
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);
113
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();
140
141  Node* ptr = array_element_address(ary, idx, type, sizetype);
142
143  if (result2 != NULL)  *result2 = elemtype;
144
145  assert(ptr != top(), "top should go hand-in-hand with stopped");
146
147  return ptr;
148}
149
150
151// returns IfNode
152IfNode* 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}
158
159// return Region node
160Node* 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}
170
171
172//------------------------------helper for tableswitch-------------------------
173void 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  }
181
182  // False branch
183  Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) );
184  set_control( iffalse );
185}
186
187void 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  }
195
196  // False branch
197  Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff) );
198  set_control( iftrue );
199}
200
201void 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}
206
207
208extern "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}
215
216
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
220
221class 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
227
228public:
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; }
234
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  }
247
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  }
254
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};
266
267
268//-------------------------------do_tableswitch--------------------------------
269void Parse::do_tableswitch() {
270  Node* lookup = pop();
271
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;
277
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  }
289
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");
314
315  // Safepoint in case if backward branch observed
316  if( makes_backward_branch && UseLoopSafepoints )
317    add_safepoint();
318
319  jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
320}
321
322
323//------------------------------do_lookupswitch--------------------------------
324void 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);
329
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  }
340
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  }
350
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");
375
376  // Safepoint in case backward branch observed
377  if( makes_backward_branch && UseLoopSafepoints )
378    add_safepoint();
379
380  jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
381}
382
383//----------------------------create_jump_tables-------------------------------
384bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) {
385  // Are jumptables enabled
386  if (!UseJumpTables)  return false;
387
388  // Are jumptables supported
389  if (!Matcher::has_match_rule(Op_Jump))  return false;
390
391  // Don't make jump table if profiling
392  if (method_data_update())  return false;
393
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".
397
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;
403
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  }
414
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  }
422
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;
426
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;
432
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)) );
436
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  }
446
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));
461
462  // Create the JumpNode
463  Node* jtn = _gvn.transform( new (C, 2) JumpNode(control(), key_val, num_cases) );
464
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}
481
482//----------------------------jump_switch_ranges-------------------------------
483void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) {
484  Block* switch_block = block();
485
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");
490
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);
498
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
509
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  }
524
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
531
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");
538
539    if (create_jump_tables(key_val, lo, hi)) return;
540
541    int nr = hi - lo + 1;
542
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--;
546
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");
550
551    Node *test_val = _gvn.intcon(mid->lo());
552
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());
556
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      }
564
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      }
578
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);
582
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    }
596
597    // in any case, process the lower range
598    jump_switch_ranges(key_val, lo, mid-1, switch_depth+1);
599  }
600
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  }
610
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}
635
636void 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));
644
645  push(res);
646}
647
648void 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));
656
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
661
662  push_pair(res_d);
663}
664
665void 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));
673
674  push(res);
675}
676
677void 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;
682
683  Node* b = pop();
684  Node* a = pop();
685
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}
720
721// Handle jsr and jsr_w bytecode
722void Parse::do_jsr() {
723  assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode");
724
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();
728
729  // Update method data
730  profile_taken_branch(jsr_bci);
731
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);
735
736  // What got pushed?
737  const Type* ret_addr = target->peek();
738  assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
739
740  // Effect on jsr on stack
741  push(_gvn.makecon(ret_addr));
742
743  // Flow to the jsr.
744  if (should_add_predicate(jsr_bci)){
745    add_predicate();
746  }
747  merge(jsr_bci);
748}
749
750// Handle ret bytecode
751void 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}
768
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.
773float Parse::dynamic_branch_prediction(float &cnt) {
774  ResourceMark rm;
775
776  cnt  = COUNT_UNKNOWN;
777
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;
784
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  }
791
792  // scale the counts to be commensurate with invocation counts:
793  taken = method()->scale_count(taken);
794  not_taken = method()->scale_count(not_taken);
795
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  }
803
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;
812
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  }
824
825  assert((cnt > 0.0f) && (prob > 0.0f),
826         "Bad frequency assignment in if");
827
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}
842
843//-----------------------------branch_prediction-------------------------------
844float 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;
850
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
856
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  }
880
881  assert(prob != PROB_UNKNOWN, "must have some guess at this point");
882  return prob;
883}
884
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.
891bool Parse::seems_never_taken(float prob) {
892  return prob < PROB_MIN;
893}
894
895//-------------------------------repush_if_args--------------------------------
896// Push arguments of an "if" bytecode back onto the stack by adjusting _sp.
897inline int Parse::repush_if_args() {
898#ifndef PRODUCT
899  if (PrintOpto && WizardMode) {
900    tty->print("defending against excessive implicit null exceptions on %s @%d in ",
901               Bytecodes::name(iter().cur_bc()), iter().cur_bci());
902    method()->print_name(); tty->cr();
903  }
904#endif
905  int bc_depth = - Bytecodes::depth(iter().cur_bc());
906  assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
907  DEBUG_ONLY(sync_jvms());   // argument(n) requires a synced jvms
908  assert(argument(0) != NULL, "must exist");
909  assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
910  _sp += bc_depth;
911  return bc_depth;
912}
913
914//----------------------------------do_ifnull----------------------------------
915void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
916  int target_bci = iter().get_dest();
917
918  Block* branch_block = successor_for_bci(target_bci);
919  Block* next_block   = successor_for_bci(iter().next_bci());
920
921  float cnt;
922  float prob = branch_prediction(cnt, btest, target_bci);
923  if (prob == PROB_UNKNOWN) {
924    // (An earlier version of do_ifnull omitted this trap for OSR methods.)
925#ifndef PRODUCT
926    if (PrintOpto && Verbose)
927      tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
928#endif
929    repush_if_args(); // to gather stats on loop
930    // We need to mark this branch as taken so that if we recompile we will
931    // see that it is possible. In the tiered system the interpreter doesn't
932    // do profiling and by the time we get to the lower tier from the interpreter
933    // the path may be cold again. Make sure it doesn't look untaken
934    profile_taken_branch(target_bci, !ProfileInterpreter);
935    uncommon_trap(Deoptimization::Reason_unreached,
936                  Deoptimization::Action_reinterpret,
937                  NULL, "cold");
938    if (EliminateAutoBox) {
939      // Mark the successor blocks as parsed
940      branch_block->next_path_num();
941      next_block->next_path_num();
942    }
943    return;
944  }
945
946  explicit_null_checks_inserted++;
947
948  // Generate real control flow
949  Node   *tst = _gvn.transform( new (C, 2) BoolNode( c, btest ) );
950
951  // Sanity check the probability value
952  assert(prob > 0.0f,"Bad probability in Parser");
953 // Need xform to put node in hash table
954  IfNode *iff = create_and_xform_if( control(), tst, prob, cnt );
955  assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
956  // True branch
957  { PreserveJVMState pjvms(this);
958    Node* iftrue  = _gvn.transform( new (C, 1) IfTrueNode (iff) );
959    set_control(iftrue);
960
961    if (stopped()) {            // Path is dead?
962      explicit_null_checks_elided++;
963      if (EliminateAutoBox) {
964        // Mark the successor block as parsed
965        branch_block->next_path_num();
966      }
967    } else {                    // Path is live.
968      // Update method data
969      profile_taken_branch(target_bci);
970      adjust_map_after_if(btest, c, prob, branch_block, next_block);
971      if (!stopped()) {
972        if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop
973          int nargs = repush_if_args(); // set original stack for uncommon_trap
974          add_predicate();
975          _sp -= nargs;
976        }
977        merge(target_bci);
978      }
979    }
980  }
981
982  // False branch
983  Node* iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) );
984  set_control(iffalse);
985
986  if (stopped()) {              // Path is dead?
987    explicit_null_checks_elided++;
988    if (EliminateAutoBox) {
989      // Mark the successor block as parsed
990      next_block->next_path_num();
991    }
992  } else  {                     // Path is live.
993    // Update method data
994    profile_not_taken_branch();
995    adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob,
996                        next_block, branch_block);
997  }
998}
999
1000//------------------------------------do_if------------------------------------
1001void Parse::do_if(BoolTest::mask btest, Node* c) {
1002  int target_bci = iter().get_dest();
1003
1004  Block* branch_block = successor_for_bci(target_bci);
1005  Block* next_block   = successor_for_bci(iter().next_bci());
1006
1007  float cnt;
1008  float prob = branch_prediction(cnt, btest, target_bci);
1009  float untaken_prob = 1.0 - prob;
1010
1011  if (prob == PROB_UNKNOWN) {
1012#ifndef PRODUCT
1013    if (PrintOpto && Verbose)
1014      tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
1015#endif
1016    repush_if_args(); // to gather stats on loop
1017    // We need to mark this branch as taken so that if we recompile we will
1018    // see that it is possible. In the tiered system the interpreter doesn't
1019    // do profiling and by the time we get to the lower tier from the interpreter
1020    // the path may be cold again. Make sure it doesn't look untaken
1021    profile_taken_branch(target_bci, !ProfileInterpreter);
1022    uncommon_trap(Deoptimization::Reason_unreached,
1023                  Deoptimization::Action_reinterpret,
1024                  NULL, "cold");
1025    if (EliminateAutoBox) {
1026      // Mark the successor blocks as parsed
1027      branch_block->next_path_num();
1028      next_block->next_path_num();
1029    }
1030    return;
1031  }
1032
1033  // Sanity check the probability value
1034  assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1035
1036  bool taken_if_true = true;
1037  // Convert BoolTest to canonical form:
1038  if (!BoolTest(btest).is_canonical()) {
1039    btest         = BoolTest(btest).negate();
1040    taken_if_true = false;
1041    // prob is NOT updated here; it remains the probability of the taken
1042    // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1043  }
1044  assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1045
1046  Node* tst0 = new (C, 2) BoolNode(c, btest);
1047  Node* tst = _gvn.transform(tst0);
1048  BoolTest::mask taken_btest   = BoolTest::illegal;
1049  BoolTest::mask untaken_btest = BoolTest::illegal;
1050
1051  if (tst->is_Bool()) {
1052    // Refresh c from the transformed bool node, since it may be
1053    // simpler than the original c.  Also re-canonicalize btest.
1054    // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)).
1055    // That can arise from statements like: if (x instanceof C) ...
1056    if (tst != tst0) {
1057      // Canonicalize one more time since transform can change it.
1058      btest = tst->as_Bool()->_test._test;
1059      if (!BoolTest(btest).is_canonical()) {
1060        // Reverse edges one more time...
1061        tst   = _gvn.transform( tst->as_Bool()->negate(&_gvn) );
1062        btest = tst->as_Bool()->_test._test;
1063        assert(BoolTest(btest).is_canonical(), "sanity");
1064        taken_if_true = !taken_if_true;
1065      }
1066      c = tst->in(1);
1067    }
1068    BoolTest::mask neg_btest = BoolTest(btest).negate();
1069    taken_btest   = taken_if_true ?     btest : neg_btest;
1070    untaken_btest = taken_if_true ? neg_btest :     btest;
1071  }
1072
1073  // Generate real control flow
1074  float true_prob = (taken_if_true ? prob : untaken_prob);
1075  IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1076  assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1077  Node* taken_branch   = new (C, 1) IfTrueNode(iff);
1078  Node* untaken_branch = new (C, 1) IfFalseNode(iff);
1079  if (!taken_if_true) {  // Finish conversion to canonical form
1080    Node* tmp      = taken_branch;
1081    taken_branch   = untaken_branch;
1082    untaken_branch = tmp;
1083  }
1084
1085  // Branch is taken:
1086  { PreserveJVMState pjvms(this);
1087    taken_branch = _gvn.transform(taken_branch);
1088    set_control(taken_branch);
1089
1090    if (stopped()) {
1091      if (EliminateAutoBox) {
1092        // Mark the successor block as parsed
1093        branch_block->next_path_num();
1094      }
1095    } else {
1096      // Update method data
1097      profile_taken_branch(target_bci);
1098      adjust_map_after_if(taken_btest, c, prob, branch_block, next_block);
1099      if (!stopped()) {
1100        if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop
1101          int nargs = repush_if_args(); // set original stack for the uncommon_trap
1102          add_predicate();
1103          _sp -= nargs;
1104        }
1105        merge(target_bci);
1106      }
1107    }
1108  }
1109
1110  untaken_branch = _gvn.transform(untaken_branch);
1111  set_control(untaken_branch);
1112
1113  // Branch not taken.
1114  if (stopped()) {
1115    if (EliminateAutoBox) {
1116      // Mark the successor block as parsed
1117      next_block->next_path_num();
1118    }
1119  } else {
1120    // Update method data
1121    profile_not_taken_branch();
1122    adjust_map_after_if(untaken_btest, c, untaken_prob,
1123                        next_block, branch_block);
1124  }
1125}
1126
1127//----------------------------adjust_map_after_if------------------------------
1128// Adjust the JVM state to reflect the result of taking this path.
1129// Basically, it means inspecting the CmpNode controlling this
1130// branch, seeing how it constrains a tested value, and then
1131// deciding if it's worth our while to encode this constraint
1132// as graph nodes in the current abstract interpretation map.
1133void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob,
1134                                Block* path, Block* other_path) {
1135  if (stopped() || !c->is_Cmp() || btest == BoolTest::illegal)
1136    return;                             // nothing to do
1137
1138  bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1139
1140  int cop = c->Opcode();
1141  if (seems_never_taken(prob) && cop == Op_CmpP && btest == BoolTest::eq) {
1142    // (An earlier version of do_if omitted '&& btest == BoolTest::eq'.)
1143    //
1144    // If this might possibly turn into an implicit null check,
1145    // and the null has never yet been seen, we need to generate
1146    // an uncommon trap, so as to recompile instead of suffering
1147    // with very slow branches.  (We'll get the slow branches if
1148    // the program ever changes phase and starts seeing nulls here.)
1149    //
1150    // The tests we worry about are of the form (p == null).
1151    // We do not simply inspect for a null constant, since a node may
1152    // optimize to 'null' later on.
1153    repush_if_args();
1154    // We need to mark this branch as taken so that if we recompile we will
1155    // see that it is possible. In the tiered system the interpreter doesn't
1156    // do profiling and by the time we get to the lower tier from the interpreter
1157    // the path may be cold again. Make sure it doesn't look untaken
1158    if (is_fallthrough) {
1159      profile_not_taken_branch(!ProfileInterpreter);
1160    } else {
1161      profile_taken_branch(iter().get_dest(), !ProfileInterpreter);
1162    }
1163    uncommon_trap(Deoptimization::Reason_unreached,
1164                  Deoptimization::Action_reinterpret,
1165                  NULL,
1166                  (is_fallthrough ? "taken always" : "taken never"));
1167    return;
1168  }
1169
1170  Node* val = c->in(1);
1171  Node* con = c->in(2);
1172  const Type* tcon = _gvn.type(con);
1173  const Type* tval = _gvn.type(val);
1174  bool have_con = tcon->singleton();
1175  if (tval->singleton()) {
1176    if (!have_con) {
1177      // Swap, so constant is in con.
1178      con  = val;
1179      tcon = tval;
1180      val  = c->in(2);
1181      tval = _gvn.type(val);
1182      btest = BoolTest(btest).commute();
1183      have_con = true;
1184    } else {
1185      // Do we have two constants?  Then leave well enough alone.
1186      have_con = false;
1187    }
1188  }
1189  if (!have_con)                        // remaining adjustments need a con
1190    return;
1191
1192
1193  int val_in_map = map()->find_edge(val);
1194  if (val_in_map < 0)  return;          // replace_in_map would be useless
1195  {
1196    JVMState* jvms = this->jvms();
1197    if (!(jvms->is_loc(val_in_map) ||
1198          jvms->is_stk(val_in_map)))
1199      return;                           // again, it would be useless
1200  }
1201
1202  // Check for a comparison to a constant, and "know" that the compared
1203  // value is constrained on this path.
1204  assert(tcon->singleton(), "");
1205  ConstraintCastNode* ccast = NULL;
1206  Node* cast = NULL;
1207
1208  switch (btest) {
1209  case BoolTest::eq:                    // Constant test?
1210    {
1211      const Type* tboth = tcon->join(tval);
1212      if (tboth == tval)  break;        // Nothing to gain.
1213      if (tcon->isa_int()) {
1214        ccast = new (C, 2) CastIINode(val, tboth);
1215      } else if (tcon == TypePtr::NULL_PTR) {
1216        // Cast to null, but keep the pointer identity temporarily live.
1217        ccast = new (C, 2) CastPPNode(val, tboth);
1218      } else {
1219        const TypeF* tf = tcon->isa_float_constant();
1220        const TypeD* td = tcon->isa_double_constant();
1221        // Exclude tests vs float/double 0 as these could be
1222        // either +0 or -0.  Just because you are equal to +0
1223        // doesn't mean you ARE +0!
1224        if ((!tf || tf->_f != 0.0) &&
1225            (!td || td->_d != 0.0))
1226          cast = con;                   // Replace non-constant val by con.
1227      }
1228    }
1229    break;
1230
1231  case BoolTest::ne:
1232    if (tcon == TypePtr::NULL_PTR) {
1233      cast = cast_not_null(val, false);
1234    }
1235    break;
1236
1237  default:
1238    // (At this point we could record int range types with CastII.)
1239    break;
1240  }
1241
1242  if (ccast != NULL) {
1243    const Type* tcc = ccast->as_Type()->type();
1244    assert(tcc != tval && tcc->higher_equal(tval), "must improve");
1245    // Delay transform() call to allow recovery of pre-cast value
1246    // at the control merge.
1247    ccast->set_req(0, control());
1248    _gvn.set_type_bottom(ccast);
1249    record_for_igvn(ccast);
1250    cast = ccast;
1251  }
1252
1253  if (cast != NULL) {                   // Here's the payoff.
1254    replace_in_map(val, cast);
1255  }
1256}
1257
1258
1259//------------------------------do_one_bytecode--------------------------------
1260// Parse this bytecode, and alter the Parsers JVM->Node mapping
1261void Parse::do_one_bytecode() {
1262  Node *a, *b, *c, *d;          // Handy temps
1263  BoolTest::mask btest;
1264  int i;
1265
1266  assert(!has_exceptions(), "bytecode entry state must be clear of throws");
1267
1268  if (C->check_node_count(NodeLimitFudgeFactor * 5,
1269                          "out of nodes parsing method")) {
1270    return;
1271  }
1272
1273#ifdef ASSERT
1274  // for setting breakpoints
1275  if (TraceOptoParse) {
1276    tty->print(" @");
1277    dump_bci(bci());
1278  }
1279#endif
1280
1281  switch (bc()) {
1282  case Bytecodes::_nop:
1283    // do nothing
1284    break;
1285  case Bytecodes::_lconst_0:
1286    push_pair(longcon(0));
1287    break;
1288
1289  case Bytecodes::_lconst_1:
1290    push_pair(longcon(1));
1291    break;
1292
1293  case Bytecodes::_fconst_0:
1294    push(zerocon(T_FLOAT));
1295    break;
1296
1297  case Bytecodes::_fconst_1:
1298    push(makecon(TypeF::ONE));
1299    break;
1300
1301  case Bytecodes::_fconst_2:
1302    push(makecon(TypeF::make(2.0f)));
1303    break;
1304
1305  case Bytecodes::_dconst_0:
1306    push_pair(zerocon(T_DOUBLE));
1307    break;
1308
1309  case Bytecodes::_dconst_1:
1310    push_pair(makecon(TypeD::ONE));
1311    break;
1312
1313  case Bytecodes::_iconst_m1:push(intcon(-1)); break;
1314  case Bytecodes::_iconst_0: push(intcon( 0)); break;
1315  case Bytecodes::_iconst_1: push(intcon( 1)); break;
1316  case Bytecodes::_iconst_2: push(intcon( 2)); break;
1317  case Bytecodes::_iconst_3: push(intcon( 3)); break;
1318  case Bytecodes::_iconst_4: push(intcon( 4)); break;
1319  case Bytecodes::_iconst_5: push(intcon( 5)); break;
1320  case Bytecodes::_bipush:   push(intcon(iter().get_constant_u1())); break;
1321  case Bytecodes::_sipush:   push(intcon(iter().get_constant_u2())); break;
1322  case Bytecodes::_aconst_null: push(null());  break;
1323  case Bytecodes::_ldc:
1324  case Bytecodes::_ldc_w:
1325  case Bytecodes::_ldc2_w:
1326    // If the constant is unresolved, run this BC once in the interpreter.
1327    {
1328      ciConstant constant = iter().get_constant();
1329      if (constant.basic_type() == T_OBJECT &&
1330          !constant.as_object()->is_loaded()) {
1331        int index = iter().get_constant_pool_index();
1332        constantTag tag = iter().get_constant_pool_tag(index);
1333        uncommon_trap(Deoptimization::make_trap_request
1334                      (Deoptimization::Reason_unloaded,
1335                       Deoptimization::Action_reinterpret,
1336                       index),
1337                      NULL, tag.internal_name());
1338        break;
1339      }
1340      assert(constant.basic_type() != T_OBJECT || !constant.as_object()->is_klass(),
1341             "must be java_mirror of klass");
1342      bool pushed = push_constant(constant, true);
1343      guarantee(pushed, "must be possible to push this constant");
1344    }
1345
1346    break;
1347
1348  case Bytecodes::_aload_0:
1349    push( local(0) );
1350    break;
1351  case Bytecodes::_aload_1:
1352    push( local(1) );
1353    break;
1354  case Bytecodes::_aload_2:
1355    push( local(2) );
1356    break;
1357  case Bytecodes::_aload_3:
1358    push( local(3) );
1359    break;
1360  case Bytecodes::_aload:
1361    push( local(iter().get_index()) );
1362    break;
1363
1364  case Bytecodes::_fload_0:
1365  case Bytecodes::_iload_0:
1366    push( local(0) );
1367    break;
1368  case Bytecodes::_fload_1:
1369  case Bytecodes::_iload_1:
1370    push( local(1) );
1371    break;
1372  case Bytecodes::_fload_2:
1373  case Bytecodes::_iload_2:
1374    push( local(2) );
1375    break;
1376  case Bytecodes::_fload_3:
1377  case Bytecodes::_iload_3:
1378    push( local(3) );
1379    break;
1380  case Bytecodes::_fload:
1381  case Bytecodes::_iload:
1382    push( local(iter().get_index()) );
1383    break;
1384  case Bytecodes::_lload_0:
1385    push_pair_local( 0 );
1386    break;
1387  case Bytecodes::_lload_1:
1388    push_pair_local( 1 );
1389    break;
1390  case Bytecodes::_lload_2:
1391    push_pair_local( 2 );
1392    break;
1393  case Bytecodes::_lload_3:
1394    push_pair_local( 3 );
1395    break;
1396  case Bytecodes::_lload:
1397    push_pair_local( iter().get_index() );
1398    break;
1399
1400  case Bytecodes::_dload_0:
1401    push_pair_local(0);
1402    break;
1403  case Bytecodes::_dload_1:
1404    push_pair_local(1);
1405    break;
1406  case Bytecodes::_dload_2:
1407    push_pair_local(2);
1408    break;
1409  case Bytecodes::_dload_3:
1410    push_pair_local(3);
1411    break;
1412  case Bytecodes::_dload:
1413    push_pair_local(iter().get_index());
1414    break;
1415  case Bytecodes::_fstore_0:
1416  case Bytecodes::_istore_0:
1417  case Bytecodes::_astore_0:
1418    set_local( 0, pop() );
1419    break;
1420  case Bytecodes::_fstore_1:
1421  case Bytecodes::_istore_1:
1422  case Bytecodes::_astore_1:
1423    set_local( 1, pop() );
1424    break;
1425  case Bytecodes::_fstore_2:
1426  case Bytecodes::_istore_2:
1427  case Bytecodes::_astore_2:
1428    set_local( 2, pop() );
1429    break;
1430  case Bytecodes::_fstore_3:
1431  case Bytecodes::_istore_3:
1432  case Bytecodes::_astore_3:
1433    set_local( 3, pop() );
1434    break;
1435  case Bytecodes::_fstore:
1436  case Bytecodes::_istore:
1437  case Bytecodes::_astore:
1438    set_local( iter().get_index(), pop() );
1439    break;
1440  // long stores
1441  case Bytecodes::_lstore_0:
1442    set_pair_local( 0, pop_pair() );
1443    break;
1444  case Bytecodes::_lstore_1:
1445    set_pair_local( 1, pop_pair() );
1446    break;
1447  case Bytecodes::_lstore_2:
1448    set_pair_local( 2, pop_pair() );
1449    break;
1450  case Bytecodes::_lstore_3:
1451    set_pair_local( 3, pop_pair() );
1452    break;
1453  case Bytecodes::_lstore:
1454    set_pair_local( iter().get_index(), pop_pair() );
1455    break;
1456
1457  // double stores
1458  case Bytecodes::_dstore_0:
1459    set_pair_local( 0, dstore_rounding(pop_pair()) );
1460    break;
1461  case Bytecodes::_dstore_1:
1462    set_pair_local( 1, dstore_rounding(pop_pair()) );
1463    break;
1464  case Bytecodes::_dstore_2:
1465    set_pair_local( 2, dstore_rounding(pop_pair()) );
1466    break;
1467  case Bytecodes::_dstore_3:
1468    set_pair_local( 3, dstore_rounding(pop_pair()) );
1469    break;
1470  case Bytecodes::_dstore:
1471    set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) );
1472    break;
1473
1474  case Bytecodes::_pop:  _sp -= 1;   break;
1475  case Bytecodes::_pop2: _sp -= 2;   break;
1476  case Bytecodes::_swap:
1477    a = pop();
1478    b = pop();
1479    push(a);
1480    push(b);
1481    break;
1482  case Bytecodes::_dup:
1483    a = pop();
1484    push(a);
1485    push(a);
1486    break;
1487  case Bytecodes::_dup_x1:
1488    a = pop();
1489    b = pop();
1490    push( a );
1491    push( b );
1492    push( a );
1493    break;
1494  case Bytecodes::_dup_x2:
1495    a = pop();
1496    b = pop();
1497    c = pop();
1498    push( a );
1499    push( c );
1500    push( b );
1501    push( a );
1502    break;
1503  case Bytecodes::_dup2:
1504    a = pop();
1505    b = pop();
1506    push( b );
1507    push( a );
1508    push( b );
1509    push( a );
1510    break;
1511
1512  case Bytecodes::_dup2_x1:
1513    // before: .. c, b, a
1514    // after:  .. b, a, c, b, a
1515    // not tested
1516    a = pop();
1517    b = pop();
1518    c = pop();
1519    push( b );
1520    push( a );
1521    push( c );
1522    push( b );
1523    push( a );
1524    break;
1525  case Bytecodes::_dup2_x2:
1526    // before: .. d, c, b, a
1527    // after:  .. b, a, d, c, b, a
1528    // not tested
1529    a = pop();
1530    b = pop();
1531    c = pop();
1532    d = pop();
1533    push( b );
1534    push( a );
1535    push( d );
1536    push( c );
1537    push( b );
1538    push( a );
1539    break;
1540
1541  case Bytecodes::_arraylength: {
1542    // Must do null-check with value on expression stack
1543    Node *ary = do_null_check(peek(), T_ARRAY);
1544    // Compile-time detect of null-exception?
1545    if (stopped())  return;
1546    a = pop();
1547    push(load_array_length(a));
1548    break;
1549  }
1550
1551  case Bytecodes::_baload: array_load(T_BYTE);   break;
1552  case Bytecodes::_caload: array_load(T_CHAR);   break;
1553  case Bytecodes::_iaload: array_load(T_INT);    break;
1554  case Bytecodes::_saload: array_load(T_SHORT);  break;
1555  case Bytecodes::_faload: array_load(T_FLOAT);  break;
1556  case Bytecodes::_aaload: array_load(T_OBJECT); break;
1557  case Bytecodes::_laload: {
1558    a = array_addressing(T_LONG, 0);
1559    if (stopped())  return;     // guaranteed null or range check
1560    _sp -= 2;                   // Pop array and index
1561    push_pair( make_load(control(), a, TypeLong::LONG, T_LONG, TypeAryPtr::LONGS));
1562    break;
1563  }
1564  case Bytecodes::_daload: {
1565    a = array_addressing(T_DOUBLE, 0);
1566    if (stopped())  return;     // guaranteed null or range check
1567    _sp -= 2;                   // Pop array and index
1568    push_pair( make_load(control(), a, Type::DOUBLE, T_DOUBLE, TypeAryPtr::DOUBLES));
1569    break;
1570  }
1571  case Bytecodes::_bastore: array_store(T_BYTE);  break;
1572  case Bytecodes::_castore: array_store(T_CHAR);  break;
1573  case Bytecodes::_iastore: array_store(T_INT);   break;
1574  case Bytecodes::_sastore: array_store(T_SHORT); break;
1575  case Bytecodes::_fastore: array_store(T_FLOAT); break;
1576  case Bytecodes::_aastore: {
1577    d = array_addressing(T_OBJECT, 1);
1578    if (stopped())  return;     // guaranteed null or range check
1579    array_store_check();
1580    c = pop();                  // Oop to store
1581    b = pop();                  // index (already used)
1582    a = pop();                  // the array itself
1583    const TypeOopPtr* elemtype  = _gvn.type(a)->is_aryptr()->elem()->make_oopptr();
1584    const TypeAryPtr* adr_type = TypeAryPtr::OOPS;
1585    Node* store = store_oop_to_array(control(), a, d, adr_type, c, elemtype, T_OBJECT);
1586    break;
1587  }
1588  case Bytecodes::_lastore: {
1589    a = array_addressing(T_LONG, 2);
1590    if (stopped())  return;     // guaranteed null or range check
1591    c = pop_pair();
1592    _sp -= 2;                   // Pop array and index
1593    store_to_memory(control(), a, c, T_LONG, TypeAryPtr::LONGS);
1594    break;
1595  }
1596  case Bytecodes::_dastore: {
1597    a = array_addressing(T_DOUBLE, 2);
1598    if (stopped())  return;     // guaranteed null or range check
1599    c = pop_pair();
1600    _sp -= 2;                   // Pop array and index
1601    c = dstore_rounding(c);
1602    store_to_memory(control(), a, c, T_DOUBLE, TypeAryPtr::DOUBLES);
1603    break;
1604  }
1605  case Bytecodes::_getfield:
1606    do_getfield();
1607    break;
1608
1609  case Bytecodes::_getstatic:
1610    do_getstatic();
1611    break;
1612
1613  case Bytecodes::_putfield:
1614    do_putfield();
1615    break;
1616
1617  case Bytecodes::_putstatic:
1618    do_putstatic();
1619    break;
1620
1621  case Bytecodes::_irem:
1622    do_irem();
1623    break;
1624  case Bytecodes::_idiv:
1625    // Must keep both values on the expression-stack during null-check
1626    do_null_check(peek(), T_INT);
1627    // Compile-time detect of null-exception?
1628    if (stopped())  return;
1629    b = pop();
1630    a = pop();
1631    push( _gvn.transform( new (C, 3) DivINode(control(),a,b) ) );
1632    break;
1633  case Bytecodes::_imul:
1634    b = pop(); a = pop();
1635    push( _gvn.transform( new (C, 3) MulINode(a,b) ) );
1636    break;
1637  case Bytecodes::_iadd:
1638    b = pop(); a = pop();
1639    push( _gvn.transform( new (C, 3) AddINode(a,b) ) );
1640    break;
1641  case Bytecodes::_ineg:
1642    a = pop();
1643    push( _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),a)) );
1644    break;
1645  case Bytecodes::_isub:
1646    b = pop(); a = pop();
1647    push( _gvn.transform( new (C, 3) SubINode(a,b) ) );
1648    break;
1649  case Bytecodes::_iand:
1650    b = pop(); a = pop();
1651    push( _gvn.transform( new (C, 3) AndINode(a,b) ) );
1652    break;
1653  case Bytecodes::_ior:
1654    b = pop(); a = pop();
1655    push( _gvn.transform( new (C, 3) OrINode(a,b) ) );
1656    break;
1657  case Bytecodes::_ixor:
1658    b = pop(); a = pop();
1659    push( _gvn.transform( new (C, 3) XorINode(a,b) ) );
1660    break;
1661  case Bytecodes::_ishl:
1662    b = pop(); a = pop();
1663    push( _gvn.transform( new (C, 3) LShiftINode(a,b) ) );
1664    break;
1665  case Bytecodes::_ishr:
1666    b = pop(); a = pop();
1667    push( _gvn.transform( new (C, 3) RShiftINode(a,b) ) );
1668    break;
1669  case Bytecodes::_iushr:
1670    b = pop(); a = pop();
1671    push( _gvn.transform( new (C, 3) URShiftINode(a,b) ) );
1672    break;
1673
1674  case Bytecodes::_fneg:
1675    a = pop();
1676    b = _gvn.transform(new (C, 2) NegFNode (a));
1677    push(b);
1678    break;
1679
1680  case Bytecodes::_fsub:
1681    b = pop();
1682    a = pop();
1683    c = _gvn.transform( new (C, 3) SubFNode(a,b) );
1684    d = precision_rounding(c);
1685    push( d );
1686    break;
1687
1688  case Bytecodes::_fadd:
1689    b = pop();
1690    a = pop();
1691    c = _gvn.transform( new (C, 3) AddFNode(a,b) );
1692    d = precision_rounding(c);
1693    push( d );
1694    break;
1695
1696  case Bytecodes::_fmul:
1697    b = pop();
1698    a = pop();
1699    c = _gvn.transform( new (C, 3) MulFNode(a,b) );
1700    d = precision_rounding(c);
1701    push( d );
1702    break;
1703
1704  case Bytecodes::_fdiv:
1705    b = pop();
1706    a = pop();
1707    c = _gvn.transform( new (C, 3) DivFNode(0,a,b) );
1708    d = precision_rounding(c);
1709    push( d );
1710    break;
1711
1712  case Bytecodes::_frem:
1713    if (Matcher::has_match_rule(Op_ModF)) {
1714      // Generate a ModF node.
1715      b = pop();
1716      a = pop();
1717      c = _gvn.transform( new (C, 3) ModFNode(0,a,b) );
1718      d = precision_rounding(c);
1719      push( d );
1720    }
1721    else {
1722      // Generate a call.
1723      modf();
1724    }
1725    break;
1726
1727  case Bytecodes::_fcmpl:
1728    b = pop();
1729    a = pop();
1730    c = _gvn.transform( new (C, 3) CmpF3Node( a, b));
1731    push(c);
1732    break;
1733  case Bytecodes::_fcmpg:
1734    b = pop();
1735    a = pop();
1736
1737    // Same as fcmpl but need to flip the unordered case.  Swap the inputs,
1738    // which negates the result sign except for unordered.  Flip the unordered
1739    // as well by using CmpF3 which implements unordered-lesser instead of
1740    // unordered-greater semantics.  Finally, commute the result bits.  Result
1741    // is same as using a CmpF3Greater except we did it with CmpF3 alone.
1742    c = _gvn.transform( new (C, 3) CmpF3Node( b, a));
1743    c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) );
1744    push(c);
1745    break;
1746
1747  case Bytecodes::_f2i:
1748    a = pop();
1749    push(_gvn.transform(new (C, 2) ConvF2INode(a)));
1750    break;
1751
1752  case Bytecodes::_d2i:
1753    a = pop_pair();
1754    b = _gvn.transform(new (C, 2) ConvD2INode(a));
1755    push( b );
1756    break;
1757
1758  case Bytecodes::_f2d:
1759    a = pop();
1760    b = _gvn.transform( new (C, 2) ConvF2DNode(a));
1761    push_pair( b );
1762    break;
1763
1764  case Bytecodes::_d2f:
1765    a = pop_pair();
1766    b = _gvn.transform( new (C, 2) ConvD2FNode(a));
1767    // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed)
1768    //b = _gvn.transform(new (C, 2) RoundFloatNode(0, b) );
1769    push( b );
1770    break;
1771
1772  case Bytecodes::_l2f:
1773    if (Matcher::convL2FSupported()) {
1774      a = pop_pair();
1775      b = _gvn.transform( new (C, 2) ConvL2FNode(a));
1776      // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits.
1777      // Rather than storing the result into an FP register then pushing
1778      // out to memory to round, the machine instruction that implements
1779      // ConvL2D is responsible for rounding.
1780      // c = precision_rounding(b);
1781      c = _gvn.transform(b);
1782      push(c);
1783    } else {
1784      l2f();
1785    }
1786    break;
1787
1788  case Bytecodes::_l2d:
1789    a = pop_pair();
1790    b = _gvn.transform( new (C, 2) ConvL2DNode(a));
1791    // For i486.ad, rounding is always necessary (see _l2f above).
1792    // c = dprecision_rounding(b);
1793    c = _gvn.transform(b);
1794    push_pair(c);
1795    break;
1796
1797  case Bytecodes::_f2l:
1798    a = pop();
1799    b = _gvn.transform( new (C, 2) ConvF2LNode(a));
1800    push_pair(b);
1801    break;
1802
1803  case Bytecodes::_d2l:
1804    a = pop_pair();
1805    b = _gvn.transform( new (C, 2) ConvD2LNode(a));
1806    push_pair(b);
1807    break;
1808
1809  case Bytecodes::_dsub:
1810    b = pop_pair();
1811    a = pop_pair();
1812    c = _gvn.transform( new (C, 3) SubDNode(a,b) );
1813    d = dprecision_rounding(c);
1814    push_pair( d );
1815    break;
1816
1817  case Bytecodes::_dadd:
1818    b = pop_pair();
1819    a = pop_pair();
1820    c = _gvn.transform( new (C, 3) AddDNode(a,b) );
1821    d = dprecision_rounding(c);
1822    push_pair( d );
1823    break;
1824
1825  case Bytecodes::_dmul:
1826    b = pop_pair();
1827    a = pop_pair();
1828    c = _gvn.transform( new (C, 3) MulDNode(a,b) );
1829    d = dprecision_rounding(c);
1830    push_pair( d );
1831    break;
1832
1833  case Bytecodes::_ddiv:
1834    b = pop_pair();
1835    a = pop_pair();
1836    c = _gvn.transform( new (C, 3) DivDNode(0,a,b) );
1837    d = dprecision_rounding(c);
1838    push_pair( d );
1839    break;
1840
1841  case Bytecodes::_dneg:
1842    a = pop_pair();
1843    b = _gvn.transform(new (C, 2) NegDNode (a));
1844    push_pair(b);
1845    break;
1846
1847  case Bytecodes::_drem:
1848    if (Matcher::has_match_rule(Op_ModD)) {
1849      // Generate a ModD node.
1850      b = pop_pair();
1851      a = pop_pair();
1852      // a % b
1853
1854      c = _gvn.transform( new (C, 3) ModDNode(0,a,b) );
1855      d = dprecision_rounding(c);
1856      push_pair( d );
1857    }
1858    else {
1859      // Generate a call.
1860      modd();
1861    }
1862    break;
1863
1864  case Bytecodes::_dcmpl:
1865    b = pop_pair();
1866    a = pop_pair();
1867    c = _gvn.transform( new (C, 3) CmpD3Node( a, b));
1868    push(c);
1869    break;
1870
1871  case Bytecodes::_dcmpg:
1872    b = pop_pair();
1873    a = pop_pair();
1874    // Same as dcmpl but need to flip the unordered case.
1875    // Commute the inputs, which negates the result sign except for unordered.
1876    // Flip the unordered as well by using CmpD3 which implements
1877    // unordered-lesser instead of unordered-greater semantics.
1878    // Finally, negate the result bits.  Result is same as using a
1879    // CmpD3Greater except we did it with CmpD3 alone.
1880    c = _gvn.transform( new (C, 3) CmpD3Node( b, a));
1881    c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) );
1882    push(c);
1883    break;
1884
1885
1886    // Note for longs -> lo word is on TOS, hi word is on TOS - 1
1887  case Bytecodes::_land:
1888    b = pop_pair();
1889    a = pop_pair();
1890    c = _gvn.transform( new (C, 3) AndLNode(a,b) );
1891    push_pair(c);
1892    break;
1893  case Bytecodes::_lor:
1894    b = pop_pair();
1895    a = pop_pair();
1896    c = _gvn.transform( new (C, 3) OrLNode(a,b) );
1897    push_pair(c);
1898    break;
1899  case Bytecodes::_lxor:
1900    b = pop_pair();
1901    a = pop_pair();
1902    c = _gvn.transform( new (C, 3) XorLNode(a,b) );
1903    push_pair(c);
1904    break;
1905
1906  case Bytecodes::_lshl:
1907    b = pop();                  // the shift count
1908    a = pop_pair();             // value to be shifted
1909    c = _gvn.transform( new (C, 3) LShiftLNode(a,b) );
1910    push_pair(c);
1911    break;
1912  case Bytecodes::_lshr:
1913    b = pop();                  // the shift count
1914    a = pop_pair();             // value to be shifted
1915    c = _gvn.transform( new (C, 3) RShiftLNode(a,b) );
1916    push_pair(c);
1917    break;
1918  case Bytecodes::_lushr:
1919    b = pop();                  // the shift count
1920    a = pop_pair();             // value to be shifted
1921    c = _gvn.transform( new (C, 3) URShiftLNode(a,b) );
1922    push_pair(c);
1923    break;
1924  case Bytecodes::_lmul:
1925    b = pop_pair();
1926    a = pop_pair();
1927    c = _gvn.transform( new (C, 3) MulLNode(a,b) );
1928    push_pair(c);
1929    break;
1930
1931  case Bytecodes::_lrem:
1932    // Must keep both values on the expression-stack during null-check
1933    assert(peek(0) == top(), "long word order");
1934    do_null_check(peek(1), T_LONG);
1935    // Compile-time detect of null-exception?
1936    if (stopped())  return;
1937    b = pop_pair();
1938    a = pop_pair();
1939    c = _gvn.transform( new (C, 3) ModLNode(control(),a,b) );
1940    push_pair(c);
1941    break;
1942
1943  case Bytecodes::_ldiv:
1944    // Must keep both values on the expression-stack during null-check
1945    assert(peek(0) == top(), "long word order");
1946    do_null_check(peek(1), T_LONG);
1947    // Compile-time detect of null-exception?
1948    if (stopped())  return;
1949    b = pop_pair();
1950    a = pop_pair();
1951    c = _gvn.transform( new (C, 3) DivLNode(control(),a,b) );
1952    push_pair(c);
1953    break;
1954
1955  case Bytecodes::_ladd:
1956    b = pop_pair();
1957    a = pop_pair();
1958    c = _gvn.transform( new (C, 3) AddLNode(a,b) );
1959    push_pair(c);
1960    break;
1961  case Bytecodes::_lsub:
1962    b = pop_pair();
1963    a = pop_pair();
1964    c = _gvn.transform( new (C, 3) SubLNode(a,b) );
1965    push_pair(c);
1966    break;
1967  case Bytecodes::_lcmp:
1968    // Safepoints are now inserted _before_ branches.  The long-compare
1969    // bytecode painfully produces a 3-way value (-1,0,+1) which requires a
1970    // slew of control flow.  These are usually followed by a CmpI vs zero and
1971    // a branch; this pattern then optimizes to the obvious long-compare and
1972    // branch.  However, if the branch is backwards there's a Safepoint
1973    // inserted.  The inserted Safepoint captures the JVM state at the
1974    // pre-branch point, i.e. it captures the 3-way value.  Thus if a
1975    // long-compare is used to control a loop the debug info will force
1976    // computation of the 3-way value, even though the generated code uses a
1977    // long-compare and branch.  We try to rectify the situation by inserting
1978    // a SafePoint here and have it dominate and kill the safepoint added at a
1979    // following backwards branch.  At this point the JVM state merely holds 2
1980    // longs but not the 3-way value.
1981    if( UseLoopSafepoints ) {
1982      switch( iter().next_bc() ) {
1983      case Bytecodes::_ifgt:
1984      case Bytecodes::_iflt:
1985      case Bytecodes::_ifge:
1986      case Bytecodes::_ifle:
1987      case Bytecodes::_ifne:
1988      case Bytecodes::_ifeq:
1989        // If this is a backwards branch in the bytecodes, add Safepoint
1990        maybe_add_safepoint(iter().next_get_dest());
1991      }
1992    }
1993    b = pop_pair();
1994    a = pop_pair();
1995    c = _gvn.transform( new (C, 3) CmpL3Node( a, b ));
1996    push(c);
1997    break;
1998
1999  case Bytecodes::_lneg:
2000    a = pop_pair();
2001    b = _gvn.transform( new (C, 3) SubLNode(longcon(0),a));
2002    push_pair(b);
2003    break;
2004  case Bytecodes::_l2i:
2005    a = pop_pair();
2006    push( _gvn.transform( new (C, 2) ConvL2INode(a)));
2007    break;
2008  case Bytecodes::_i2l:
2009    a = pop();
2010    b = _gvn.transform( new (C, 2) ConvI2LNode(a));
2011    push_pair(b);
2012    break;
2013  case Bytecodes::_i2b:
2014    // Sign extend
2015    a = pop();
2016    a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(24)) );
2017    a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(24)) );
2018    push( a );
2019    break;
2020  case Bytecodes::_i2s:
2021    a = pop();
2022    a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(16)) );
2023    a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(16)) );
2024    push( a );
2025    break;
2026  case Bytecodes::_i2c:
2027    a = pop();
2028    push( _gvn.transform( new (C, 3) AndINode(a,_gvn.intcon(0xFFFF)) ) );
2029    break;
2030
2031  case Bytecodes::_i2f:
2032    a = pop();
2033    b = _gvn.transform( new (C, 2) ConvI2FNode(a) ) ;
2034    c = precision_rounding(b);
2035    push (b);
2036    break;
2037
2038  case Bytecodes::_i2d:
2039    a = pop();
2040    b = _gvn.transform( new (C, 2) ConvI2DNode(a));
2041    push_pair(b);
2042    break;
2043
2044  case Bytecodes::_iinc:        // Increment local
2045    i = iter().get_index();     // Get local index
2046    set_local( i, _gvn.transform( new (C, 3) AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2047    break;
2048
2049  // Exit points of synchronized methods must have an unlock node
2050  case Bytecodes::_return:
2051    return_current(NULL);
2052    break;
2053
2054  case Bytecodes::_ireturn:
2055  case Bytecodes::_areturn:
2056  case Bytecodes::_freturn:
2057    return_current(pop());
2058    break;
2059  case Bytecodes::_lreturn:
2060    return_current(pop_pair());
2061    break;
2062  case Bytecodes::_dreturn:
2063    return_current(pop_pair());
2064    break;
2065
2066  case Bytecodes::_athrow:
2067    // null exception oop throws NULL pointer exception
2068    do_null_check(peek(), T_OBJECT);
2069    if (stopped())  return;
2070    // Hook the thrown exception directly to subsequent handlers.
2071    if (BailoutToInterpreterForThrows) {
2072      // Keep method interpreted from now on.
2073      uncommon_trap(Deoptimization::Reason_unhandled,
2074                    Deoptimization::Action_make_not_compilable);
2075      return;
2076    }
2077    if (env()->jvmti_can_post_on_exceptions()) {
2078      // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2079      uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2080    }
2081    // Here if either can_post_on_exceptions or should_post_on_exceptions is false
2082    add_exception_state(make_exception_state(peek()));
2083    break;
2084
2085  case Bytecodes::_goto:   // fall through
2086  case Bytecodes::_goto_w: {
2087    int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest();
2088
2089    // If this is a backwards branch in the bytecodes, add Safepoint
2090    maybe_add_safepoint(target_bci);
2091
2092    // Update method data
2093    profile_taken_branch(target_bci);
2094
2095    // Add loop predicate if it goes to a loop
2096    if (should_add_predicate(target_bci)){
2097      add_predicate();
2098    }
2099    // Merge the current control into the target basic block
2100    merge(target_bci);
2101
2102    // See if we can get some profile data and hand it off to the next block
2103    Block *target_block = block()->successor_for_bci(target_bci);
2104    if (target_block->pred_count() != 1)  break;
2105    ciMethodData* methodData = method()->method_data();
2106    if (!methodData->is_mature())  break;
2107    ciProfileData* data = methodData->bci_to_data(bci());
2108    assert( data->is_JumpData(), "" );
2109    int taken = ((ciJumpData*)data)->taken();
2110    taken = method()->scale_count(taken);
2111    target_block->set_count(taken);
2112    break;
2113  }
2114
2115  case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
2116  case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2117  handle_if_null:
2118    // If this is a backwards branch in the bytecodes, add Safepoint
2119    maybe_add_safepoint(iter().get_dest());
2120    a = null();
2121    b = pop();
2122    c = _gvn.transform( new (C, 3) CmpPNode(b, a) );
2123    do_ifnull(btest, c);
2124    break;
2125
2126  case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2127  case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2128  handle_if_acmp:
2129    // If this is a backwards branch in the bytecodes, add Safepoint
2130    maybe_add_safepoint(iter().get_dest());
2131    a = pop();
2132    b = pop();
2133    c = _gvn.transform( new (C, 3) CmpPNode(b, a) );
2134    do_if(btest, c);
2135    break;
2136
2137  case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2138  case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2139  case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2140  case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2141  case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2142  case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2143  handle_ifxx:
2144    // If this is a backwards branch in the bytecodes, add Safepoint
2145    maybe_add_safepoint(iter().get_dest());
2146    a = _gvn.intcon(0);
2147    b = pop();
2148    c = _gvn.transform( new (C, 3) CmpINode(b, a) );
2149    do_if(btest, c);
2150    break;
2151
2152  case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2153  case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2154  case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2155  case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp;
2156  case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp;
2157  case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp;
2158  handle_if_icmp:
2159    // If this is a backwards branch in the bytecodes, add Safepoint
2160    maybe_add_safepoint(iter().get_dest());
2161    a = pop();
2162    b = pop();
2163    c = _gvn.transform( new (C, 3) CmpINode( b, a ) );
2164    do_if(btest, c);
2165    break;
2166
2167  case Bytecodes::_tableswitch:
2168    do_tableswitch();
2169    break;
2170
2171  case Bytecodes::_lookupswitch:
2172    do_lookupswitch();
2173    break;
2174
2175  case Bytecodes::_invokestatic:
2176  case Bytecodes::_invokedynamic:
2177  case Bytecodes::_invokespecial:
2178  case Bytecodes::_invokevirtual:
2179  case Bytecodes::_invokeinterface:
2180    do_call();
2181    break;
2182  case Bytecodes::_checkcast:
2183    do_checkcast();
2184    break;
2185  case Bytecodes::_instanceof:
2186    do_instanceof();
2187    break;
2188  case Bytecodes::_anewarray:
2189    do_anewarray();
2190    break;
2191  case Bytecodes::_newarray:
2192    do_newarray((BasicType)iter().get_index());
2193    break;
2194  case Bytecodes::_multianewarray:
2195    do_multianewarray();
2196    break;
2197  case Bytecodes::_new:
2198    do_new();
2199    break;
2200
2201  case Bytecodes::_jsr:
2202  case Bytecodes::_jsr_w:
2203    do_jsr();
2204    break;
2205
2206  case Bytecodes::_ret:
2207    do_ret();
2208    break;
2209
2210
2211  case Bytecodes::_monitorenter:
2212    do_monitor_enter();
2213    break;
2214
2215  case Bytecodes::_monitorexit:
2216    do_monitor_exit();
2217    break;
2218
2219  case Bytecodes::_breakpoint:
2220    // Breakpoint set concurrently to compile
2221    // %%% use an uncommon trap?
2222    C->record_failure("breakpoint in method");
2223    return;
2224
2225  default:
2226#ifndef PRODUCT
2227    map()->dump(99);
2228#endif
2229    tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) );
2230    ShouldNotReachHere();
2231  }
2232
2233#ifndef PRODUCT
2234  IdealGraphPrinter *printer = IdealGraphPrinter::printer();
2235  if(printer) {
2236    char buffer[256];
2237    sprintf(buffer, "Bytecode %d: %s", bci(), Bytecodes::name(bc()));
2238    bool old = printer->traverse_outs();
2239    printer->set_traverse_outs(true);
2240    printer->print_method(C, buffer, 4);
2241    printer->set_traverse_outs(old);
2242  }
2243#endif
2244}
2245