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 sc->add_control(csj); 538 sc->push_int(csj->in(TypeFunc::Parms)); 539 continue; 540 } 541 } 542 sc->push_string(arg); 543 } 544 continue; 545 } else { 546 // some unhandled signature 547 #ifndef PRODUCT 548 if (PrintOptimizeStringConcat) { 549 tty->print("giving up because encountered unexpected signature "); 550 cnode->tf()->dump(); tty->cr(); 551 cnode->in(TypeFunc::Parms + 1)->dump(); 552 } 553 #endif 554 break; 555 } 556 } 557 return NULL; 558 } 559 560 561 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*): 562 Phase(StringOpts), 563 _gvn(gvn), 564 _visited(Thread::current()->resource_area()) { 565 566 assert(OptimizeStringConcat, "shouldn't be here"); 567 568 size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"), 569 ciSymbol::make("[I"), true); 570 if (size_table_field == NULL) { 571 // Something wrong so give up. 572 assert(false, "why can't we find Integer.sizeTable?"); 573 return; 574 } 575 576 // Collect the types needed to talk about the various slices of memory 577 char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS); 578 579 // For each locally allocated StringBuffer see if the usages can be 580 // collapsed into a single String construction. 581 582 // Run through the list of allocation looking for SB.toString to see 583 // if it's possible to fuse the usage of the SB into a single String 584 // construction. 585 GrowableArray<StringConcat*> concats; 586 Node_List toStrings = collect_toString_calls(); 587 while (toStrings.size() > 0) { 588 StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava()); 589 if (sc != NULL) { 590 concats.push(sc); 591 } 592 } 593 594 // try to coalesce separate concats 595 restart: 596 for (int c = 0; c < concats.length(); c++) { 597 StringConcat* sc = concats.at(c); 598 for (int i = 0; i < sc->num_arguments(); i++) { 599 Node* arg = sc->argument_uncast(i); 600 if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) { 601 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 602 for (int o = 0; o < concats.length(); o++) { 603 if (c == o) continue; 604 StringConcat* other = concats.at(o); 605 if (other->end() == csj) { 606 #ifndef PRODUCT 607 if (PrintOptimizeStringConcat) { 608 tty->print_cr("considering stacked concats"); 609 } 610 #endif 611 612 StringConcat* merged = sc->merge(other, arg); 613 if (merged->validate_control_flow()) { 614 #ifndef PRODUCT 615 if (PrintOptimizeStringConcat) { 616 tty->print_cr("stacking would succeed"); 617 } 618 #endif 619 if (c < o) { 620 concats.remove_at(o); 621 concats.at_put(c, merged); 622 } else { 623 concats.remove_at(c); 624 concats.at_put(o, merged); 625 } 626 goto restart; 627 } else { 628 #ifndef PRODUCT 629 if (PrintOptimizeStringConcat) { 630 tty->print_cr("stacking would fail"); 631 } 632 #endif 633 } 634 } 635 } 636 } 637 } 638 } 639 640 641 for (int c = 0; c < concats.length(); c++) { 642 StringConcat* sc = concats.at(c); 643 replace_string_concat(sc); 644 } 645 646 remove_dead_nodes(); 647 } 648 649 void PhaseStringOpts::record_dead_node(Node* dead) { 650 dead_worklist.push(dead); 651 } 652 653 void PhaseStringOpts::remove_dead_nodes() { 654 // Delete any dead nodes to make things clean enough that escape 655 // analysis doesn't get unhappy. 656 while (dead_worklist.size() > 0) { 657 Node* use = dead_worklist.pop(); 658 int opc = use->Opcode(); 659 switch (opc) { 660 case Op_Region: { 661 uint i = 1; 662 for (i = 1; i < use->req(); i++) { 663 if (use->in(i) != C->top()) { 664 break; 665 } 666 } 667 if (i >= use->req()) { 668 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 669 Node* m = i.get(); 670 if (m->is_Phi()) { 671 dead_worklist.push(m); 672 } 673 } 674 C->gvn_replace_by(use, C->top()); 675 } 676 break; 677 } 678 case Op_AddP: 679 case Op_CreateEx: { 680 // Recurisvely clean up references to CreateEx so EA doesn't 681 // get unhappy about the partially collapsed graph. 682 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 683 Node* m = i.get(); 684 if (m->is_AddP()) { 685 dead_worklist.push(m); 686 } 687 } 688 C->gvn_replace_by(use, C->top()); 689 break; 690 } 691 case Op_Phi: 692 if (use->in(0) == C->top()) { 693 C->gvn_replace_by(use, C->top()); 694 } 695 break; 696 } 697 } 698 } 699 700 701 bool StringConcat::validate_control_flow() { 702 // We found all the calls and arguments now lets see if it's 703 // safe to transform the graph as we would expect. 704 705 // Check to see if this resulted in too many uncommon traps previously 706 if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(), 707 Deoptimization::Reason_intrinsic)) { 708 return false; 709 } 710 711 // Walk backwards over the control flow from toString to the 712 // allocation and make sure all the control flow is ok. This 713 // means it's either going to be eliminated once the calls are 714 // removed or it can safely be transformed into an uncommon 715 // trap. 716 717 int null_check_count = 0; 718 Unique_Node_List ctrl_path; 719 720 assert(_control.contains(_begin), "missing"); 721 assert(_control.contains(_end), "missing"); 722 723 // Collect the nodes that we know about and will eliminate into ctrl_path 724 for (uint i = 0; i < _control.size(); i++) { 725 // Push the call and it's control projection 726 Node* n = _control.at(i); 727 if (n->is_Allocate()) { 728 AllocateNode* an = n->as_Allocate(); 729 InitializeNode* init = an->initialization(); 730 ctrl_path.push(init); 731 ctrl_path.push(init->as_Multi()->proj_out(0)); 732 } 733 if (n->is_Call()) { 734 CallNode* cn = n->as_Call(); 735 ctrl_path.push(cn); 736 ctrl_path.push(cn->proj_out(0)); 737 ctrl_path.push(cn->proj_out(0)->unique_out()); 738 ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0)); 739 } else { 740 ShouldNotReachHere(); 741 } 742 } 743 744 // Skip backwards through the control checking for unexpected contro flow 745 Node* ptr = _end; 746 bool fail = false; 747 while (ptr != _begin) { 748 if (ptr->is_Call() && ctrl_path.member(ptr)) { 749 ptr = ptr->in(0); 750 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) { 751 ptr = ptr->in(0)->in(0)->in(0); 752 assert(ctrl_path.member(ptr), "should be a known piece of control"); 753 } else if (ptr->is_IfTrue()) { 754 IfNode* iff = ptr->in(0)->as_If(); 755 BoolNode* b = iff->in(1)->isa_Bool(); 756 Node* cmp = b->in(1); 757 Node* v1 = cmp->in(1); 758 Node* v2 = cmp->in(2); 759 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con); 760 761 // Null check of the return of append which can simply be eliminated 762 if (b->_test._test == BoolTest::ne && 763 v2->bottom_type() == TypePtr::NULL_PTR && 764 v1->is_Proj() && ctrl_path.member(v1->in(0))) { 765 // NULL check of the return value of the append 766 null_check_count++; 767 if (otherproj->outcnt() == 1) { 768 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 769 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 770 ctrl_path.push(call); 771 } 772 } 773 _control.push(ptr); 774 ptr = ptr->in(0)->in(0); 775 continue; 776 } 777 778 // A test which leads to an uncommon trap which should be safe. 779 // Later this trap will be converted into a trap that restarts 780 // at the beginning. 781 if (otherproj->outcnt() == 1) { 782 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 783 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 784 // control flow leads to uct so should be ok 785 _uncommon_traps.push(call); 786 ctrl_path.push(call); 787 ptr = ptr->in(0)->in(0); 788 continue; 789 } 790 } 791 792 #ifndef PRODUCT 793 // Some unexpected control flow we don't know how to handle. 794 if (PrintOptimizeStringConcat) { 795 tty->print_cr("failing with unknown test"); 796 b->dump(); 797 cmp->dump(); 798 v1->dump(); 799 v2->dump(); 800 tty->cr(); 801 } 802 #endif 803 fail = true; 804 break; 805 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) { 806 ptr = ptr->in(0)->in(0); 807 } else if (ptr->is_Region()) { 808 Node* copy = ptr->as_Region()->is_copy(); 809 if (copy != NULL) { 810 ptr = copy; 811 continue; 812 } 813 if (ptr->req() == 3 && 814 ptr->in(1) != NULL && ptr->in(1)->is_Proj() && 815 ptr->in(2) != NULL && ptr->in(2)->is_Proj() && 816 ptr->in(1)->in(0) == ptr->in(2)->in(0) && 817 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) { 818 // Simple diamond. 819 // XXX should check for possibly merging stores. simple data merges are ok. 820 ptr = ptr->in(1)->in(0)->in(0); 821 continue; 822 } 823 #ifndef PRODUCT 824 if (PrintOptimizeStringConcat) { 825 tty->print_cr("fusion would fail for region"); 826 _begin->dump(); 827 ptr->dump(2); 828 } 829 #endif 830 fail = true; 831 break; 832 } else { 833 // other unknown control 834 if (!fail) { 835 #ifndef PRODUCT 836 if (PrintOptimizeStringConcat) { 837 tty->print_cr("fusion would fail for"); 838 _begin->dump(); 839 } 840 #endif 841 fail = true; 842 } 843 #ifndef PRODUCT 844 if (PrintOptimizeStringConcat) { 845 ptr->dump(); 846 } 847 #endif 848 ptr = ptr->in(0); 849 } 850 } 851 #ifndef PRODUCT 852 if (PrintOptimizeStringConcat && fail) { 853 tty->cr(); 854 } 855 #endif 856 if (fail) return !fail; 857 858 // Validate that all these results produced are contained within 859 // this cluster of objects. First collect all the results produced 860 // by calls in the region. 861 _stringopts->_visited.Clear(); 862 Node_List worklist; 863 Node* final_result = _end->proj_out(TypeFunc::Parms); 864 for (uint i = 0; i < _control.size(); i++) { 865 CallNode* cnode = _control.at(i)->isa_Call(); 866 if (cnode != NULL) { 867 _stringopts->_visited.test_set(cnode->_idx); 868 } 869 Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL; 870 if (result != NULL && result != final_result) { 871 worklist.push(result); 872 } 873 } 874 875 Node* last_result = NULL; 876 while (worklist.size() > 0) { 877 Node* result = worklist.pop(); 878 if (_stringopts->_visited.test_set(result->_idx)) 879 continue; 880 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 881 Node *use = i.get(); 882 if (ctrl_path.member(use)) { 883 // already checked this 884 continue; 885 } 886 int opc = use->Opcode(); 887 if (opc == Op_CmpP || opc == Op_Node) { 888 ctrl_path.push(use); 889 continue; 890 } 891 if (opc == Op_CastPP || opc == Op_CheckCastPP) { 892 for (SimpleDUIterator j(use); j.has_next(); j.next()) { 893 worklist.push(j.get()); 894 } 895 worklist.push(use->in(1)); 896 ctrl_path.push(use); 897 continue; 898 } 899 #ifndef PRODUCT 900 if (PrintOptimizeStringConcat) { 901 if (result != last_result) { 902 last_result = result; 903 tty->print_cr("extra uses for result:"); 904 last_result->dump(); 905 } 906 use->dump(); 907 } 908 #endif 909 fail = true; 910 break; 911 } 912 } 913 914 #ifndef PRODUCT 915 if (PrintOptimizeStringConcat && !fail) { 916 ttyLocker ttyl; 917 tty->cr(); 918 tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size()); 919 _begin->jvms()->dump_spec(tty); tty->cr(); 920 for (int i = 0; i < num_arguments(); i++) { 921 argument(i)->dump(); 922 } 923 _control.dump(); 924 tty->cr(); 925 } 926 #endif 927 928 return !fail; 929 } 930 931 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) { 932 const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror()); 933 Node* klass_node = __ makecon(mirror_type); 934 BasicType bt = field->layout_type(); 935 ciType* field_klass = field->type(); 936 937 const Type *type; 938 if( bt == T_OBJECT ) { 939 if (!field->type()->is_loaded()) { 940 type = TypeInstPtr::BOTTOM; 941 } else if (field->is_constant()) { 942 // This can happen if the constant oop is non-perm. 943 ciObject* con = field->constant_value().as_object(); 944 // Do not "join" in the previous type; it doesn't add value, 945 // and may yield a vacuous result if the field is of interface type. 946 type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr(); 947 assert(type != NULL, "field singleton type must be consistent"); 948 return __ makecon(type); 949 } else { 950 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 951 } 952 } else { 953 type = Type::get_const_basic_type(bt); 954 } 955 956 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()), 957 type, T_OBJECT, 958 C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes()))); 959 } 960 961 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { 962 RegionNode *final_merge = new (C, 3) RegionNode(3); 963 kit.gvn().set_type(final_merge, Type::CONTROL); 964 Node* final_size = new (C, 3) PhiNode(final_merge, TypeInt::INT); 965 kit.gvn().set_type(final_size, TypeInt::INT); 966 967 IfNode* iff = kit.create_and_map_if(kit.control(), 968 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 969 PROB_FAIR, COUNT_UNKNOWN); 970 Node* is_min = __ IfFalse(iff); 971 final_merge->init_req(1, is_min); 972 final_size->init_req(1, __ intcon(11)); 973 974 kit.set_control(__ IfTrue(iff)); 975 if (kit.stopped()) { 976 final_merge->init_req(2, C->top()); 977 final_size->init_req(2, C->top()); 978 } else { 979 980 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 981 RegionNode *r = new (C, 3) RegionNode(3); 982 kit.gvn().set_type(r, Type::CONTROL); 983 Node *phi = new (C, 3) PhiNode(r, TypeInt::INT); 984 kit.gvn().set_type(phi, TypeInt::INT); 985 Node *size = new (C, 3) PhiNode(r, TypeInt::INT); 986 kit.gvn().set_type(size, TypeInt::INT); 987 Node* chk = __ CmpI(arg, __ intcon(0)); 988 Node* p = __ Bool(chk, BoolTest::lt); 989 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN); 990 Node* lessthan = __ IfTrue(iff); 991 Node* greaterequal = __ IfFalse(iff); 992 r->init_req(1, lessthan); 993 phi->init_req(1, __ SubI(__ intcon(0), arg)); 994 size->init_req(1, __ intcon(1)); 995 r->init_req(2, greaterequal); 996 phi->init_req(2, arg); 997 size->init_req(2, __ intcon(0)); 998 kit.set_control(r); 999 C->record_for_igvn(r); 1000 C->record_for_igvn(phi); 1001 C->record_for_igvn(size); 1002 1003 // for (int i=0; ; i++) 1004 // if (x <= sizeTable[i]) 1005 // return i+1; 1006 1007 // Add loop predicate first. 1008 kit.add_predicate(); 1009 1010 RegionNode *loop = new (C, 3) RegionNode(3); 1011 loop->init_req(1, kit.control()); 1012 kit.gvn().set_type(loop, Type::CONTROL); 1013 1014 Node *index = new (C, 3) PhiNode(loop, TypeInt::INT); 1015 index->init_req(1, __ intcon(0)); 1016 kit.gvn().set_type(index, TypeInt::INT); 1017 kit.set_control(loop); 1018 Node* sizeTable = fetch_static_field(kit, size_table_field); 1019 1020 Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS); 1021 C->record_for_igvn(value); 1022 Node* limit = __ CmpI(phi, value); 1023 Node* limitb = __ Bool(limit, BoolTest::le); 1024 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN); 1025 Node* lessEqual = __ IfTrue(iff2); 1026 Node* greater = __ IfFalse(iff2); 1027 1028 loop->init_req(2, greater); 1029 index->init_req(2, __ AddI(index, __ intcon(1))); 1030 1031 kit.set_control(lessEqual); 1032 C->record_for_igvn(loop); 1033 C->record_for_igvn(index); 1034 1035 final_merge->init_req(2, kit.control()); 1036 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1))); 1037 } 1038 1039 kit.set_control(final_merge); 1040 C->record_for_igvn(final_merge); 1041 C->record_for_igvn(final_size); 1042 1043 return final_size; 1044 } 1045 1046 void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) { 1047 RegionNode *final_merge = new (C, 4) RegionNode(4); 1048 kit.gvn().set_type(final_merge, Type::CONTROL); 1049 Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1050 kit.gvn().set_type(final_mem, Type::MEMORY); 1051 1052 // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive 1053 { 1054 // i == MIN_VALUE 1055 IfNode* iff = kit.create_and_map_if(kit.control(), 1056 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 1057 PROB_FAIR, COUNT_UNKNOWN); 1058 1059 Node* old_mem = kit.memory(char_adr_idx); 1060 1061 kit.set_control(__ IfFalse(iff)); 1062 if (kit.stopped()) { 1063 // Statically not equal to MIN_VALUE so this path is dead 1064 final_merge->init_req(3, kit.control()); 1065 } else { 1066 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())), 1067 char_array, start); 1068 final_merge->init_req(3, kit.control()); 1069 final_mem->init_req(3, kit.memory(char_adr_idx)); 1070 } 1071 1072 kit.set_control(__ IfTrue(iff)); 1073 kit.set_memory(old_mem, char_adr_idx); 1074 } 1075 1076 1077 // Simplified version of Integer.getChars 1078 1079 // int q, r; 1080 // int charPos = index; 1081 Node* charPos = end; 1082 1083 // char sign = 0; 1084 1085 Node* i = arg; 1086 Node* sign = __ intcon(0); 1087 1088 // if (i < 0) { 1089 // sign = '-'; 1090 // i = -i; 1091 // } 1092 { 1093 IfNode* iff = kit.create_and_map_if(kit.control(), 1094 __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt), 1095 PROB_FAIR, COUNT_UNKNOWN); 1096 1097 RegionNode *merge = new (C, 3) RegionNode(3); 1098 kit.gvn().set_type(merge, Type::CONTROL); 1099 i = new (C, 3) PhiNode(merge, TypeInt::INT); 1100 kit.gvn().set_type(i, TypeInt::INT); 1101 sign = new (C, 3) PhiNode(merge, TypeInt::INT); 1102 kit.gvn().set_type(sign, TypeInt::INT); 1103 1104 merge->init_req(1, __ IfTrue(iff)); 1105 i->init_req(1, __ SubI(__ intcon(0), arg)); 1106 sign->init_req(1, __ intcon('-')); 1107 merge->init_req(2, __ IfFalse(iff)); 1108 i->init_req(2, arg); 1109 sign->init_req(2, __ intcon(0)); 1110 1111 kit.set_control(merge); 1112 1113 C->record_for_igvn(merge); 1114 C->record_for_igvn(i); 1115 C->record_for_igvn(sign); 1116 } 1117 1118 // for (;;) { 1119 // q = i / 10; 1120 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 1121 // buf [--charPos] = digits [r]; 1122 // i = q; 1123 // if (i == 0) break; 1124 // } 1125 1126 { 1127 // Add loop predicate first. 1128 kit.add_predicate(); 1129 1130 RegionNode *head = new (C, 3) RegionNode(3); 1131 head->init_req(1, kit.control()); 1132 kit.gvn().set_type(head, Type::CONTROL); 1133 Node *i_phi = new (C, 3) PhiNode(head, TypeInt::INT); 1134 i_phi->init_req(1, i); 1135 kit.gvn().set_type(i_phi, TypeInt::INT); 1136 charPos = PhiNode::make(head, charPos); 1137 kit.gvn().set_type(charPos, TypeInt::INT); 1138 Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS); 1139 kit.gvn().set_type(mem, Type::MEMORY); 1140 kit.set_control(head); 1141 kit.set_memory(mem, char_adr_idx); 1142 1143 Node* q = __ DivI(NULL, i_phi, __ intcon(10)); 1144 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)), 1145 __ LShiftI(q, __ intcon(1)))); 1146 Node* m1 = __ SubI(charPos, __ intcon(1)); 1147 Node* ch = __ AddI(r, __ intcon('0')); 1148 1149 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1150 ch, T_CHAR, char_adr_idx); 1151 1152 1153 IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne), 1154 PROB_FAIR, COUNT_UNKNOWN); 1155 Node* ne = __ IfTrue(iff); 1156 Node* eq = __ IfFalse(iff); 1157 1158 head->init_req(2, ne); 1159 mem->init_req(2, st); 1160 i_phi->init_req(2, q); 1161 charPos->init_req(2, m1); 1162 1163 charPos = m1; 1164 1165 kit.set_control(eq); 1166 kit.set_memory(st, char_adr_idx); 1167 1168 C->record_for_igvn(head); 1169 C->record_for_igvn(mem); 1170 C->record_for_igvn(i_phi); 1171 C->record_for_igvn(charPos); 1172 } 1173 1174 { 1175 // if (sign != 0) { 1176 // buf [--charPos] = sign; 1177 // } 1178 IfNode* iff = kit.create_and_map_if(kit.control(), 1179 __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne), 1180 PROB_FAIR, COUNT_UNKNOWN); 1181 1182 final_merge->init_req(2, __ IfFalse(iff)); 1183 final_mem->init_req(2, kit.memory(char_adr_idx)); 1184 1185 kit.set_control(__ IfTrue(iff)); 1186 if (kit.stopped()) { 1187 final_merge->init_req(1, C->top()); 1188 final_mem->init_req(1, C->top()); 1189 } else { 1190 Node* m1 = __ SubI(charPos, __ intcon(1)); 1191 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR), 1192 sign, T_CHAR, char_adr_idx); 1193 1194 final_merge->init_req(1, kit.control()); 1195 final_mem->init_req(1, st); 1196 } 1197 1198 kit.set_control(final_merge); 1199 kit.set_memory(final_mem, char_adr_idx); 1200 1201 C->record_for_igvn(final_merge); 1202 C->record_for_igvn(final_mem); 1203 } 1204 } 1205 1206 1207 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) { 1208 Node* string = str; 1209 Node* offset = kit.load_String_offset(kit.control(), string); 1210 Node* count = kit.load_String_length(kit.control(), string); 1211 Node* value = kit.load_String_value (kit.control(), string); 1212 1213 // copy the contents 1214 if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) { 1215 // For small constant strings just emit individual stores. 1216 // A length of 6 seems like a good space/speed tradeof. 1217 int c = count->get_int(); 1218 int o = offset->get_int(); 1219 const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr(); 1220 ciTypeArray* value_array = t->const_oop()->as_type_array(); 1221 for (int e = 0; e < c; e++) { 1222 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1223 __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx); 1224 start = __ AddI(start, __ intcon(1)); 1225 } 1226 } else { 1227 Node* src_ptr = kit.array_element_address(value, offset, T_CHAR); 1228 Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR); 1229 Node* c = count; 1230 Node* extra = NULL; 1231 #ifdef _LP64 1232 c = __ ConvI2L(c); 1233 extra = C->top(); 1234 #endif 1235 Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP, 1236 OptoRuntime::fast_arraycopy_Type(), 1237 CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()), 1238 "jshort_disjoint_arraycopy", TypeAryPtr::CHARS, 1239 src_ptr, dst_ptr, c, extra); 1240 start = __ AddI(start, count); 1241 } 1242 return start; 1243 } 1244 1245 1246 void PhaseStringOpts::replace_string_concat(StringConcat* sc) { 1247 // Log a little info about the transformation 1248 sc->maybe_log_transform(); 1249 1250 // pull the JVMState of the allocation into a SafePointNode to serve as 1251 // as a shim for the insertion of the new code. 1252 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C); 1253 uint size = sc->begin()->req(); 1254 SafePointNode* map = new (C, size) SafePointNode(size, jvms); 1255 1256 // copy the control and memory state from the final call into our 1257 // new starting state. This allows any preceeding tests to feed 1258 // into the new section of code. 1259 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) { 1260 map->init_req(i1, sc->end()->in(i1)); 1261 } 1262 // blow away old allocation arguments 1263 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) { 1264 map->init_req(i1, C->top()); 1265 } 1266 // Copy the rest of the inputs for the JVMState 1267 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) { 1268 map->init_req(i1, sc->begin()->in(i1)); 1269 } 1270 // Make sure the memory state is a MergeMem for parsing. 1271 if (!map->in(TypeFunc::Memory)->is_MergeMem()) { 1272 map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory))); 1273 } 1274 1275 jvms->set_map(map); 1276 map->ensure_stack(jvms, jvms->method()->max_stack()); 1277 1278 1279 // disconnect all the old StringBuilder calls from the graph 1280 sc->eliminate_unneeded_control(); 1281 1282 // At this point all the old work has been completely removed from 1283 // the graph and the saved JVMState exists at the point where the 1284 // final toString call used to be. 1285 GraphKit kit(jvms); 1286 1287 // There may be uncommon traps which are still using the 1288 // intermediate states and these need to be rewritten to point at 1289 // the JVMState at the beginning of the transformation. 1290 sc->convert_uncommon_traps(kit, jvms); 1291 1292 // Now insert the logic to compute the size of the string followed 1293 // by all the logic to construct array and resulting string. 1294 1295 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string())); 1296 1297 // Create a region for the overflow checks to merge into. 1298 int args = MAX2(sc->num_arguments(), 1); 1299 RegionNode* overflow = new (C, args) RegionNode(args); 1300 kit.gvn().set_type(overflow, Type::CONTROL); 1301 1302 // Create a hook node to hold onto the individual sizes since they 1303 // are need for the copying phase. 1304 Node* string_sizes = new (C, args) Node(args); 1305 1306 Node* length = __ intcon(0); 1307 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1308 Node* arg = sc->argument(argi); 1309 switch (sc->mode(argi)) { 1310 case StringConcat::IntMode: { 1311 Node* string_size = int_stringSize(kit, arg); 1312 1313 // accumulate total 1314 length = __ AddI(length, string_size); 1315 1316 // Cache this value for the use by int_toString 1317 string_sizes->init_req(argi, string_size); 1318 break; 1319 } 1320 case StringConcat::StringNullCheckMode: { 1321 const Type* type = kit.gvn().type(arg); 1322 assert(type != TypePtr::NULL_PTR, "missing check"); 1323 if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1324 // Null check with uncommont trap since 1325 // StringBuilder(null) throws exception. 1326 // Use special uncommon trap instead of 1327 // calling normal do_null_check(). 1328 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1329 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1330 overflow->add_req(__ IfFalse(iff)); 1331 Node* notnull = __ IfTrue(iff); 1332 kit.set_control(notnull); // set control for the cast_not_null 1333 arg = kit.cast_not_null(arg, false); 1334 sc->set_argument(argi, arg); 1335 } 1336 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity"); 1337 // Fallthrough to add string length. 1338 } 1339 case StringConcat::StringMode: { 1340 const Type* type = kit.gvn().type(arg); 1341 if (type == TypePtr::NULL_PTR) { 1342 // replace the argument with the null checked version 1343 arg = null_string; 1344 sc->set_argument(argi, arg); 1345 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1346 // s = s != null ? s : "null"; 1347 // length = length + (s.count - s.offset); 1348 RegionNode *r = new (C, 3) RegionNode(3); 1349 kit.gvn().set_type(r, Type::CONTROL); 1350 Node *phi = new (C, 3) PhiNode(r, type); 1351 kit.gvn().set_type(phi, phi->bottom_type()); 1352 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1353 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1354 Node* notnull = __ IfTrue(iff); 1355 Node* isnull = __ IfFalse(iff); 1356 kit.set_control(notnull); // set control for the cast_not_null 1357 r->init_req(1, notnull); 1358 phi->init_req(1, kit.cast_not_null(arg, false)); 1359 r->init_req(2, isnull); 1360 phi->init_req(2, null_string); 1361 kit.set_control(r); 1362 C->record_for_igvn(r); 1363 C->record_for_igvn(phi); 1364 // replace the argument with the null checked version 1365 arg = phi; 1366 sc->set_argument(argi, arg); 1367 } 1368 1369 Node* count = kit.load_String_length(kit.control(), arg); 1370 1371 length = __ AddI(length, count); 1372 string_sizes->init_req(argi, NULL); 1373 break; 1374 } 1375 case StringConcat::CharMode: { 1376 // one character only 1377 length = __ AddI(length, __ intcon(1)); 1378 break; 1379 } 1380 default: 1381 ShouldNotReachHere(); 1382 } 1383 if (argi > 0) { 1384 // Check that the sum hasn't overflowed 1385 IfNode* iff = kit.create_and_map_if(kit.control(), 1386 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt), 1387 PROB_MIN, COUNT_UNKNOWN); 1388 kit.set_control(__ IfFalse(iff)); 1389 overflow->set_req(argi, __ IfTrue(iff)); 1390 } 1391 } 1392 1393 { 1394 // Hook 1395 PreserveJVMState pjvms(&kit); 1396 kit.set_control(overflow); 1397 C->record_for_igvn(overflow); 1398 kit.uncommon_trap(Deoptimization::Reason_intrinsic, 1399 Deoptimization::Action_make_not_entrant); 1400 } 1401 1402 // length now contains the number of characters needed for the 1403 // char[] so create a new AllocateArray for the char[] 1404 Node* char_array = NULL; 1405 { 1406 PreserveReexecuteState preexecs(&kit); 1407 // The original jvms is for an allocation of either a String or 1408 // StringBuffer so no stack adjustment is necessary for proper 1409 // reexecution. If we deoptimize in the slow path the bytecode 1410 // will be reexecuted and the char[] allocation will be thrown away. 1411 kit.jvms()->set_should_reexecute(true); 1412 char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))), 1413 length, 1); 1414 } 1415 1416 // Mark the allocation so that zeroing is skipped since the code 1417 // below will overwrite the entire array 1418 AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn); 1419 char_alloc->maybe_set_complete(_gvn); 1420 1421 // Now copy the string representations into the final char[] 1422 Node* start = __ intcon(0); 1423 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1424 Node* arg = sc->argument(argi); 1425 switch (sc->mode(argi)) { 1426 case StringConcat::IntMode: { 1427 Node* end = __ AddI(start, string_sizes->in(argi)); 1428 // getChars words backwards so pass the ending point as well as the start 1429 int_getChars(kit, arg, char_array, start, end); 1430 start = end; 1431 break; 1432 } 1433 case StringConcat::StringNullCheckMode: 1434 case StringConcat::StringMode: { 1435 start = copy_string(kit, arg, char_array, start); 1436 break; 1437 } 1438 case StringConcat::CharMode: { 1439 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR), 1440 arg, T_CHAR, char_adr_idx); 1441 start = __ AddI(start, __ intcon(1)); 1442 break; 1443 } 1444 default: 1445 ShouldNotReachHere(); 1446 } 1447 } 1448 1449 // If we're not reusing an existing String allocation then allocate one here. 1450 Node* result = sc->string_alloc(); 1451 if (result == NULL) { 1452 PreserveReexecuteState preexecs(&kit); 1453 // The original jvms is for an allocation of either a String or 1454 // StringBuffer so no stack adjustment is necessary for proper 1455 // reexecution. 1456 kit.jvms()->set_should_reexecute(true); 1457 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass()))); 1458 } 1459 1460 // Intialize the string 1461 if (java_lang_String::has_offset_field()) { 1462 kit.store_String_offset(kit.control(), result, __ intcon(0)); 1463 kit.store_String_length(kit.control(), result, length); 1464 } 1465 kit.store_String_value(kit.control(), result, char_array); 1466 1467 // hook up the outgoing control and result 1468 kit.replace_call(sc->end(), result); 1469 1470 // Unhook any hook nodes 1471 string_sizes->disconnect_inputs(NULL); 1472 sc->cleanup(); 1473 }