src/share/vm/opto/parse2.cpp

Fri, 04 Mar 2016 16:15:48 +0300

author
vkempik
date
Fri, 04 Mar 2016 16:15:48 +0300
changeset 8318
ea7ac121a5d3
parent 8285
535618ab1c04
child 8415
d109bda16490
permissions
-rw-r--r--

8130150: Implement BigInteger.montgomeryMultiply intrinsic
Reviewed-by: kvn, mdoerr

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

mercurial