src/share/vm/opto/parse2.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8604
04d83ba48607
child 9041
95a08233f46c
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial