never@1515: /* kvn@3760: * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. never@1515: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. never@1515: * never@1515: * This code is free software; you can redistribute it and/or modify it never@1515: * under the terms of the GNU General Public License version 2 only, as never@1515: * published by the Free Software Foundation. never@1515: * never@1515: * This code is distributed in the hope that it will be useful, but WITHOUT never@1515: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or never@1515: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License never@1515: * version 2 for more details (a copy is included in the LICENSE file that never@1515: * accompanied this code). never@1515: * never@1515: * You should have received a copy of the GNU General Public License version never@1515: * 2 along with this work; if not, write to the Free Software Foundation, never@1515: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. never@1515: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. never@1515: * never@1515: */ never@1515: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "compiler/compileLog.hpp" stefank@2314: #include "opto/addnode.hpp" stefank@2314: #include "opto/callGenerator.hpp" stefank@2314: #include "opto/callnode.hpp" stefank@2314: #include "opto/divnode.hpp" stefank@2314: #include "opto/graphKit.hpp" stefank@2314: #include "opto/idealKit.hpp" stefank@2314: #include "opto/rootnode.hpp" stefank@2314: #include "opto/runtime.hpp" stefank@2314: #include "opto/stringopts.hpp" stefank@2314: #include "opto/subnode.hpp" never@1515: never@1515: #define __ kit. never@1515: never@1515: class StringConcat : public ResourceObj { never@1515: private: never@1515: PhaseStringOpts* _stringopts; never@1515: Node* _string_alloc; never@1515: AllocateNode* _begin; // The allocation the begins the pattern never@1515: CallStaticJavaNode* _end; // The final call of the pattern. Will either be never@1515: // SB.toString or or String.(SB.toString) never@1515: bool _multiple; // indicates this is a fusion of two or more never@1515: // separate StringBuilders never@1515: never@1515: Node* _arguments; // The list of arguments to be concatenated never@1515: GrowableArray _mode; // into a String along with a mode flag never@1515: // indicating how to treat the value. never@1515: never@1515: Node_List _control; // List of control nodes that will be deleted never@1515: Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten never@1515: // to restart at the initial JVMState. never@1515: public: never@1515: // Mode for converting arguments to Strings never@1515: enum { never@1515: StringMode, never@1515: IntMode, kvn@2413: CharMode, kvn@2413: StringNullCheckMode never@1515: }; never@1515: never@1515: StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end): never@1515: _end(end), never@1515: _begin(NULL), never@1515: _multiple(false), never@1515: _string_alloc(NULL), never@1515: _stringopts(stringopts) { kvn@4115: _arguments = new (_stringopts->C) Node(1); never@1515: _arguments->del_req(0); never@1515: } never@1515: never@1515: bool validate_control_flow(); never@1515: never@1515: void merge_add() { never@1515: #if 0 never@1515: // XXX This is place holder code for reusing an existing String never@1515: // allocation but the logic for checking the state safety is never@1515: // probably inadequate at the moment. never@1515: CallProjections endprojs; never@1515: sc->end()->extract_projections(&endprojs, false); never@1515: if (endprojs.resproj != NULL) { never@1515: for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) { never@1515: CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); never@1515: if (use != NULL && use->method() != NULL && never@2172: use->method()->intrinsic_id() == vmIntrinsics::_String_String && never@1515: use->in(TypeFunc::Parms + 1) == endprojs.resproj) { never@1515: // Found useless new String(sb.toString()) so reuse the newly allocated String never@1515: // when creating the result instead of allocating a new one. never@1515: sc->set_string_alloc(use->in(TypeFunc::Parms)); never@1515: sc->set_end(use); never@1515: } never@1515: } never@1515: } never@1515: #endif never@1515: } never@1515: never@1515: StringConcat* merge(StringConcat* other, Node* arg); never@1515: never@1515: void set_allocation(AllocateNode* alloc) { never@1515: _begin = alloc; never@1515: } never@1515: never@1515: void append(Node* value, int mode) { never@1515: _arguments->add_req(value); never@1515: _mode.append(mode); never@1515: } never@1515: void push(Node* value, int mode) { never@1515: _arguments->ins_req(0, value); never@1515: _mode.insert_before(0, mode); never@1515: } kvn@3889: never@1515: void push_string(Node* value) { never@1515: push(value, StringMode); never@1515: } kvn@2413: void push_string_null_check(Node* value) { kvn@2413: push(value, StringNullCheckMode); kvn@2413: } never@1515: void push_int(Node* value) { never@1515: push(value, IntMode); never@1515: } never@1515: void push_char(Node* value) { never@1515: push(value, CharMode); never@1515: } never@1515: kvn@3889: static bool is_SB_toString(Node* call) { kvn@3889: if (call->is_CallStaticJava()) { kvn@3889: CallStaticJavaNode* csj = call->as_CallStaticJava(); kvn@3889: ciMethod* m = csj->method(); kvn@3889: if (m != NULL && kvn@3889: (m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString || kvn@3889: m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) { kvn@3889: return true; kvn@3889: } kvn@3889: } kvn@3889: return false; kvn@3889: } kvn@3889: kvn@3889: static Node* skip_string_null_check(Node* value) { kvn@3889: // Look for a diamond shaped Null check of toString() result kvn@3889: // (could be code from String.valueOf()): kvn@3889: // (Proj == NULL) ? "null":"CastPP(Proj)#NotNULL kvn@3889: if (value->is_Phi()) { kvn@3889: int true_path = value->as_Phi()->is_diamond_phi(); kvn@3889: if (true_path != 0) { kvn@3889: // phi->region->if_proj->ifnode->bool kvn@3889: BoolNode* b = value->in(0)->in(1)->in(0)->in(1)->as_Bool(); kvn@3889: Node* cmp = b->in(1); kvn@3889: Node* v1 = cmp->in(1); kvn@3889: Node* v2 = cmp->in(2); kvn@3889: // Null check of the return of toString which can simply be skipped. kvn@3889: if (b->_test._test == BoolTest::ne && kvn@3889: v2->bottom_type() == TypePtr::NULL_PTR && kvn@3889: value->in(true_path)->Opcode() == Op_CastPP && kvn@3889: value->in(true_path)->in(1) == v1 && kvn@3889: v1->is_Proj() && is_SB_toString(v1->in(0))) { kvn@3889: return v1; kvn@3889: } kvn@3889: } kvn@3889: } kvn@3889: return value; kvn@3889: } kvn@3889: never@1515: Node* argument(int i) { never@1515: return _arguments->in(i); never@1515: } kvn@3889: Node* argument_uncast(int i) { kvn@3889: Node* arg = argument(i); kvn@3889: int amode = mode(i); kvn@3889: if (amode == StringConcat::StringMode || kvn@3889: amode == StringConcat::StringNullCheckMode) { kvn@3889: arg = skip_string_null_check(arg); kvn@3889: } kvn@3889: return arg; kvn@3889: } never@1515: void set_argument(int i, Node* value) { never@1515: _arguments->set_req(i, value); never@1515: } never@1515: int num_arguments() { never@1515: return _mode.length(); never@1515: } never@1515: int mode(int i) { never@1515: return _mode.at(i); never@1515: } never@1515: void add_control(Node* ctrl) { never@1515: assert(!_control.contains(ctrl), "only push once"); never@1515: _control.push(ctrl); never@1515: } never@1515: CallStaticJavaNode* end() { return _end; } never@1515: AllocateNode* begin() { return _begin; } never@1515: Node* string_alloc() { return _string_alloc; } never@1515: never@1515: void eliminate_unneeded_control(); never@1515: void eliminate_initialize(InitializeNode* init); never@1515: void eliminate_call(CallNode* call); never@1515: never@1515: void maybe_log_transform() { never@1515: CompileLog* log = _stringopts->C->log(); never@1515: if (log != NULL) { never@1515: log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'", never@1515: num_arguments(), never@1515: _string_alloc != NULL, never@1515: _multiple); never@1515: JVMState* p = _begin->jvms(); never@1515: while (p != NULL) { never@1515: log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); never@1515: p = p->caller(); never@1515: } never@1515: log->tail("replace_string_concat"); never@1515: } never@1515: } never@1515: never@1515: void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) { never@1515: for (uint u = 0; u < _uncommon_traps.size(); u++) { never@1515: Node* uct = _uncommon_traps.at(u); never@1515: never@1515: // Build a new call using the jvms state of the allocate twisti@2103: address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point(); never@1515: const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type(); never@1515: const TypePtr* no_memory_effects = NULL; never@1515: Compile* C = _stringopts->C; kvn@4115: CallStaticJavaNode* call = new (C) CallStaticJavaNode(call_type, call_addr, "uncommon_trap", kvn@4115: jvms->bci(), no_memory_effects); never@1515: for (int e = 0; e < TypeFunc::Parms; e++) { never@1515: call->init_req(e, uct->in(e)); never@1515: } never@1515: // Set the trap request to record intrinsic failure if this trap never@1515: // is taken too many times. Ideally we would handle then traps by never@1515: // doing the original bookkeeping in the MDO so that if it caused never@1515: // the code to be thrown out we could still recompile and use the never@1515: // optimization. Failing the uncommon traps doesn't really mean never@1515: // that the optimization is a bad idea but there's no other way to never@1515: // do the MDO updates currently. never@1515: int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic, never@1515: Deoptimization::Action_make_not_entrant); never@1515: call->init_req(TypeFunc::Parms, __ intcon(trap_request)); never@1515: kit.add_safepoint_edges(call); never@1515: never@1515: _stringopts->gvn()->transform(call); never@1515: C->gvn_replace_by(uct, call); bharadwaj@4315: uct->disconnect_inputs(NULL, C); never@1515: } never@1515: } never@1515: never@1515: void cleanup() { never@1515: // disconnect the hook node bharadwaj@4315: _arguments->disconnect_inputs(NULL, _stringopts->C); never@1515: } never@1515: }; never@1515: never@1515: never@1515: void StringConcat::eliminate_unneeded_control() { never@1515: for (uint i = 0; i < _control.size(); i++) { never@1515: Node* n = _control.at(i); kvn@3889: if (n->is_Allocate()) { kvn@3889: eliminate_initialize(n->as_Allocate()->initialization()); kvn@3889: } never@1515: if (n->is_Call()) { never@1515: if (n != _end) { never@1515: eliminate_call(n->as_Call()); never@1515: } never@1515: } else if (n->is_IfTrue()) { never@1515: Compile* C = _stringopts->C; never@1515: C->gvn_replace_by(n, n->in(0)->in(0)); roland@4409: // get rid of the other projection roland@4409: C->gvn_replace_by(n->in(0)->as_If()->proj_out(false), C->top()); never@1515: } never@1515: } never@1515: } never@1515: never@1515: never@1515: StringConcat* StringConcat::merge(StringConcat* other, Node* arg) { never@1515: StringConcat* result = new StringConcat(_stringopts, _end); never@1515: for (uint x = 0; x < _control.size(); x++) { never@1515: Node* n = _control.at(x); never@1515: if (n->is_Call()) { never@1515: result->_control.push(n); never@1515: } never@1515: } never@1515: for (uint x = 0; x < other->_control.size(); x++) { never@1515: Node* n = other->_control.at(x); never@1515: if (n->is_Call()) { never@1515: result->_control.push(n); never@1515: } never@1515: } never@1515: assert(result->_control.contains(other->_end), "what?"); never@1515: assert(result->_control.contains(_begin), "what?"); never@1515: for (int x = 0; x < num_arguments(); x++) { kvn@3889: Node* argx = argument_uncast(x); kvn@3889: if (argx == arg) { never@1515: // replace the toString result with the all the arguments that never@1515: // made up the other StringConcat never@1515: for (int y = 0; y < other->num_arguments(); y++) { never@1515: result->append(other->argument(y), other->mode(y)); never@1515: } never@1515: } else { kvn@3889: result->append(argx, mode(x)); never@1515: } never@1515: } never@1515: result->set_allocation(other->_begin); never@1515: result->_multiple = true; never@1515: return result; never@1515: } never@1515: never@1515: never@1515: void StringConcat::eliminate_call(CallNode* call) { never@1515: Compile* C = _stringopts->C; never@1515: CallProjections projs; never@1515: call->extract_projections(&projs, false); never@1515: if (projs.fallthrough_catchproj != NULL) { never@1515: C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control)); never@1515: } never@1515: if (projs.fallthrough_memproj != NULL) { never@1515: C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory)); never@1515: } never@1515: if (projs.catchall_memproj != NULL) { never@1515: C->gvn_replace_by(projs.catchall_memproj, C->top()); never@1515: } never@1515: if (projs.fallthrough_ioproj != NULL) { never@1515: C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O)); never@1515: } never@1515: if (projs.catchall_ioproj != NULL) { never@1515: C->gvn_replace_by(projs.catchall_ioproj, C->top()); never@1515: } never@1515: if (projs.catchall_catchproj != NULL) { never@1515: // EA can't cope with the partially collapsed graph this never@1515: // creates so put it on the worklist to be collapsed later. never@1515: for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) { never@1515: Node *use = i.get(); never@1515: int opc = use->Opcode(); never@1515: if (opc == Op_CreateEx || opc == Op_Region) { never@1515: _stringopts->record_dead_node(use); never@1515: } never@1515: } never@1515: C->gvn_replace_by(projs.catchall_catchproj, C->top()); never@1515: } never@1515: if (projs.resproj != NULL) { never@1515: C->gvn_replace_by(projs.resproj, C->top()); never@1515: } never@1515: C->gvn_replace_by(call, C->top()); never@1515: } never@1515: never@1515: void StringConcat::eliminate_initialize(InitializeNode* init) { never@1515: Compile* C = _stringopts->C; never@1515: never@1515: // Eliminate Initialize node. never@1515: assert(init->outcnt() <= 2, "only a control and memory projection expected"); never@1515: assert(init->req() <= InitializeNode::RawStores, "no pending inits"); never@1515: Node *ctrl_proj = init->proj_out(TypeFunc::Control); never@1515: if (ctrl_proj != NULL) { never@1515: C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control)); never@1515: } never@1515: Node *mem_proj = init->proj_out(TypeFunc::Memory); never@1515: if (mem_proj != NULL) { never@1515: Node *mem = init->in(TypeFunc::Memory); never@1515: C->gvn_replace_by(mem_proj, mem); never@1515: } never@1515: C->gvn_replace_by(init, C->top()); bharadwaj@4315: init->disconnect_inputs(NULL, C); never@1515: } never@1515: never@1515: Node_List PhaseStringOpts::collect_toString_calls() { never@1515: Node_List string_calls; never@1515: Node_List worklist; never@1515: never@1515: _visited.Clear(); never@1515: never@1515: // Prime the worklist never@1515: for (uint i = 1; i < C->root()->len(); i++) { never@1515: Node* n = C->root()->in(i); never@1515: if (n != NULL && !_visited.test_set(n->_idx)) { never@1515: worklist.push(n); never@1515: } never@1515: } never@1515: never@1515: while (worklist.size() > 0) { never@1515: Node* ctrl = worklist.pop(); kvn@3889: if (StringConcat::is_SB_toString(ctrl)) { never@1515: CallStaticJavaNode* csj = ctrl->as_CallStaticJava(); kvn@3889: string_calls.push(csj); never@1515: } never@1515: if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) { never@1515: worklist.push(ctrl->in(0)); never@1515: } never@1515: if (ctrl->is_Region()) { never@1515: for (uint i = 1; i < ctrl->len(); i++) { never@1515: if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) { never@1515: worklist.push(ctrl->in(i)); never@1515: } never@1515: } never@1515: } never@1515: } never@1515: return string_calls; never@1515: } never@1515: never@1515: never@1515: StringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) { never@1515: ciMethod* m = call->method(); never@1515: ciSymbol* string_sig; never@1515: ciSymbol* int_sig; never@1515: ciSymbol* char_sig; never@1515: if (m->holder() == C->env()->StringBuilder_klass()) { never@1515: string_sig = ciSymbol::String_StringBuilder_signature(); never@1515: int_sig = ciSymbol::int_StringBuilder_signature(); never@1515: char_sig = ciSymbol::char_StringBuilder_signature(); never@1515: } else if (m->holder() == C->env()->StringBuffer_klass()) { never@1515: string_sig = ciSymbol::String_StringBuffer_signature(); never@1515: int_sig = ciSymbol::int_StringBuffer_signature(); never@1515: char_sig = ciSymbol::char_StringBuffer_signature(); never@1515: } else { never@1515: return NULL; never@1515: } never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print("considering toString call in "); never@1515: call->jvms()->dump_spec(tty); tty->cr(); never@1515: } never@1515: #endif never@1515: never@1515: StringConcat* sc = new StringConcat(this, call); never@1515: never@1515: AllocateNode* alloc = NULL; never@1515: InitializeNode* init = NULL; never@1515: never@1515: // possible opportunity for StringBuilder fusion never@1515: CallStaticJavaNode* cnode = call; never@1515: while (cnode) { never@1515: Node* recv = cnode->in(TypeFunc::Parms)->uncast(); never@1515: if (recv->is_Proj()) { never@1515: recv = recv->in(0); never@1515: } never@1515: cnode = recv->isa_CallStaticJava(); never@1515: if (cnode == NULL) { never@1515: alloc = recv->isa_Allocate(); never@1515: if (alloc == NULL) { never@1515: break; never@1515: } never@1515: // Find the constructor call never@1515: Node* result = alloc->result_cast(); roland@4409: if (result == NULL || !result->is_CheckCastPP() || alloc->in(TypeFunc::Memory)->is_top()) { never@1515: // strange looking allocation never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print("giving up because allocation looks strange "); never@1515: alloc->jvms()->dump_spec(tty); tty->cr(); never@1515: } never@1515: #endif never@1515: break; never@1515: } never@1515: Node* constructor = NULL; never@1515: for (SimpleDUIterator i(result); i.has_next(); i.next()) { never@1515: CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); never@2172: if (use != NULL && never@2172: use->method() != NULL && never@2172: !use->method()->is_static() && never@1515: use->method()->name() == ciSymbol::object_initializer_name() && never@1515: use->method()->holder() == m->holder()) { never@1515: // Matched the constructor. never@1515: ciSymbol* sig = use->method()->signature()->as_symbol(); never@1515: if (sig == ciSymbol::void_method_signature() || never@1515: sig == ciSymbol::int_void_signature() || never@1515: sig == ciSymbol::string_void_signature()) { never@1515: if (sig == ciSymbol::string_void_signature()) { never@1515: // StringBuilder(String) so pick this up as the first argument never@1515: assert(use->in(TypeFunc::Parms + 1) != NULL, "what?"); kvn@2413: const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1)); kvn@2413: if (type == TypePtr::NULL_PTR) { kvn@2413: // StringBuilder(null) throws exception. kvn@2413: #ifndef PRODUCT kvn@2413: if (PrintOptimizeStringConcat) { kvn@2413: tty->print("giving up because StringBuilder(null) throws exception"); kvn@2413: alloc->jvms()->dump_spec(tty); tty->cr(); kvn@2413: } kvn@2413: #endif kvn@2413: return NULL; kvn@2413: } kvn@2413: // StringBuilder(str) argument needs null check. kvn@2413: sc->push_string_null_check(use->in(TypeFunc::Parms + 1)); never@1515: } never@1515: // The int variant takes an initial size for the backing never@1515: // array so just treat it like the void version. never@1515: constructor = use; never@1515: } else { never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print("unexpected constructor signature: %s", sig->as_utf8()); never@1515: } never@1515: #endif never@1515: } never@1515: break; never@1515: } never@1515: } never@1515: if (constructor == NULL) { never@1515: // couldn't find constructor never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print("giving up because couldn't find constructor "); kvn@2413: alloc->jvms()->dump_spec(tty); tty->cr(); never@1515: } never@1515: #endif never@1515: break; never@1515: } never@1515: never@1515: // Walked all the way back and found the constructor call so see never@1515: // if this call converted into a direct string concatenation. never@1515: sc->add_control(call); never@1515: sc->add_control(constructor); never@1515: sc->add_control(alloc); never@1515: sc->set_allocation(alloc); never@1515: if (sc->validate_control_flow()) { never@1515: return sc; never@1515: } else { never@1515: return NULL; never@1515: } never@1515: } else if (cnode->method() == NULL) { never@1515: break; never@2172: } else if (!cnode->method()->is_static() && never@2172: cnode->method()->holder() == m->holder() && never@1515: cnode->method()->name() == ciSymbol::append_name() && never@1515: (cnode->method()->signature()->as_symbol() == string_sig || never@1515: cnode->method()->signature()->as_symbol() == char_sig || never@1515: cnode->method()->signature()->as_symbol() == int_sig)) { never@1515: sc->add_control(cnode); never@1515: Node* arg = cnode->in(TypeFunc::Parms + 1); never@1515: if (cnode->method()->signature()->as_symbol() == int_sig) { never@1515: sc->push_int(arg); never@1515: } else if (cnode->method()->signature()->as_symbol() == char_sig) { never@1515: sc->push_char(arg); never@1515: } else { never@1515: if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) { never@1515: CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); never@1515: if (csj->method() != NULL && kvn@3927: csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString && kvn@3927: arg->outcnt() == 1) { kvn@3927: // _control is the list of StringBuilder calls nodes which kvn@3927: // will be replaced by new String code after this optimization. kvn@3927: // Integer::toString() call is not part of StringBuilder calls kvn@3927: // chain. It could be eliminated only if its result is used kvn@3927: // only by this SB calls chain. kvn@3927: // Another limitation: it should be used only once because kvn@3927: // it is unknown that it is used only by this SB calls chain kvn@3927: // until all related SB calls nodes are collected. kvn@3927: assert(arg->unique_out() == cnode, "sanity"); never@1515: sc->add_control(csj); never@1515: sc->push_int(csj->in(TypeFunc::Parms)); never@1515: continue; never@1515: } never@1515: } never@1515: sc->push_string(arg); never@1515: } never@1515: continue; never@1515: } else { never@1515: // some unhandled signature never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print("giving up because encountered unexpected signature "); never@1515: cnode->tf()->dump(); tty->cr(); never@1515: cnode->in(TypeFunc::Parms + 1)->dump(); never@1515: } never@1515: #endif never@1515: break; never@1515: } never@1515: } never@1515: return NULL; never@1515: } never@1515: never@1515: never@1515: PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*): never@1515: Phase(StringOpts), never@1515: _gvn(gvn), never@1515: _visited(Thread::current()->resource_area()) { never@1515: never@1515: assert(OptimizeStringConcat, "shouldn't be here"); never@1515: never@1515: size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"), never@1515: ciSymbol::make("[I"), true); never@1515: if (size_table_field == NULL) { never@1515: // Something wrong so give up. never@1515: assert(false, "why can't we find Integer.sizeTable?"); never@1515: return; never@1515: } never@1515: never@1515: // Collect the types needed to talk about the various slices of memory never@1515: char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS); never@1515: never@1515: // For each locally allocated StringBuffer see if the usages can be never@1515: // collapsed into a single String construction. never@1515: never@1515: // Run through the list of allocation looking for SB.toString to see never@1515: // if it's possible to fuse the usage of the SB into a single String never@1515: // construction. never@1515: GrowableArray concats; never@1515: Node_List toStrings = collect_toString_calls(); never@1515: while (toStrings.size() > 0) { never@1515: StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava()); never@1515: if (sc != NULL) { never@1515: concats.push(sc); never@1515: } never@1515: } never@1515: never@1515: // try to coalesce separate concats never@1515: restart: never@1515: for (int c = 0; c < concats.length(); c++) { never@1515: StringConcat* sc = concats.at(c); never@1515: for (int i = 0; i < sc->num_arguments(); i++) { kvn@3889: Node* arg = sc->argument_uncast(i); kvn@3889: if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) { never@1515: CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); kvn@3889: for (int o = 0; o < concats.length(); o++) { kvn@3889: if (c == o) continue; kvn@3889: StringConcat* other = concats.at(o); kvn@3889: if (other->end() == csj) { kvn@3889: #ifndef PRODUCT kvn@3889: if (PrintOptimizeStringConcat) { kvn@3889: tty->print_cr("considering stacked concats"); kvn@3889: } kvn@3889: #endif kvn@3889: kvn@3889: StringConcat* merged = sc->merge(other, arg); kvn@3889: if (merged->validate_control_flow()) { never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { kvn@3889: tty->print_cr("stacking would succeed"); never@1515: } never@1515: #endif kvn@3889: if (c < o) { kvn@3889: concats.remove_at(o); kvn@3889: concats.at_put(c, merged); kvn@3889: } else { kvn@3889: concats.remove_at(c); kvn@3889: concats.at_put(o, merged); kvn@3889: } kvn@3889: goto restart; kvn@3889: } else { never@1515: #ifndef PRODUCT kvn@3889: if (PrintOptimizeStringConcat) { kvn@3889: tty->print_cr("stacking would fail"); kvn@3889: } never@1515: #endif never@1515: } never@1515: } never@1515: } never@1515: } never@1515: } never@1515: } never@1515: never@1515: never@1515: for (int c = 0; c < concats.length(); c++) { never@1515: StringConcat* sc = concats.at(c); never@1515: replace_string_concat(sc); never@1515: } never@1515: never@1515: remove_dead_nodes(); never@1515: } never@1515: never@1515: void PhaseStringOpts::record_dead_node(Node* dead) { never@1515: dead_worklist.push(dead); never@1515: } never@1515: never@1515: void PhaseStringOpts::remove_dead_nodes() { never@1515: // Delete any dead nodes to make things clean enough that escape never@1515: // analysis doesn't get unhappy. never@1515: while (dead_worklist.size() > 0) { never@1515: Node* use = dead_worklist.pop(); never@1515: int opc = use->Opcode(); never@1515: switch (opc) { never@1515: case Op_Region: { never@1515: uint i = 1; never@1515: for (i = 1; i < use->req(); i++) { never@1515: if (use->in(i) != C->top()) { never@1515: break; never@1515: } never@1515: } never@1515: if (i >= use->req()) { never@1515: for (SimpleDUIterator i(use); i.has_next(); i.next()) { never@1515: Node* m = i.get(); never@1515: if (m->is_Phi()) { never@1515: dead_worklist.push(m); never@1515: } never@1515: } never@1515: C->gvn_replace_by(use, C->top()); never@1515: } never@1515: break; never@1515: } never@1515: case Op_AddP: never@1515: case Op_CreateEx: { never@1515: // Recurisvely clean up references to CreateEx so EA doesn't never@1515: // get unhappy about the partially collapsed graph. never@1515: for (SimpleDUIterator i(use); i.has_next(); i.next()) { never@1515: Node* m = i.get(); never@1515: if (m->is_AddP()) { never@1515: dead_worklist.push(m); never@1515: } never@1515: } never@1515: C->gvn_replace_by(use, C->top()); never@1515: break; never@1515: } never@1515: case Op_Phi: never@1515: if (use->in(0) == C->top()) { never@1515: C->gvn_replace_by(use, C->top()); never@1515: } never@1515: break; never@1515: } never@1515: } never@1515: } never@1515: never@1515: never@1515: bool StringConcat::validate_control_flow() { never@1515: // We found all the calls and arguments now lets see if it's never@1515: // safe to transform the graph as we would expect. never@1515: never@1515: // Check to see if this resulted in too many uncommon traps previously never@1515: if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(), never@1515: Deoptimization::Reason_intrinsic)) { never@1515: return false; never@1515: } never@1515: never@1515: // Walk backwards over the control flow from toString to the never@1515: // allocation and make sure all the control flow is ok. This never@1515: // means it's either going to be eliminated once the calls are never@1515: // removed or it can safely be transformed into an uncommon never@1515: // trap. never@1515: never@1515: int null_check_count = 0; never@1515: Unique_Node_List ctrl_path; never@1515: never@1515: assert(_control.contains(_begin), "missing"); never@1515: assert(_control.contains(_end), "missing"); never@1515: never@1515: // Collect the nodes that we know about and will eliminate into ctrl_path never@1515: for (uint i = 0; i < _control.size(); i++) { never@1515: // Push the call and it's control projection never@1515: Node* n = _control.at(i); never@1515: if (n->is_Allocate()) { never@1515: AllocateNode* an = n->as_Allocate(); never@1515: InitializeNode* init = an->initialization(); never@1515: ctrl_path.push(init); never@1515: ctrl_path.push(init->as_Multi()->proj_out(0)); never@1515: } never@1515: if (n->is_Call()) { never@1515: CallNode* cn = n->as_Call(); never@1515: ctrl_path.push(cn); never@1515: ctrl_path.push(cn->proj_out(0)); never@1515: ctrl_path.push(cn->proj_out(0)->unique_out()); roland@4357: if (cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0) != NULL) { roland@4357: ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0)); roland@4357: } never@1515: } else { never@1515: ShouldNotReachHere(); never@1515: } never@1515: } never@1515: never@1515: // Skip backwards through the control checking for unexpected contro flow never@1515: Node* ptr = _end; never@1515: bool fail = false; never@1515: while (ptr != _begin) { never@1515: if (ptr->is_Call() && ctrl_path.member(ptr)) { never@1515: ptr = ptr->in(0); never@1515: } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) { never@1515: ptr = ptr->in(0)->in(0)->in(0); never@1515: assert(ctrl_path.member(ptr), "should be a known piece of control"); never@1515: } else if (ptr->is_IfTrue()) { never@1515: IfNode* iff = ptr->in(0)->as_If(); never@1515: BoolNode* b = iff->in(1)->isa_Bool(); roland@4357: roland@4357: if (b == NULL) { roland@4357: fail = true; roland@4357: break; roland@4357: } roland@4357: never@1515: Node* cmp = b->in(1); never@1515: Node* v1 = cmp->in(1); never@1515: Node* v2 = cmp->in(2); never@1515: Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con); never@1515: never@1515: // Null check of the return of append which can simply be eliminated never@1515: if (b->_test._test == BoolTest::ne && never@1515: v2->bottom_type() == TypePtr::NULL_PTR && never@1515: v1->is_Proj() && ctrl_path.member(v1->in(0))) { never@1515: // NULL check of the return value of the append never@1515: null_check_count++; never@1515: if (otherproj->outcnt() == 1) { never@1515: CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); never@1515: if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { never@1515: ctrl_path.push(call); never@1515: } never@1515: } never@1515: _control.push(ptr); never@1515: ptr = ptr->in(0)->in(0); never@1515: continue; never@1515: } never@1515: never@1515: // A test which leads to an uncommon trap which should be safe. never@1515: // Later this trap will be converted into a trap that restarts never@1515: // at the beginning. never@1515: if (otherproj->outcnt() == 1) { never@1515: CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); never@1515: if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { never@1515: // control flow leads to uct so should be ok never@1515: _uncommon_traps.push(call); never@1515: ctrl_path.push(call); never@1515: ptr = ptr->in(0)->in(0); never@1515: continue; never@1515: } never@1515: } never@1515: never@1515: #ifndef PRODUCT never@1515: // Some unexpected control flow we don't know how to handle. never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print_cr("failing with unknown test"); never@1515: b->dump(); never@1515: cmp->dump(); never@1515: v1->dump(); never@1515: v2->dump(); never@1515: tty->cr(); never@1515: } never@1515: #endif kvn@2927: fail = true; never@1515: break; never@1515: } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) { never@1515: ptr = ptr->in(0)->in(0); never@1515: } else if (ptr->is_Region()) { never@1515: Node* copy = ptr->as_Region()->is_copy(); never@1515: if (copy != NULL) { never@1515: ptr = copy; never@1515: continue; never@1515: } never@1515: if (ptr->req() == 3 && never@1515: ptr->in(1) != NULL && ptr->in(1)->is_Proj() && never@1515: ptr->in(2) != NULL && ptr->in(2)->is_Proj() && never@1515: ptr->in(1)->in(0) == ptr->in(2)->in(0) && never@1515: ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) { never@1515: // Simple diamond. never@1515: // XXX should check for possibly merging stores. simple data merges are ok. roland@4409: // The IGVN will make this simple diamond go away when it roland@4409: // transforms the Region. Make sure it sees it. roland@4409: Compile::current()->record_for_igvn(ptr); never@1515: ptr = ptr->in(1)->in(0)->in(0); never@1515: continue; never@1515: } never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print_cr("fusion would fail for region"); never@1515: _begin->dump(); never@1515: ptr->dump(2); never@1515: } never@1515: #endif never@1515: fail = true; never@1515: break; never@1515: } else { never@1515: // other unknown control never@1515: if (!fail) { never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: tty->print_cr("fusion would fail for"); never@1515: _begin->dump(); never@1515: } never@1515: #endif never@1515: fail = true; never@1515: } never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: ptr->dump(); never@1515: } never@1515: #endif never@1515: ptr = ptr->in(0); never@1515: } never@1515: } never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat && fail) { never@1515: tty->cr(); never@1515: } never@1515: #endif never@1515: if (fail) return !fail; never@1515: never@1515: // Validate that all these results produced are contained within never@1515: // this cluster of objects. First collect all the results produced never@1515: // by calls in the region. never@1515: _stringopts->_visited.Clear(); never@1515: Node_List worklist; never@1515: Node* final_result = _end->proj_out(TypeFunc::Parms); never@1515: for (uint i = 0; i < _control.size(); i++) { never@1515: CallNode* cnode = _control.at(i)->isa_Call(); never@1515: if (cnode != NULL) { never@1515: _stringopts->_visited.test_set(cnode->_idx); never@1515: } never@1515: Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL; never@1515: if (result != NULL && result != final_result) { never@1515: worklist.push(result); never@1515: } never@1515: } never@1515: never@1515: Node* last_result = NULL; never@1515: while (worklist.size() > 0) { never@1515: Node* result = worklist.pop(); never@1515: if (_stringopts->_visited.test_set(result->_idx)) never@1515: continue; never@1515: for (SimpleDUIterator i(result); i.has_next(); i.next()) { never@1515: Node *use = i.get(); never@1515: if (ctrl_path.member(use)) { never@1515: // already checked this never@1515: continue; never@1515: } never@1515: int opc = use->Opcode(); never@1515: if (opc == Op_CmpP || opc == Op_Node) { never@1515: ctrl_path.push(use); never@1515: continue; never@1515: } never@1515: if (opc == Op_CastPP || opc == Op_CheckCastPP) { never@1515: for (SimpleDUIterator j(use); j.has_next(); j.next()) { never@1515: worklist.push(j.get()); never@1515: } never@1515: worklist.push(use->in(1)); never@1515: ctrl_path.push(use); never@1515: continue; never@1515: } never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat) { never@1515: if (result != last_result) { never@1515: last_result = result; never@1515: tty->print_cr("extra uses for result:"); never@1515: last_result->dump(); never@1515: } never@1515: use->dump(); never@1515: } never@1515: #endif never@1515: fail = true; never@1515: break; never@1515: } never@1515: } never@1515: never@1515: #ifndef PRODUCT never@1515: if (PrintOptimizeStringConcat && !fail) { never@1515: ttyLocker ttyl; never@1515: tty->cr(); never@1515: tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size()); never@1515: _begin->jvms()->dump_spec(tty); tty->cr(); never@1515: for (int i = 0; i < num_arguments(); i++) { never@1515: argument(i)->dump(); never@1515: } never@1515: _control.dump(); never@1515: tty->cr(); never@1515: } never@1515: #endif never@1515: never@1515: return !fail; never@1515: } never@1515: never@1515: Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) { never@3746: const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror()); never@3746: Node* klass_node = __ makecon(mirror_type); never@1515: BasicType bt = field->layout_type(); never@1515: ciType* field_klass = field->type(); never@1515: never@1515: const Type *type; never@1515: if( bt == T_OBJECT ) { never@1515: if (!field->type()->is_loaded()) { never@1515: type = TypeInstPtr::BOTTOM; never@1515: } else if (field->is_constant()) { never@1515: // This can happen if the constant oop is non-perm. never@1515: ciObject* con = field->constant_value().as_object(); never@1515: // Do not "join" in the previous type; it doesn't add value, never@1515: // and may yield a vacuous result if the field is of interface type. jcoomes@2661: type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr(); never@1515: assert(type != NULL, "field singleton type must be consistent"); never@3746: return __ makecon(type); never@1515: } else { never@1515: type = TypeOopPtr::make_from_klass(field_klass->as_klass()); never@1515: } never@1515: } else { never@1515: type = Type::get_const_basic_type(bt); never@1515: } never@1515: never@1515: return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()), never@1515: type, T_OBJECT, never@3746: C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes()))); never@1515: } never@1515: never@1515: Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { kvn@4115: RegionNode *final_merge = new (C) RegionNode(3); never@1515: kit.gvn().set_type(final_merge, Type::CONTROL); kvn@4115: Node* final_size = new (C) PhiNode(final_merge, TypeInt::INT); never@1515: kit.gvn().set_type(final_size, TypeInt::INT); never@1515: never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), never@1515: __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), never@1515: PROB_FAIR, COUNT_UNKNOWN); never@1515: Node* is_min = __ IfFalse(iff); never@1515: final_merge->init_req(1, is_min); never@1515: final_size->init_req(1, __ intcon(11)); never@1515: never@1515: kit.set_control(__ IfTrue(iff)); never@1515: if (kit.stopped()) { never@1515: final_merge->init_req(2, C->top()); never@1515: final_size->init_req(2, C->top()); never@1515: } else { never@1515: never@1515: // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); kvn@4115: RegionNode *r = new (C) RegionNode(3); never@1515: kit.gvn().set_type(r, Type::CONTROL); kvn@4115: Node *phi = new (C) PhiNode(r, TypeInt::INT); never@1515: kit.gvn().set_type(phi, TypeInt::INT); kvn@4115: Node *size = new (C) PhiNode(r, TypeInt::INT); never@1515: kit.gvn().set_type(size, TypeInt::INT); never@1515: Node* chk = __ CmpI(arg, __ intcon(0)); never@1515: Node* p = __ Bool(chk, BoolTest::lt); never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN); never@1515: Node* lessthan = __ IfTrue(iff); never@1515: Node* greaterequal = __ IfFalse(iff); never@1515: r->init_req(1, lessthan); never@1515: phi->init_req(1, __ SubI(__ intcon(0), arg)); never@1515: size->init_req(1, __ intcon(1)); never@1515: r->init_req(2, greaterequal); never@1515: phi->init_req(2, arg); never@1515: size->init_req(2, __ intcon(0)); never@1515: kit.set_control(r); never@1515: C->record_for_igvn(r); never@1515: C->record_for_igvn(phi); never@1515: C->record_for_igvn(size); never@1515: never@1515: // for (int i=0; ; i++) never@1515: // if (x <= sizeTable[i]) never@1515: // return i+1; kvn@2665: kvn@2665: // Add loop predicate first. kvn@2665: kit.add_predicate(); kvn@2665: kvn@4115: RegionNode *loop = new (C) RegionNode(3); never@1515: loop->init_req(1, kit.control()); never@1515: kit.gvn().set_type(loop, Type::CONTROL); never@1515: kvn@4115: Node *index = new (C) PhiNode(loop, TypeInt::INT); never@1515: index->init_req(1, __ intcon(0)); never@1515: kit.gvn().set_type(index, TypeInt::INT); never@1515: kit.set_control(loop); never@1515: Node* sizeTable = fetch_static_field(kit, size_table_field); never@1515: never@1515: Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS); never@1515: C->record_for_igvn(value); never@1515: Node* limit = __ CmpI(phi, value); never@1515: Node* limitb = __ Bool(limit, BoolTest::le); never@1515: IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN); never@1515: Node* lessEqual = __ IfTrue(iff2); never@1515: Node* greater = __ IfFalse(iff2); never@1515: never@1515: loop->init_req(2, greater); never@1515: index->init_req(2, __ AddI(index, __ intcon(1))); never@1515: never@1515: kit.set_control(lessEqual); never@1515: C->record_for_igvn(loop); never@1515: C->record_for_igvn(index); never@1515: never@1515: final_merge->init_req(2, kit.control()); never@1515: final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1))); never@1515: } never@1515: never@1515: kit.set_control(final_merge); never@1515: C->record_for_igvn(final_merge); never@1515: C->record_for_igvn(final_size); never@1515: never@1515: return final_size; never@1515: } never@1515: never@1515: void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) { kvn@4115: RegionNode *final_merge = new (C) RegionNode(4); never@1515: kit.gvn().set_type(final_merge, Type::CONTROL); never@1515: Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); never@1515: kit.gvn().set_type(final_mem, Type::MEMORY); never@1515: never@1515: // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive never@1515: { never@1515: // i == MIN_VALUE never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), never@1515: __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), never@1515: PROB_FAIR, COUNT_UNKNOWN); never@1515: never@1515: Node* old_mem = kit.memory(char_adr_idx); never@1515: never@1515: kit.set_control(__ IfFalse(iff)); never@1515: if (kit.stopped()) { never@1515: // Statically not equal to MIN_VALUE so this path is dead never@1515: final_merge->init_req(3, kit.control()); never@1515: } else { never@1515: copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())), never@1515: char_array, start); never@1515: final_merge->init_req(3, kit.control()); never@1515: final_mem->init_req(3, kit.memory(char_adr_idx)); never@1515: } never@1515: never@1515: kit.set_control(__ IfTrue(iff)); never@1515: kit.set_memory(old_mem, char_adr_idx); never@1515: } never@1515: never@1515: never@1515: // Simplified version of Integer.getChars never@1515: never@1515: // int q, r; never@1515: // int charPos = index; never@1515: Node* charPos = end; never@1515: never@1515: // char sign = 0; never@1515: never@1515: Node* i = arg; never@1515: Node* sign = __ intcon(0); never@1515: never@1515: // if (i < 0) { never@1515: // sign = '-'; never@1515: // i = -i; never@1515: // } never@1515: { never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), never@1515: __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt), never@1515: PROB_FAIR, COUNT_UNKNOWN); never@1515: kvn@4115: RegionNode *merge = new (C) RegionNode(3); never@1515: kit.gvn().set_type(merge, Type::CONTROL); kvn@4115: i = new (C) PhiNode(merge, TypeInt::INT); never@1515: kit.gvn().set_type(i, TypeInt::INT); kvn@4115: sign = new (C) PhiNode(merge, TypeInt::INT); never@1515: kit.gvn().set_type(sign, TypeInt::INT); never@1515: never@1515: merge->init_req(1, __ IfTrue(iff)); never@1515: i->init_req(1, __ SubI(__ intcon(0), arg)); never@1515: sign->init_req(1, __ intcon('-')); never@1515: merge->init_req(2, __ IfFalse(iff)); never@1515: i->init_req(2, arg); never@1515: sign->init_req(2, __ intcon(0)); never@1515: never@1515: kit.set_control(merge); never@1515: never@1515: C->record_for_igvn(merge); never@1515: C->record_for_igvn(i); never@1515: C->record_for_igvn(sign); never@1515: } never@1515: never@1515: // for (;;) { never@1515: // q = i / 10; never@1515: // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... never@1515: // buf [--charPos] = digits [r]; never@1515: // i = q; never@1515: // if (i == 0) break; never@1515: // } never@1515: never@1515: { kvn@2665: // Add loop predicate first. kvn@2665: kit.add_predicate(); kvn@2665: kvn@4115: RegionNode *head = new (C) RegionNode(3); never@1515: head->init_req(1, kit.control()); never@1515: kit.gvn().set_type(head, Type::CONTROL); kvn@4115: Node *i_phi = new (C) PhiNode(head, TypeInt::INT); never@1515: i_phi->init_req(1, i); never@1515: kit.gvn().set_type(i_phi, TypeInt::INT); never@1515: charPos = PhiNode::make(head, charPos); never@1515: kit.gvn().set_type(charPos, TypeInt::INT); never@1515: Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); never@1515: kit.gvn().set_type(mem, Type::MEMORY); never@1515: kit.set_control(head); never@1515: kit.set_memory(mem, char_adr_idx); never@1515: never@1685: Node* q = __ DivI(NULL, i_phi, __ intcon(10)); never@1515: Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)), never@1515: __ LShiftI(q, __ intcon(1)))); never@1515: Node* m1 = __ SubI(charPos, __ intcon(1)); never@1515: Node* ch = __ AddI(r, __ intcon('0')); never@1515: never@1515: Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), never@1515: ch, T_CHAR, char_adr_idx); never@1515: never@1515: never@1515: IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne), never@1515: PROB_FAIR, COUNT_UNKNOWN); never@1515: Node* ne = __ IfTrue(iff); never@1515: Node* eq = __ IfFalse(iff); never@1515: never@1515: head->init_req(2, ne); never@1515: mem->init_req(2, st); never@1515: i_phi->init_req(2, q); never@1515: charPos->init_req(2, m1); never@1515: never@1515: charPos = m1; never@1515: never@1515: kit.set_control(eq); never@1515: kit.set_memory(st, char_adr_idx); never@1515: never@1515: C->record_for_igvn(head); never@1515: C->record_for_igvn(mem); never@1515: C->record_for_igvn(i_phi); never@1515: C->record_for_igvn(charPos); never@1515: } never@1515: never@1515: { never@1515: // if (sign != 0) { never@1515: // buf [--charPos] = sign; never@1515: // } never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), never@1515: __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne), never@1515: PROB_FAIR, COUNT_UNKNOWN); never@1515: never@1515: final_merge->init_req(2, __ IfFalse(iff)); never@1515: final_mem->init_req(2, kit.memory(char_adr_idx)); never@1515: never@1515: kit.set_control(__ IfTrue(iff)); never@1515: if (kit.stopped()) { never@1515: final_merge->init_req(1, C->top()); never@1515: final_mem->init_req(1, C->top()); never@1515: } else { never@1515: Node* m1 = __ SubI(charPos, __ intcon(1)); never@1515: Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), never@1515: sign, T_CHAR, char_adr_idx); never@1515: never@1515: final_merge->init_req(1, kit.control()); never@1515: final_mem->init_req(1, st); never@1515: } never@1515: never@1515: kit.set_control(final_merge); never@1515: kit.set_memory(final_mem, char_adr_idx); never@1515: never@1515: C->record_for_igvn(final_merge); never@1515: C->record_for_igvn(final_mem); never@1515: } never@1515: } never@1515: never@1515: never@1515: Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) { never@1515: Node* string = str; kvn@3760: Node* offset = kit.load_String_offset(kit.control(), string); kvn@3760: Node* count = kit.load_String_length(kit.control(), string); kvn@3760: Node* value = kit.load_String_value (kit.control(), string); never@1515: never@1515: // copy the contents never@1515: if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) { never@1515: // For small constant strings just emit individual stores. never@1515: // A length of 6 seems like a good space/speed tradeof. never@1515: int c = count->get_int(); never@1515: int o = offset->get_int(); never@1515: const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr(); never@1515: ciTypeArray* value_array = t->const_oop()->as_type_array(); never@1515: for (int e = 0; e < c; e++) { never@1515: __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), never@1515: __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx); never@1515: start = __ AddI(start, __ intcon(1)); never@1515: } never@1515: } else { never@1515: Node* src_ptr = kit.array_element_address(value, offset, T_CHAR); never@1515: Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR); never@1515: Node* c = count; never@1515: Node* extra = NULL; never@1515: #ifdef _LP64 never@1515: c = __ ConvI2L(c); never@1515: extra = C->top(); never@1515: #endif never@1515: Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP, never@1515: OptoRuntime::fast_arraycopy_Type(), never@1515: CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()), never@1515: "jshort_disjoint_arraycopy", TypeAryPtr::CHARS, never@1515: src_ptr, dst_ptr, c, extra); never@1515: start = __ AddI(start, count); never@1515: } never@1515: return start; never@1515: } never@1515: never@1515: never@1515: void PhaseStringOpts::replace_string_concat(StringConcat* sc) { never@1515: // Log a little info about the transformation never@1515: sc->maybe_log_transform(); never@1515: never@1515: // pull the JVMState of the allocation into a SafePointNode to serve as never@1515: // as a shim for the insertion of the new code. never@1515: JVMState* jvms = sc->begin()->jvms()->clone_shallow(C); never@1515: uint size = sc->begin()->req(); kvn@4115: SafePointNode* map = new (C) SafePointNode(size, jvms); never@1515: never@1515: // copy the control and memory state from the final call into our never@1515: // new starting state. This allows any preceeding tests to feed never@1515: // into the new section of code. never@1515: for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) { never@1515: map->init_req(i1, sc->end()->in(i1)); never@1515: } never@1515: // blow away old allocation arguments never@1515: for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) { never@1515: map->init_req(i1, C->top()); never@1515: } never@1515: // Copy the rest of the inputs for the JVMState never@1515: for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) { never@1515: map->init_req(i1, sc->begin()->in(i1)); never@1515: } never@1515: // Make sure the memory state is a MergeMem for parsing. never@1515: if (!map->in(TypeFunc::Memory)->is_MergeMem()) { never@1515: map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory))); never@1515: } never@1515: never@1515: jvms->set_map(map); never@1515: map->ensure_stack(jvms, jvms->method()->max_stack()); never@1515: never@1515: never@1515: // disconnect all the old StringBuilder calls from the graph never@1515: sc->eliminate_unneeded_control(); never@1515: never@1515: // At this point all the old work has been completely removed from never@1515: // the graph and the saved JVMState exists at the point where the never@1515: // final toString call used to be. never@1515: GraphKit kit(jvms); never@1515: never@1515: // There may be uncommon traps which are still using the never@1515: // intermediate states and these need to be rewritten to point at never@1515: // the JVMState at the beginning of the transformation. never@1515: sc->convert_uncommon_traps(kit, jvms); never@1515: never@1515: // Now insert the logic to compute the size of the string followed never@1515: // by all the logic to construct array and resulting string. never@1515: never@1515: Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string())); never@1515: never@1515: // Create a region for the overflow checks to merge into. never@1515: int args = MAX2(sc->num_arguments(), 1); kvn@4115: RegionNode* overflow = new (C) RegionNode(args); never@1515: kit.gvn().set_type(overflow, Type::CONTROL); never@1515: never@1515: // Create a hook node to hold onto the individual sizes since they never@1515: // are need for the copying phase. kvn@4115: Node* string_sizes = new (C) Node(args); never@1515: never@1515: Node* length = __ intcon(0); never@1515: for (int argi = 0; argi < sc->num_arguments(); argi++) { never@1515: Node* arg = sc->argument(argi); never@1515: switch (sc->mode(argi)) { never@1515: case StringConcat::IntMode: { never@1515: Node* string_size = int_stringSize(kit, arg); never@1515: never@1515: // accumulate total never@1515: length = __ AddI(length, string_size); never@1515: never@1515: // Cache this value for the use by int_toString never@1515: string_sizes->init_req(argi, string_size); never@1515: break; never@1515: } kvn@2413: case StringConcat::StringNullCheckMode: { kvn@2413: const Type* type = kit.gvn().type(arg); kvn@2413: assert(type != TypePtr::NULL_PTR, "missing check"); kvn@2413: if (!type->higher_equal(TypeInstPtr::NOTNULL)) { kvn@2413: // Null check with uncommont trap since kvn@2413: // StringBuilder(null) throws exception. kvn@2413: // Use special uncommon trap instead of kvn@2413: // calling normal do_null_check(). kvn@2413: Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); kvn@2413: IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); kvn@2413: overflow->add_req(__ IfFalse(iff)); kvn@2413: Node* notnull = __ IfTrue(iff); kvn@2413: kit.set_control(notnull); // set control for the cast_not_null kvn@2413: arg = kit.cast_not_null(arg, false); kvn@2413: sc->set_argument(argi, arg); kvn@2413: } kvn@2413: assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity"); kvn@2413: // Fallthrough to add string length. kvn@2413: } never@1515: case StringConcat::StringMode: { never@1515: const Type* type = kit.gvn().type(arg); never@1515: if (type == TypePtr::NULL_PTR) { never@1515: // replace the argument with the null checked version never@1515: arg = null_string; never@1515: sc->set_argument(argi, arg); never@1515: } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) { never@1515: // s = s != null ? s : "null"; never@1515: // length = length + (s.count - s.offset); kvn@4115: RegionNode *r = new (C) RegionNode(3); never@1515: kit.gvn().set_type(r, Type::CONTROL); kvn@4115: Node *phi = new (C) PhiNode(r, type); never@1515: kit.gvn().set_type(phi, phi->bottom_type()); never@1515: Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); never@1515: Node* notnull = __ IfTrue(iff); never@1515: Node* isnull = __ IfFalse(iff); never@1685: kit.set_control(notnull); // set control for the cast_not_null never@1515: r->init_req(1, notnull); never@1685: phi->init_req(1, kit.cast_not_null(arg, false)); never@1515: r->init_req(2, isnull); never@1515: phi->init_req(2, null_string); never@1515: kit.set_control(r); never@1515: C->record_for_igvn(r); never@1515: C->record_for_igvn(phi); never@1515: // replace the argument with the null checked version never@1515: arg = phi; never@1515: sc->set_argument(argi, arg); never@1515: } kvn@3760: kvn@3760: Node* count = kit.load_String_length(kit.control(), arg); kvn@3760: never@1515: length = __ AddI(length, count); never@1515: string_sizes->init_req(argi, NULL); never@1515: break; never@1515: } never@1515: case StringConcat::CharMode: { never@1515: // one character only never@1515: length = __ AddI(length, __ intcon(1)); never@1515: break; never@1515: } never@1515: default: never@1515: ShouldNotReachHere(); never@1515: } never@1515: if (argi > 0) { never@1515: // Check that the sum hasn't overflowed never@1515: IfNode* iff = kit.create_and_map_if(kit.control(), never@1515: __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt), never@1515: PROB_MIN, COUNT_UNKNOWN); never@1515: kit.set_control(__ IfFalse(iff)); never@1515: overflow->set_req(argi, __ IfTrue(iff)); never@1515: } never@1515: } never@1515: never@1515: { never@1515: // Hook never@1515: PreserveJVMState pjvms(&kit); never@1515: kit.set_control(overflow); kvn@2413: C->record_for_igvn(overflow); never@1515: kit.uncommon_trap(Deoptimization::Reason_intrinsic, never@1515: Deoptimization::Action_make_not_entrant); never@1515: } never@1515: roland@4357: Node* result; roland@4357: if (!kit.stopped()) { roland@4357: roland@4357: // length now contains the number of characters needed for the roland@4357: // char[] so create a new AllocateArray for the char[] roland@4357: Node* char_array = NULL; roland@4357: { roland@4357: PreserveReexecuteState preexecs(&kit); roland@4357: // The original jvms is for an allocation of either a String or roland@4357: // StringBuffer so no stack adjustment is necessary for proper roland@4357: // reexecution. If we deoptimize in the slow path the bytecode roland@4357: // will be reexecuted and the char[] allocation will be thrown away. roland@4357: kit.jvms()->set_should_reexecute(true); roland@4357: char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))), roland@4357: length, 1); roland@4357: } roland@4357: roland@4357: // Mark the allocation so that zeroing is skipped since the code roland@4357: // below will overwrite the entire array roland@4357: AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn); roland@4357: char_alloc->maybe_set_complete(_gvn); roland@4357: roland@4357: // Now copy the string representations into the final char[] roland@4357: Node* start = __ intcon(0); roland@4357: for (int argi = 0; argi < sc->num_arguments(); argi++) { roland@4357: Node* arg = sc->argument(argi); roland@4357: switch (sc->mode(argi)) { roland@4357: case StringConcat::IntMode: { roland@4357: Node* end = __ AddI(start, string_sizes->in(argi)); roland@4357: // getChars words backwards so pass the ending point as well as the start roland@4357: int_getChars(kit, arg, char_array, start, end); roland@4357: start = end; roland@4357: break; roland@4357: } roland@4357: case StringConcat::StringNullCheckMode: roland@4357: case StringConcat::StringMode: { roland@4357: start = copy_string(kit, arg, char_array, start); roland@4357: break; roland@4357: } roland@4357: case StringConcat::CharMode: { roland@4357: __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), roland@4357: arg, T_CHAR, char_adr_idx); roland@4357: start = __ AddI(start, __ intcon(1)); roland@4357: break; roland@4357: } roland@4357: default: roland@4357: ShouldNotReachHere(); roland@4357: } roland@4357: } roland@4357: roland@4357: // If we're not reusing an existing String allocation then allocate one here. roland@4357: result = sc->string_alloc(); roland@4357: if (result == NULL) { roland@4357: PreserveReexecuteState preexecs(&kit); roland@4357: // The original jvms is for an allocation of either a String or roland@4357: // StringBuffer so no stack adjustment is necessary for proper roland@4357: // reexecution. roland@4357: kit.jvms()->set_should_reexecute(true); roland@4357: result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass()))); roland@4357: } roland@4357: roland@4357: // Intialize the string roland@4357: if (java_lang_String::has_offset_field()) { roland@4357: kit.store_String_offset(kit.control(), result, __ intcon(0)); roland@4357: kit.store_String_length(kit.control(), result, length); roland@4357: } roland@4357: kit.store_String_value(kit.control(), result, char_array); roland@4357: } else { roland@4357: result = C->top(); never@1515: } never@1515: // hook up the outgoing control and result never@1515: kit.replace_call(sc->end(), result); never@1515: never@1515: // Unhook any hook nodes bharadwaj@4315: string_sizes->disconnect_inputs(NULL, C); never@1515: sc->cleanup(); never@1515: }