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