1 /* 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "compiler/compileLog.hpp" 27 #include "opto/addnode.hpp" 28 #include "opto/callGenerator.hpp" 29 #include "opto/callnode.hpp" 30 #include "opto/divnode.hpp" 31 #include "opto/graphKit.hpp" 32 #include "opto/idealKit.hpp" 33 #include "opto/rootnode.hpp" 34 #include "opto/runtime.hpp" 35 #include "opto/stringopts.hpp" 36 #include "opto/subnode.hpp" 37 38 #define __ kit. 39 40 class StringConcat : public ResourceObj { 41 private: 42 PhaseStringOpts* _stringopts; 43 Node* _string_alloc; 44 AllocateNode* _begin; // The allocation the begins the pattern 45 CallStaticJavaNode* _end; // The final call of the pattern. Will either be 46 // SB.toString or or String.<init>(SB.toString) 47 bool _multiple; // indicates this is a fusion of two or more 48 // separate StringBuilders 49 50 Node* _arguments; // The list of arguments to be concatenated 51 GrowableArray<int> _mode; // into a String along with a mode flag 52 // indicating how to treat the value. 53 54 Node_List _control; // List of control nodes that will be deleted 55 Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten 56 // to restart at the initial JVMState. 57 public: 58 // Mode for converting arguments to Strings 59 enum { 60 StringMode, 61 IntMode, 62 CharMode, 63 StringNullCheckMode 64 }; 65 66 StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end): 67 _end(end), 68 _begin(NULL), 69 _multiple(false), 70 _string_alloc(NULL), 71 _stringopts(stringopts) { 72 _arguments = new (_stringopts->C, 1) Node(1); 73 _arguments->del_req(0); 74 } 75 76 bool validate_control_flow(); 77 78 void merge_add() { 79 #if 0 80 // XXX This is place holder code for reusing an existing String 81 // allocation but the logic for checking the state safety is 82 // probably inadequate at the moment. 83 CallProjections endprojs; 84 sc->end()->extract_projections(&endprojs, false); 85 if (endprojs.resproj != NULL) { 86 for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) { 87 CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 88 if (use != NULL && use->method() != NULL && 89 use->method()->intrinsic_id() == vmIntrinsics::_String_String && 90 use->in(TypeFunc::Parms + 1) == endprojs.resproj) { 91 // Found useless new String(sb.toString()) so reuse the newly allocated String 92 // when creating the result instead of allocating a new one. 93 sc->set_string_alloc(use->in(TypeFunc::Parms)); 94 sc->set_end(use); 95 } 96 } 97 } 98 #endif 99 } 100 101 StringConcat* merge(StringConcat* other, Node* arg); 102 103 void set_allocation(AllocateNode* alloc) { 104 _begin = alloc; 105 } 106 107 void append(Node* value, int mode) { 108 _arguments->add_req(value); 109 _mode.append(mode); 110 } 111 void push(Node* value, int mode) { 112 _arguments->ins_req(0, value); 113 _mode.insert_before(0, mode); 114 } 115 116 void push_string(Node* value) { 117 push(value, StringMode); 118 } 119 void push_string_null_check(Node* value) { 120 push(value, StringNullCheckMode); 121 } 122 void push_int(Node* value) { 123 push(value, IntMode); 124 } 125 void push_char(Node* value) { 126 push(value, CharMode); 127 } 128 129 static bool is_SB_toString(Node* call) { 130 if (call->is_CallStaticJava()) { 131 CallStaticJavaNode* csj = call->as_CallStaticJava(); 132 ciMethod* m = csj->method(); 133 if (m != NULL && 134 (m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString || 135 m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) { 136 return true; 137 } 138 } 139 return false; 140 } 141 142 static Node* skip_string_null_check(Node* value) { 143 // Look for a diamond shaped Null check of toString() result 144 // (could be code from String.valueOf()): 145 // (Proj == NULL) ? "null":"CastPP(Proj)#NotNULL 146 if (value->is_Phi()) { 147 int true_path = value->as_Phi()->is_diamond_phi(); 148 if (true_path != 0) { 149 // phi->region->if_proj->ifnode->bool 150 BoolNode* b = value->in(0)->in(1)->in(0)->in(1)->as_Bool(); 151 Node* cmp = b->in(1); 152 Node* v1 = cmp->in(1); 153 Node* v2 = cmp->in(2); 154 // Null check of the return of toString which can simply be skipped. 155 if (b->_test._test == BoolTest::ne && 156 v2->bottom_type() == TypePtr::NULL_PTR && 157 value->in(true_path)->Opcode() == Op_CastPP && 158 value->in(true_path)->in(1) == v1 && 159 v1->is_Proj() && is_SB_toString(v1->in(0))) { 160 return v1; 161 } 162 } 163 } 164 return value; 165 } 166 167 Node* argument(int i) { 168 return _arguments->in(i); 169 } 170 Node* argument_uncast(int i) { 171 Node* arg = argument(i); 172 int amode = mode(i); 173 if (amode == StringConcat::StringMode || 174 amode == StringConcat::StringNullCheckMode) { 175 arg = skip_string_null_check(arg); 176 } 177 return arg; 178 } 179 void set_argument(int i, Node* value) { 180 _arguments->set_req(i, value); 181 } 182 int num_arguments() { 183 return _mode.length(); 184 } 185 int mode(int i) { 186 return _mode.at(i); 187 } 188 void add_control(Node* ctrl) { 189 assert(!_control.contains(ctrl), "only push once"); 190 _control.push(ctrl); 191 } 192 CallStaticJavaNode* end() { return _end; } 193 AllocateNode* begin() { return _begin; } 194 Node* string_alloc() { return _string_alloc; } 195 196 void eliminate_unneeded_control(); 197 void eliminate_initialize(InitializeNode* init); 198 void eliminate_call(CallNode* call); 199 200 void maybe_log_transform() { 201 CompileLog* log = _stringopts->C->log(); 202 if (log != NULL) { 203 log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'", 204 num_arguments(), 205 _string_alloc != NULL, 206 _multiple); 207 JVMState* p = _begin->jvms(); 208 while (p != NULL) { 209 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 210 p = p->caller(); 211 } 212 log->tail("replace_string_concat"); 213 } 214 } 215 216 void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) { 217 for (uint u = 0; u < _uncommon_traps.size(); u++) { 218 Node* uct = _uncommon_traps.at(u); 219 220 // Build a new call using the jvms state of the allocate 221 address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point(); 222 const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type(); 223 int size = call_type->domain()->cnt(); 224 const TypePtr* no_memory_effects = NULL; 225 Compile* C = _stringopts->C; 226 CallStaticJavaNode* call = new (C, size) CallStaticJavaNode(call_type, call_addr, "uncommon_trap", 227 jvms->bci(), no_memory_effects); 228 for (int e = 0; e < TypeFunc::Parms; e++) { 229 call->init_req(e, uct->in(e)); 230 } 231 // Set the trap request to record intrinsic failure if this trap 232 // is taken too many times. Ideally we would handle then traps by 233 // doing the original bookkeeping in the MDO so that if it caused 234 // the code to be thrown out we could still recompile and use the 235 // optimization. Failing the uncommon traps doesn't really mean 236 // that the optimization is a bad idea but there's no other way to 237 // do the MDO updates currently. 238 int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic, 239 Deoptimization::Action_make_not_entrant); 240 call->init_req(TypeFunc::Parms, __ intcon(trap_request)); 241 kit.add_safepoint_edges(call); 242 243 _stringopts->gvn()->transform(call); 244 C->gvn_replace_by(uct, call); 245 uct->disconnect_inputs(NULL); 246 } 247 } 248 249 void cleanup() { 250 // disconnect the hook node 251 _arguments->disconnect_inputs(NULL); 252 } 253 }; 254 255 256 void StringConcat::eliminate_unneeded_control() { 257 for (uint i = 0; i < _control.size(); i++) { 258 Node* n = _control.at(i); 259 if (n->is_Allocate()) { 260 eliminate_initialize(n->as_Allocate()->initialization()); 261 } 262 if (n->is_Call()) { 263 if (n != _end) { 264 eliminate_call(n->as_Call()); 265 } 266 } else if (n->is_IfTrue()) { 267 Compile* C = _stringopts->C; 268 C->gvn_replace_by(n, n->in(0)->in(0)); 269 C->gvn_replace_by(n->in(0), C->top()); 270 } 271 } 272 } 273 274 275 StringConcat* StringConcat::merge(StringConcat* other, Node* arg) { 276 StringConcat* result = new StringConcat(_stringopts, _end); 277 for (uint x = 0; x < _control.size(); x++) { 278 Node* n = _control.at(x); 279 if (n->is_Call()) { 280 result->_control.push(n); 281 } 282 } 283 for (uint x = 0; x < other->_control.size(); x++) { 284 Node* n = other->_control.at(x); 285 if (n->is_Call()) { 286 result->_control.push(n); 287 } 288 } 289 assert(result->_control.contains(other->_end), "what?"); 290 assert(result->_control.contains(_begin), "what?"); 291 for (int x = 0; x < num_arguments(); x++) { 292 Node* argx = argument_uncast(x); 293 if (argx == arg) { 294 // replace the toString result with the all the arguments that 295 // made up the other StringConcat 296 for (int y = 0; y < other->num_arguments(); y++) { 297 result->append(other->argument(y), other->mode(y)); 298 } 299 } else { 300 result->append(argx, mode(x)); 301 } 302 } 303 result->set_allocation(other->_begin); 304 result->_multiple = true; 305 return result; 306 } 307 308 309 void StringConcat::eliminate_call(CallNode* call) { 310 Compile* C = _stringopts->C; 311 CallProjections projs; 312 call->extract_projections(&projs, false); 313 if (projs.fallthrough_catchproj != NULL) { 314 C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control)); 315 } 316 if (projs.fallthrough_memproj != NULL) { 317 C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory)); 318 } 319 if (projs.catchall_memproj != NULL) { 320 C->gvn_replace_by(projs.catchall_memproj, C->top()); 321 } 322 if (projs.fallthrough_ioproj != NULL) { 323 C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O)); 324 } 325 if (projs.catchall_ioproj != NULL) { 326 C->gvn_replace_by(projs.catchall_ioproj, C->top()); 327 } 328 if (projs.catchall_catchproj != NULL) { 329 // EA can't cope with the partially collapsed graph this 330 // creates so put it on the worklist to be collapsed later. 331 for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) { 332 Node *use = i.get(); 333 int opc = use->Opcode(); 334 if (opc == Op_CreateEx || opc == Op_Region) { 335 _stringopts->record_dead_node(use); 336 } 337 } 338 C->gvn_replace_by(projs.catchall_catchproj, C->top()); 339 } 340 if (projs.resproj != NULL) { 341 C->gvn_replace_by(projs.resproj, C->top()); 342 } 343 C->gvn_replace_by(call, C->top()); 344 } 345 346 void StringConcat::eliminate_initialize(InitializeNode* init) { 347 Compile* C = _stringopts->C; 348 349 // Eliminate Initialize node. 350 assert(init->outcnt() <= 2, "only a control and memory projection expected"); 351 assert(init->req() <= InitializeNode::RawStores, "no pending inits"); 352 Node *ctrl_proj = init->proj_out(TypeFunc::Control); 353 if (ctrl_proj != NULL) { 354 C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control)); 355 } 356 Node *mem_proj = init->proj_out(TypeFunc::Memory); 357 if (mem_proj != NULL) { 358 Node *mem = init->in(TypeFunc::Memory); 359 C->gvn_replace_by(mem_proj, mem); 360 } 361 C->gvn_replace_by(init, C->top()); 362 init->disconnect_inputs(NULL); 363 } 364 365 Node_List PhaseStringOpts::collect_toString_calls() { 366 Node_List string_calls; 367 Node_List worklist; 368 369 _visited.Clear(); 370 371 // Prime the worklist 372 for (uint i = 1; i < C->root()->len(); i++) { 373 Node* n = C->root()->in(i); 374 if (n != NULL && !_visited.test_set(n->_idx)) { 375 worklist.push(n); 376 } 377 } 378 379 while (worklist.size() > 0) { 380 Node* ctrl = worklist.pop(); 381 if (StringConcat::is_SB_toString(ctrl)) { 382 CallStaticJavaNode* csj = ctrl->as_CallStaticJava(); 383 string_calls.push(csj); 384 } 385 if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) { 386 worklist.push(ctrl->in(0)); 387 } 388 if (ctrl->is_Region()) { 389 for (uint i = 1; i < ctrl->len(); i++) { 390 if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) { 391 worklist.push(ctrl->in(i)); 392 } 393 } 394 } 395 } 396 return string_calls; 397 } 398 399 400 StringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) { 401 ciMethod* m = call->method(); 402 ciSymbol* string_sig; 403 ciSymbol* int_sig; 404 ciSymbol* char_sig; 405 if (m->holder() == C->env()->StringBuilder_klass()) { 406 string_sig = ciSymbol::String_StringBuilder_signature(); 407 int_sig = ciSymbol::int_StringBuilder_signature(); 408 char_sig = ciSymbol::char_StringBuilder_signature(); 409 } else if (m->holder() == C->env()->StringBuffer_klass()) { 410 string_sig = ciSymbol::String_StringBuffer_signature(); 411 int_sig = ciSymbol::int_StringBuffer_signature(); 412 char_sig = ciSymbol::char_StringBuffer_signature(); 413 } else { 414 return NULL; 415 } 416 #ifndef PRODUCT 417 if (PrintOptimizeStringConcat) { 418 tty->print("considering toString call in "); 419 call->jvms()->dump_spec(tty); tty->cr(); 420 } 421 #endif 422 423 StringConcat* sc = new StringConcat(this, call); 424 425 AllocateNode* alloc = NULL; 426 InitializeNode* init = NULL; 427 428 // possible opportunity for StringBuilder fusion 429 CallStaticJavaNode* cnode = call; 430 while (cnode) { 431 Node* recv = cnode->in(TypeFunc::Parms)->uncast(); 432 if (recv->is_Proj()) { 433 recv = recv->in(0); 434 } 435 cnode = recv->isa_CallStaticJava(); 436 if (cnode == NULL) { 437 alloc = recv->isa_Allocate(); 438 if (alloc == NULL) { 439 break; 440 } 441 // Find the constructor call 442 Node* result = alloc->result_cast(); 443 if (result == NULL || !result->is_CheckCastPP()) { 444 // strange looking allocation 445 #ifndef PRODUCT 446 if (PrintOptimizeStringConcat) { 447 tty->print("giving up because allocation looks strange "); 448 alloc->jvms()->dump_spec(tty); tty->cr(); 449 } 450 #endif 451 break; 452 } 453 Node* constructor = NULL; 454 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 455 CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 456 if (use != NULL && 457 use->method() != NULL && 458 !use->method()->is_static() && 459 use->method()->name() == ciSymbol::object_initializer_name() && 460 use->method()->holder() == m->holder()) { 461 // Matched the constructor. 462 ciSymbol* sig = use->method()->signature()->as_symbol(); 463 if (sig == ciSymbol::void_method_signature() || 464 sig == ciSymbol::int_void_signature() || 465 sig == ciSymbol::string_void_signature()) { 466 if (sig == ciSymbol::string_void_signature()) { 467 // StringBuilder(String) so pick this up as the first argument 468 assert(use->in(TypeFunc::Parms + 1) != NULL, "what?"); 469 const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1)); 470 if (type == TypePtr::NULL_PTR) { 471 // StringBuilder(null) throws exception. 472 #ifndef PRODUCT 473 if (PrintOptimizeStringConcat) { 474 tty->print("giving up because StringBuilder(null) throws exception"); 475 alloc->jvms()->dump_spec(tty); tty->cr(); 476 } 477 #endif 478 return NULL; 479 } 480 // StringBuilder(str) argument needs null check. 481 sc->push_string_null_check(use->in(TypeFunc::Parms + 1)); 482 } 483 // The int variant takes an initial size for the backing 484 // array so just treat it like the void version. 485 constructor = use; 486 } else { 487 #ifndef PRODUCT 488 if (PrintOptimizeStringConcat) { 489 tty->print("unexpected constructor signature: %s", sig->as_utf8()); 490 } 491 #endif 492 } 493 break; 494 } 495 } 496 if (constructor == NULL) { 497 // couldn't find constructor 498 #ifndef PRODUCT 499 if (PrintOptimizeStringConcat) { 500 tty->print("giving up because couldn't find constructor "); 501 alloc->jvms()->dump_spec(tty); tty->cr(); 502 } 503 #endif 504 break; 505 } 506 507 // Walked all the way back and found the constructor call so see 508 // if this call converted into a direct string concatenation. 509 sc->add_control(call); 510 sc->add_control(constructor); 511 sc->add_control(alloc); 512 sc->set_allocation(alloc); 513 if (sc->validate_control_flow()) { 514 return sc; 515 } else { 516 return NULL; 517 } 518 } else if (cnode->method() == NULL) { 519 break; 520 } else if (!cnode->method()->is_static() && 521 cnode->method()->holder() == m->holder() && 522 cnode->method()->name() == ciSymbol::append_name() && 523 (cnode->method()->signature()->as_symbol() == string_sig || 524 cnode->method()->signature()->as_symbol() == char_sig || 525 cnode->method()->signature()->as_symbol() == int_sig)) { 526 sc->add_control(cnode); 527 Node* arg = cnode->in(TypeFunc::Parms + 1); 528 if (cnode->method()->signature()->as_symbol() == int_sig) { 529 sc->push_int(arg); 530 } else if (cnode->method()->signature()->as_symbol() == char_sig) { 531 sc->push_char(arg); 532 } else { 533 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) { 534 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 535 if (csj->method() != NULL && 536 csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString) { 537 if (arg->outcnt() == 1) { 538 // _control is the list of StringBuilder calls nodes which 539 // will be eliminated by this String concatenation optimization. 540 // Integer::toString() call is not part of StringBuilder calls 541 // chain. It's node could be eliminated only if it's result 542 // is used only by this chain. 543 // An other limitation: it should be used only once because 544 // it is unknown that it is used only by this SB calls chain 545 // until all chain's nodes are collected. 546 // 547 // Note, it is fine to not eliminate Integer::toString() call 548 // node. It will be inlined later and it's code will fold if 549 // it's result is not used. 550 assert(arg->unique_out() == cnode, "sanity"); 551 sc->add_control(csj); 552 } 553 sc->push_int(csj->in(TypeFunc::Parms)); 554 continue; 555 } 556 } 557 sc->push_string(arg); 558 } 559 continue; 560 } else { 561 // some unhandled signature 562 #ifndef PRODUCT 563 if (PrintOptimizeStringConcat) { 564 tty->print("giving up because encountered unexpected signature "); 565 cnode->tf()->dump(); tty->cr(); 566 cnode->in(TypeFunc::Parms + 1)->dump(); 567 } 568 #endif 569 break; 570 } 571 } 572 return NULL; 573 } 574 575 576 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*): 577 Phase(StringOpts), 578 _gvn(gvn), 579 _visited(Thread::current()->resource_area()) { 580 581 assert(OptimizeStringConcat, "shouldn't be here"); 582 583 size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"), 584 ciSymbol::make("[I"), true); 585 if (size_table_field == NULL) { 586 // Something wrong so give up. 587 assert(false, "why can't we find Integer.sizeTable?"); 588 return; 589 } 590 591 // Collect the types needed to talk about the various slices of memory 592 char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS); 593 594 // For each locally allocated StringBuffer see if the usages can be 595 // collapsed into a single String construction. 596 597 // Run through the list of allocation looking for SB.toString to see 598 // if it's possible to fuse the usage of the SB into a single String 599 // construction. 600 GrowableArray<StringConcat*> concats; 601 Node_List toStrings = collect_toString_calls(); 602 while (toStrings.size() > 0) { 603 StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava()); 604 if (sc != NULL) { 605 concats.push(sc); 606 } 607 } 608 609 // try to coalesce separate concats 610 restart: 611 for (int c = 0; c < concats.length(); c++) { 612 StringConcat* sc = concats.at(c); 613 for (int i = 0; i < sc->num_arguments(); i++) { 614 Node* arg = sc->argument_uncast(i); 615 if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) { 616 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 617 for (int o = 0; o < concats.length(); o++) { 618 if (c == o) continue; 619 StringConcat* other = concats.at(o); 620 if (other->end() == csj) { 621 #ifndef PRODUCT 622 if (PrintOptimizeStringConcat) { 623 tty->print_cr("considering stacked concats"); 624 } 625 #endif 626 627 StringConcat* merged = sc->merge(other, arg); 628 if (merged->validate_control_flow()) { 629 #ifndef PRODUCT 630 if (PrintOptimizeStringConcat) { 631 tty->print_cr("stacking would succeed"); 632 } 633 #endif 634 if (c < o) { 635 concats.remove_at(o); 636 concats.at_put(c, merged); 637 } else { 638 concats.remove_at(c); 639 concats.at_put(o, merged); 640 } 641 goto restart; 642 } else { 643 #ifndef PRODUCT 644 if (PrintOptimizeStringConcat) { 645 tty->print_cr("stacking would fail"); 646 } 647 #endif 648 } 649 } 650 } 651 } 652 } 653 } 654 655 656 for (int c = 0; c < concats.length(); c++) { 657 StringConcat* sc = concats.at(c); 658 replace_string_concat(sc); 659 } 660 661 remove_dead_nodes(); 662 } 663 664 void PhaseStringOpts::record_dead_node(Node* dead) { 665 dead_worklist.push(dead); 666 } 667 668 void PhaseStringOpts::remove_dead_nodes() { 669 // Delete any dead nodes to make things clean enough that escape 670 // analysis doesn't get unhappy. 671 while (dead_worklist.size() > 0) { 672 Node* use = dead_worklist.pop(); 673 int opc = use->Opcode(); 674 switch (opc) { 675 case Op_Region: { 676 uint i = 1; 677 for (i = 1; i < use->req(); i++) { 678 if (use->in(i) != C->top()) { 679 break; 680 } 681 } 682 if (i >= use->req()) { 683 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 684 Node* m = i.get(); 685 if (m->is_Phi()) { 686 dead_worklist.push(m); 687 } 688 } 689 C->gvn_replace_by(use, C->top()); 690 } 691 break; 692 } 693 case Op_AddP: 694 case Op_CreateEx: { 695 // Recurisvely clean up references to CreateEx so EA doesn't 696 // get unhappy about the partially collapsed graph. 697 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 698 Node* m = i.get(); 699 if (m->is_AddP()) { 700 dead_worklist.push(m); 701 } 702 } 703 C->gvn_replace_by(use, C->top()); 704 break; 705 } 706 case Op_Phi: 707 if (use->in(0) == C->top()) { 708 C->gvn_replace_by(use, C->top()); 709 } 710 break; 711 } 712 } 713 } 714 715 716 bool StringConcat::validate_control_flow() { 717 // We found all the calls and arguments now lets see if it's 718 // safe to transform the graph as we would expect. 719 720 // Check to see if this resulted in too many uncommon traps previously 721 if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(), 722 Deoptimization::Reason_intrinsic)) { 723 return false; 724 } 725 726 // Walk backwards over the control flow from toString to the 727 // allocation and make sure all the control flow is ok. This 728 // means it's either going to be eliminated once the calls are 729 // removed or it can safely be transformed into an uncommon 730 // trap. 731 732 int null_check_count = 0; 733 Unique_Node_List ctrl_path; 734 735 assert(_control.contains(_begin), "missing"); 736 assert(_control.contains(_end), "missing"); 737 738 // Collect the nodes that we know about and will eliminate into ctrl_path 739 for (uint i = 0; i < _control.size(); i++) { 740 // Push the call and it's control projection 741 Node* n = _control.at(i); 742 if (n->is_Allocate()) { 743 AllocateNode* an = n->as_Allocate(); 744 InitializeNode* init = an->initialization(); 745 ctrl_path.push(init); 746 ctrl_path.push(init->as_Multi()->proj_out(0)); 747 } 748 if (n->is_Call()) { 749 CallNode* cn = n->as_Call(); 750 ctrl_path.push(cn); 751 ctrl_path.push(cn->proj_out(0)); 752 ctrl_path.push(cn->proj_out(0)->unique_out()); 753 ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0)); 754 } else { 755 ShouldNotReachHere(); 756 } 757 } 758 759 // Skip backwards through the control checking for unexpected contro flow 760 Node* ptr = _end; 761 bool fail = false; 762 while (ptr != _begin) { 763 if (ptr->is_Call() && ctrl_path.member(ptr)) { 764 ptr = ptr->in(0); 765 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) { 766 ptr = ptr->in(0)->in(0)->in(0); 767 assert(ctrl_path.member(ptr), "should be a known piece of control"); 768 } else if (ptr->is_IfTrue()) { 769 IfNode* iff = ptr->in(0)->as_If(); 770 BoolNode* b = iff->in(1)->isa_Bool(); 771 Node* cmp = b->in(1); 772 Node* v1 = cmp->in(1); 773 Node* v2 = cmp->in(2); 774 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con); 775 776 // Null check of the return of append which can simply be eliminated 777 if (b->_test._test == BoolTest::ne && 778 v2->bottom_type() == TypePtr::NULL_PTR && 779 v1->is_Proj() && ctrl_path.member(v1->in(0))) { 780 // NULL check of the return value of the append 781 null_check_count++; 782 if (otherproj->outcnt() == 1) { 783 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 784 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 785 ctrl_path.push(call); 786 } 787 } 788 _control.push(ptr); 789 ptr = ptr->in(0)->in(0); 790 continue; 791 } 792 793 // A test which leads to an uncommon trap which should be safe. 794 // Later this trap will be converted into a trap that restarts 795 // at the beginning. 796 if (otherproj->outcnt() == 1) { 797 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 798 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 799 // control flow leads to uct so should be ok 800 _uncommon_traps.push(call); 801 ctrl_path.push(call); 802 ptr = ptr->in(0)->in(0); 803 continue; 804 } 805 } 806 807 #ifndef PRODUCT 808 // Some unexpected control flow we don't know how to handle. 809 if (PrintOptimizeStringConcat) { 810 tty->print_cr("failing with unknown test"); 811 b->dump(); 812 cmp->dump(); 813 v1->dump(); 814 v2->dump(); 815 tty->cr(); 816 } 817 #endif 818 fail = true; 819 break; 820 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) { 821 ptr = ptr->in(0)->in(0); 822 } else if (ptr->is_Region()) { 823 Node* copy = ptr->as_Region()->is_copy(); 824 if (copy != NULL) { 825 ptr = copy; 826 continue; 827 } 828 if (ptr->req() == 3 && 829 ptr->in(1) != NULL && ptr->in(1)->is_Proj() && 830 ptr->in(2) != NULL && ptr->in(2)->is_Proj() && 831 ptr->in(1)->in(0) == ptr->in(2)->in(0) && 832 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) { 833 // Simple diamond. 834 // XXX should check for possibly merging stores. simple data merges are ok. 835 ptr = ptr->in(1)->in(0)->in(0); 836 continue; 837 } 838 #ifndef PRODUCT 839 if (PrintOptimizeStringConcat) { 840 tty->print_cr("fusion would fail for region"); 841 _begin->dump(); 842 ptr->dump(2); 843 } 844 #endif 845 fail = true; 846 break; 847 } else { 848 // other unknown control 849 if (!fail) { 850 #ifndef PRODUCT 851 if (PrintOptimizeStringConcat) { 852 tty->print_cr("fusion would fail for"); 853 _begin->dump(); 854 } 855 #endif 856 fail = true; 857 } 858 #ifndef PRODUCT 859 if (PrintOptimizeStringConcat) { 860 ptr->dump(); 861 } 862 #endif 863 ptr = ptr->in(0); 864 } 865 } 866 #ifndef PRODUCT 867 if (PrintOptimizeStringConcat && fail) { 868 tty->cr(); 869 } 870 #endif 871 if (fail) return !fail; 872 873 // Validate that all these results produced are contained within 874 // this cluster of objects. First collect all the results produced 875 // by calls in the region. 876 _stringopts->_visited.Clear(); 877 Node_List worklist; 878 Node* final_result = _end->proj_out(TypeFunc::Parms); 879 for (uint i = 0; i < _control.size(); i++) { 880 CallNode* cnode = _control.at(i)->isa_Call(); 881 if (cnode != NULL) { 882 _stringopts->_visited.test_set(cnode->_idx); 883 } 884 Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL; 885 if (result != NULL && result != final_result) { 886 worklist.push(result); 887 } 888 } 889 890 Node* last_result = NULL; 891 while (worklist.size() > 0) { 892 Node* result = worklist.pop(); 893 if (_stringopts->_visited.test_set(result->_idx)) 894 continue; 895 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 896 Node *use = i.get(); 897 if (ctrl_path.member(use)) { 898 // already checked this 899 continue; 900 } 901 int opc = use->Opcode(); 902 if (opc == Op_CmpP || opc == Op_Node) { 903 ctrl_path.push(use); 904 continue; 905 } 906 if (opc == Op_CastPP || opc == Op_CheckCastPP) { 907 for (SimpleDUIterator j(use); j.has_next(); j.next()) { 908 worklist.push(j.get()); 909 } 910 worklist.push(use->in(1)); 911 ctrl_path.push(use); 912 continue; 913 } 914 #ifndef PRODUCT 915 if (PrintOptimizeStringConcat) { 916 if (result != last_result) { 917 last_result = result; 918 tty->print_cr("extra uses for result:"); 919 last_result->dump(); 920 } 921 use->dump(); 922 } 923 #endif 924 fail = true; 925 break; 926 } 927 } 928 929 #ifndef PRODUCT 930 if (PrintOptimizeStringConcat && !fail) { 931 ttyLocker ttyl; 932 tty->cr(); 933 tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size()); 934 _begin->jvms()->dump_spec(tty); tty->cr(); 935 for (int i = 0; i < num_arguments(); i++) { 936 argument(i)->dump(); 937 } 938 _control.dump(); 939 tty->cr(); 940 } 941 #endif 942 943 return !fail; 944 } 945 946 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) { 947 const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror()); 948 Node* klass_node = __ makecon(mirror_type); 949 BasicType bt = field->layout_type(); 950 ciType* field_klass = field->type(); 951 952 const Type *type; 953 if( bt == T_OBJECT ) { 954 if (!field->type()->is_loaded()) { 955 type = TypeInstPtr::BOTTOM; 956 } else if (field->is_constant()) { 957 // This can happen if the constant oop is non-perm. 958 ciObject* con = field->constant_value().as_object(); 959 // Do not "join" in the previous type; it doesn't add value, 960 // and may yield a vacuous result if the field is of interface type. 961 type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr(); 962 assert(type != NULL, "field singleton type must be consistent"); 963 return __ makecon(type); 964 } else { 965 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 966 } 967 } else { 968 type = Type::get_const_basic_type(bt); 969 } 970 971 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()), 972 type, T_OBJECT, 973 C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes()))); 974 } 975 976 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { 977 RegionNode *final_merge = new (C, 3) RegionNode(3); 978 kit.gvn().set_type(final_merge, Type::CONTROL); 979 Node* final_size = new (C, 3) PhiNode(final_merge, TypeInt::INT); 980 kit.gvn().set_type(final_size, TypeInt::INT); 981 982 IfNode* iff = kit.create_and_map_if(kit.control(), 983 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 984 PROB_FAIR, COUNT_UNKNOWN); 985 Node* is_min = __ IfFalse(iff); 986 final_merge->init_req(1, is_min); 987 final_size->init_req(1, __ intcon(11)); 988 989 kit.set_control(__ IfTrue(iff)); 990 if (kit.stopped()) { 991 final_merge->init_req(2, C->top()); 992 final_size->init_req(2, C->top()); 993 } else { 994 995 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 996 RegionNode *r = new (C, 3) RegionNode(3); 997 kit.gvn().set_type(r, Type::CONTROL); 998 Node *phi = new (C, 3) PhiNode(r, TypeInt::INT); 999 kit.gvn().set_type(phi, TypeInt::INT); 1000 Node *size = new (C, 3) PhiNode(r, TypeInt::INT); 1001 kit.gvn().set_type(size, TypeInt::INT); 1002 Node* chk = __ CmpI(arg, __ intcon(0)); 1003 Node* p = __ Bool(chk, BoolTest::lt); 1004 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN); 1005 Node* lessthan = __ IfTrue(iff); 1006 Node* greaterequal = __ IfFalse(iff); 1007 r->init_req(1, lessthan); 1008 phi->init_req(1, __ SubI(__ intcon(0), arg)); 1009 size->init_req(1, __ intcon(1)); 1010 r->init_req(2, greaterequal); 1011 phi->init_req(2, arg); 1012 size->init_req(2, __ intcon(0)); 1013 kit.set_control(r); 1014 C->record_for_igvn(r); 1015 C->record_for_igvn(phi); 1016 C->record_for_igvn(size); 1017 1018 // for (int i=0; ; i++) 1019 // if (x <= sizeTable[i]) 1020 // return i+1; 1021 1022 // Add loop predicate first. 1023 kit.add_predicate(); 1024 1025 RegionNode *loop = new (C, 3) RegionNode(3); 1026 loop->init_req(1, kit.control()); 1027 kit.gvn().set_type(loop, Type::CONTROL); 1028 1029 Node *index = new (C, 3) PhiNode(loop, TypeInt::INT); 1030 index->init_req(1, __ intcon(0)); 1031 kit.gvn().set_type(index, TypeInt::INT); 1032 kit.set_control(loop); 1033 Node* sizeTable = fetch_static_field(kit, size_table_field); 1034 1035 Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS); 1036 C->record_for_igvn(value); 1037 Node* limit = __ CmpI(phi, value); 1038 Node* limitb = __ Bool(limit, BoolTest::le); 1039 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN); 1040 Node* lessEqual = __ IfTrue(iff2); 1041 Node* greater = __ IfFalse(iff2); 1042 1043 loop->init_req(2, greater); 1044 index->init_req(2, __ AddI(index, __ intcon(1))); 1045 1046 kit.set_control(lessEqual); 1047 C->record_for_igvn(loop); 1048 C->record_for_igvn(index); 1049 1050 final_merge->init_req(2, kit.control()); 1051 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1))); 1052 } 1053 1054 kit.set_control(final_merge); 1055 C->record_for_igvn(final_merge); 1056 C->record_for_igvn(final_size); 1057 1058 return final_size; 1059 } 1060 1061 void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) { 1062 RegionNode *final_merge = new (C, 4) RegionNode(4); 1063 kit.gvn().set_type(final_merge, Type::CONTROL); 1064 Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1065 kit.gvn().set_type(final_mem, Type::MEMORY); 1066 1067 // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive 1068 { 1069 // i == MIN_VALUE 1070 IfNode* iff = kit.create_and_map_if(kit.control(), 1071 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 1072 PROB_FAIR, COUNT_UNKNOWN); 1073 1074 Node* old_mem = kit.memory(char_adr_idx); 1075 1076 kit.set_control(__ IfFalse(iff)); 1077 if (kit.stopped()) { 1078 // Statically not equal to MIN_VALUE so this path is dead 1079 final_merge->init_req(3, kit.control()); 1080 } else { 1081 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())), 1082 char_array, start); 1083 final_merge->init_req(3, kit.control()); 1084 final_mem->init_req(3, kit.memory(char_adr_idx)); 1085 } 1086 1087 kit.set_control(__ IfTrue(iff)); 1088 kit.set_memory(old_mem, char_adr_idx); 1089 } 1090 1091 1092 // Simplified version of Integer.getChars 1093 1094 // int q, r; 1095 // int charPos = index; 1096 Node* charPos = end; 1097 1098 // char sign = 0; 1099 1100 Node* i = arg; 1101 Node* sign = __ intcon(0); 1102 1103 // if (i < 0) { 1104 // sign = '-'; 1105 // i = -i; 1106 // } 1107 { 1108 IfNode* iff = kit.create_and_map_if(kit.control(), 1109 __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt), 1110 PROB_FAIR, COUNT_UNKNOWN); 1111 1112 RegionNode *merge = new (C, 3) RegionNode(3); 1113 kit.gvn().set_type(merge, Type::CONTROL); 1114 i = new (C, 3) PhiNode(merge, TypeInt::INT); 1115 kit.gvn().set_type(i, TypeInt::INT); 1116 sign = new (C, 3) PhiNode(merge, TypeInt::INT); 1117 kit.gvn().set_type(sign, TypeInt::INT); 1118 1119 merge->init_req(1, __ IfTrue(iff)); 1120 i->init_req(1, __ SubI(__ intcon(0), arg)); 1121 sign->init_req(1, __ intcon('-')); 1122 merge->init_req(2, __ IfFalse(iff)); 1123 i->init_req(2, arg); 1124 sign->init_req(2, __ intcon(0)); 1125 1126 kit.set_control(merge); 1127 1128 C->record_for_igvn(merge); 1129 C->record_for_igvn(i); 1130 C->record_for_igvn(sign); 1131 } 1132 1133 // for (;;) { 1134 // q = i / 10; 1135 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 1136 // buf [--charPos] = digits [r]; 1137 // i = q; 1138 // if (i == 0) break; 1139 // } 1140 1141 { 1142 // Add loop predicate first. 1143 kit.add_predicate(); 1144 1145 RegionNode *head = new (C, 3) RegionNode(3); 1146 head->init_req(1, kit.control()); 1147 kit.gvn().set_type(head, Type::CONTROL); 1148 Node *i_phi = new (C, 3) PhiNode(head, TypeInt::INT); 1149 i_phi->init_req(1, i); 1150 kit.gvn().set_type(i_phi, TypeInt::INT); 1151 charPos = PhiNode::make(head, charPos); 1152 kit.gvn().set_type(charPos, TypeInt::INT); 1153 Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1154 kit.gvn().set_type(mem, Type::MEMORY); 1155 kit.set_control(head); 1156 kit.set_memory(mem, char_adr_idx); 1157 1158 Node* q = __ DivI(NULL, i_phi, __ intcon(10)); 1159 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)), 1160 __ LShiftI(q, __ intcon(1)))); 1161 Node* m1 = __ SubI(charPos, __ intcon(1)); 1162 Node* ch = __ AddI(r, __ intcon('0')); 1163 1164 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1165 ch, T_CHAR, char_adr_idx); 1166 1167 1168 IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne), 1169 PROB_FAIR, COUNT_UNKNOWN); 1170 Node* ne = __ IfTrue(iff); 1171 Node* eq = __ IfFalse(iff); 1172 1173 head->init_req(2, ne); 1174 mem->init_req(2, st); 1175 i_phi->init_req(2, q); 1176 charPos->init_req(2, m1); 1177 1178 charPos = m1; 1179 1180 kit.set_control(eq); 1181 kit.set_memory(st, char_adr_idx); 1182 1183 C->record_for_igvn(head); 1184 C->record_for_igvn(mem); 1185 C->record_for_igvn(i_phi); 1186 C->record_for_igvn(charPos); 1187 } 1188 1189 { 1190 // if (sign != 0) { 1191 // buf [--charPos] = sign; 1192 // } 1193 IfNode* iff = kit.create_and_map_if(kit.control(), 1194 __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne), 1195 PROB_FAIR, COUNT_UNKNOWN); 1196 1197 final_merge->init_req(2, __ IfFalse(iff)); 1198 final_mem->init_req(2, kit.memory(char_adr_idx)); 1199 1200 kit.set_control(__ IfTrue(iff)); 1201 if (kit.stopped()) { 1202 final_merge->init_req(1, C->top()); 1203 final_mem->init_req(1, C->top()); 1204 } else { 1205 Node* m1 = __ SubI(charPos, __ intcon(1)); 1206 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1207 sign, T_CHAR, char_adr_idx); 1208 1209 final_merge->init_req(1, kit.control()); 1210 final_mem->init_req(1, st); 1211 } 1212 1213 kit.set_control(final_merge); 1214 kit.set_memory(final_mem, char_adr_idx); 1215 1216 C->record_for_igvn(final_merge); 1217 C->record_for_igvn(final_mem); 1218 } 1219 } 1220 1221 1222 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) { 1223 Node* string = str; 1224 Node* offset = kit.load_String_offset(kit.control(), string); 1225 Node* count = kit.load_String_length(kit.control(), string); 1226 Node* value = kit.load_String_value (kit.control(), string); 1227 1228 // copy the contents 1229 if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) { 1230 // For small constant strings just emit individual stores. 1231 // A length of 6 seems like a good space/speed tradeof. 1232 int c = count->get_int(); 1233 int o = offset->get_int(); 1234 const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr(); 1235 ciTypeArray* value_array = t->const_oop()->as_type_array(); 1236 for (int e = 0; e < c; e++) { 1237 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1238 __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx); 1239 start = __ AddI(start, __ intcon(1)); 1240 } 1241 } else { 1242 Node* src_ptr = kit.array_element_address(value, offset, T_CHAR); 1243 Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR); 1244 Node* c = count; 1245 Node* extra = NULL; 1246 #ifdef _LP64 1247 c = __ ConvI2L(c); 1248 extra = C->top(); 1249 #endif 1250 Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP, 1251 OptoRuntime::fast_arraycopy_Type(), 1252 CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()), 1253 "jshort_disjoint_arraycopy", TypeAryPtr::CHARS, 1254 src_ptr, dst_ptr, c, extra); 1255 start = __ AddI(start, count); 1256 } 1257 return start; 1258 } 1259 1260 1261 void PhaseStringOpts::replace_string_concat(StringConcat* sc) { 1262 // Log a little info about the transformation 1263 sc->maybe_log_transform(); 1264 1265 // pull the JVMState of the allocation into a SafePointNode to serve as 1266 // as a shim for the insertion of the new code. 1267 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C); 1268 uint size = sc->begin()->req(); 1269 SafePointNode* map = new (C, size) SafePointNode(size, jvms); 1270 1271 // copy the control and memory state from the final call into our 1272 // new starting state. This allows any preceeding tests to feed 1273 // into the new section of code. 1274 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) { 1275 map->init_req(i1, sc->end()->in(i1)); 1276 } 1277 // blow away old allocation arguments 1278 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) { 1279 map->init_req(i1, C->top()); 1280 } 1281 // Copy the rest of the inputs for the JVMState 1282 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) { 1283 map->init_req(i1, sc->begin()->in(i1)); 1284 } 1285 // Make sure the memory state is a MergeMem for parsing. 1286 if (!map->in(TypeFunc::Memory)->is_MergeMem()) { 1287 map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory))); 1288 } 1289 1290 jvms->set_map(map); 1291 map->ensure_stack(jvms, jvms->method()->max_stack()); 1292 1293 1294 // disconnect all the old StringBuilder calls from the graph 1295 sc->eliminate_unneeded_control(); 1296 1297 // At this point all the old work has been completely removed from 1298 // the graph and the saved JVMState exists at the point where the 1299 // final toString call used to be. 1300 GraphKit kit(jvms); 1301 1302 // There may be uncommon traps which are still using the 1303 // intermediate states and these need to be rewritten to point at 1304 // the JVMState at the beginning of the transformation. 1305 sc->convert_uncommon_traps(kit, jvms); 1306 1307 // Now insert the logic to compute the size of the string followed 1308 // by all the logic to construct array and resulting string. 1309 1310 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string())); 1311 1312 // Create a region for the overflow checks to merge into. 1313 int args = MAX2(sc->num_arguments(), 1); 1314 RegionNode* overflow = new (C, args) RegionNode(args); 1315 kit.gvn().set_type(overflow, Type::CONTROL); 1316 1317 // Create a hook node to hold onto the individual sizes since they 1318 // are need for the copying phase. 1319 Node* string_sizes = new (C, args) Node(args); 1320 1321 Node* length = __ intcon(0); 1322 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1323 Node* arg = sc->argument(argi); 1324 switch (sc->mode(argi)) { 1325 case StringConcat::IntMode: { 1326 Node* string_size = int_stringSize(kit, arg); 1327 1328 // accumulate total 1329 length = __ AddI(length, string_size); 1330 1331 // Cache this value for the use by int_toString 1332 string_sizes->init_req(argi, string_size); 1333 break; 1334 } 1335 case StringConcat::StringNullCheckMode: { 1336 const Type* type = kit.gvn().type(arg); 1337 assert(type != TypePtr::NULL_PTR, "missing check"); 1338 if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1339 // Null check with uncommont trap since 1340 // StringBuilder(null) throws exception. 1341 // Use special uncommon trap instead of 1342 // calling normal do_null_check(). 1343 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1344 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1345 overflow->add_req(__ IfFalse(iff)); 1346 Node* notnull = __ IfTrue(iff); 1347 kit.set_control(notnull); // set control for the cast_not_null 1348 arg = kit.cast_not_null(arg, false); 1349 sc->set_argument(argi, arg); 1350 } 1351 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity"); 1352 // Fallthrough to add string length. 1353 } 1354 case StringConcat::StringMode: { 1355 const Type* type = kit.gvn().type(arg); 1356 if (type == TypePtr::NULL_PTR) { 1357 // replace the argument with the null checked version 1358 arg = null_string; 1359 sc->set_argument(argi, arg); 1360 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1361 // s = s != null ? s : "null"; 1362 // length = length + (s.count - s.offset); 1363 RegionNode *r = new (C, 3) RegionNode(3); 1364 kit.gvn().set_type(r, Type::CONTROL); 1365 Node *phi = new (C, 3) PhiNode(r, type); 1366 kit.gvn().set_type(phi, phi->bottom_type()); 1367 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1368 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1369 Node* notnull = __ IfTrue(iff); 1370 Node* isnull = __ IfFalse(iff); 1371 kit.set_control(notnull); // set control for the cast_not_null 1372 r->init_req(1, notnull); 1373 phi->init_req(1, kit.cast_not_null(arg, false)); 1374 r->init_req(2, isnull); 1375 phi->init_req(2, null_string); 1376 kit.set_control(r); 1377 C->record_for_igvn(r); 1378 C->record_for_igvn(phi); 1379 // replace the argument with the null checked version 1380 arg = phi; 1381 sc->set_argument(argi, arg); 1382 } 1383 1384 Node* count = kit.load_String_length(kit.control(), arg); 1385 1386 length = __ AddI(length, count); 1387 string_sizes->init_req(argi, NULL); 1388 break; 1389 } 1390 case StringConcat::CharMode: { 1391 // one character only 1392 length = __ AddI(length, __ intcon(1)); 1393 break; 1394 } 1395 default: 1396 ShouldNotReachHere(); 1397 } 1398 if (argi > 0) { 1399 // Check that the sum hasn't overflowed 1400 IfNode* iff = kit.create_and_map_if(kit.control(), 1401 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt), 1402 PROB_MIN, COUNT_UNKNOWN); 1403 kit.set_control(__ IfFalse(iff)); 1404 overflow->set_req(argi, __ IfTrue(iff)); 1405 } 1406 } 1407 1408 { 1409 // Hook 1410 PreserveJVMState pjvms(&kit); 1411 kit.set_control(overflow); 1412 C->record_for_igvn(overflow); 1413 kit.uncommon_trap(Deoptimization::Reason_intrinsic, 1414 Deoptimization::Action_make_not_entrant); 1415 } 1416 1417 // length now contains the number of characters needed for the 1418 // char[] so create a new AllocateArray for the char[] 1419 Node* char_array = NULL; 1420 { 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. If we deoptimize in the slow path the bytecode 1425 // will be reexecuted and the char[] allocation will be thrown away. 1426 kit.jvms()->set_should_reexecute(true); 1427 char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))), 1428 length, 1); 1429 } 1430 1431 // Mark the allocation so that zeroing is skipped since the code 1432 // below will overwrite the entire array 1433 AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn); 1434 char_alloc->maybe_set_complete(_gvn); 1435 1436 // Now copy the string representations into the final char[] 1437 Node* start = __ intcon(0); 1438 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1439 Node* arg = sc->argument(argi); 1440 switch (sc->mode(argi)) { 1441 case StringConcat::IntMode: { 1442 Node* end = __ AddI(start, string_sizes->in(argi)); 1443 // getChars words backwards so pass the ending point as well as the start 1444 int_getChars(kit, arg, char_array, start, end); 1445 start = end; 1446 break; 1447 } 1448 case StringConcat::StringNullCheckMode: 1449 case StringConcat::StringMode: { 1450 start = copy_string(kit, arg, char_array, start); 1451 break; 1452 } 1453 case StringConcat::CharMode: { 1454 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1455 arg, T_CHAR, char_adr_idx); 1456 start = __ AddI(start, __ intcon(1)); 1457 break; 1458 } 1459 default: 1460 ShouldNotReachHere(); 1461 } 1462 } 1463 1464 // If we're not reusing an existing String allocation then allocate one here. 1465 Node* result = sc->string_alloc(); 1466 if (result == NULL) { 1467 PreserveReexecuteState preexecs(&kit); 1468 // The original jvms is for an allocation of either a String or 1469 // StringBuffer so no stack adjustment is necessary for proper 1470 // reexecution. 1471 kit.jvms()->set_should_reexecute(true); 1472 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass()))); 1473 } 1474 1475 // Intialize the string 1476 if (java_lang_String::has_offset_field()) { 1477 kit.store_String_offset(kit.control(), result, __ intcon(0)); 1478 kit.store_String_length(kit.control(), result, length); 1479 } 1480 kit.store_String_value(kit.control(), result, char_array); 1481 1482 // hook up the outgoing control and result 1483 kit.replace_call(sc->end(), result); 1484 1485 // Unhook any hook nodes 1486 string_sizes->disconnect_inputs(NULL); 1487 sc->cleanup(); 1488 }