parse2.cpp revision 2273:1d1603768966
124269Speter/* 224269Speter * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 324269Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 424269Speter * 524269Speter * This code is free software; you can redistribute it and/or modify it 624269Speter * under the terms of the GNU General Public License version 2 only, as 724269Speter * published by the Free Software Foundation. 824269Speter * 924269Speter * This code is distributed in the hope that it will be useful, but WITHOUT 1024269Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1124269Speter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1224269Speter * version 2 for more details (a copy is included in the LICENSE file that 1324269Speter * accompanied this code). 1424269Speter * 1524269Speter * You should have received a copy of the GNU General Public License version 1624269Speter * 2 along with this work; if not, write to the Free Software Foundation, 1724269Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1824269Speter * 1924269Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2024269Speter * or visit www.oracle.com if you need additional information or have any 2124269Speter * questions. 2224269Speter * 2324269Speter */ 2424269Speter 2524269Speter#include "precompiled.hpp" 2624269Speter#include "ci/ciMethodData.hpp" 2724269Speter#include "classfile/systemDictionary.hpp" 2824269Speter#include "classfile/vmSymbols.hpp" 2924269Speter#include "compiler/compileLog.hpp" 3024269Speter#include "interpreter/linkResolver.hpp" 3124269Speter#include "memory/universe.inline.hpp" 3224269Speter#include "opto/addnode.hpp" 3324269Speter#include "opto/divnode.hpp" 3424269Speter#include "opto/idealGraphPrinter.hpp" 3524269Speter#include "opto/matcher.hpp" 3624269Speter#include "opto/memnode.hpp" 3724269Speter#include "opto/mulnode.hpp" 3824269Speter#include "opto/parse.hpp" 3924269Speter#include "opto/runtime.hpp" 4024269Speter#include "runtime/deoptimization.hpp" 4124269Speter#include "runtime/sharedRuntime.hpp" 4224269Speter 4324269Speterextern int explicit_null_checks_inserted, 4424269Speter explicit_null_checks_elided; 4524269Speter 4624269Speter//---------------------------------array_load---------------------------------- 4724269Spetervoid Parse::array_load(BasicType elem_type) { 4824269Speter const Type* elem = Type::TOP; 4924269Speter Node* adr = array_addressing(elem_type, 0, &elem); 5024269Speter if (stopped()) return; // guaranteed null or range check 5124269Speter _sp -= 2; // Pop array and index 5224269Speter const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type); 5324269Speter Node* ld = make_load(control(), adr, elem, elem_type, adr_type); 5424269Speter push(ld); 5524269Speter} 5624269Speter 5724269Speter 5824269Speter//--------------------------------array_store---------------------------------- 5924269Spetervoid Parse::array_store(BasicType elem_type) { 6024269Speter Node* adr = array_addressing(elem_type, 1); 6124269Speter if (stopped()) return; // guaranteed null or range check 6224269Speter Node* val = pop(); 6324269Speter _sp -= 2; // Pop array and index 6424269Speter const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type); 6524269Speter store_to_memory(control(), adr, val, elem_type, adr_type); 6624269Speter} 6724269Speter 6824269Speter 6924269Speter//------------------------------array_addressing------------------------------- 7024269Speter// Pull array and index from the stack. Compute pointer-to-element. 7124269SpeterNode* Parse::array_addressing(BasicType type, int vals, const Type* *result2) { 7224269Speter Node *idx = peek(0+vals); // Get from stack without popping 7324269Speter Node *ary = peek(1+vals); // in case of exception 7424269Speter 7524269Speter // Null check the array base, with correct stack contents 7624269Speter ary = do_null_check(ary, T_ARRAY); 7724269Speter // Compile-time detect of null-exception? 7824269Speter if (stopped()) return top(); 7924269Speter 8024269Speter const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr(); 8124269Speter const TypeInt* sizetype = arytype->size(); 8224269Speter const Type* elemtype = arytype->elem(); 8324269Speter 8424269Speter if (UseUniqueSubclasses && result2 != NULL) { 8524269Speter const Type* el = elemtype->make_ptr(); 8624269Speter if (el && el->isa_instptr()) { 8724269Speter const TypeInstPtr* toop = el->is_instptr(); 8824269Speter if (toop->klass()->as_instance_klass()->unique_concrete_subklass()) { 8924269Speter // If we load from "AbstractClass[]" we must see "ConcreteSubClass". 9024269Speter const Type* subklass = Type::get_const_type(toop->klass()); 9124269Speter elemtype = subklass->join(el); 9224269Speter } 9324269Speter } 9424269Speter } 9524269Speter 9624269Speter // Check for big class initializers with all constant offsets 9724269Speter // feeding into a known-size array. 9824269Speter const TypeInt* idxtype = _gvn.type(idx)->is_int(); 9924269Speter // See if the highest idx value is less than the lowest array bound, 10024269Speter // and if the idx value cannot be negative: 10124269Speter bool need_range_check = true; 10224269Speter if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) { 10324269Speter need_range_check = false; 10424269Speter if (C->log() != NULL) C->log()->elem("observe that='!need_range_check'"); 10524269Speter } 10624269Speter 10724269Speter if (!arytype->klass()->is_loaded()) { 10824269Speter // Only fails for some -Xcomp runs 10924269Speter // The class is unloaded. We have to run this bytecode in the interpreter. 11024269Speter uncommon_trap(Deoptimization::Reason_unloaded, 11124269Speter Deoptimization::Action_reinterpret, 11224269Speter arytype->klass(), "!loaded array"); 11324269Speter return top(); 11424269Speter } 11524269Speter 11624269Speter // Do the range check 11724269Speter if (GenerateRangeChecks && need_range_check) { 11824269Speter Node* tst; 11924269Speter if (sizetype->_hi <= 0) { 12024269Speter // The greatest array bound is negative, so we can conclude that we're 12124269Speter // compiling unreachable code, but the unsigned compare trick used below 12224269Speter // only works with non-negative lengths. Instead, hack "tst" to be zero so 12324269Speter // the uncommon_trap path will always be taken. 12424269Speter tst = _gvn.intcon(0); 12524269Speter } else { 12624269Speter // Range is constant in array-oop, so we can use the original state of mem 12724269Speter Node* len = load_array_length(ary); 12824269Speter 12924269Speter // Test length vs index (standard trick using unsigned compare) 13024269Speter Node* chk = _gvn.transform( new (C, 3) CmpUNode(idx, len) ); 13124269Speter BoolTest::mask btest = BoolTest::lt; 13224269Speter tst = _gvn.transform( new (C, 2) BoolNode(chk, btest) ); 13324269Speter } 13424269Speter // Branch to failure if out of bounds 13524269Speter { BuildCutout unless(this, tst, PROB_MAX); 13624269Speter if (C->allow_range_check_smearing()) { 13724269Speter // Do not use builtin_throw, since range checks are sometimes 13824269Speter // made more stringent by an optimistic transformation. 13924269Speter // This creates "tentative" range checks at this point, 14024269Speter // which are not guaranteed to throw exceptions. 14124269Speter // See IfNode::Ideal, is_range_check, adjust_check. 14224269Speter uncommon_trap(Deoptimization::Reason_range_check, 14324269Speter Deoptimization::Action_make_not_entrant, 14424269Speter NULL, "range_check"); 14524269Speter } else { 14624269Speter // If we have already recompiled with the range-check-widening 14724269Speter // heroic optimization turned off, then we must really be throwing 14824269Speter // range check exceptions. 14924269Speter builtin_throw(Deoptimization::Reason_range_check, idx); 15024269Speter } 15124269Speter } 15224269Speter } 15324269Speter // Check for always knowing you are throwing a range-check exception 15424269Speter if (stopped()) return top(); 15524269Speter 15624269Speter Node* ptr = array_element_address(ary, idx, type, sizetype); 15724269Speter 15824269Speter if (result2 != NULL) *result2 = elemtype; 15924269Speter 16024269Speter assert(ptr != top(), "top should go hand-in-hand with stopped"); 16124269Speter 16224269Speter return ptr; 16324269Speter} 16424269Speter 16524269Speter 16624269Speter// returns IfNode 16724269SpeterIfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask) { 16824269Speter Node *cmp = _gvn.transform( new (C, 3) CmpINode( a, b)); // two cases: shiftcount > 32 and shiftcount <= 32 16924269Speter Node *tst = _gvn.transform( new (C, 2) BoolNode( cmp, mask)); 17024269Speter IfNode *iff = create_and_map_if( control(), tst, ((mask == BoolTest::eq) ? PROB_STATIC_INFREQUENT : PROB_FAIR), COUNT_UNKNOWN ); 17124269Speter return iff; 17224269Speter} 17324269Speter 17424269Speter// return Region node 17524269SpeterNode* Parse::jump_if_join(Node* iffalse, Node* iftrue) { 17624269Speter Node *region = new (C, 3) RegionNode(3); // 2 results 17724269Speter record_for_igvn(region); 17824269Speter region->init_req(1, iffalse); 17924269Speter region->init_req(2, iftrue ); 18024269Speter _gvn.set_type(region, Type::CONTROL); 18124269Speter region = _gvn.transform(region); 18224269Speter set_control (region); 18324269Speter return region; 18424269Speter} 18524269Speter 18624269Speter 18724269Speter//------------------------------helper for tableswitch------------------------- 18824269Spetervoid Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) { 18924269Speter // True branch, use existing map info 19024269Speter { PreserveJVMState pjvms(this); 19124269Speter Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode (iff) ); 19224269Speter set_control( iftrue ); 19324269Speter profile_switch_case(prof_table_index); 19424269Speter merge_new_path(dest_bci_if_true); 19524269Speter } 19624269Speter 19724269Speter // False branch 19824269Speter Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) ); 19924269Speter set_control( iffalse ); 20024269Speter} 20124269Speter 20224269Spetervoid Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) { 20324269Speter // True branch, use existing map info 20424269Speter { PreserveJVMState pjvms(this); 20524269Speter Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode (iff) ); 20624269Speter set_control( iffalse ); 20724269Speter profile_switch_case(prof_table_index); 20824269Speter merge_new_path(dest_bci_if_true); 20924269Speter } 21024269Speter 21124269Speter // False branch 21224269Speter Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff) ); 21324269Speter set_control( iftrue ); 21424269Speter} 21524269Speter 21624269Spetervoid Parse::jump_if_always_fork(int dest_bci, int prof_table_index) { 21724269Speter // False branch, use existing map and control() 21824269Speter profile_switch_case(prof_table_index); 21924269Speter merge_new_path(dest_bci); 22024269Speter} 22124269Speter 22224269Speter 22324269Speterextern "C" { 22424269Speter static int jint_cmp(const void *i, const void *j) { 22524269Speter int a = *(jint *)i; 22624269Speter int b = *(jint *)j; 22724269Speter return a > b ? 1 : a < b ? -1 : 0; 22824269Speter } 22924269Speter} 23024269Speter 23124269Speter 23224269Speter// Default value for methodData switch indexing. Must be a negative value to avoid 23324269Speter// conflict with any legal switch index. 23424269Speter#define NullTableIndex -1 23524269Speter 23624269Speterclass SwitchRange : public StackObj { 23724269Speter // a range of integers coupled with a bci destination 23824269Speter jint _lo; // inclusive lower limit 23924269Speter jint _hi; // inclusive upper limit 24024269Speter int _dest; 24124269Speter int _table_index; // index into method data table 24224269Speter 24324269Speterpublic: 24424269Speter jint lo() const { return _lo; } 24524269Speter jint hi() const { return _hi; } 24624269Speter int dest() const { return _dest; } 24724269Speter int table_index() const { return _table_index; } 24824269Speter bool is_singleton() const { return _lo == _hi; } 24924269Speter 25024269Speter void setRange(jint lo, jint hi, int dest, int table_index) { 25124269Speter assert(lo <= hi, "must be a non-empty range"); 25224269Speter _lo = lo, _hi = hi; _dest = dest; _table_index = table_index; 25324269Speter } 25424269Speter bool adjoinRange(jint lo, jint hi, int dest, int table_index) { 25524269Speter assert(lo <= hi, "must be a non-empty range"); 25624269Speter if (lo == _hi+1 && dest == _dest && table_index == _table_index) { 25724269Speter _hi = hi; 25824269Speter return true; 25924269Speter } 26024269Speter return false; 26124269Speter } 26224269Speter 26324269Speter void set (jint value, int dest, int table_index) { 26424269Speter setRange(value, value, dest, table_index); 26524269Speter } 26624269Speter bool adjoin(jint value, int dest, int table_index) { 26724269Speter return adjoinRange(value, value, dest, table_index); 26824269Speter } 26924269Speter 27024269Speter void print(ciEnv* env) { 27124269Speter if (is_singleton()) 27224269Speter tty->print(" {%d}=>%d", lo(), dest()); 27324269Speter else if (lo() == min_jint) 27424269Speter tty->print(" {..%d}=>%d", hi(), dest()); 27524269Speter else if (hi() == max_jint) 27624269Speter tty->print(" {%d..}=>%d", lo(), dest()); 27724269Speter else 27824269Speter tty->print(" {%d..%d}=>%d", lo(), hi(), dest()); 27924269Speter } 28024269Speter}; 28124269Speter 28224269Speter 28324269Speter//-------------------------------do_tableswitch-------------------------------- 28424269Spetervoid Parse::do_tableswitch() { 28524269Speter Node* lookup = pop(); 28624269Speter 28724269Speter // Get information about tableswitch 28824269Speter int default_dest = iter().get_dest_table(0); 28924269Speter int lo_index = iter().get_int_table(1); 29024269Speter int hi_index = iter().get_int_table(2); 29124269Speter int len = hi_index - lo_index + 1; 29224269Speter 29324269Speter if (len < 1) { 29424269Speter // If this is a backward branch, add safepoint 29524269Speter maybe_add_safepoint(default_dest); 29624269Speter merge(default_dest); 29724269Speter return; 29824269Speter } 29924269Speter 30024269Speter // generate decision tree, using trichotomy when possible 30124269Speter int rnum = len+2; 30224269Speter bool makes_backward_branch = false; 30324269Speter SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 30424269Speter int rp = -1; 30524269Speter if (lo_index != min_jint) { 30624269Speter ranges[++rp].setRange(min_jint, lo_index-1, default_dest, NullTableIndex); 30724269Speter } 30824269Speter for (int j = 0; j < len; j++) { 30924269Speter jint match_int = lo_index+j; 31024269Speter int dest = iter().get_dest_table(j+3); 31124269Speter makes_backward_branch |= (dest <= bci()); 31224269Speter int table_index = method_data_update() ? j : NullTableIndex; 31324269Speter if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index)) { 31424269Speter ranges[++rp].set(match_int, dest, table_index); 31524269Speter } 31624269Speter } 31724269Speter jint highest = lo_index+(len-1); 31824269Speter assert(ranges[rp].hi() == highest, ""); 31924269Speter if (highest != max_jint 32024269Speter && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex)) { 32124269Speter ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex); 32224269Speter } 32324269Speter assert(rp < len+2, "not too many ranges"); 32424269Speter 32524269Speter // Safepoint in case if backward branch observed 32624269Speter if( makes_backward_branch && UseLoopSafepoints ) 32724269Speter add_safepoint(); 32824269Speter 32924269Speter jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 33024269Speter} 33124269Speter 33224269Speter 33324269Speter//------------------------------do_lookupswitch-------------------------------- 33424269Spetervoid Parse::do_lookupswitch() { 33524269Speter Node *lookup = pop(); // lookup value 33624269Speter // Get information about lookupswitch 33724269Speter int default_dest = iter().get_dest_table(0); 33824269Speter int len = iter().get_int_table(1); 33924269Speter 34024269Speter if (len < 1) { // If this is a backward branch, add safepoint 34124269Speter maybe_add_safepoint(default_dest); 34224269Speter merge(default_dest); 34324269Speter return; 34424269Speter } 34524269Speter 34624269Speter // generate decision tree, using trichotomy when possible 34724269Speter jint* table = NEW_RESOURCE_ARRAY(jint, len*2); 34824269Speter { 34924269Speter for( int j = 0; j < len; j++ ) { 35024269Speter table[j+j+0] = iter().get_int_table(2+j+j); 35124269Speter table[j+j+1] = iter().get_dest_table(2+j+j+1); 35224269Speter } 35324269Speter qsort( table, len, 2*sizeof(table[0]), jint_cmp ); 35424269Speter } 35524269Speter 35624269Speter int rnum = len*2+1; 35724269Speter bool makes_backward_branch = false; 35824269Speter SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 35924269Speter int rp = -1; 36024269Speter for( int j = 0; j < len; j++ ) { 36124269Speter jint match_int = table[j+j+0]; 36224269Speter int dest = table[j+j+1]; 36324269Speter int next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1; 36424269Speter int table_index = method_data_update() ? j : NullTableIndex; 36524269Speter makes_backward_branch |= (dest <= bci()); 36624269Speter if( match_int != next_lo ) { 36724269Speter ranges[++rp].setRange(next_lo, match_int-1, default_dest, NullTableIndex); 36824269Speter } 36924269Speter if( rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index) ) { 37024269Speter ranges[++rp].set(match_int, dest, table_index); 37124269Speter } 37224269Speter } 37324269Speter jint highest = table[2*(len-1)]; 37424269Speter assert(ranges[rp].hi() == highest, ""); 37524269Speter if( highest != max_jint 37624269Speter && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex) ) { 37724269Speter ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex); 37824269Speter } 37924269Speter assert(rp < rnum, "not too many ranges"); 38024269Speter 38124269Speter // Safepoint in case backward branch observed 38224269Speter if( makes_backward_branch && UseLoopSafepoints ) 38324269Speter add_safepoint(); 38424269Speter 38524269Speter jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 38624269Speter} 38724269Speter 38824269Speter//----------------------------create_jump_tables------------------------------- 38924269Speterbool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) { 39024269Speter // Are jumptables enabled 39124269Speter if (!UseJumpTables) return false; 39224269Speter 39324269Speter // Are jumptables supported 39424269Speter if (!Matcher::has_match_rule(Op_Jump)) return false; 39524269Speter 39624269Speter // Don't make jump table if profiling 39724269Speter if (method_data_update()) return false; 39824269Speter 39924269Speter // Decide if a guard is needed to lop off big ranges at either (or 40024269Speter // both) end(s) of the input set. We'll call this the default target 40124269Speter // even though we can't be sure that it is the true "default". 40224269Speter 40324269Speter bool needs_guard = false; 40424269Speter int default_dest; 40524269Speter int64 total_outlier_size = 0; 40624269Speter int64 hi_size = ((int64)hi->hi()) - ((int64)hi->lo()) + 1; 40724269Speter int64 lo_size = ((int64)lo->hi()) - ((int64)lo->lo()) + 1; 40824269Speter 40924269Speter if (lo->dest() == hi->dest()) { 41024269Speter total_outlier_size = hi_size + lo_size; 41124269Speter default_dest = lo->dest(); 41224269Speter } else if (lo_size > hi_size) { 41324269Speter total_outlier_size = lo_size; 41424269Speter default_dest = lo->dest(); 41524269Speter } else { 41624269Speter total_outlier_size = hi_size; 41724269Speter default_dest = hi->dest(); 41824269Speter } 41924269Speter 42024269Speter // If a guard test will eliminate very sparse end ranges, then 42124269Speter // it is worth the cost of an extra jump. 42224269Speter if (total_outlier_size > (MaxJumpTableSparseness * 4)) { 42324269Speter needs_guard = true; 42424269Speter if (default_dest == lo->dest()) lo++; 42524269Speter if (default_dest == hi->dest()) hi--; 42624269Speter } 42724269Speter 42824269Speter // Find the total number of cases and ranges 42924269Speter int64 num_cases = ((int64)hi->hi()) - ((int64)lo->lo()) + 1; 43024269Speter int num_range = hi - lo + 1; 43124269Speter 43224269Speter // Don't create table if: too large, too small, or too sparse. 43324269Speter if (num_cases < MinJumpTableSize || num_cases > MaxJumpTableSize) 43424269Speter return false; 43524269Speter if (num_cases > (MaxJumpTableSparseness * num_range)) 43624269Speter return false; 43724269Speter 43824269Speter // Normalize table lookups to zero 43924269Speter int lowval = lo->lo(); 44024269Speter key_val = _gvn.transform( new (C, 3) SubINode(key_val, _gvn.intcon(lowval)) ); 44124269Speter 44224269Speter // Generate a guard to protect against input keyvals that aren't 44324269Speter // in the switch domain. 44424269Speter if (needs_guard) { 44524269Speter Node* size = _gvn.intcon(num_cases); 44624269Speter Node* cmp = _gvn.transform( new (C, 3) CmpUNode(key_val, size) ); 44724269Speter Node* tst = _gvn.transform( new (C, 2) BoolNode(cmp, BoolTest::ge) ); 44824269Speter IfNode* iff = create_and_map_if( control(), tst, PROB_FAIR, COUNT_UNKNOWN); 44924269Speter jump_if_true_fork(iff, default_dest, NullTableIndex); 45024269Speter } 45124269Speter 45224269Speter // Create an ideal node JumpTable that has projections 45324269Speter // of all possible ranges for a switch statement 45424269Speter // The key_val input must be converted to a pointer offset and scaled. 45524269Speter // Compare Parse::array_addressing above. 45624269Speter#ifdef _LP64 45724269Speter // Clean the 32-bit int into a real 64-bit offset. 45824269Speter // Otherwise, the jint value 0 might turn into an offset of 0x0800000000. 45924269Speter const TypeLong* lkeytype = TypeLong::make(CONST64(0), num_cases-1, Type::WidenMin); 46024269Speter key_val = _gvn.transform( new (C, 2) ConvI2LNode(key_val, lkeytype) ); 46124269Speter#endif 46224269Speter // Shift the value by wordsize so we have an index into the table, rather 46324269Speter // than a switch value 46424269Speter Node *shiftWord = _gvn.MakeConX(wordSize); 46524269Speter key_val = _gvn.transform( new (C, 3) MulXNode( key_val, shiftWord)); 46624269Speter 46724269Speter // Create the JumpNode 46824269Speter Node* jtn = _gvn.transform( new (C, 2) JumpNode(control(), key_val, num_cases) ); 46924269Speter 47024269Speter // These are the switch destinations hanging off the jumpnode 47124269Speter int i = 0; 47224269Speter for (SwitchRange* r = lo; r <= hi; r++) { 47324269Speter for (int j = r->lo(); j <= r->hi(); j++, i++) { 47424269Speter Node* input = _gvn.transform(new (C, 1) JumpProjNode(jtn, i, r->dest(), j - lowval)); 47524269Speter { 47624269Speter PreserveJVMState pjvms(this); 47724269Speter set_control(input); 47824269Speter jump_if_always_fork(r->dest(), r->table_index()); 47924269Speter } 48024269Speter } 48124269Speter } 48224269Speter assert(i == num_cases, "miscount of cases"); 48324269Speter stop_and_kill_map(); // no more uses for this JVMS 48424269Speter return true; 48524269Speter} 48624269Speter 48724269Speter//----------------------------jump_switch_ranges------------------------------- 48824269Spetervoid Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) { 48924269Speter Block* switch_block = block(); 49024269Speter 49124269Speter if (switch_depth == 0) { 49224269Speter // Do special processing for the top-level call. 49324269Speter assert(lo->lo() == min_jint, "initial range must exhaust Type::INT"); 49424269Speter assert(hi->hi() == max_jint, "initial range must exhaust Type::INT"); 49524269Speter 49624269Speter // Decrement pred-numbers for the unique set of nodes. 49724269Speter#ifdef ASSERT 49824269Speter // Ensure that the block's successors are a (duplicate-free) set. 49924269Speter int successors_counted = 0; // block occurrences in [hi..lo] 50024269Speter int unique_successors = switch_block->num_successors(); 50124269Speter for (int i = 0; i < unique_successors; i++) { 50224269Speter Block* target = switch_block->successor_at(i); 50324269Speter 50424269Speter // Check that the set of successors is the same in both places. 50524269Speter int successors_found = 0; 50624269Speter for (SwitchRange* p = lo; p <= hi; p++) { 50724269Speter if (p->dest() == target->start()) successors_found++; 50824269Speter } 50924269Speter assert(successors_found > 0, "successor must be known"); 51024269Speter successors_counted += successors_found; 51124269Speter } 51224269Speter assert(successors_counted == (hi-lo)+1, "no unexpected successors"); 51324269Speter#endif 51424269Speter 51524269Speter // Maybe prune the inputs, based on the type of key_val. 51624269Speter jint min_val = min_jint; 51724269Speter jint max_val = max_jint; 51824269Speter const TypeInt* ti = key_val->bottom_type()->isa_int(); 51924269Speter if (ti != NULL) { 52024269Speter min_val = ti->_lo; 52124269Speter max_val = ti->_hi; 52224269Speter assert(min_val <= max_val, "invalid int type"); 52324269Speter } 52424269Speter while (lo->hi() < min_val) lo++; 52524269Speter if (lo->lo() < min_val) lo->setRange(min_val, lo->hi(), lo->dest(), lo->table_index()); 52624269Speter while (hi->lo() > max_val) hi--; 52724269Speter if (hi->hi() > max_val) hi->setRange(hi->lo(), max_val, hi->dest(), hi->table_index()); 52824269Speter } 52924269Speter 53024269Speter#ifndef PRODUCT 53124269Speter if (switch_depth == 0) { 532 _max_switch_depth = 0; 533 _est_switch_depth = log2_intptr((hi-lo+1)-1)+1; 534 } 535#endif 536 537 assert(lo <= hi, "must be a non-empty set of ranges"); 538 if (lo == hi) { 539 jump_if_always_fork(lo->dest(), lo->table_index()); 540 } else { 541 assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges"); 542 assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges"); 543 544 if (create_jump_tables(key_val, lo, hi)) return; 545 546 int nr = hi - lo + 1; 547 548 SwitchRange* mid = lo + nr/2; 549 // if there is an easy choice, pivot at a singleton: 550 if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton()) mid--; 551 552 assert(lo < mid && mid <= hi, "good pivot choice"); 553 assert(nr != 2 || mid == hi, "should pick higher of 2"); 554 assert(nr != 3 || mid == hi-1, "should pick middle of 3"); 555 556 Node *test_val = _gvn.intcon(mid->lo()); 557 558 if (mid->is_singleton()) { 559 IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne); 560 jump_if_false_fork(iff_ne, mid->dest(), mid->table_index()); 561 562 // Special Case: If there are exactly three ranges, and the high 563 // and low range each go to the same place, omit the "gt" test, 564 // since it will not discriminate anything. 565 bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest()); 566 if (eq_test_only) { 567 assert(mid == hi-1, ""); 568 } 569 570 // if there is a higher range, test for it and process it: 571 if (mid < hi && !eq_test_only) { 572 // two comparisons of same values--should enable 1 test for 2 branches 573 // Use BoolTest::le instead of BoolTest::gt 574 IfNode *iff_le = jump_if_fork_int(key_val, test_val, BoolTest::le); 575 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff_le) ); 576 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff_le) ); 577 { PreserveJVMState pjvms(this); 578 set_control(iffalse); 579 jump_switch_ranges(key_val, mid+1, hi, switch_depth+1); 580 } 581 set_control(iftrue); 582 } 583 584 } else { 585 // mid is a range, not a singleton, so treat mid..hi as a unit 586 IfNode *iff_ge = jump_if_fork_int(key_val, test_val, BoolTest::ge); 587 588 // if there is a higher range, test for it and process it: 589 if (mid == hi) { 590 jump_if_true_fork(iff_ge, mid->dest(), mid->table_index()); 591 } else { 592 Node *iftrue = _gvn.transform( new (C, 1) IfTrueNode(iff_ge) ); 593 Node *iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff_ge) ); 594 { PreserveJVMState pjvms(this); 595 set_control(iftrue); 596 jump_switch_ranges(key_val, mid, hi, switch_depth+1); 597 } 598 set_control(iffalse); 599 } 600 } 601 602 // in any case, process the lower range 603 jump_switch_ranges(key_val, lo, mid-1, switch_depth+1); 604 } 605 606 // Decrease pred_count for each successor after all is done. 607 if (switch_depth == 0) { 608 int unique_successors = switch_block->num_successors(); 609 for (int i = 0; i < unique_successors; i++) { 610 Block* target = switch_block->successor_at(i); 611 // Throw away the pre-allocated path for each unique successor. 612 target->next_path_num(); 613 } 614 } 615 616#ifndef PRODUCT 617 _max_switch_depth = MAX2(switch_depth, _max_switch_depth); 618 if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) { 619 SwitchRange* r; 620 int nsing = 0; 621 for( r = lo; r <= hi; r++ ) { 622 if( r->is_singleton() ) nsing++; 623 } 624 tty->print(">>> "); 625 _method->print_short_name(); 626 tty->print_cr(" switch decision tree"); 627 tty->print_cr(" %d ranges (%d singletons), max_depth=%d, est_depth=%d", 628 hi-lo+1, nsing, _max_switch_depth, _est_switch_depth); 629 if (_max_switch_depth > _est_switch_depth) { 630 tty->print_cr("******** BAD SWITCH DEPTH ********"); 631 } 632 tty->print(" "); 633 for( r = lo; r <= hi; r++ ) { 634 r->print(env()); 635 } 636 tty->print_cr(""); 637 } 638#endif 639} 640 641void Parse::modf() { 642 Node *f2 = pop(); 643 Node *f1 = pop(); 644 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::modf_Type(), 645 CAST_FROM_FN_PTR(address, SharedRuntime::frem), 646 "frem", NULL, //no memory effects 647 f1, f2); 648 Node* res = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0)); 649 650 push(res); 651} 652 653void Parse::modd() { 654 Node *d2 = pop_pair(); 655 Node *d1 = pop_pair(); 656 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(), 657 CAST_FROM_FN_PTR(address, SharedRuntime::drem), 658 "drem", NULL, //no memory effects 659 d1, top(), d2, top()); 660 Node* res_d = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0)); 661 662#ifdef ASSERT 663 Node* res_top = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 1)); 664 assert(res_top == top(), "second value must be top"); 665#endif 666 667 push_pair(res_d); 668} 669 670void Parse::l2f() { 671 Node* f2 = pop(); 672 Node* f1 = pop(); 673 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(), 674 CAST_FROM_FN_PTR(address, SharedRuntime::l2f), 675 "l2f", NULL, //no memory effects 676 f1, f2); 677 Node* res = _gvn.transform(new (C, 1) ProjNode(c, TypeFunc::Parms + 0)); 678 679 push(res); 680} 681 682void Parse::do_irem() { 683 // Must keep both values on the expression-stack during null-check 684 do_null_check(peek(), T_INT); 685 // Compile-time detect of null-exception? 686 if (stopped()) return; 687 688 Node* b = pop(); 689 Node* a = pop(); 690 691 const Type *t = _gvn.type(b); 692 if (t != Type::TOP) { 693 const TypeInt *ti = t->is_int(); 694 if (ti->is_con()) { 695 int divisor = ti->get_con(); 696 // check for positive power of 2 697 if (divisor > 0 && 698 (divisor & ~(divisor-1)) == divisor) { 699 // yes ! 700 Node *mask = _gvn.intcon((divisor - 1)); 701 // Sigh, must handle negative dividends 702 Node *zero = _gvn.intcon(0); 703 IfNode *ifff = jump_if_fork_int(a, zero, BoolTest::lt); 704 Node *iff = _gvn.transform( new (C, 1) IfFalseNode(ifff) ); 705 Node *ift = _gvn.transform( new (C, 1) IfTrueNode (ifff) ); 706 Node *reg = jump_if_join(ift, iff); 707 Node *phi = PhiNode::make(reg, NULL, TypeInt::INT); 708 // Negative path; negate/and/negate 709 Node *neg = _gvn.transform( new (C, 3) SubINode(zero, a) ); 710 Node *andn= _gvn.transform( new (C, 3) AndINode(neg, mask) ); 711 Node *negn= _gvn.transform( new (C, 3) SubINode(zero, andn) ); 712 phi->init_req(1, negn); 713 // Fast positive case 714 Node *andx = _gvn.transform( new (C, 3) AndINode(a, mask) ); 715 phi->init_req(2, andx); 716 // Push the merge 717 push( _gvn.transform(phi) ); 718 return; 719 } 720 } 721 } 722 // Default case 723 push( _gvn.transform( new (C, 3) ModINode(control(),a,b) ) ); 724} 725 726// Handle jsr and jsr_w bytecode 727void Parse::do_jsr() { 728 assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode"); 729 730 // Store information about current state, tagged with new _jsr_bci 731 int return_bci = iter().next_bci(); 732 int jsr_bci = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest(); 733 734 // Update method data 735 profile_taken_branch(jsr_bci); 736 737 // The way we do things now, there is only one successor block 738 // for the jsr, because the target code is cloned by ciTypeFlow. 739 Block* target = successor_for_bci(jsr_bci); 740 741 // What got pushed? 742 const Type* ret_addr = target->peek(); 743 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)"); 744 745 // Effect on jsr on stack 746 push(_gvn.makecon(ret_addr)); 747 748 // Flow to the jsr. 749 merge(jsr_bci); 750} 751 752// Handle ret bytecode 753void Parse::do_ret() { 754 // Find to whom we return. 755#if 0 // %%%% MAKE THIS WORK 756 Node* con = local(); 757 const TypePtr* tp = con->bottom_type()->isa_ptr(); 758 assert(tp && tp->singleton(), ""); 759 int return_bci = (int) tp->get_con(); 760 merge(return_bci); 761#else 762 assert(block()->num_successors() == 1, "a ret can only go one place now"); 763 Block* target = block()->successor_at(0); 764 assert(!target->is_ready(), "our arrival must be expected"); 765 profile_ret(target->flow()->start()); 766 int pnum = target->next_path_num(); 767 merge_common(target, pnum); 768#endif 769} 770 771//--------------------------dynamic_branch_prediction-------------------------- 772// Try to gather dynamic branch prediction behavior. Return a probability 773// of the branch being taken and set the "cnt" field. Returns a -1.0 774// if we need to use static prediction for some reason. 775float Parse::dynamic_branch_prediction(float &cnt) { 776 ResourceMark rm; 777 778 cnt = COUNT_UNKNOWN; 779 780 // Use MethodData information if it is available 781 // FIXME: free the ProfileData structure 782 ciMethodData* methodData = method()->method_data(); 783 if (!methodData->is_mature()) return PROB_UNKNOWN; 784 ciProfileData* data = methodData->bci_to_data(bci()); 785 if (!data->is_JumpData()) return PROB_UNKNOWN; 786 787 // get taken and not taken values 788 int taken = data->as_JumpData()->taken(); 789 int not_taken = 0; 790 if (data->is_BranchData()) { 791 not_taken = data->as_BranchData()->not_taken(); 792 } 793 794 // scale the counts to be commensurate with invocation counts: 795 taken = method()->scale_count(taken); 796 not_taken = method()->scale_count(not_taken); 797 798 // Give up if too few counts to be meaningful 799 if (taken + not_taken < 40) { 800 if (C->log() != NULL) { 801 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken); 802 } 803 return PROB_UNKNOWN; 804 } 805 806 // Compute frequency that we arrive here 807 int sum = taken + not_taken; 808 // Adjust, if this block is a cloned private block but the 809 // Jump counts are shared. Taken the private counts for 810 // just this path instead of the shared counts. 811 if( block()->count() > 0 ) 812 sum = block()->count(); 813 cnt = (float)sum / (float)FreqCountInvocations; 814 815 // Pin probability to sane limits 816 float prob; 817 if( !taken ) 818 prob = (0+PROB_MIN) / 2; 819 else if( !not_taken ) 820 prob = (1+PROB_MAX) / 2; 821 else { // Compute probability of true path 822 prob = (float)taken / (float)(taken + not_taken); 823 if (prob > PROB_MAX) prob = PROB_MAX; 824 if (prob < PROB_MIN) prob = PROB_MIN; 825 } 826 827 assert((cnt > 0.0f) && (prob > 0.0f), 828 "Bad frequency assignment in if"); 829 830 if (C->log() != NULL) { 831 const char* prob_str = NULL; 832 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always"; 833 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never"; 834 char prob_str_buf[30]; 835 if (prob_str == NULL) { 836 sprintf(prob_str_buf, "%g", prob); 837 prob_str = prob_str_buf; 838 } 839 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%g' prob='%s'", 840 iter().get_dest(), taken, not_taken, cnt, prob_str); 841 } 842 return prob; 843} 844 845//-----------------------------branch_prediction------------------------------- 846float Parse::branch_prediction(float& cnt, 847 BoolTest::mask btest, 848 int target_bci) { 849 float prob = dynamic_branch_prediction(cnt); 850 // If prob is unknown, switch to static prediction 851 if (prob != PROB_UNKNOWN) return prob; 852 853 prob = PROB_FAIR; // Set default value 854 if (btest == BoolTest::eq) // Exactly equal test? 855 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent 856 else if (btest == BoolTest::ne) 857 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent 858 859 // If this is a conditional test guarding a backwards branch, 860 // assume its a loop-back edge. Make it a likely taken branch. 861 if (target_bci < bci()) { 862 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt 863 // Since it's an OSR, we probably have profile data, but since 864 // branch_prediction returned PROB_UNKNOWN, the counts are too small. 865 // Let's make a special check here for completely zero counts. 866 ciMethodData* methodData = method()->method_data(); 867 if (!methodData->is_empty()) { 868 ciProfileData* data = methodData->bci_to_data(bci()); 869 // Only stop for truly zero counts, which mean an unknown part 870 // of the OSR-ed method, and we want to deopt to gather more stats. 871 // If you have ANY counts, then this loop is simply 'cold' relative 872 // to the OSR loop. 873 if (data->as_BranchData()->taken() + 874 data->as_BranchData()->not_taken() == 0 ) { 875 // This is the only way to return PROB_UNKNOWN: 876 return PROB_UNKNOWN; 877 } 878 } 879 } 880 prob = PROB_STATIC_FREQUENT; // Likely to take backwards branch 881 } 882 883 assert(prob != PROB_UNKNOWN, "must have some guess at this point"); 884 return prob; 885} 886 887// The magic constants are chosen so as to match the output of 888// branch_prediction() when the profile reports a zero taken count. 889// It is important to distinguish zero counts unambiguously, because 890// some branches (e.g., _213_javac.Assembler.eliminate) validly produce 891// very small but nonzero probabilities, which if confused with zero 892// counts would keep the program recompiling indefinitely. 893bool Parse::seems_never_taken(float prob) { 894 return prob < PROB_MIN; 895} 896 897// True if the comparison seems to be the kind that will not change its 898// statistics from true to false. See comments in adjust_map_after_if. 899// This question is only asked along paths which are already 900// classifed as untaken (by seems_never_taken), so really, 901// if a path is never taken, its controlling comparison is 902// already acting in a stable fashion. If the comparison 903// seems stable, we will put an expensive uncommon trap 904// on the untaken path. To be conservative, and to allow 905// partially executed counted loops to be compiled fully, 906// we will plant uncommon traps only after pointer comparisons. 907bool Parse::seems_stable_comparison(BoolTest::mask btest, Node* cmp) { 908 for (int depth = 4; depth > 0; depth--) { 909 // The following switch can find CmpP here over half the time for 910 // dynamic language code rich with type tests. 911 // Code using counted loops or array manipulations (typical 912 // of benchmarks) will have many (>80%) CmpI instructions. 913 switch (cmp->Opcode()) { 914 case Op_CmpP: 915 // A never-taken null check looks like CmpP/BoolTest::eq. 916 // These certainly should be closed off as uncommon traps. 917 if (btest == BoolTest::eq) 918 return true; 919 // A never-failed type check looks like CmpP/BoolTest::ne. 920 // Let's put traps on those, too, so that we don't have to compile 921 // unused paths with indeterminate dynamic type information. 922 if (ProfileDynamicTypes) 923 return true; 924 return false; 925 926 case Op_CmpI: 927 // A small minority (< 10%) of CmpP are masked as CmpI, 928 // as if by boolean conversion ((p == q? 1: 0) != 0). 929 // Detect that here, even if it hasn't optimized away yet. 930 // Specifically, this covers the 'instanceof' operator. 931 if (btest == BoolTest::ne || btest == BoolTest::eq) { 932 if (_gvn.type(cmp->in(2))->singleton() && 933 cmp->in(1)->is_Phi()) { 934 PhiNode* phi = cmp->in(1)->as_Phi(); 935 int true_path = phi->is_diamond_phi(); 936 if (true_path > 0 && 937 _gvn.type(phi->in(1))->singleton() && 938 _gvn.type(phi->in(2))->singleton()) { 939 // phi->region->if_proj->ifnode->bool->cmp 940 BoolNode* bol = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); 941 btest = bol->_test._test; 942 cmp = bol->in(1); 943 continue; 944 } 945 } 946 } 947 return false; 948 } 949 } 950 return false; 951} 952 953//-------------------------------repush_if_args-------------------------------- 954// Push arguments of an "if" bytecode back onto the stack by adjusting _sp. 955inline int Parse::repush_if_args() { 956#ifndef PRODUCT 957 if (PrintOpto && WizardMode) { 958 tty->print("defending against excessive implicit null exceptions on %s @%d in ", 959 Bytecodes::name(iter().cur_bc()), iter().cur_bci()); 960 method()->print_name(); tty->cr(); 961 } 962#endif 963 int bc_depth = - Bytecodes::depth(iter().cur_bc()); 964 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches"); 965 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms 966 assert(argument(0) != NULL, "must exist"); 967 assert(bc_depth == 1 || argument(1) != NULL, "two must exist"); 968 _sp += bc_depth; 969 return bc_depth; 970} 971 972//----------------------------------do_ifnull---------------------------------- 973void Parse::do_ifnull(BoolTest::mask btest, Node *c) { 974 int target_bci = iter().get_dest(); 975 976 Block* branch_block = successor_for_bci(target_bci); 977 Block* next_block = successor_for_bci(iter().next_bci()); 978 979 float cnt; 980 float prob = branch_prediction(cnt, btest, target_bci); 981 if (prob == PROB_UNKNOWN) { 982 // (An earlier version of do_ifnull omitted this trap for OSR methods.) 983#ifndef PRODUCT 984 if (PrintOpto && Verbose) 985 tty->print_cr("Never-taken edge stops compilation at bci %d",bci()); 986#endif 987 repush_if_args(); // to gather stats on loop 988 // We need to mark this branch as taken so that if we recompile we will 989 // see that it is possible. In the tiered system the interpreter doesn't 990 // do profiling and by the time we get to the lower tier from the interpreter 991 // the path may be cold again. Make sure it doesn't look untaken 992 profile_taken_branch(target_bci, !ProfileInterpreter); 993 uncommon_trap(Deoptimization::Reason_unreached, 994 Deoptimization::Action_reinterpret, 995 NULL, "cold"); 996 if (EliminateAutoBox) { 997 // Mark the successor blocks as parsed 998 branch_block->next_path_num(); 999 next_block->next_path_num(); 1000 } 1001 return; 1002 } 1003 1004 explicit_null_checks_inserted++; 1005 1006 // Generate real control flow 1007 Node *tst = _gvn.transform( new (C, 2) BoolNode( c, btest ) ); 1008 1009 // Sanity check the probability value 1010 assert(prob > 0.0f,"Bad probability in Parser"); 1011 // Need xform to put node in hash table 1012 IfNode *iff = create_and_xform_if( control(), tst, prob, cnt ); 1013 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 1014 // True branch 1015 { PreserveJVMState pjvms(this); 1016 Node* iftrue = _gvn.transform( new (C, 1) IfTrueNode (iff) ); 1017 set_control(iftrue); 1018 1019 if (stopped()) { // Path is dead? 1020 explicit_null_checks_elided++; 1021 if (EliminateAutoBox) { 1022 // Mark the successor block as parsed 1023 branch_block->next_path_num(); 1024 } 1025 } else { // Path is live. 1026 // Update method data 1027 profile_taken_branch(target_bci); 1028 adjust_map_after_if(btest, c, prob, branch_block, next_block); 1029 if (!stopped()) { 1030 merge(target_bci); 1031 } 1032 } 1033 } 1034 1035 // False branch 1036 Node* iffalse = _gvn.transform( new (C, 1) IfFalseNode(iff) ); 1037 set_control(iffalse); 1038 1039 if (stopped()) { // Path is dead? 1040 explicit_null_checks_elided++; 1041 if (EliminateAutoBox) { 1042 // Mark the successor block as parsed 1043 next_block->next_path_num(); 1044 } 1045 } else { // Path is live. 1046 // Update method data 1047 profile_not_taken_branch(); 1048 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, 1049 next_block, branch_block); 1050 } 1051} 1052 1053//------------------------------------do_if------------------------------------ 1054void Parse::do_if(BoolTest::mask btest, Node* c) { 1055 int target_bci = iter().get_dest(); 1056 1057 Block* branch_block = successor_for_bci(target_bci); 1058 Block* next_block = successor_for_bci(iter().next_bci()); 1059 1060 float cnt; 1061 float prob = branch_prediction(cnt, btest, target_bci); 1062 float untaken_prob = 1.0 - prob; 1063 1064 if (prob == PROB_UNKNOWN) { 1065#ifndef PRODUCT 1066 if (PrintOpto && Verbose) 1067 tty->print_cr("Never-taken edge stops compilation at bci %d",bci()); 1068#endif 1069 repush_if_args(); // to gather stats on loop 1070 // We need to mark this branch as taken so that if we recompile we will 1071 // see that it is possible. In the tiered system the interpreter doesn't 1072 // do profiling and by the time we get to the lower tier from the interpreter 1073 // the path may be cold again. Make sure it doesn't look untaken 1074 profile_taken_branch(target_bci, !ProfileInterpreter); 1075 uncommon_trap(Deoptimization::Reason_unreached, 1076 Deoptimization::Action_reinterpret, 1077 NULL, "cold"); 1078 if (EliminateAutoBox) { 1079 // Mark the successor blocks as parsed 1080 branch_block->next_path_num(); 1081 next_block->next_path_num(); 1082 } 1083 return; 1084 } 1085 1086 // Sanity check the probability value 1087 assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser"); 1088 1089 bool taken_if_true = true; 1090 // Convert BoolTest to canonical form: 1091 if (!BoolTest(btest).is_canonical()) { 1092 btest = BoolTest(btest).negate(); 1093 taken_if_true = false; 1094 // prob is NOT updated here; it remains the probability of the taken 1095 // path (as opposed to the prob of the path guarded by an 'IfTrueNode'). 1096 } 1097 assert(btest != BoolTest::eq, "!= is the only canonical exact test"); 1098 1099 Node* tst0 = new (C, 2) BoolNode(c, btest); 1100 Node* tst = _gvn.transform(tst0); 1101 BoolTest::mask taken_btest = BoolTest::illegal; 1102 BoolTest::mask untaken_btest = BoolTest::illegal; 1103 1104 if (tst->is_Bool()) { 1105 // Refresh c from the transformed bool node, since it may be 1106 // simpler than the original c. Also re-canonicalize btest. 1107 // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)). 1108 // That can arise from statements like: if (x instanceof C) ... 1109 if (tst != tst0) { 1110 // Canonicalize one more time since transform can change it. 1111 btest = tst->as_Bool()->_test._test; 1112 if (!BoolTest(btest).is_canonical()) { 1113 // Reverse edges one more time... 1114 tst = _gvn.transform( tst->as_Bool()->negate(&_gvn) ); 1115 btest = tst->as_Bool()->_test._test; 1116 assert(BoolTest(btest).is_canonical(), "sanity"); 1117 taken_if_true = !taken_if_true; 1118 } 1119 c = tst->in(1); 1120 } 1121 BoolTest::mask neg_btest = BoolTest(btest).negate(); 1122 taken_btest = taken_if_true ? btest : neg_btest; 1123 untaken_btest = taken_if_true ? neg_btest : btest; 1124 } 1125 1126 // Generate real control flow 1127 float true_prob = (taken_if_true ? prob : untaken_prob); 1128 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt); 1129 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 1130 Node* taken_branch = new (C, 1) IfTrueNode(iff); 1131 Node* untaken_branch = new (C, 1) IfFalseNode(iff); 1132 if (!taken_if_true) { // Finish conversion to canonical form 1133 Node* tmp = taken_branch; 1134 taken_branch = untaken_branch; 1135 untaken_branch = tmp; 1136 } 1137 1138 // Branch is taken: 1139 { PreserveJVMState pjvms(this); 1140 taken_branch = _gvn.transform(taken_branch); 1141 set_control(taken_branch); 1142 1143 if (stopped()) { 1144 if (EliminateAutoBox) { 1145 // Mark the successor block as parsed 1146 branch_block->next_path_num(); 1147 } 1148 } else { 1149 // Update method data 1150 profile_taken_branch(target_bci); 1151 adjust_map_after_if(taken_btest, c, prob, branch_block, next_block); 1152 if (!stopped()) { 1153 merge(target_bci); 1154 } 1155 } 1156 } 1157 1158 untaken_branch = _gvn.transform(untaken_branch); 1159 set_control(untaken_branch); 1160 1161 // Branch not taken. 1162 if (stopped()) { 1163 if (EliminateAutoBox) { 1164 // Mark the successor block as parsed 1165 next_block->next_path_num(); 1166 } 1167 } else { 1168 // Update method data 1169 profile_not_taken_branch(); 1170 adjust_map_after_if(untaken_btest, c, untaken_prob, 1171 next_block, branch_block); 1172 } 1173} 1174 1175//----------------------------adjust_map_after_if------------------------------ 1176// Adjust the JVM state to reflect the result of taking this path. 1177// Basically, it means inspecting the CmpNode controlling this 1178// branch, seeing how it constrains a tested value, and then 1179// deciding if it's worth our while to encode this constraint 1180// as graph nodes in the current abstract interpretation map. 1181void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, 1182 Block* path, Block* other_path) { 1183 if (stopped() || !c->is_Cmp() || btest == BoolTest::illegal) 1184 return; // nothing to do 1185 1186 bool is_fallthrough = (path == successor_for_bci(iter().next_bci())); 1187 1188 if (seems_never_taken(prob) && seems_stable_comparison(btest, c)) { 1189 // If this might possibly turn into an implicit null check, 1190 // and the null has never yet been seen, we need to generate 1191 // an uncommon trap, so as to recompile instead of suffering 1192 // with very slow branches. (We'll get the slow branches if 1193 // the program ever changes phase and starts seeing nulls here.) 1194 // 1195 // We do not inspect for a null constant, since a node may 1196 // optimize to 'null' later on. 1197 // 1198 // Null checks, and other tests which expect inequality, 1199 // show btest == BoolTest::eq along the non-taken branch. 1200 // On the other hand, type tests, must-be-null tests, 1201 // and other tests which expect pointer equality, 1202 // show btest == BoolTest::ne along the non-taken branch. 1203 // We prune both types of branches if they look unused. 1204 repush_if_args(); 1205 // We need to mark this branch as taken so that if we recompile we will 1206 // see that it is possible. In the tiered system the interpreter doesn't 1207 // do profiling and by the time we get to the lower tier from the interpreter 1208 // the path may be cold again. Make sure it doesn't look untaken 1209 if (is_fallthrough) { 1210 profile_not_taken_branch(!ProfileInterpreter); 1211 } else { 1212 profile_taken_branch(iter().get_dest(), !ProfileInterpreter); 1213 } 1214 uncommon_trap(Deoptimization::Reason_unreached, 1215 Deoptimization::Action_reinterpret, 1216 NULL, 1217 (is_fallthrough ? "taken always" : "taken never")); 1218 return; 1219 } 1220 1221 Node* val = c->in(1); 1222 Node* con = c->in(2); 1223 const Type* tcon = _gvn.type(con); 1224 const Type* tval = _gvn.type(val); 1225 bool have_con = tcon->singleton(); 1226 if (tval->singleton()) { 1227 if (!have_con) { 1228 // Swap, so constant is in con. 1229 con = val; 1230 tcon = tval; 1231 val = c->in(2); 1232 tval = _gvn.type(val); 1233 btest = BoolTest(btest).commute(); 1234 have_con = true; 1235 } else { 1236 // Do we have two constants? Then leave well enough alone. 1237 have_con = false; 1238 } 1239 } 1240 if (!have_con) // remaining adjustments need a con 1241 return; 1242 1243 1244 int val_in_map = map()->find_edge(val); 1245 if (val_in_map < 0) return; // replace_in_map would be useless 1246 { 1247 JVMState* jvms = this->jvms(); 1248 if (!(jvms->is_loc(val_in_map) || 1249 jvms->is_stk(val_in_map))) 1250 return; // again, it would be useless 1251 } 1252 1253 // Check for a comparison to a constant, and "know" that the compared 1254 // value is constrained on this path. 1255 assert(tcon->singleton(), ""); 1256 ConstraintCastNode* ccast = NULL; 1257 Node* cast = NULL; 1258 1259 switch (btest) { 1260 case BoolTest::eq: // Constant test? 1261 { 1262 const Type* tboth = tcon->join(tval); 1263 if (tboth == tval) break; // Nothing to gain. 1264 if (tcon->isa_int()) { 1265 ccast = new (C, 2) CastIINode(val, tboth); 1266 } else if (tcon == TypePtr::NULL_PTR) { 1267 // Cast to null, but keep the pointer identity temporarily live. 1268 ccast = new (C, 2) CastPPNode(val, tboth); 1269 } else { 1270 const TypeF* tf = tcon->isa_float_constant(); 1271 const TypeD* td = tcon->isa_double_constant(); 1272 // Exclude tests vs float/double 0 as these could be 1273 // either +0 or -0. Just because you are equal to +0 1274 // doesn't mean you ARE +0! 1275 if ((!tf || tf->_f != 0.0) && 1276 (!td || td->_d != 0.0)) 1277 cast = con; // Replace non-constant val by con. 1278 } 1279 } 1280 break; 1281 1282 case BoolTest::ne: 1283 if (tcon == TypePtr::NULL_PTR) { 1284 cast = cast_not_null(val, false); 1285 } 1286 break; 1287 1288 default: 1289 // (At this point we could record int range types with CastII.) 1290 break; 1291 } 1292 1293 if (ccast != NULL) { 1294 const Type* tcc = ccast->as_Type()->type(); 1295 assert(tcc != tval && tcc->higher_equal(tval), "must improve"); 1296 // Delay transform() call to allow recovery of pre-cast value 1297 // at the control merge. 1298 ccast->set_req(0, control()); 1299 _gvn.set_type_bottom(ccast); 1300 record_for_igvn(ccast); 1301 cast = ccast; 1302 } 1303 1304 if (cast != NULL) { // Here's the payoff. 1305 replace_in_map(val, cast); 1306 } 1307} 1308 1309 1310//------------------------------do_one_bytecode-------------------------------- 1311// Parse this bytecode, and alter the Parsers JVM->Node mapping 1312void Parse::do_one_bytecode() { 1313 Node *a, *b, *c, *d; // Handy temps 1314 BoolTest::mask btest; 1315 int i; 1316 1317 assert(!has_exceptions(), "bytecode entry state must be clear of throws"); 1318 1319 if (C->check_node_count(NodeLimitFudgeFactor * 5, 1320 "out of nodes parsing method")) { 1321 return; 1322 } 1323 1324#ifdef ASSERT 1325 // for setting breakpoints 1326 if (TraceOptoParse) { 1327 tty->print(" @"); 1328 dump_bci(bci()); 1329 } 1330#endif 1331 1332 switch (bc()) { 1333 case Bytecodes::_nop: 1334 // do nothing 1335 break; 1336 case Bytecodes::_lconst_0: 1337 push_pair(longcon(0)); 1338 break; 1339 1340 case Bytecodes::_lconst_1: 1341 push_pair(longcon(1)); 1342 break; 1343 1344 case Bytecodes::_fconst_0: 1345 push(zerocon(T_FLOAT)); 1346 break; 1347 1348 case Bytecodes::_fconst_1: 1349 push(makecon(TypeF::ONE)); 1350 break; 1351 1352 case Bytecodes::_fconst_2: 1353 push(makecon(TypeF::make(2.0f))); 1354 break; 1355 1356 case Bytecodes::_dconst_0: 1357 push_pair(zerocon(T_DOUBLE)); 1358 break; 1359 1360 case Bytecodes::_dconst_1: 1361 push_pair(makecon(TypeD::ONE)); 1362 break; 1363 1364 case Bytecodes::_iconst_m1:push(intcon(-1)); break; 1365 case Bytecodes::_iconst_0: push(intcon( 0)); break; 1366 case Bytecodes::_iconst_1: push(intcon( 1)); break; 1367 case Bytecodes::_iconst_2: push(intcon( 2)); break; 1368 case Bytecodes::_iconst_3: push(intcon( 3)); break; 1369 case Bytecodes::_iconst_4: push(intcon( 4)); break; 1370 case Bytecodes::_iconst_5: push(intcon( 5)); break; 1371 case Bytecodes::_bipush: push(intcon(iter().get_constant_u1())); break; 1372 case Bytecodes::_sipush: push(intcon(iter().get_constant_u2())); break; 1373 case Bytecodes::_aconst_null: push(null()); break; 1374 case Bytecodes::_ldc: 1375 case Bytecodes::_ldc_w: 1376 case Bytecodes::_ldc2_w: 1377 // If the constant is unresolved, run this BC once in the interpreter. 1378 { 1379 ciConstant constant = iter().get_constant(); 1380 if (constant.basic_type() == T_OBJECT && 1381 !constant.as_object()->is_loaded()) { 1382 int index = iter().get_constant_pool_index(); 1383 constantTag tag = iter().get_constant_pool_tag(index); 1384 uncommon_trap(Deoptimization::make_trap_request 1385 (Deoptimization::Reason_unloaded, 1386 Deoptimization::Action_reinterpret, 1387 index), 1388 NULL, tag.internal_name()); 1389 break; 1390 } 1391 assert(constant.basic_type() != T_OBJECT || !constant.as_object()->is_klass(), 1392 "must be java_mirror of klass"); 1393 bool pushed = push_constant(constant, true); 1394 guarantee(pushed, "must be possible to push this constant"); 1395 } 1396 1397 break; 1398 1399 case Bytecodes::_aload_0: 1400 push( local(0) ); 1401 break; 1402 case Bytecodes::_aload_1: 1403 push( local(1) ); 1404 break; 1405 case Bytecodes::_aload_2: 1406 push( local(2) ); 1407 break; 1408 case Bytecodes::_aload_3: 1409 push( local(3) ); 1410 break; 1411 case Bytecodes::_aload: 1412 push( local(iter().get_index()) ); 1413 break; 1414 1415 case Bytecodes::_fload_0: 1416 case Bytecodes::_iload_0: 1417 push( local(0) ); 1418 break; 1419 case Bytecodes::_fload_1: 1420 case Bytecodes::_iload_1: 1421 push( local(1) ); 1422 break; 1423 case Bytecodes::_fload_2: 1424 case Bytecodes::_iload_2: 1425 push( local(2) ); 1426 break; 1427 case Bytecodes::_fload_3: 1428 case Bytecodes::_iload_3: 1429 push( local(3) ); 1430 break; 1431 case Bytecodes::_fload: 1432 case Bytecodes::_iload: 1433 push( local(iter().get_index()) ); 1434 break; 1435 case Bytecodes::_lload_0: 1436 push_pair_local( 0 ); 1437 break; 1438 case Bytecodes::_lload_1: 1439 push_pair_local( 1 ); 1440 break; 1441 case Bytecodes::_lload_2: 1442 push_pair_local( 2 ); 1443 break; 1444 case Bytecodes::_lload_3: 1445 push_pair_local( 3 ); 1446 break; 1447 case Bytecodes::_lload: 1448 push_pair_local( iter().get_index() ); 1449 break; 1450 1451 case Bytecodes::_dload_0: 1452 push_pair_local(0); 1453 break; 1454 case Bytecodes::_dload_1: 1455 push_pair_local(1); 1456 break; 1457 case Bytecodes::_dload_2: 1458 push_pair_local(2); 1459 break; 1460 case Bytecodes::_dload_3: 1461 push_pair_local(3); 1462 break; 1463 case Bytecodes::_dload: 1464 push_pair_local(iter().get_index()); 1465 break; 1466 case Bytecodes::_fstore_0: 1467 case Bytecodes::_istore_0: 1468 case Bytecodes::_astore_0: 1469 set_local( 0, pop() ); 1470 break; 1471 case Bytecodes::_fstore_1: 1472 case Bytecodes::_istore_1: 1473 case Bytecodes::_astore_1: 1474 set_local( 1, pop() ); 1475 break; 1476 case Bytecodes::_fstore_2: 1477 case Bytecodes::_istore_2: 1478 case Bytecodes::_astore_2: 1479 set_local( 2, pop() ); 1480 break; 1481 case Bytecodes::_fstore_3: 1482 case Bytecodes::_istore_3: 1483 case Bytecodes::_astore_3: 1484 set_local( 3, pop() ); 1485 break; 1486 case Bytecodes::_fstore: 1487 case Bytecodes::_istore: 1488 case Bytecodes::_astore: 1489 set_local( iter().get_index(), pop() ); 1490 break; 1491 // long stores 1492 case Bytecodes::_lstore_0: 1493 set_pair_local( 0, pop_pair() ); 1494 break; 1495 case Bytecodes::_lstore_1: 1496 set_pair_local( 1, pop_pair() ); 1497 break; 1498 case Bytecodes::_lstore_2: 1499 set_pair_local( 2, pop_pair() ); 1500 break; 1501 case Bytecodes::_lstore_3: 1502 set_pair_local( 3, pop_pair() ); 1503 break; 1504 case Bytecodes::_lstore: 1505 set_pair_local( iter().get_index(), pop_pair() ); 1506 break; 1507 1508 // double stores 1509 case Bytecodes::_dstore_0: 1510 set_pair_local( 0, dstore_rounding(pop_pair()) ); 1511 break; 1512 case Bytecodes::_dstore_1: 1513 set_pair_local( 1, dstore_rounding(pop_pair()) ); 1514 break; 1515 case Bytecodes::_dstore_2: 1516 set_pair_local( 2, dstore_rounding(pop_pair()) ); 1517 break; 1518 case Bytecodes::_dstore_3: 1519 set_pair_local( 3, dstore_rounding(pop_pair()) ); 1520 break; 1521 case Bytecodes::_dstore: 1522 set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) ); 1523 break; 1524 1525 case Bytecodes::_pop: _sp -= 1; break; 1526 case Bytecodes::_pop2: _sp -= 2; break; 1527 case Bytecodes::_swap: 1528 a = pop(); 1529 b = pop(); 1530 push(a); 1531 push(b); 1532 break; 1533 case Bytecodes::_dup: 1534 a = pop(); 1535 push(a); 1536 push(a); 1537 break; 1538 case Bytecodes::_dup_x1: 1539 a = pop(); 1540 b = pop(); 1541 push( a ); 1542 push( b ); 1543 push( a ); 1544 break; 1545 case Bytecodes::_dup_x2: 1546 a = pop(); 1547 b = pop(); 1548 c = pop(); 1549 push( a ); 1550 push( c ); 1551 push( b ); 1552 push( a ); 1553 break; 1554 case Bytecodes::_dup2: 1555 a = pop(); 1556 b = pop(); 1557 push( b ); 1558 push( a ); 1559 push( b ); 1560 push( a ); 1561 break; 1562 1563 case Bytecodes::_dup2_x1: 1564 // before: .. c, b, a 1565 // after: .. b, a, c, b, a 1566 // not tested 1567 a = pop(); 1568 b = pop(); 1569 c = pop(); 1570 push( b ); 1571 push( a ); 1572 push( c ); 1573 push( b ); 1574 push( a ); 1575 break; 1576 case Bytecodes::_dup2_x2: 1577 // before: .. d, c, b, a 1578 // after: .. b, a, d, c, b, a 1579 // not tested 1580 a = pop(); 1581 b = pop(); 1582 c = pop(); 1583 d = pop(); 1584 push( b ); 1585 push( a ); 1586 push( d ); 1587 push( c ); 1588 push( b ); 1589 push( a ); 1590 break; 1591 1592 case Bytecodes::_arraylength: { 1593 // Must do null-check with value on expression stack 1594 Node *ary = do_null_check(peek(), T_ARRAY); 1595 // Compile-time detect of null-exception? 1596 if (stopped()) return; 1597 a = pop(); 1598 push(load_array_length(a)); 1599 break; 1600 } 1601 1602 case Bytecodes::_baload: array_load(T_BYTE); break; 1603 case Bytecodes::_caload: array_load(T_CHAR); break; 1604 case Bytecodes::_iaload: array_load(T_INT); break; 1605 case Bytecodes::_saload: array_load(T_SHORT); break; 1606 case Bytecodes::_faload: array_load(T_FLOAT); break; 1607 case Bytecodes::_aaload: array_load(T_OBJECT); break; 1608 case Bytecodes::_laload: { 1609 a = array_addressing(T_LONG, 0); 1610 if (stopped()) return; // guaranteed null or range check 1611 _sp -= 2; // Pop array and index 1612 push_pair( make_load(control(), a, TypeLong::LONG, T_LONG, TypeAryPtr::LONGS)); 1613 break; 1614 } 1615 case Bytecodes::_daload: { 1616 a = array_addressing(T_DOUBLE, 0); 1617 if (stopped()) return; // guaranteed null or range check 1618 _sp -= 2; // Pop array and index 1619 push_pair( make_load(control(), a, Type::DOUBLE, T_DOUBLE, TypeAryPtr::DOUBLES)); 1620 break; 1621 } 1622 case Bytecodes::_bastore: array_store(T_BYTE); break; 1623 case Bytecodes::_castore: array_store(T_CHAR); break; 1624 case Bytecodes::_iastore: array_store(T_INT); break; 1625 case Bytecodes::_sastore: array_store(T_SHORT); break; 1626 case Bytecodes::_fastore: array_store(T_FLOAT); break; 1627 case Bytecodes::_aastore: { 1628 d = array_addressing(T_OBJECT, 1); 1629 if (stopped()) return; // guaranteed null or range check 1630 array_store_check(); 1631 c = pop(); // Oop to store 1632 b = pop(); // index (already used) 1633 a = pop(); // the array itself 1634 const TypeOopPtr* elemtype = _gvn.type(a)->is_aryptr()->elem()->make_oopptr(); 1635 const TypeAryPtr* adr_type = TypeAryPtr::OOPS; 1636 Node* store = store_oop_to_array(control(), a, d, adr_type, c, elemtype, T_OBJECT); 1637 break; 1638 } 1639 case Bytecodes::_lastore: { 1640 a = array_addressing(T_LONG, 2); 1641 if (stopped()) return; // guaranteed null or range check 1642 c = pop_pair(); 1643 _sp -= 2; // Pop array and index 1644 store_to_memory(control(), a, c, T_LONG, TypeAryPtr::LONGS); 1645 break; 1646 } 1647 case Bytecodes::_dastore: { 1648 a = array_addressing(T_DOUBLE, 2); 1649 if (stopped()) return; // guaranteed null or range check 1650 c = pop_pair(); 1651 _sp -= 2; // Pop array and index 1652 c = dstore_rounding(c); 1653 store_to_memory(control(), a, c, T_DOUBLE, TypeAryPtr::DOUBLES); 1654 break; 1655 } 1656 case Bytecodes::_getfield: 1657 do_getfield(); 1658 break; 1659 1660 case Bytecodes::_getstatic: 1661 do_getstatic(); 1662 break; 1663 1664 case Bytecodes::_putfield: 1665 do_putfield(); 1666 break; 1667 1668 case Bytecodes::_putstatic: 1669 do_putstatic(); 1670 break; 1671 1672 case Bytecodes::_irem: 1673 do_irem(); 1674 break; 1675 case Bytecodes::_idiv: 1676 // Must keep both values on the expression-stack during null-check 1677 do_null_check(peek(), T_INT); 1678 // Compile-time detect of null-exception? 1679 if (stopped()) return; 1680 b = pop(); 1681 a = pop(); 1682 push( _gvn.transform( new (C, 3) DivINode(control(),a,b) ) ); 1683 break; 1684 case Bytecodes::_imul: 1685 b = pop(); a = pop(); 1686 push( _gvn.transform( new (C, 3) MulINode(a,b) ) ); 1687 break; 1688 case Bytecodes::_iadd: 1689 b = pop(); a = pop(); 1690 push( _gvn.transform( new (C, 3) AddINode(a,b) ) ); 1691 break; 1692 case Bytecodes::_ineg: 1693 a = pop(); 1694 push( _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),a)) ); 1695 break; 1696 case Bytecodes::_isub: 1697 b = pop(); a = pop(); 1698 push( _gvn.transform( new (C, 3) SubINode(a,b) ) ); 1699 break; 1700 case Bytecodes::_iand: 1701 b = pop(); a = pop(); 1702 push( _gvn.transform( new (C, 3) AndINode(a,b) ) ); 1703 break; 1704 case Bytecodes::_ior: 1705 b = pop(); a = pop(); 1706 push( _gvn.transform( new (C, 3) OrINode(a,b) ) ); 1707 break; 1708 case Bytecodes::_ixor: 1709 b = pop(); a = pop(); 1710 push( _gvn.transform( new (C, 3) XorINode(a,b) ) ); 1711 break; 1712 case Bytecodes::_ishl: 1713 b = pop(); a = pop(); 1714 push( _gvn.transform( new (C, 3) LShiftINode(a,b) ) ); 1715 break; 1716 case Bytecodes::_ishr: 1717 b = pop(); a = pop(); 1718 push( _gvn.transform( new (C, 3) RShiftINode(a,b) ) ); 1719 break; 1720 case Bytecodes::_iushr: 1721 b = pop(); a = pop(); 1722 push( _gvn.transform( new (C, 3) URShiftINode(a,b) ) ); 1723 break; 1724 1725 case Bytecodes::_fneg: 1726 a = pop(); 1727 b = _gvn.transform(new (C, 2) NegFNode (a)); 1728 push(b); 1729 break; 1730 1731 case Bytecodes::_fsub: 1732 b = pop(); 1733 a = pop(); 1734 c = _gvn.transform( new (C, 3) SubFNode(a,b) ); 1735 d = precision_rounding(c); 1736 push( d ); 1737 break; 1738 1739 case Bytecodes::_fadd: 1740 b = pop(); 1741 a = pop(); 1742 c = _gvn.transform( new (C, 3) AddFNode(a,b) ); 1743 d = precision_rounding(c); 1744 push( d ); 1745 break; 1746 1747 case Bytecodes::_fmul: 1748 b = pop(); 1749 a = pop(); 1750 c = _gvn.transform( new (C, 3) MulFNode(a,b) ); 1751 d = precision_rounding(c); 1752 push( d ); 1753 break; 1754 1755 case Bytecodes::_fdiv: 1756 b = pop(); 1757 a = pop(); 1758 c = _gvn.transform( new (C, 3) DivFNode(0,a,b) ); 1759 d = precision_rounding(c); 1760 push( d ); 1761 break; 1762 1763 case Bytecodes::_frem: 1764 if (Matcher::has_match_rule(Op_ModF)) { 1765 // Generate a ModF node. 1766 b = pop(); 1767 a = pop(); 1768 c = _gvn.transform( new (C, 3) ModFNode(0,a,b) ); 1769 d = precision_rounding(c); 1770 push( d ); 1771 } 1772 else { 1773 // Generate a call. 1774 modf(); 1775 } 1776 break; 1777 1778 case Bytecodes::_fcmpl: 1779 b = pop(); 1780 a = pop(); 1781 c = _gvn.transform( new (C, 3) CmpF3Node( a, b)); 1782 push(c); 1783 break; 1784 case Bytecodes::_fcmpg: 1785 b = pop(); 1786 a = pop(); 1787 1788 // Same as fcmpl but need to flip the unordered case. Swap the inputs, 1789 // which negates the result sign except for unordered. Flip the unordered 1790 // as well by using CmpF3 which implements unordered-lesser instead of 1791 // unordered-greater semantics. Finally, commute the result bits. Result 1792 // is same as using a CmpF3Greater except we did it with CmpF3 alone. 1793 c = _gvn.transform( new (C, 3) CmpF3Node( b, a)); 1794 c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) ); 1795 push(c); 1796 break; 1797 1798 case Bytecodes::_f2i: 1799 a = pop(); 1800 push(_gvn.transform(new (C, 2) ConvF2INode(a))); 1801 break; 1802 1803 case Bytecodes::_d2i: 1804 a = pop_pair(); 1805 b = _gvn.transform(new (C, 2) ConvD2INode(a)); 1806 push( b ); 1807 break; 1808 1809 case Bytecodes::_f2d: 1810 a = pop(); 1811 b = _gvn.transform( new (C, 2) ConvF2DNode(a)); 1812 push_pair( b ); 1813 break; 1814 1815 case Bytecodes::_d2f: 1816 a = pop_pair(); 1817 b = _gvn.transform( new (C, 2) ConvD2FNode(a)); 1818 // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed) 1819 //b = _gvn.transform(new (C, 2) RoundFloatNode(0, b) ); 1820 push( b ); 1821 break; 1822 1823 case Bytecodes::_l2f: 1824 if (Matcher::convL2FSupported()) { 1825 a = pop_pair(); 1826 b = _gvn.transform( new (C, 2) ConvL2FNode(a)); 1827 // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits. 1828 // Rather than storing the result into an FP register then pushing 1829 // out to memory to round, the machine instruction that implements 1830 // ConvL2D is responsible for rounding. 1831 // c = precision_rounding(b); 1832 c = _gvn.transform(b); 1833 push(c); 1834 } else { 1835 l2f(); 1836 } 1837 break; 1838 1839 case Bytecodes::_l2d: 1840 a = pop_pair(); 1841 b = _gvn.transform( new (C, 2) ConvL2DNode(a)); 1842 // For i486.ad, rounding is always necessary (see _l2f above). 1843 // c = dprecision_rounding(b); 1844 c = _gvn.transform(b); 1845 push_pair(c); 1846 break; 1847 1848 case Bytecodes::_f2l: 1849 a = pop(); 1850 b = _gvn.transform( new (C, 2) ConvF2LNode(a)); 1851 push_pair(b); 1852 break; 1853 1854 case Bytecodes::_d2l: 1855 a = pop_pair(); 1856 b = _gvn.transform( new (C, 2) ConvD2LNode(a)); 1857 push_pair(b); 1858 break; 1859 1860 case Bytecodes::_dsub: 1861 b = pop_pair(); 1862 a = pop_pair(); 1863 c = _gvn.transform( new (C, 3) SubDNode(a,b) ); 1864 d = dprecision_rounding(c); 1865 push_pair( d ); 1866 break; 1867 1868 case Bytecodes::_dadd: 1869 b = pop_pair(); 1870 a = pop_pair(); 1871 c = _gvn.transform( new (C, 3) AddDNode(a,b) ); 1872 d = dprecision_rounding(c); 1873 push_pair( d ); 1874 break; 1875 1876 case Bytecodes::_dmul: 1877 b = pop_pair(); 1878 a = pop_pair(); 1879 c = _gvn.transform( new (C, 3) MulDNode(a,b) ); 1880 d = dprecision_rounding(c); 1881 push_pair( d ); 1882 break; 1883 1884 case Bytecodes::_ddiv: 1885 b = pop_pair(); 1886 a = pop_pair(); 1887 c = _gvn.transform( new (C, 3) DivDNode(0,a,b) ); 1888 d = dprecision_rounding(c); 1889 push_pair( d ); 1890 break; 1891 1892 case Bytecodes::_dneg: 1893 a = pop_pair(); 1894 b = _gvn.transform(new (C, 2) NegDNode (a)); 1895 push_pair(b); 1896 break; 1897 1898 case Bytecodes::_drem: 1899 if (Matcher::has_match_rule(Op_ModD)) { 1900 // Generate a ModD node. 1901 b = pop_pair(); 1902 a = pop_pair(); 1903 // a % b 1904 1905 c = _gvn.transform( new (C, 3) ModDNode(0,a,b) ); 1906 d = dprecision_rounding(c); 1907 push_pair( d ); 1908 } 1909 else { 1910 // Generate a call. 1911 modd(); 1912 } 1913 break; 1914 1915 case Bytecodes::_dcmpl: 1916 b = pop_pair(); 1917 a = pop_pair(); 1918 c = _gvn.transform( new (C, 3) CmpD3Node( a, b)); 1919 push(c); 1920 break; 1921 1922 case Bytecodes::_dcmpg: 1923 b = pop_pair(); 1924 a = pop_pair(); 1925 // Same as dcmpl but need to flip the unordered case. 1926 // Commute the inputs, which negates the result sign except for unordered. 1927 // Flip the unordered as well by using CmpD3 which implements 1928 // unordered-lesser instead of unordered-greater semantics. 1929 // Finally, negate the result bits. Result is same as using a 1930 // CmpD3Greater except we did it with CmpD3 alone. 1931 c = _gvn.transform( new (C, 3) CmpD3Node( b, a)); 1932 c = _gvn.transform( new (C, 3) SubINode(_gvn.intcon(0),c) ); 1933 push(c); 1934 break; 1935 1936 1937 // Note for longs -> lo word is on TOS, hi word is on TOS - 1 1938 case Bytecodes::_land: 1939 b = pop_pair(); 1940 a = pop_pair(); 1941 c = _gvn.transform( new (C, 3) AndLNode(a,b) ); 1942 push_pair(c); 1943 break; 1944 case Bytecodes::_lor: 1945 b = pop_pair(); 1946 a = pop_pair(); 1947 c = _gvn.transform( new (C, 3) OrLNode(a,b) ); 1948 push_pair(c); 1949 break; 1950 case Bytecodes::_lxor: 1951 b = pop_pair(); 1952 a = pop_pair(); 1953 c = _gvn.transform( new (C, 3) XorLNode(a,b) ); 1954 push_pair(c); 1955 break; 1956 1957 case Bytecodes::_lshl: 1958 b = pop(); // the shift count 1959 a = pop_pair(); // value to be shifted 1960 c = _gvn.transform( new (C, 3) LShiftLNode(a,b) ); 1961 push_pair(c); 1962 break; 1963 case Bytecodes::_lshr: 1964 b = pop(); // the shift count 1965 a = pop_pair(); // value to be shifted 1966 c = _gvn.transform( new (C, 3) RShiftLNode(a,b) ); 1967 push_pair(c); 1968 break; 1969 case Bytecodes::_lushr: 1970 b = pop(); // the shift count 1971 a = pop_pair(); // value to be shifted 1972 c = _gvn.transform( new (C, 3) URShiftLNode(a,b) ); 1973 push_pair(c); 1974 break; 1975 case Bytecodes::_lmul: 1976 b = pop_pair(); 1977 a = pop_pair(); 1978 c = _gvn.transform( new (C, 3) MulLNode(a,b) ); 1979 push_pair(c); 1980 break; 1981 1982 case Bytecodes::_lrem: 1983 // Must keep both values on the expression-stack during null-check 1984 assert(peek(0) == top(), "long word order"); 1985 do_null_check(peek(1), T_LONG); 1986 // Compile-time detect of null-exception? 1987 if (stopped()) return; 1988 b = pop_pair(); 1989 a = pop_pair(); 1990 c = _gvn.transform( new (C, 3) ModLNode(control(),a,b) ); 1991 push_pair(c); 1992 break; 1993 1994 case Bytecodes::_ldiv: 1995 // Must keep both values on the expression-stack during null-check 1996 assert(peek(0) == top(), "long word order"); 1997 do_null_check(peek(1), T_LONG); 1998 // Compile-time detect of null-exception? 1999 if (stopped()) return; 2000 b = pop_pair(); 2001 a = pop_pair(); 2002 c = _gvn.transform( new (C, 3) DivLNode(control(),a,b) ); 2003 push_pair(c); 2004 break; 2005 2006 case Bytecodes::_ladd: 2007 b = pop_pair(); 2008 a = pop_pair(); 2009 c = _gvn.transform( new (C, 3) AddLNode(a,b) ); 2010 push_pair(c); 2011 break; 2012 case Bytecodes::_lsub: 2013 b = pop_pair(); 2014 a = pop_pair(); 2015 c = _gvn.transform( new (C, 3) SubLNode(a,b) ); 2016 push_pair(c); 2017 break; 2018 case Bytecodes::_lcmp: 2019 // Safepoints are now inserted _before_ branches. The long-compare 2020 // bytecode painfully produces a 3-way value (-1,0,+1) which requires a 2021 // slew of control flow. These are usually followed by a CmpI vs zero and 2022 // a branch; this pattern then optimizes to the obvious long-compare and 2023 // branch. However, if the branch is backwards there's a Safepoint 2024 // inserted. The inserted Safepoint captures the JVM state at the 2025 // pre-branch point, i.e. it captures the 3-way value. Thus if a 2026 // long-compare is used to control a loop the debug info will force 2027 // computation of the 3-way value, even though the generated code uses a 2028 // long-compare and branch. We try to rectify the situation by inserting 2029 // a SafePoint here and have it dominate and kill the safepoint added at a 2030 // following backwards branch. At this point the JVM state merely holds 2 2031 // longs but not the 3-way value. 2032 if( UseLoopSafepoints ) { 2033 switch( iter().next_bc() ) { 2034 case Bytecodes::_ifgt: 2035 case Bytecodes::_iflt: 2036 case Bytecodes::_ifge: 2037 case Bytecodes::_ifle: 2038 case Bytecodes::_ifne: 2039 case Bytecodes::_ifeq: 2040 // If this is a backwards branch in the bytecodes, add Safepoint 2041 maybe_add_safepoint(iter().next_get_dest()); 2042 } 2043 } 2044 b = pop_pair(); 2045 a = pop_pair(); 2046 c = _gvn.transform( new (C, 3) CmpL3Node( a, b )); 2047 push(c); 2048 break; 2049 2050 case Bytecodes::_lneg: 2051 a = pop_pair(); 2052 b = _gvn.transform( new (C, 3) SubLNode(longcon(0),a)); 2053 push_pair(b); 2054 break; 2055 case Bytecodes::_l2i: 2056 a = pop_pair(); 2057 push( _gvn.transform( new (C, 2) ConvL2INode(a))); 2058 break; 2059 case Bytecodes::_i2l: 2060 a = pop(); 2061 b = _gvn.transform( new (C, 2) ConvI2LNode(a)); 2062 push_pair(b); 2063 break; 2064 case Bytecodes::_i2b: 2065 // Sign extend 2066 a = pop(); 2067 a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(24)) ); 2068 a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(24)) ); 2069 push( a ); 2070 break; 2071 case Bytecodes::_i2s: 2072 a = pop(); 2073 a = _gvn.transform( new (C, 3) LShiftINode(a,_gvn.intcon(16)) ); 2074 a = _gvn.transform( new (C, 3) RShiftINode(a,_gvn.intcon(16)) ); 2075 push( a ); 2076 break; 2077 case Bytecodes::_i2c: 2078 a = pop(); 2079 push( _gvn.transform( new (C, 3) AndINode(a,_gvn.intcon(0xFFFF)) ) ); 2080 break; 2081 2082 case Bytecodes::_i2f: 2083 a = pop(); 2084 b = _gvn.transform( new (C, 2) ConvI2FNode(a) ) ; 2085 c = precision_rounding(b); 2086 push (b); 2087 break; 2088 2089 case Bytecodes::_i2d: 2090 a = pop(); 2091 b = _gvn.transform( new (C, 2) ConvI2DNode(a)); 2092 push_pair(b); 2093 break; 2094 2095 case Bytecodes::_iinc: // Increment local 2096 i = iter().get_index(); // Get local index 2097 set_local( i, _gvn.transform( new (C, 3) AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) ); 2098 break; 2099 2100 // Exit points of synchronized methods must have an unlock node 2101 case Bytecodes::_return: 2102 return_current(NULL); 2103 break; 2104 2105 case Bytecodes::_ireturn: 2106 case Bytecodes::_areturn: 2107 case Bytecodes::_freturn: 2108 return_current(pop()); 2109 break; 2110 case Bytecodes::_lreturn: 2111 return_current(pop_pair()); 2112 break; 2113 case Bytecodes::_dreturn: 2114 return_current(pop_pair()); 2115 break; 2116 2117 case Bytecodes::_athrow: 2118 // null exception oop throws NULL pointer exception 2119 do_null_check(peek(), T_OBJECT); 2120 if (stopped()) return; 2121 // Hook the thrown exception directly to subsequent handlers. 2122 if (BailoutToInterpreterForThrows) { 2123 // Keep method interpreted from now on. 2124 uncommon_trap(Deoptimization::Reason_unhandled, 2125 Deoptimization::Action_make_not_compilable); 2126 return; 2127 } 2128 if (env()->jvmti_can_post_on_exceptions()) { 2129 // check if we must post exception events, take uncommon trap if so (with must_throw = false) 2130 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false); 2131 } 2132 // Here if either can_post_on_exceptions or should_post_on_exceptions is false 2133 add_exception_state(make_exception_state(peek())); 2134 break; 2135 2136 case Bytecodes::_goto: // fall through 2137 case Bytecodes::_goto_w: { 2138 int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest(); 2139 2140 // If this is a backwards branch in the bytecodes, add Safepoint 2141 maybe_add_safepoint(target_bci); 2142 2143 // Update method data 2144 profile_taken_branch(target_bci); 2145 2146 // Merge the current control into the target basic block 2147 merge(target_bci); 2148 2149 // See if we can get some profile data and hand it off to the next block 2150 Block *target_block = block()->successor_for_bci(target_bci); 2151 if (target_block->pred_count() != 1) break; 2152 ciMethodData* methodData = method()->method_data(); 2153 if (!methodData->is_mature()) break; 2154 ciProfileData* data = methodData->bci_to_data(bci()); 2155 assert( data->is_JumpData(), "" ); 2156 int taken = ((ciJumpData*)data)->taken(); 2157 taken = method()->scale_count(taken); 2158 target_block->set_count(taken); 2159 break; 2160 } 2161 2162 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null; 2163 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null; 2164 handle_if_null: 2165 // If this is a backwards branch in the bytecodes, add Safepoint 2166 maybe_add_safepoint(iter().get_dest()); 2167 a = null(); 2168 b = pop(); 2169 c = _gvn.transform( new (C, 3) CmpPNode(b, a) ); 2170 do_ifnull(btest, c); 2171 break; 2172 2173 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp; 2174 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp; 2175 handle_if_acmp: 2176 // If this is a backwards branch in the bytecodes, add Safepoint 2177 maybe_add_safepoint(iter().get_dest()); 2178 a = pop(); 2179 b = pop(); 2180 c = _gvn.transform( new (C, 3) CmpPNode(b, a) ); 2181 do_if(btest, c); 2182 break; 2183 2184 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx; 2185 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx; 2186 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx; 2187 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx; 2188 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx; 2189 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx; 2190 handle_ifxx: 2191 // If this is a backwards branch in the bytecodes, add Safepoint 2192 maybe_add_safepoint(iter().get_dest()); 2193 a = _gvn.intcon(0); 2194 b = pop(); 2195 c = _gvn.transform( new (C, 3) CmpINode(b, a) ); 2196 do_if(btest, c); 2197 break; 2198 2199 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp; 2200 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp; 2201 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp; 2202 case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp; 2203 case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp; 2204 case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp; 2205 handle_if_icmp: 2206 // If this is a backwards branch in the bytecodes, add Safepoint 2207 maybe_add_safepoint(iter().get_dest()); 2208 a = pop(); 2209 b = pop(); 2210 c = _gvn.transform( new (C, 3) CmpINode( b, a ) ); 2211 do_if(btest, c); 2212 break; 2213 2214 case Bytecodes::_tableswitch: 2215 do_tableswitch(); 2216 break; 2217 2218 case Bytecodes::_lookupswitch: 2219 do_lookupswitch(); 2220 break; 2221 2222 case Bytecodes::_invokestatic: 2223 case Bytecodes::_invokedynamic: 2224 case Bytecodes::_invokespecial: 2225 case Bytecodes::_invokevirtual: 2226 case Bytecodes::_invokeinterface: 2227 do_call(); 2228 break; 2229 case Bytecodes::_checkcast: 2230 do_checkcast(); 2231 break; 2232 case Bytecodes::_instanceof: 2233 do_instanceof(); 2234 break; 2235 case Bytecodes::_anewarray: 2236 do_anewarray(); 2237 break; 2238 case Bytecodes::_newarray: 2239 do_newarray((BasicType)iter().get_index()); 2240 break; 2241 case Bytecodes::_multianewarray: 2242 do_multianewarray(); 2243 break; 2244 case Bytecodes::_new: 2245 do_new(); 2246 break; 2247 2248 case Bytecodes::_jsr: 2249 case Bytecodes::_jsr_w: 2250 do_jsr(); 2251 break; 2252 2253 case Bytecodes::_ret: 2254 do_ret(); 2255 break; 2256 2257 2258 case Bytecodes::_monitorenter: 2259 do_monitor_enter(); 2260 break; 2261 2262 case Bytecodes::_monitorexit: 2263 do_monitor_exit(); 2264 break; 2265 2266 case Bytecodes::_breakpoint: 2267 // Breakpoint set concurrently to compile 2268 // %%% use an uncommon trap? 2269 C->record_failure("breakpoint in method"); 2270 return; 2271 2272 default: 2273#ifndef PRODUCT 2274 map()->dump(99); 2275#endif 2276 tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) ); 2277 ShouldNotReachHere(); 2278 } 2279 2280#ifndef PRODUCT 2281 IdealGraphPrinter *printer = IdealGraphPrinter::printer(); 2282 if(printer) { 2283 char buffer[256]; 2284 sprintf(buffer, "Bytecode %d: %s", bci(), Bytecodes::name(bc())); 2285 bool old = printer->traverse_outs(); 2286 printer->set_traverse_outs(true); 2287 printer->print_method(C, buffer, 4); 2288 printer->set_traverse_outs(old); 2289 } 2290#endif 2291} 2292