stringopts.cpp revision 1978:2ddb2fab82cb
1193640Sariff/* 2193640Sariff * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 3193640Sariff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4193640Sariff * 5193640Sariff * This code is free software; you can redistribute it and/or modify it 6193640Sariff * under the terms of the GNU General Public License version 2 only, as 7193640Sariff * published by the Free Software Foundation. 8193640Sariff * 9193640Sariff * This code is distributed in the hope that it will be useful, but WITHOUT 10193640Sariff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11193640Sariff * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12193640Sariff * version 2 for more details (a copy is included in the LICENSE file that 13193640Sariff * accompanied this code). 14193640Sariff * 15193640Sariff * You should have received a copy of the GNU General Public License version 16193640Sariff * 2 along with this work; if not, write to the Free Software Foundation, 17193640Sariff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18193640Sariff * 19193640Sariff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20193640Sariff * or visit www.oracle.com if you need additional information or have any 21193640Sariff * questions. 22193640Sariff * 23193640Sariff */ 24193640Sariff 25193640Sariff#include "precompiled.hpp" 26193640Sariff#include "compiler/compileLog.hpp" 27193640Sariff#include "opto/addnode.hpp" 28193640Sariff#include "opto/callGenerator.hpp" 29193640Sariff#include "opto/callnode.hpp" 30193640Sariff#include "opto/divnode.hpp" 31193640Sariff#include "opto/graphKit.hpp" 32193640Sariff#include "opto/idealKit.hpp" 33193640Sariff#include "opto/rootnode.hpp" 34193640Sariff#include "opto/runtime.hpp" 35193640Sariff#include "opto/stringopts.hpp" 36193640Sariff#include "opto/subnode.hpp" 37193640Sariff 38193640Sariff#define __ kit. 39193640Sariff 40193640Sariffclass StringConcat : public ResourceObj { 41193640Sariff private: 42193640Sariff PhaseStringOpts* _stringopts; 43193640Sariff Node* _string_alloc; 44193640Sariff AllocateNode* _begin; // The allocation the begins the pattern 45193640Sariff CallStaticJavaNode* _end; // The final call of the pattern. Will either be 46193640Sariff // SB.toString or or String.<init>(SB.toString) 47193640Sariff bool _multiple; // indicates this is a fusion of two or more 48193640Sariff // separate StringBuilders 49193640Sariff 50193640Sariff Node* _arguments; // The list of arguments to be concatenated 51193640Sariff GrowableArray<int> _mode; // into a String along with a mode flag 52193640Sariff // indicating how to treat the value. 53193640Sariff 54193640Sariff Node_List _control; // List of control nodes that will be deleted 55193640Sariff Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten 56193640Sariff // to restart at the initial JVMState. 57193640Sariff public: 58193640Sariff // Mode for converting arguments to Strings 59193640Sariff enum { 60193640Sariff StringMode, 61193640Sariff IntMode, 62193640Sariff CharMode, 63193640Sariff StringNullCheckMode 64193640Sariff }; 65193640Sariff 66193640Sariff StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end): 67193640Sariff _end(end), 68193640Sariff _begin(NULL), 69193640Sariff _multiple(false), 70193640Sariff _string_alloc(NULL), 71193640Sariff _stringopts(stringopts) { 72193640Sariff _arguments = new (_stringopts->C, 1) Node(1); 73193640Sariff _arguments->del_req(0); 74193640Sariff } 75193640Sariff 76193640Sariff bool validate_control_flow(); 77193640Sariff 78193640Sariff void merge_add() { 79193640Sariff#if 0 80193640Sariff // XXX This is place holder code for reusing an existing String 81193640Sariff // allocation but the logic for checking the state safety is 82193640Sariff // probably inadequate at the moment. 83193640Sariff CallProjections endprojs; 84193640Sariff sc->end()->extract_projections(&endprojs, false); 85193640Sariff if (endprojs.resproj != NULL) { 86193640Sariff for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) { 87193640Sariff CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 88193640Sariff if (use != NULL && use->method() != NULL && 89193640Sariff use->method()->intrinsic_id() == vmIntrinsics::_String_String && 90193640Sariff use->in(TypeFunc::Parms + 1) == endprojs.resproj) { 91193640Sariff // Found useless new String(sb.toString()) so reuse the newly allocated String 92193640Sariff // when creating the result instead of allocating a new one. 93193640Sariff sc->set_string_alloc(use->in(TypeFunc::Parms)); 94193640Sariff sc->set_end(use); 95193640Sariff } 96193640Sariff } 97193640Sariff } 98193640Sariff#endif 99193640Sariff } 100193640Sariff 101193640Sariff StringConcat* merge(StringConcat* other, Node* arg); 102193640Sariff 103193640Sariff void set_allocation(AllocateNode* alloc) { 104193640Sariff _begin = alloc; 105193640Sariff } 106193640Sariff 107193640Sariff void append(Node* value, int mode) { 108193640Sariff _arguments->add_req(value); 109193640Sariff _mode.append(mode); 110193640Sariff } 111193640Sariff void push(Node* value, int mode) { 112193640Sariff _arguments->ins_req(0, value); 113193640Sariff _mode.insert_before(0, mode); 114193640Sariff } 115193640Sariff void push_string(Node* value) { 116193640Sariff push(value, StringMode); 117193640Sariff } 118193640Sariff void push_string_null_check(Node* value) { 119193640Sariff push(value, StringNullCheckMode); 120193640Sariff } 121193640Sariff void push_int(Node* value) { 122193640Sariff push(value, IntMode); 123193640Sariff } 124193640Sariff void push_char(Node* value) { 125193640Sariff push(value, CharMode); 126193640Sariff } 127193640Sariff 128193640Sariff Node* argument(int i) { 129193640Sariff return _arguments->in(i); 130193640Sariff } 131193640Sariff void set_argument(int i, Node* value) { 132193640Sariff _arguments->set_req(i, value); 133193640Sariff } 134193640Sariff int num_arguments() { 135193640Sariff return _mode.length(); 136193640Sariff } 137193640Sariff int mode(int i) { 138193640Sariff return _mode.at(i); 139193640Sariff } 140193640Sariff void add_control(Node* ctrl) { 141193640Sariff assert(!_control.contains(ctrl), "only push once"); 142193640Sariff _control.push(ctrl); 143193640Sariff } 144193640Sariff CallStaticJavaNode* end() { return _end; } 145193640Sariff AllocateNode* begin() { return _begin; } 146193640Sariff Node* string_alloc() { return _string_alloc; } 147193640Sariff 148193640Sariff void eliminate_unneeded_control(); 149193640Sariff void eliminate_initialize(InitializeNode* init); 150193640Sariff void eliminate_call(CallNode* call); 151193640Sariff 152193640Sariff void maybe_log_transform() { 153193640Sariff CompileLog* log = _stringopts->C->log(); 154193640Sariff if (log != NULL) { 155193640Sariff log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'", 156193640Sariff num_arguments(), 157193640Sariff _string_alloc != NULL, 158193640Sariff _multiple); 159193640Sariff JVMState* p = _begin->jvms(); 160193640Sariff while (p != NULL) { 161193640Sariff log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 162193640Sariff p = p->caller(); 163193640Sariff } 164193640Sariff log->tail("replace_string_concat"); 165193640Sariff } 166193640Sariff } 167193640Sariff 168193640Sariff void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) { 169193640Sariff for (uint u = 0; u < _uncommon_traps.size(); u++) { 170193640Sariff Node* uct = _uncommon_traps.at(u); 171193640Sariff 172193640Sariff // Build a new call using the jvms state of the allocate 173193640Sariff address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point(); 174193640Sariff const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type(); 175193640Sariff int size = call_type->domain()->cnt(); 176193640Sariff const TypePtr* no_memory_effects = NULL; 177193640Sariff Compile* C = _stringopts->C; 178193640Sariff CallStaticJavaNode* call = new (C, size) CallStaticJavaNode(call_type, call_addr, "uncommon_trap", 179193640Sariff jvms->bci(), no_memory_effects); 180193640Sariff for (int e = 0; e < TypeFunc::Parms; e++) { 181193640Sariff call->init_req(e, uct->in(e)); 182193640Sariff } 183193640Sariff // Set the trap request to record intrinsic failure if this trap 184193640Sariff // is taken too many times. Ideally we would handle then traps by 185193640Sariff // doing the original bookkeeping in the MDO so that if it caused 186193640Sariff // the code to be thrown out we could still recompile and use the 187193640Sariff // optimization. Failing the uncommon traps doesn't really mean 188193640Sariff // that the optimization is a bad idea but there's no other way to 189193640Sariff // do the MDO updates currently. 190193640Sariff int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic, 191193640Sariff Deoptimization::Action_make_not_entrant); 192193640Sariff call->init_req(TypeFunc::Parms, __ intcon(trap_request)); 193193640Sariff kit.add_safepoint_edges(call); 194193640Sariff 195193640Sariff _stringopts->gvn()->transform(call); 196193640Sariff C->gvn_replace_by(uct, call); 197193640Sariff uct->disconnect_inputs(NULL); 198193640Sariff } 199193640Sariff } 200193640Sariff 201193640Sariff void cleanup() { 202193640Sariff // disconnect the hook node 203193640Sariff _arguments->disconnect_inputs(NULL); 204193640Sariff } 205193640Sariff}; 206193640Sariff 207193640Sariff 208193640Sariffvoid StringConcat::eliminate_unneeded_control() { 209193640Sariff eliminate_initialize(begin()->initialization()); 210193640Sariff for (uint i = 0; i < _control.size(); i++) { 211193640Sariff Node* n = _control.at(i); 212193640Sariff if (n->is_Call()) { 213193640Sariff if (n != _end) { 214193640Sariff eliminate_call(n->as_Call()); 215193640Sariff } 216193640Sariff } else if (n->is_IfTrue()) { 217193640Sariff Compile* C = _stringopts->C; 218193640Sariff C->gvn_replace_by(n, n->in(0)->in(0)); 219193640Sariff C->gvn_replace_by(n->in(0), C->top()); 220193640Sariff } 221193640Sariff } 222193640Sariff} 223193640Sariff 224193640Sariff 225193640SariffStringConcat* StringConcat::merge(StringConcat* other, Node* arg) { 226193640Sariff StringConcat* result = new StringConcat(_stringopts, _end); 227193640Sariff for (uint x = 0; x < _control.size(); x++) { 228193640Sariff Node* n = _control.at(x); 229193640Sariff if (n->is_Call()) { 230193640Sariff result->_control.push(n); 231193640Sariff } 232193640Sariff } 233193640Sariff for (uint x = 0; x < other->_control.size(); x++) { 234193640Sariff Node* n = other->_control.at(x); 235193640Sariff if (n->is_Call()) { 236193640Sariff result->_control.push(n); 237193640Sariff } 238193640Sariff } 239193640Sariff assert(result->_control.contains(other->_end), "what?"); 240193640Sariff assert(result->_control.contains(_begin), "what?"); 241193640Sariff for (int x = 0; x < num_arguments(); x++) { 242193640Sariff if (argument(x) == arg) { 243193640Sariff // replace the toString result with the all the arguments that 244193640Sariff // made up the other StringConcat 245193640Sariff for (int y = 0; y < other->num_arguments(); y++) { 246193640Sariff result->append(other->argument(y), other->mode(y)); 247193640Sariff } 248193640Sariff } else { 249193640Sariff result->append(argument(x), mode(x)); 250193640Sariff } 251193640Sariff } 252193640Sariff result->set_allocation(other->_begin); 253193640Sariff result->_multiple = true; 254193640Sariff return result; 255193640Sariff} 256193640Sariff 257193640Sariff 258193640Sariffvoid StringConcat::eliminate_call(CallNode* call) { 259193640Sariff Compile* C = _stringopts->C; 260193640Sariff CallProjections projs; 261193640Sariff call->extract_projections(&projs, false); 262193640Sariff if (projs.fallthrough_catchproj != NULL) { 263193640Sariff C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control)); 264193640Sariff } 265193640Sariff if (projs.fallthrough_memproj != NULL) { 266193640Sariff C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory)); 267193640Sariff } 268193640Sariff if (projs.catchall_memproj != NULL) { 269193640Sariff C->gvn_replace_by(projs.catchall_memproj, C->top()); 270193640Sariff } 271193640Sariff if (projs.fallthrough_ioproj != NULL) { 272193640Sariff C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O)); 273193640Sariff } 274193640Sariff if (projs.catchall_ioproj != NULL) { 275193640Sariff C->gvn_replace_by(projs.catchall_ioproj, C->top()); 276193640Sariff } 277193640Sariff if (projs.catchall_catchproj != NULL) { 278193640Sariff // EA can't cope with the partially collapsed graph this 279193640Sariff // creates so put it on the worklist to be collapsed later. 280193640Sariff for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) { 281193640Sariff Node *use = i.get(); 282193640Sariff int opc = use->Opcode(); 283193640Sariff if (opc == Op_CreateEx || opc == Op_Region) { 284193640Sariff _stringopts->record_dead_node(use); 285193640Sariff } 286193640Sariff } 287193640Sariff C->gvn_replace_by(projs.catchall_catchproj, C->top()); 288193640Sariff } 289193640Sariff if (projs.resproj != NULL) { 290193640Sariff C->gvn_replace_by(projs.resproj, C->top()); 291193640Sariff } 292193640Sariff C->gvn_replace_by(call, C->top()); 293193640Sariff} 294193640Sariff 295193640Sariffvoid StringConcat::eliminate_initialize(InitializeNode* init) { 296193640Sariff Compile* C = _stringopts->C; 297193640Sariff 298193640Sariff // Eliminate Initialize node. 299193640Sariff assert(init->outcnt() <= 2, "only a control and memory projection expected"); 300193640Sariff assert(init->req() <= InitializeNode::RawStores, "no pending inits"); 301193640Sariff Node *ctrl_proj = init->proj_out(TypeFunc::Control); 302193640Sariff if (ctrl_proj != NULL) { 303193640Sariff C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control)); 304193640Sariff } 305193640Sariff Node *mem_proj = init->proj_out(TypeFunc::Memory); 306193640Sariff if (mem_proj != NULL) { 307193640Sariff Node *mem = init->in(TypeFunc::Memory); 308193640Sariff C->gvn_replace_by(mem_proj, mem); 309193640Sariff } 310193640Sariff C->gvn_replace_by(init, C->top()); 311193640Sariff init->disconnect_inputs(NULL); 312193640Sariff} 313193640Sariff 314193640SariffNode_List PhaseStringOpts::collect_toString_calls() { 315193640Sariff Node_List string_calls; 316193640Sariff Node_List worklist; 317193640Sariff 318193640Sariff _visited.Clear(); 319193640Sariff 320193640Sariff // Prime the worklist 321193640Sariff for (uint i = 1; i < C->root()->len(); i++) { 322193640Sariff Node* n = C->root()->in(i); 323193640Sariff if (n != NULL && !_visited.test_set(n->_idx)) { 324193640Sariff worklist.push(n); 325193640Sariff } 326193640Sariff } 327193640Sariff 328193640Sariff while (worklist.size() > 0) { 329193640Sariff Node* ctrl = worklist.pop(); 330193640Sariff if (ctrl->is_CallStaticJava()) { 331193640Sariff CallStaticJavaNode* csj = ctrl->as_CallStaticJava(); 332193640Sariff ciMethod* m = csj->method(); 333193640Sariff if (m != NULL && 334193640Sariff (m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString || 335193640Sariff m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString)) { 336193640Sariff string_calls.push(csj); 337193640Sariff } 338193640Sariff } 339193640Sariff if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) { 340193640Sariff worklist.push(ctrl->in(0)); 341193640Sariff } 342193640Sariff if (ctrl->is_Region()) { 343193640Sariff for (uint i = 1; i < ctrl->len(); i++) { 344193640Sariff if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) { 345193640Sariff worklist.push(ctrl->in(i)); 346193640Sariff } 347193640Sariff } 348193640Sariff } 349193640Sariff } 350193640Sariff return string_calls; 351193640Sariff} 352193640Sariff 353193640Sariff 354193640SariffStringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) { 355193640Sariff ciMethod* m = call->method(); 356193640Sariff ciSymbol* string_sig; 357193640Sariff ciSymbol* int_sig; 358193640Sariff ciSymbol* char_sig; 359193640Sariff if (m->holder() == C->env()->StringBuilder_klass()) { 360193640Sariff string_sig = ciSymbol::String_StringBuilder_signature(); 361193640Sariff int_sig = ciSymbol::int_StringBuilder_signature(); 362193640Sariff char_sig = ciSymbol::char_StringBuilder_signature(); 363193640Sariff } else if (m->holder() == C->env()->StringBuffer_klass()) { 364193640Sariff string_sig = ciSymbol::String_StringBuffer_signature(); 365193640Sariff int_sig = ciSymbol::int_StringBuffer_signature(); 366193640Sariff char_sig = ciSymbol::char_StringBuffer_signature(); 367193640Sariff } else { 368193640Sariff return NULL; 369193640Sariff } 370193640Sariff#ifndef PRODUCT 371193640Sariff if (PrintOptimizeStringConcat) { 372193640Sariff tty->print("considering toString call in "); 373193640Sariff call->jvms()->dump_spec(tty); tty->cr(); 374193640Sariff } 375193640Sariff#endif 376193640Sariff 377193640Sariff StringConcat* sc = new StringConcat(this, call); 378193640Sariff 379193640Sariff AllocateNode* alloc = NULL; 380193640Sariff InitializeNode* init = NULL; 381193640Sariff 382193640Sariff // possible opportunity for StringBuilder fusion 383193640Sariff CallStaticJavaNode* cnode = call; 384193640Sariff while (cnode) { 385193640Sariff Node* recv = cnode->in(TypeFunc::Parms)->uncast(); 386193640Sariff if (recv->is_Proj()) { 387193640Sariff recv = recv->in(0); 388193640Sariff } 389193640Sariff cnode = recv->isa_CallStaticJava(); 390193640Sariff if (cnode == NULL) { 391193640Sariff alloc = recv->isa_Allocate(); 392193640Sariff if (alloc == NULL) { 393193640Sariff break; 394193640Sariff } 395193640Sariff // Find the constructor call 396193640Sariff Node* result = alloc->result_cast(); 397193640Sariff if (result == NULL || !result->is_CheckCastPP()) { 398193640Sariff // strange looking allocation 399193640Sariff#ifndef PRODUCT 400193640Sariff if (PrintOptimizeStringConcat) { 401193640Sariff tty->print("giving up because allocation looks strange "); 402193640Sariff alloc->jvms()->dump_spec(tty); tty->cr(); 403193640Sariff } 404193640Sariff#endif 405193640Sariff break; 406193640Sariff } 407193640Sariff Node* constructor = NULL; 408193640Sariff for (SimpleDUIterator i(result); i.has_next(); i.next()) { 409193640Sariff CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 410193640Sariff if (use != NULL && 411193640Sariff use->method() != NULL && 412193640Sariff !use->method()->is_static() && 413193640Sariff use->method()->name() == ciSymbol::object_initializer_name() && 414193640Sariff use->method()->holder() == m->holder()) { 415193640Sariff // Matched the constructor. 416193640Sariff ciSymbol* sig = use->method()->signature()->as_symbol(); 417193640Sariff if (sig == ciSymbol::void_method_signature() || 418193640Sariff sig == ciSymbol::int_void_signature() || 419193640Sariff sig == ciSymbol::string_void_signature()) { 420193640Sariff if (sig == ciSymbol::string_void_signature()) { 421193640Sariff // StringBuilder(String) so pick this up as the first argument 422193640Sariff assert(use->in(TypeFunc::Parms + 1) != NULL, "what?"); 423193640Sariff const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1)); 424193640Sariff if (type == TypePtr::NULL_PTR) { 425193640Sariff // StringBuilder(null) throws exception. 426193640Sariff#ifndef PRODUCT 427193640Sariff if (PrintOptimizeStringConcat) { 428193640Sariff tty->print("giving up because StringBuilder(null) throws exception"); 429193640Sariff alloc->jvms()->dump_spec(tty); tty->cr(); 430193640Sariff } 431193640Sariff#endif 432193640Sariff return NULL; 433193640Sariff } 434193640Sariff // StringBuilder(str) argument needs null check. 435193640Sariff sc->push_string_null_check(use->in(TypeFunc::Parms + 1)); 436193640Sariff } 437193640Sariff // The int variant takes an initial size for the backing 438193640Sariff // array so just treat it like the void version. 439193640Sariff constructor = use; 440193640Sariff } else { 441193640Sariff#ifndef PRODUCT 442193640Sariff if (PrintOptimizeStringConcat) { 443193640Sariff tty->print("unexpected constructor signature: %s", sig->as_utf8()); 444193640Sariff } 445193640Sariff#endif 446193640Sariff } 447193640Sariff break; 448193640Sariff } 449193640Sariff } 450193640Sariff if (constructor == NULL) { 451193640Sariff // couldn't find constructor 452193640Sariff#ifndef PRODUCT 453193640Sariff if (PrintOptimizeStringConcat) { 454193640Sariff tty->print("giving up because couldn't find constructor "); 455193640Sariff alloc->jvms()->dump_spec(tty); tty->cr(); 456193640Sariff } 457193640Sariff#endif 458193640Sariff break; 459193640Sariff } 460193640Sariff 461193640Sariff // Walked all the way back and found the constructor call so see 462193640Sariff // if this call converted into a direct string concatenation. 463193640Sariff sc->add_control(call); 464193640Sariff sc->add_control(constructor); 465193640Sariff sc->add_control(alloc); 466193640Sariff sc->set_allocation(alloc); 467193640Sariff if (sc->validate_control_flow()) { 468 return sc; 469 } else { 470 return NULL; 471 } 472 } else if (cnode->method() == NULL) { 473 break; 474 } else if (!cnode->method()->is_static() && 475 cnode->method()->holder() == m->holder() && 476 cnode->method()->name() == ciSymbol::append_name() && 477 (cnode->method()->signature()->as_symbol() == string_sig || 478 cnode->method()->signature()->as_symbol() == char_sig || 479 cnode->method()->signature()->as_symbol() == int_sig)) { 480 sc->add_control(cnode); 481 Node* arg = cnode->in(TypeFunc::Parms + 1); 482 if (cnode->method()->signature()->as_symbol() == int_sig) { 483 sc->push_int(arg); 484 } else if (cnode->method()->signature()->as_symbol() == char_sig) { 485 sc->push_char(arg); 486 } else { 487 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) { 488 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 489 if (csj->method() != NULL && 490 csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString) { 491 sc->add_control(csj); 492 sc->push_int(csj->in(TypeFunc::Parms)); 493 continue; 494 } 495 } 496 sc->push_string(arg); 497 } 498 continue; 499 } else { 500 // some unhandled signature 501#ifndef PRODUCT 502 if (PrintOptimizeStringConcat) { 503 tty->print("giving up because encountered unexpected signature "); 504 cnode->tf()->dump(); tty->cr(); 505 cnode->in(TypeFunc::Parms + 1)->dump(); 506 } 507#endif 508 break; 509 } 510 } 511 return NULL; 512} 513 514 515PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*): 516 Phase(StringOpts), 517 _gvn(gvn), 518 _visited(Thread::current()->resource_area()) { 519 520 assert(OptimizeStringConcat, "shouldn't be here"); 521 522 size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"), 523 ciSymbol::make("[I"), true); 524 if (size_table_field == NULL) { 525 // Something wrong so give up. 526 assert(false, "why can't we find Integer.sizeTable?"); 527 return; 528 } 529 530 // Collect the types needed to talk about the various slices of memory 531 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(), 532 false, NULL, 0); 533 534 const TypePtr* value_field_type = string_type->add_offset(java_lang_String::value_offset_in_bytes()); 535 const TypePtr* offset_field_type = string_type->add_offset(java_lang_String::offset_offset_in_bytes()); 536 const TypePtr* count_field_type = string_type->add_offset(java_lang_String::count_offset_in_bytes()); 537 538 value_field_idx = C->get_alias_index(value_field_type); 539 count_field_idx = C->get_alias_index(count_field_type); 540 offset_field_idx = C->get_alias_index(offset_field_type); 541 char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS); 542 543 // For each locally allocated StringBuffer see if the usages can be 544 // collapsed into a single String construction. 545 546 // Run through the list of allocation looking for SB.toString to see 547 // if it's possible to fuse the usage of the SB into a single String 548 // construction. 549 GrowableArray<StringConcat*> concats; 550 Node_List toStrings = collect_toString_calls(); 551 while (toStrings.size() > 0) { 552 StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava()); 553 if (sc != NULL) { 554 concats.push(sc); 555 } 556 } 557 558 // try to coalesce separate concats 559 restart: 560 for (int c = 0; c < concats.length(); c++) { 561 StringConcat* sc = concats.at(c); 562 for (int i = 0; i < sc->num_arguments(); i++) { 563 Node* arg = sc->argument(i); 564 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) { 565 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 566 if (csj->method() != NULL && 567 (csj->method()->intrinsic_id() == vmIntrinsics::_StringBuilder_toString || 568 csj->method()->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) { 569 for (int o = 0; o < concats.length(); o++) { 570 if (c == o) continue; 571 StringConcat* other = concats.at(o); 572 if (other->end() == csj) { 573#ifndef PRODUCT 574 if (PrintOptimizeStringConcat) { 575 tty->print_cr("considering stacked concats"); 576 } 577#endif 578 579 StringConcat* merged = sc->merge(other, arg); 580 if (merged->validate_control_flow()) { 581#ifndef PRODUCT 582 if (PrintOptimizeStringConcat) { 583 tty->print_cr("stacking would succeed"); 584 } 585#endif 586 if (c < o) { 587 concats.remove_at(o); 588 concats.at_put(c, merged); 589 } else { 590 concats.remove_at(c); 591 concats.at_put(o, merged); 592 } 593 goto restart; 594 } else { 595#ifndef PRODUCT 596 if (PrintOptimizeStringConcat) { 597 tty->print_cr("stacking would fail"); 598 } 599#endif 600 } 601 } 602 } 603 } 604 } 605 } 606 } 607 608 609 for (int c = 0; c < concats.length(); c++) { 610 StringConcat* sc = concats.at(c); 611 replace_string_concat(sc); 612 } 613 614 remove_dead_nodes(); 615} 616 617void PhaseStringOpts::record_dead_node(Node* dead) { 618 dead_worklist.push(dead); 619} 620 621void PhaseStringOpts::remove_dead_nodes() { 622 // Delete any dead nodes to make things clean enough that escape 623 // analysis doesn't get unhappy. 624 while (dead_worklist.size() > 0) { 625 Node* use = dead_worklist.pop(); 626 int opc = use->Opcode(); 627 switch (opc) { 628 case Op_Region: { 629 uint i = 1; 630 for (i = 1; i < use->req(); i++) { 631 if (use->in(i) != C->top()) { 632 break; 633 } 634 } 635 if (i >= use->req()) { 636 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 637 Node* m = i.get(); 638 if (m->is_Phi()) { 639 dead_worklist.push(m); 640 } 641 } 642 C->gvn_replace_by(use, C->top()); 643 } 644 break; 645 } 646 case Op_AddP: 647 case Op_CreateEx: { 648 // Recurisvely clean up references to CreateEx so EA doesn't 649 // get unhappy about the partially collapsed graph. 650 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 651 Node* m = i.get(); 652 if (m->is_AddP()) { 653 dead_worklist.push(m); 654 } 655 } 656 C->gvn_replace_by(use, C->top()); 657 break; 658 } 659 case Op_Phi: 660 if (use->in(0) == C->top()) { 661 C->gvn_replace_by(use, C->top()); 662 } 663 break; 664 } 665 } 666} 667 668 669bool StringConcat::validate_control_flow() { 670 // We found all the calls and arguments now lets see if it's 671 // safe to transform the graph as we would expect. 672 673 // Check to see if this resulted in too many uncommon traps previously 674 if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(), 675 Deoptimization::Reason_intrinsic)) { 676 return false; 677 } 678 679 // Walk backwards over the control flow from toString to the 680 // allocation and make sure all the control flow is ok. This 681 // means it's either going to be eliminated once the calls are 682 // removed or it can safely be transformed into an uncommon 683 // trap. 684 685 int null_check_count = 0; 686 Unique_Node_List ctrl_path; 687 688 assert(_control.contains(_begin), "missing"); 689 assert(_control.contains(_end), "missing"); 690 691 // Collect the nodes that we know about and will eliminate into ctrl_path 692 for (uint i = 0; i < _control.size(); i++) { 693 // Push the call and it's control projection 694 Node* n = _control.at(i); 695 if (n->is_Allocate()) { 696 AllocateNode* an = n->as_Allocate(); 697 InitializeNode* init = an->initialization(); 698 ctrl_path.push(init); 699 ctrl_path.push(init->as_Multi()->proj_out(0)); 700 } 701 if (n->is_Call()) { 702 CallNode* cn = n->as_Call(); 703 ctrl_path.push(cn); 704 ctrl_path.push(cn->proj_out(0)); 705 ctrl_path.push(cn->proj_out(0)->unique_out()); 706 ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0)); 707 } else { 708 ShouldNotReachHere(); 709 } 710 } 711 712 // Skip backwards through the control checking for unexpected contro flow 713 Node* ptr = _end; 714 bool fail = false; 715 while (ptr != _begin) { 716 if (ptr->is_Call() && ctrl_path.member(ptr)) { 717 ptr = ptr->in(0); 718 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) { 719 ptr = ptr->in(0)->in(0)->in(0); 720 assert(ctrl_path.member(ptr), "should be a known piece of control"); 721 } else if (ptr->is_IfTrue()) { 722 IfNode* iff = ptr->in(0)->as_If(); 723 BoolNode* b = iff->in(1)->isa_Bool(); 724 Node* cmp = b->in(1); 725 Node* v1 = cmp->in(1); 726 Node* v2 = cmp->in(2); 727 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con); 728 729 // Null check of the return of append which can simply be eliminated 730 if (b->_test._test == BoolTest::ne && 731 v2->bottom_type() == TypePtr::NULL_PTR && 732 v1->is_Proj() && ctrl_path.member(v1->in(0))) { 733 // NULL check of the return value of the append 734 null_check_count++; 735 if (otherproj->outcnt() == 1) { 736 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 737 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 738 ctrl_path.push(call); 739 } 740 } 741 _control.push(ptr); 742 ptr = ptr->in(0)->in(0); 743 continue; 744 } 745 746 // A test which leads to an uncommon trap which should be safe. 747 // Later this trap will be converted into a trap that restarts 748 // at the beginning. 749 if (otherproj->outcnt() == 1) { 750 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 751 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 752 // control flow leads to uct so should be ok 753 _uncommon_traps.push(call); 754 ctrl_path.push(call); 755 ptr = ptr->in(0)->in(0); 756 continue; 757 } 758 } 759 760#ifndef PRODUCT 761 // Some unexpected control flow we don't know how to handle. 762 if (PrintOptimizeStringConcat) { 763 tty->print_cr("failing with unknown test"); 764 b->dump(); 765 cmp->dump(); 766 v1->dump(); 767 v2->dump(); 768 tty->cr(); 769 } 770#endif 771 break; 772 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) { 773 ptr = ptr->in(0)->in(0); 774 } else if (ptr->is_Region()) { 775 Node* copy = ptr->as_Region()->is_copy(); 776 if (copy != NULL) { 777 ptr = copy; 778 continue; 779 } 780 if (ptr->req() == 3 && 781 ptr->in(1) != NULL && ptr->in(1)->is_Proj() && 782 ptr->in(2) != NULL && ptr->in(2)->is_Proj() && 783 ptr->in(1)->in(0) == ptr->in(2)->in(0) && 784 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) { 785 // Simple diamond. 786 // XXX should check for possibly merging stores. simple data merges are ok. 787 ptr = ptr->in(1)->in(0)->in(0); 788 continue; 789 } 790#ifndef PRODUCT 791 if (PrintOptimizeStringConcat) { 792 tty->print_cr("fusion would fail for region"); 793 _begin->dump(); 794 ptr->dump(2); 795 } 796#endif 797 fail = true; 798 break; 799 } else { 800 // other unknown control 801 if (!fail) { 802#ifndef PRODUCT 803 if (PrintOptimizeStringConcat) { 804 tty->print_cr("fusion would fail for"); 805 _begin->dump(); 806 } 807#endif 808 fail = true; 809 } 810#ifndef PRODUCT 811 if (PrintOptimizeStringConcat) { 812 ptr->dump(); 813 } 814#endif 815 ptr = ptr->in(0); 816 } 817 } 818#ifndef PRODUCT 819 if (PrintOptimizeStringConcat && fail) { 820 tty->cr(); 821 } 822#endif 823 if (fail) return !fail; 824 825 // Validate that all these results produced are contained within 826 // this cluster of objects. First collect all the results produced 827 // by calls in the region. 828 _stringopts->_visited.Clear(); 829 Node_List worklist; 830 Node* final_result = _end->proj_out(TypeFunc::Parms); 831 for (uint i = 0; i < _control.size(); i++) { 832 CallNode* cnode = _control.at(i)->isa_Call(); 833 if (cnode != NULL) { 834 _stringopts->_visited.test_set(cnode->_idx); 835 } 836 Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL; 837 if (result != NULL && result != final_result) { 838 worklist.push(result); 839 } 840 } 841 842 Node* last_result = NULL; 843 while (worklist.size() > 0) { 844 Node* result = worklist.pop(); 845 if (_stringopts->_visited.test_set(result->_idx)) 846 continue; 847 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 848 Node *use = i.get(); 849 if (ctrl_path.member(use)) { 850 // already checked this 851 continue; 852 } 853 int opc = use->Opcode(); 854 if (opc == Op_CmpP || opc == Op_Node) { 855 ctrl_path.push(use); 856 continue; 857 } 858 if (opc == Op_CastPP || opc == Op_CheckCastPP) { 859 for (SimpleDUIterator j(use); j.has_next(); j.next()) { 860 worklist.push(j.get()); 861 } 862 worklist.push(use->in(1)); 863 ctrl_path.push(use); 864 continue; 865 } 866#ifndef PRODUCT 867 if (PrintOptimizeStringConcat) { 868 if (result != last_result) { 869 last_result = result; 870 tty->print_cr("extra uses for result:"); 871 last_result->dump(); 872 } 873 use->dump(); 874 } 875#endif 876 fail = true; 877 break; 878 } 879 } 880 881#ifndef PRODUCT 882 if (PrintOptimizeStringConcat && !fail) { 883 ttyLocker ttyl; 884 tty->cr(); 885 tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size()); 886 _begin->jvms()->dump_spec(tty); tty->cr(); 887 for (int i = 0; i < num_arguments(); i++) { 888 argument(i)->dump(); 889 } 890 _control.dump(); 891 tty->cr(); 892 } 893#endif 894 895 return !fail; 896} 897 898Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) { 899 const TypeKlassPtr* klass_type = TypeKlassPtr::make(field->holder()); 900 Node* klass_node = __ makecon(klass_type); 901 BasicType bt = field->layout_type(); 902 ciType* field_klass = field->type(); 903 904 const Type *type; 905 if( bt == T_OBJECT ) { 906 if (!field->type()->is_loaded()) { 907 type = TypeInstPtr::BOTTOM; 908 } else if (field->is_constant()) { 909 // This can happen if the constant oop is non-perm. 910 ciObject* con = field->constant_value().as_object(); 911 // Do not "join" in the previous type; it doesn't add value, 912 // and may yield a vacuous result if the field is of interface type. 913 type = TypeOopPtr::make_from_constant(con)->isa_oopptr(); 914 assert(type != NULL, "field singleton type must be consistent"); 915 } else { 916 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 917 } 918 } else { 919 type = Type::get_const_basic_type(bt); 920 } 921 922 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()), 923 type, T_OBJECT, 924 C->get_alias_index(klass_type->add_offset(field->offset_in_bytes()))); 925} 926 927Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { 928 RegionNode *final_merge = new (C, 3) RegionNode(3); 929 kit.gvn().set_type(final_merge, Type::CONTROL); 930 Node* final_size = new (C, 3) PhiNode(final_merge, TypeInt::INT); 931 kit.gvn().set_type(final_size, TypeInt::INT); 932 933 IfNode* iff = kit.create_and_map_if(kit.control(), 934 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 935 PROB_FAIR, COUNT_UNKNOWN); 936 Node* is_min = __ IfFalse(iff); 937 final_merge->init_req(1, is_min); 938 final_size->init_req(1, __ intcon(11)); 939 940 kit.set_control(__ IfTrue(iff)); 941 if (kit.stopped()) { 942 final_merge->init_req(2, C->top()); 943 final_size->init_req(2, C->top()); 944 } else { 945 946 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 947 RegionNode *r = new (C, 3) RegionNode(3); 948 kit.gvn().set_type(r, Type::CONTROL); 949 Node *phi = new (C, 3) PhiNode(r, TypeInt::INT); 950 kit.gvn().set_type(phi, TypeInt::INT); 951 Node *size = new (C, 3) PhiNode(r, TypeInt::INT); 952 kit.gvn().set_type(size, TypeInt::INT); 953 Node* chk = __ CmpI(arg, __ intcon(0)); 954 Node* p = __ Bool(chk, BoolTest::lt); 955 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN); 956 Node* lessthan = __ IfTrue(iff); 957 Node* greaterequal = __ IfFalse(iff); 958 r->init_req(1, lessthan); 959 phi->init_req(1, __ SubI(__ intcon(0), arg)); 960 size->init_req(1, __ intcon(1)); 961 r->init_req(2, greaterequal); 962 phi->init_req(2, arg); 963 size->init_req(2, __ intcon(0)); 964 kit.set_control(r); 965 C->record_for_igvn(r); 966 C->record_for_igvn(phi); 967 C->record_for_igvn(size); 968 969 // for (int i=0; ; i++) 970 // if (x <= sizeTable[i]) 971 // return i+1; 972 RegionNode *loop = new (C, 3) RegionNode(3); 973 loop->init_req(1, kit.control()); 974 kit.gvn().set_type(loop, Type::CONTROL); 975 976 Node *index = new (C, 3) PhiNode(loop, TypeInt::INT); 977 index->init_req(1, __ intcon(0)); 978 kit.gvn().set_type(index, TypeInt::INT); 979 kit.set_control(loop); 980 Node* sizeTable = fetch_static_field(kit, size_table_field); 981 982 Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS); 983 C->record_for_igvn(value); 984 Node* limit = __ CmpI(phi, value); 985 Node* limitb = __ Bool(limit, BoolTest::le); 986 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN); 987 Node* lessEqual = __ IfTrue(iff2); 988 Node* greater = __ IfFalse(iff2); 989 990 loop->init_req(2, greater); 991 index->init_req(2, __ AddI(index, __ intcon(1))); 992 993 kit.set_control(lessEqual); 994 C->record_for_igvn(loop); 995 C->record_for_igvn(index); 996 997 final_merge->init_req(2, kit.control()); 998 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1))); 999 } 1000 1001 kit.set_control(final_merge); 1002 C->record_for_igvn(final_merge); 1003 C->record_for_igvn(final_size); 1004 1005 return final_size; 1006} 1007 1008void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) { 1009 RegionNode *final_merge = new (C, 4) RegionNode(4); 1010 kit.gvn().set_type(final_merge, Type::CONTROL); 1011 Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1012 kit.gvn().set_type(final_mem, Type::MEMORY); 1013 1014 // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive 1015 { 1016 // i == MIN_VALUE 1017 IfNode* iff = kit.create_and_map_if(kit.control(), 1018 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 1019 PROB_FAIR, COUNT_UNKNOWN); 1020 1021 Node* old_mem = kit.memory(char_adr_idx); 1022 1023 kit.set_control(__ IfFalse(iff)); 1024 if (kit.stopped()) { 1025 // Statically not equal to MIN_VALUE so this path is dead 1026 final_merge->init_req(3, kit.control()); 1027 } else { 1028 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())), 1029 char_array, start); 1030 final_merge->init_req(3, kit.control()); 1031 final_mem->init_req(3, kit.memory(char_adr_idx)); 1032 } 1033 1034 kit.set_control(__ IfTrue(iff)); 1035 kit.set_memory(old_mem, char_adr_idx); 1036 } 1037 1038 1039 // Simplified version of Integer.getChars 1040 1041 // int q, r; 1042 // int charPos = index; 1043 Node* charPos = end; 1044 1045 // char sign = 0; 1046 1047 Node* i = arg; 1048 Node* sign = __ intcon(0); 1049 1050 // if (i < 0) { 1051 // sign = '-'; 1052 // i = -i; 1053 // } 1054 { 1055 IfNode* iff = kit.create_and_map_if(kit.control(), 1056 __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt), 1057 PROB_FAIR, COUNT_UNKNOWN); 1058 1059 RegionNode *merge = new (C, 3) RegionNode(3); 1060 kit.gvn().set_type(merge, Type::CONTROL); 1061 i = new (C, 3) PhiNode(merge, TypeInt::INT); 1062 kit.gvn().set_type(i, TypeInt::INT); 1063 sign = new (C, 3) PhiNode(merge, TypeInt::INT); 1064 kit.gvn().set_type(sign, TypeInt::INT); 1065 1066 merge->init_req(1, __ IfTrue(iff)); 1067 i->init_req(1, __ SubI(__ intcon(0), arg)); 1068 sign->init_req(1, __ intcon('-')); 1069 merge->init_req(2, __ IfFalse(iff)); 1070 i->init_req(2, arg); 1071 sign->init_req(2, __ intcon(0)); 1072 1073 kit.set_control(merge); 1074 1075 C->record_for_igvn(merge); 1076 C->record_for_igvn(i); 1077 C->record_for_igvn(sign); 1078 } 1079 1080 // for (;;) { 1081 // q = i / 10; 1082 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 1083 // buf [--charPos] = digits [r]; 1084 // i = q; 1085 // if (i == 0) break; 1086 // } 1087 1088 { 1089 RegionNode *head = new (C, 3) RegionNode(3); 1090 head->init_req(1, kit.control()); 1091 kit.gvn().set_type(head, Type::CONTROL); 1092 Node *i_phi = new (C, 3) PhiNode(head, TypeInt::INT); 1093 i_phi->init_req(1, i); 1094 kit.gvn().set_type(i_phi, TypeInt::INT); 1095 charPos = PhiNode::make(head, charPos); 1096 kit.gvn().set_type(charPos, TypeInt::INT); 1097 Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1098 kit.gvn().set_type(mem, Type::MEMORY); 1099 kit.set_control(head); 1100 kit.set_memory(mem, char_adr_idx); 1101 1102 Node* q = __ DivI(NULL, i_phi, __ intcon(10)); 1103 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)), 1104 __ LShiftI(q, __ intcon(1)))); 1105 Node* m1 = __ SubI(charPos, __ intcon(1)); 1106 Node* ch = __ AddI(r, __ intcon('0')); 1107 1108 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1109 ch, T_CHAR, char_adr_idx); 1110 1111 1112 IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne), 1113 PROB_FAIR, COUNT_UNKNOWN); 1114 Node* ne = __ IfTrue(iff); 1115 Node* eq = __ IfFalse(iff); 1116 1117 head->init_req(2, ne); 1118 mem->init_req(2, st); 1119 i_phi->init_req(2, q); 1120 charPos->init_req(2, m1); 1121 1122 charPos = m1; 1123 1124 kit.set_control(eq); 1125 kit.set_memory(st, char_adr_idx); 1126 1127 C->record_for_igvn(head); 1128 C->record_for_igvn(mem); 1129 C->record_for_igvn(i_phi); 1130 C->record_for_igvn(charPos); 1131 } 1132 1133 { 1134 // if (sign != 0) { 1135 // buf [--charPos] = sign; 1136 // } 1137 IfNode* iff = kit.create_and_map_if(kit.control(), 1138 __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne), 1139 PROB_FAIR, COUNT_UNKNOWN); 1140 1141 final_merge->init_req(2, __ IfFalse(iff)); 1142 final_mem->init_req(2, kit.memory(char_adr_idx)); 1143 1144 kit.set_control(__ IfTrue(iff)); 1145 if (kit.stopped()) { 1146 final_merge->init_req(1, C->top()); 1147 final_mem->init_req(1, C->top()); 1148 } else { 1149 Node* m1 = __ SubI(charPos, __ intcon(1)); 1150 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1151 sign, T_CHAR, char_adr_idx); 1152 1153 final_merge->init_req(1, kit.control()); 1154 final_mem->init_req(1, st); 1155 } 1156 1157 kit.set_control(final_merge); 1158 kit.set_memory(final_mem, char_adr_idx); 1159 1160 C->record_for_igvn(final_merge); 1161 C->record_for_igvn(final_mem); 1162 } 1163} 1164 1165 1166Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) { 1167 Node* string = str; 1168 Node* offset = kit.make_load(NULL, 1169 kit.basic_plus_adr(string, string, java_lang_String::offset_offset_in_bytes()), 1170 TypeInt::INT, T_INT, offset_field_idx); 1171 Node* count = kit.make_load(NULL, 1172 kit.basic_plus_adr(string, string, java_lang_String::count_offset_in_bytes()), 1173 TypeInt::INT, T_INT, count_field_idx); 1174 const TypeAryPtr* value_type = TypeAryPtr::make(TypePtr::NotNull, 1175 TypeAry::make(TypeInt::CHAR,TypeInt::POS), 1176 ciTypeArrayKlass::make(T_CHAR), true, 0); 1177 Node* value = kit.make_load(NULL, 1178 kit.basic_plus_adr(string, string, java_lang_String::value_offset_in_bytes()), 1179 value_type, T_OBJECT, value_field_idx); 1180 1181 // copy the contents 1182 if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) { 1183 // For small constant strings just emit individual stores. 1184 // A length of 6 seems like a good space/speed tradeof. 1185 int c = count->get_int(); 1186 int o = offset->get_int(); 1187 const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr(); 1188 ciTypeArray* value_array = t->const_oop()->as_type_array(); 1189 for (int e = 0; e < c; e++) { 1190 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1191 __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx); 1192 start = __ AddI(start, __ intcon(1)); 1193 } 1194 } else { 1195 Node* src_ptr = kit.array_element_address(value, offset, T_CHAR); 1196 Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR); 1197 Node* c = count; 1198 Node* extra = NULL; 1199#ifdef _LP64 1200 c = __ ConvI2L(c); 1201 extra = C->top(); 1202#endif 1203 Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP, 1204 OptoRuntime::fast_arraycopy_Type(), 1205 CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()), 1206 "jshort_disjoint_arraycopy", TypeAryPtr::CHARS, 1207 src_ptr, dst_ptr, c, extra); 1208 start = __ AddI(start, count); 1209 } 1210 return start; 1211} 1212 1213 1214void PhaseStringOpts::replace_string_concat(StringConcat* sc) { 1215 // Log a little info about the transformation 1216 sc->maybe_log_transform(); 1217 1218 // pull the JVMState of the allocation into a SafePointNode to serve as 1219 // as a shim for the insertion of the new code. 1220 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C); 1221 uint size = sc->begin()->req(); 1222 SafePointNode* map = new (C, size) SafePointNode(size, jvms); 1223 1224 // copy the control and memory state from the final call into our 1225 // new starting state. This allows any preceeding tests to feed 1226 // into the new section of code. 1227 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) { 1228 map->init_req(i1, sc->end()->in(i1)); 1229 } 1230 // blow away old allocation arguments 1231 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) { 1232 map->init_req(i1, C->top()); 1233 } 1234 // Copy the rest of the inputs for the JVMState 1235 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) { 1236 map->init_req(i1, sc->begin()->in(i1)); 1237 } 1238 // Make sure the memory state is a MergeMem for parsing. 1239 if (!map->in(TypeFunc::Memory)->is_MergeMem()) { 1240 map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory))); 1241 } 1242 1243 jvms->set_map(map); 1244 map->ensure_stack(jvms, jvms->method()->max_stack()); 1245 1246 1247 // disconnect all the old StringBuilder calls from the graph 1248 sc->eliminate_unneeded_control(); 1249 1250 // At this point all the old work has been completely removed from 1251 // the graph and the saved JVMState exists at the point where the 1252 // final toString call used to be. 1253 GraphKit kit(jvms); 1254 1255 // There may be uncommon traps which are still using the 1256 // intermediate states and these need to be rewritten to point at 1257 // the JVMState at the beginning of the transformation. 1258 sc->convert_uncommon_traps(kit, jvms); 1259 1260 // Now insert the logic to compute the size of the string followed 1261 // by all the logic to construct array and resulting string. 1262 1263 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string())); 1264 1265 // Create a region for the overflow checks to merge into. 1266 int args = MAX2(sc->num_arguments(), 1); 1267 RegionNode* overflow = new (C, args) RegionNode(args); 1268 kit.gvn().set_type(overflow, Type::CONTROL); 1269 1270 // Create a hook node to hold onto the individual sizes since they 1271 // are need for the copying phase. 1272 Node* string_sizes = new (C, args) Node(args); 1273 1274 Node* length = __ intcon(0); 1275 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1276 Node* arg = sc->argument(argi); 1277 switch (sc->mode(argi)) { 1278 case StringConcat::IntMode: { 1279 Node* string_size = int_stringSize(kit, arg); 1280 1281 // accumulate total 1282 length = __ AddI(length, string_size); 1283 1284 // Cache this value for the use by int_toString 1285 string_sizes->init_req(argi, string_size); 1286 break; 1287 } 1288 case StringConcat::StringNullCheckMode: { 1289 const Type* type = kit.gvn().type(arg); 1290 assert(type != TypePtr::NULL_PTR, "missing check"); 1291 if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1292 // Null check with uncommont trap since 1293 // StringBuilder(null) throws exception. 1294 // Use special uncommon trap instead of 1295 // calling normal do_null_check(). 1296 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1297 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1298 overflow->add_req(__ IfFalse(iff)); 1299 Node* notnull = __ IfTrue(iff); 1300 kit.set_control(notnull); // set control for the cast_not_null 1301 arg = kit.cast_not_null(arg, false); 1302 sc->set_argument(argi, arg); 1303 } 1304 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity"); 1305 // Fallthrough to add string length. 1306 } 1307 case StringConcat::StringMode: { 1308 const Type* type = kit.gvn().type(arg); 1309 if (type == TypePtr::NULL_PTR) { 1310 // replace the argument with the null checked version 1311 arg = null_string; 1312 sc->set_argument(argi, arg); 1313 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1314 // s = s != null ? s : "null"; 1315 // length = length + (s.count - s.offset); 1316 RegionNode *r = new (C, 3) RegionNode(3); 1317 kit.gvn().set_type(r, Type::CONTROL); 1318 Node *phi = new (C, 3) PhiNode(r, type); 1319 kit.gvn().set_type(phi, phi->bottom_type()); 1320 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1321 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1322 Node* notnull = __ IfTrue(iff); 1323 Node* isnull = __ IfFalse(iff); 1324 kit.set_control(notnull); // set control for the cast_not_null 1325 r->init_req(1, notnull); 1326 phi->init_req(1, kit.cast_not_null(arg, false)); 1327 r->init_req(2, isnull); 1328 phi->init_req(2, null_string); 1329 kit.set_control(r); 1330 C->record_for_igvn(r); 1331 C->record_for_igvn(phi); 1332 // replace the argument with the null checked version 1333 arg = phi; 1334 sc->set_argument(argi, arg); 1335 } 1336 // Node* offset = kit.make_load(NULL, kit.basic_plus_adr(arg, arg, offset_offset), 1337 // TypeInt::INT, T_INT, offset_field_idx); 1338 Node* count = kit.make_load(NULL, kit.basic_plus_adr(arg, arg, java_lang_String::count_offset_in_bytes()), 1339 TypeInt::INT, T_INT, count_field_idx); 1340 length = __ AddI(length, count); 1341 string_sizes->init_req(argi, NULL); 1342 break; 1343 } 1344 case StringConcat::CharMode: { 1345 // one character only 1346 length = __ AddI(length, __ intcon(1)); 1347 break; 1348 } 1349 default: 1350 ShouldNotReachHere(); 1351 } 1352 if (argi > 0) { 1353 // Check that the sum hasn't overflowed 1354 IfNode* iff = kit.create_and_map_if(kit.control(), 1355 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt), 1356 PROB_MIN, COUNT_UNKNOWN); 1357 kit.set_control(__ IfFalse(iff)); 1358 overflow->set_req(argi, __ IfTrue(iff)); 1359 } 1360 } 1361 1362 { 1363 // Hook 1364 PreserveJVMState pjvms(&kit); 1365 kit.set_control(overflow); 1366 C->record_for_igvn(overflow); 1367 kit.uncommon_trap(Deoptimization::Reason_intrinsic, 1368 Deoptimization::Action_make_not_entrant); 1369 } 1370 1371 // length now contains the number of characters needed for the 1372 // char[] so create a new AllocateArray for the char[] 1373 Node* char_array = NULL; 1374 { 1375 PreserveReexecuteState preexecs(&kit); 1376 // The original jvms is for an allocation of either a String or 1377 // StringBuffer so no stack adjustment is necessary for proper 1378 // reexecution. If we deoptimize in the slow path the bytecode 1379 // will be reexecuted and the char[] allocation will be thrown away. 1380 kit.jvms()->set_should_reexecute(true); 1381 char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))), 1382 length, 1); 1383 } 1384 1385 // Mark the allocation so that zeroing is skipped since the code 1386 // below will overwrite the entire array 1387 AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn); 1388 char_alloc->maybe_set_complete(_gvn); 1389 1390 // Now copy the string representations into the final char[] 1391 Node* start = __ intcon(0); 1392 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1393 Node* arg = sc->argument(argi); 1394 switch (sc->mode(argi)) { 1395 case StringConcat::IntMode: { 1396 Node* end = __ AddI(start, string_sizes->in(argi)); 1397 // getChars words backwards so pass the ending point as well as the start 1398 int_getChars(kit, arg, char_array, start, end); 1399 start = end; 1400 break; 1401 } 1402 case StringConcat::StringNullCheckMode: 1403 case StringConcat::StringMode: { 1404 start = copy_string(kit, arg, char_array, start); 1405 break; 1406 } 1407 case StringConcat::CharMode: { 1408 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1409 arg, T_CHAR, char_adr_idx); 1410 start = __ AddI(start, __ intcon(1)); 1411 break; 1412 } 1413 default: 1414 ShouldNotReachHere(); 1415 } 1416 } 1417 1418 // If we're not reusing an existing String allocation then allocate one here. 1419 Node* result = sc->string_alloc(); 1420 if (result == NULL) { 1421 PreserveReexecuteState preexecs(&kit); 1422 // The original jvms is for an allocation of either a String or 1423 // StringBuffer so no stack adjustment is necessary for proper 1424 // reexecution. 1425 kit.jvms()->set_should_reexecute(true); 1426 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass()))); 1427 } 1428 1429 // Intialize the string 1430 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::offset_offset_in_bytes()), 1431 __ intcon(0), T_INT, offset_field_idx); 1432 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::count_offset_in_bytes()), 1433 length, T_INT, count_field_idx); 1434 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::value_offset_in_bytes()), 1435 char_array, T_OBJECT, value_field_idx); 1436 1437 // hook up the outgoing control and result 1438 kit.replace_call(sc->end(), result); 1439 1440 // Unhook any hook nodes 1441 string_sizes->disconnect_inputs(NULL); 1442 sc->cleanup(); 1443} 1444