1 /* 2 * Copyright (c) 2009, 2020, 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 "ci/ciSymbols.hpp" 27 #include "classfile/javaClasses.hpp" 28 #include "compiler/compileLog.hpp" 29 #include "opto/addnode.hpp" 30 #include "opto/callGenerator.hpp" 31 #include "opto/callnode.hpp" 32 #include "opto/divnode.hpp" 33 #include "opto/graphKit.hpp" 34 #include "opto/idealKit.hpp" 35 #include "opto/rootnode.hpp" 36 #include "opto/runtime.hpp" 37 #include "opto/stringopts.hpp" 38 #include "opto/subnode.hpp" 39 #include "runtime/sharedRuntime.hpp" 40 #include "runtime/stubRoutines.hpp" 41 42 #define __ kit. 43 44 class StringConcat : public ResourceObj { 45 private: 46 PhaseStringOpts* _stringopts; 47 Node* _string_alloc; 48 AllocateNode* _begin; // The allocation the begins the pattern 49 CallStaticJavaNode* _end; // The final call of the pattern. Will either be 50 // SB.toString or or String.<init>(SB.toString) 51 bool _multiple; // indicates this is a fusion of two or more 52 // separate StringBuilders 53 54 Node* _arguments; // The list of arguments to be concatenated 55 GrowableArray<int> _mode; // into a String along with a mode flag 56 // indicating how to treat the value. 57 Node_List _constructors; // List of constructors (many in case of stacked concat) 58 Node_List _control; // List of control nodes that will be deleted 59 Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten 60 // to restart at the initial JVMState. 61 62 public: 63 // Mode for converting arguments to Strings 64 enum { 65 StringMode, 66 IntMode, 67 CharMode, 68 StringNullCheckMode, 69 NegativeIntCheckMode 70 }; 71 72 StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end): 73 _stringopts(stringopts), 74 _string_alloc(NULL), 75 _begin(NULL), 76 _end(end), 77 _multiple(false) { 78 _arguments = new Node(1); 79 _arguments->del_req(0); 80 } 81 82 bool validate_mem_flow(); 83 bool validate_control_flow(); 84 85 void merge_add() { 86 #if 0 87 // XXX This is place holder code for reusing an existing String 88 // allocation but the logic for checking the state safety is 89 // probably inadequate at the moment. 90 CallProjections endprojs; 91 sc->end()->extract_projections(&endprojs, false); 92 if (endprojs.resproj != NULL) { 93 for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) { 94 CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 95 if (use != NULL && use->method() != NULL && 96 use->method()->intrinsic_id() == vmIntrinsics::_String_String && 97 use->in(TypeFunc::Parms + 1) == endprojs.resproj) { 98 // Found useless new String(sb.toString()) so reuse the newly allocated String 99 // when creating the result instead of allocating a new one. 100 sc->set_string_alloc(use->in(TypeFunc::Parms)); 101 sc->set_end(use); 102 } 103 } 104 } 105 #endif 106 } 107 108 StringConcat* merge(StringConcat* other, Node* arg); 109 110 void set_allocation(AllocateNode* alloc) { 111 _begin = alloc; 112 } 113 114 void append(Node* value, int mode) { 115 _arguments->add_req(value); 116 _mode.append(mode); 117 } 118 void push(Node* value, int mode) { 119 _arguments->ins_req(0, value); 120 _mode.insert_before(0, mode); 121 } 122 123 void push_string(Node* value) { 124 push(value, StringMode); 125 } 126 127 void push_string_null_check(Node* value) { 128 push(value, StringNullCheckMode); 129 } 130 131 void push_negative_int_check(Node* value) { 132 push(value, NegativeIntCheckMode); 133 } 134 135 void push_int(Node* value) { 136 push(value, IntMode); 137 } 138 139 void push_char(Node* value) { 140 push(value, CharMode); 141 } 142 143 static bool is_SB_toString(Node* call) { 144 if (call->is_CallStaticJava()) { 145 CallStaticJavaNode* csj = call->as_CallStaticJava(); 146 ciMethod* m = csj->method(); 147 if (m != NULL && 148 (m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString || 149 m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) { 150 return true; 151 } 152 } 153 return false; 154 } 155 156 static Node* skip_string_null_check(Node* value) { 157 // Look for a diamond shaped Null check of toString() result 158 // (could be code from String.valueOf()): 159 // (Proj == NULL) ? "null":"CastPP(Proj)#NotNULL 160 if (value->is_Phi()) { 161 int true_path = value->as_Phi()->is_diamond_phi(); 162 if (true_path != 0) { 163 // phi->region->if_proj->ifnode->bool 164 BoolNode* b = value->in(0)->in(1)->in(0)->in(1)->as_Bool(); 165 Node* cmp = b->in(1); 166 Node* v1 = cmp->in(1); 167 Node* v2 = cmp->in(2); 168 // Null check of the return of toString which can simply be skipped. 169 if (b->_test._test == BoolTest::ne && 170 v2->bottom_type() == TypePtr::NULL_PTR && 171 value->in(true_path)->Opcode() == Op_CastPP && 172 value->in(true_path)->in(1) == v1 && 173 v1->is_Proj() && is_SB_toString(v1->in(0))) { 174 return v1; 175 } 176 } 177 } 178 return value; 179 } 180 181 Node* argument(int i) { 182 return _arguments->in(i); 183 } 184 Node* argument_uncast(int i) { 185 Node* arg = argument(i); 186 int amode = mode(i); 187 if (amode == StringConcat::StringMode || 188 amode == StringConcat::StringNullCheckMode) { 189 arg = skip_string_null_check(arg); 190 } 191 return arg; 192 } 193 void set_argument(int i, Node* value) { 194 _arguments->set_req(i, value); 195 } 196 int num_arguments() { 197 return _mode.length(); 198 } 199 int mode(int i) { 200 return _mode.at(i); 201 } 202 void add_control(Node* ctrl) { 203 assert(!_control.contains(ctrl), "only push once"); 204 _control.push(ctrl); 205 } 206 void add_constructor(Node* init) { 207 assert(!_constructors.contains(init), "only push once"); 208 _constructors.push(init); 209 } 210 CallStaticJavaNode* end() { return _end; } 211 AllocateNode* begin() { return _begin; } 212 Node* string_alloc() { return _string_alloc; } 213 214 void eliminate_unneeded_control(); 215 void eliminate_initialize(InitializeNode* init); 216 void eliminate_call(CallNode* call); 217 218 void maybe_log_transform() { 219 CompileLog* log = _stringopts->C->log(); 220 if (log != NULL) { 221 log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'", 222 num_arguments(), 223 _string_alloc != NULL, 224 _multiple); 225 JVMState* p = _begin->jvms(); 226 while (p != NULL) { 227 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 228 p = p->caller(); 229 } 230 log->tail("replace_string_concat"); 231 } 232 } 233 234 void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) { 235 for (uint u = 0; u < _uncommon_traps.size(); u++) { 236 Node* uct = _uncommon_traps.at(u); 237 238 // Build a new call using the jvms state of the allocate 239 address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point(); 240 const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type(); 241 const TypePtr* no_memory_effects = NULL; 242 Compile* C = _stringopts->C; 243 CallStaticJavaNode* call = new CallStaticJavaNode(call_type, call_addr, "uncommon_trap", 244 no_memory_effects); 245 for (int e = 0; e < TypeFunc::Parms; e++) { 246 call->init_req(e, uct->in(e)); 247 } 248 // Set the trap request to record intrinsic failure if this trap 249 // is taken too many times. Ideally we would handle then traps by 250 // doing the original bookkeeping in the MDO so that if it caused 251 // the code to be thrown out we could still recompile and use the 252 // optimization. Failing the uncommon traps doesn't really mean 253 // that the optimization is a bad idea but there's no other way to 254 // do the MDO updates currently. 255 int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic, 256 Deoptimization::Action_make_not_entrant); 257 call->init_req(TypeFunc::Parms, __ intcon(trap_request)); 258 kit.add_safepoint_edges(call); 259 260 _stringopts->gvn()->transform(call); 261 C->gvn_replace_by(uct, call); 262 uct->disconnect_inputs(C); 263 } 264 } 265 266 void cleanup() { 267 // disconnect the hook node 268 _arguments->disconnect_inputs(_stringopts->C); 269 } 270 }; 271 272 273 void StringConcat::eliminate_unneeded_control() { 274 for (uint i = 0; i < _control.size(); i++) { 275 Node* n = _control.at(i); 276 if (n->is_Allocate()) { 277 eliminate_initialize(n->as_Allocate()->initialization()); 278 } 279 if (n->is_Call()) { 280 if (n != _end) { 281 eliminate_call(n->as_Call()); 282 } 283 } else if (n->is_IfTrue()) { 284 Compile* C = _stringopts->C; 285 C->gvn_replace_by(n, n->in(0)->in(0)); 286 // get rid of the other projection 287 C->gvn_replace_by(n->in(0)->as_If()->proj_out(false), C->top()); 288 } else if (n->is_Region()) { 289 Node* iff = n->in(1)->in(0); 290 assert(n->req() == 3 && n->in(2)->in(0) == iff, "not a diamond"); 291 assert(iff->is_If(), "no if for the diamond"); 292 Node* bol = iff->in(1); 293 assert(bol->is_Bool(), "unexpected if shape"); 294 Node* cmp = bol->in(1); 295 assert(cmp->is_Cmp(), "unexpected if shape"); 296 if (cmp->in(1)->is_top() || cmp->in(2)->is_top()) { 297 // This region should lose its Phis and be optimized out by igvn but there's a chance the if folds to top first 298 // which then causes a reachable part of the graph to become dead. 299 Compile* C = _stringopts->C; 300 C->gvn_replace_by(n, iff->in(0)); 301 } 302 } 303 } 304 } 305 306 307 StringConcat* StringConcat::merge(StringConcat* other, Node* arg) { 308 StringConcat* result = new StringConcat(_stringopts, _end); 309 for (uint x = 0; x < _control.size(); x++) { 310 Node* n = _control.at(x); 311 if (n->is_Call()) { 312 result->_control.push(n); 313 } 314 } 315 for (uint x = 0; x < other->_control.size(); x++) { 316 Node* n = other->_control.at(x); 317 if (n->is_Call()) { 318 result->_control.push(n); 319 } 320 } 321 assert(result->_control.contains(other->_end), "what?"); 322 assert(result->_control.contains(_begin), "what?"); 323 for (int x = 0; x < num_arguments(); x++) { 324 Node* argx = argument_uncast(x); 325 if (argx == arg) { 326 // replace the toString result with the all the arguments that 327 // made up the other StringConcat 328 for (int y = 0; y < other->num_arguments(); y++) { 329 result->append(other->argument(y), other->mode(y)); 330 } 331 } else { 332 result->append(argx, mode(x)); 333 } 334 } 335 result->set_allocation(other->_begin); 336 for (uint i = 0; i < _constructors.size(); i++) { 337 result->add_constructor(_constructors.at(i)); 338 } 339 for (uint i = 0; i < other->_constructors.size(); i++) { 340 result->add_constructor(other->_constructors.at(i)); 341 } 342 result->_multiple = true; 343 return result; 344 } 345 346 347 void StringConcat::eliminate_call(CallNode* call) { 348 Compile* C = _stringopts->C; 349 CallProjections projs; 350 call->extract_projections(&projs, false); 351 if (projs.fallthrough_catchproj != NULL) { 352 C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control)); 353 } 354 if (projs.fallthrough_memproj != NULL) { 355 C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory)); 356 } 357 if (projs.catchall_memproj != NULL) { 358 C->gvn_replace_by(projs.catchall_memproj, C->top()); 359 } 360 if (projs.fallthrough_ioproj != NULL) { 361 C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O)); 362 } 363 if (projs.catchall_ioproj != NULL) { 364 C->gvn_replace_by(projs.catchall_ioproj, C->top()); 365 } 366 if (projs.catchall_catchproj != NULL) { 367 // EA can't cope with the partially collapsed graph this 368 // creates so put it on the worklist to be collapsed later. 369 for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) { 370 Node *use = i.get(); 371 int opc = use->Opcode(); 372 if (opc == Op_CreateEx || opc == Op_Region) { 373 _stringopts->record_dead_node(use); 374 } 375 } 376 C->gvn_replace_by(projs.catchall_catchproj, C->top()); 377 } 378 if (projs.resproj != NULL) { 379 C->gvn_replace_by(projs.resproj, C->top()); 380 } 381 C->gvn_replace_by(call, C->top()); 382 } 383 384 void StringConcat::eliminate_initialize(InitializeNode* init) { 385 Compile* C = _stringopts->C; 386 387 // Eliminate Initialize node. 388 assert(init->outcnt() <= 2, "only a control and memory projection expected"); 389 assert(init->req() <= InitializeNode::RawStores, "no pending inits"); 390 Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control); 391 if (ctrl_proj != NULL) { 392 C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control)); 393 } 394 Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory); 395 if (mem_proj != NULL) { 396 Node *mem = init->in(TypeFunc::Memory); 397 C->gvn_replace_by(mem_proj, mem); 398 } 399 C->gvn_replace_by(init, C->top()); 400 init->disconnect_inputs(C); 401 } 402 403 Node_List PhaseStringOpts::collect_toString_calls() { 404 Node_List string_calls; 405 Node_List worklist; 406 407 _visited.clear(); 408 409 // Prime the worklist 410 for (uint i = 1; i < C->root()->len(); i++) { 411 Node* n = C->root()->in(i); 412 if (n != NULL && !_visited.test_set(n->_idx)) { 413 worklist.push(n); 414 } 415 } 416 417 while (worklist.size() > 0) { 418 Node* ctrl = worklist.pop(); 419 if (StringConcat::is_SB_toString(ctrl)) { 420 CallStaticJavaNode* csj = ctrl->as_CallStaticJava(); 421 string_calls.push(csj); 422 } 423 if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) { 424 worklist.push(ctrl->in(0)); 425 } 426 if (ctrl->is_Region()) { 427 for (uint i = 1; i < ctrl->len(); i++) { 428 if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) { 429 worklist.push(ctrl->in(i)); 430 } 431 } 432 } 433 } 434 return string_calls; 435 } 436 437 438 StringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) { 439 ciMethod* m = call->method(); 440 ciSymbol* string_sig; 441 ciSymbol* int_sig; 442 ciSymbol* char_sig; 443 if (m->holder() == C->env()->StringBuilder_klass()) { 444 string_sig = ciSymbols::String_StringBuilder_signature(); 445 int_sig = ciSymbols::int_StringBuilder_signature(); 446 char_sig = ciSymbols::char_StringBuilder_signature(); 447 } else if (m->holder() == C->env()->StringBuffer_klass()) { 448 string_sig = ciSymbols::String_StringBuffer_signature(); 449 int_sig = ciSymbols::int_StringBuffer_signature(); 450 char_sig = ciSymbols::char_StringBuffer_signature(); 451 } else { 452 return NULL; 453 } 454 #ifndef PRODUCT 455 if (PrintOptimizeStringConcat) { 456 tty->print("considering toString call in "); 457 call->jvms()->dump_spec(tty); tty->cr(); 458 } 459 #endif 460 461 StringConcat* sc = new StringConcat(this, call); 462 463 AllocateNode* alloc = NULL; 464 InitializeNode* init = NULL; 465 466 // possible opportunity for StringBuilder fusion 467 CallStaticJavaNode* cnode = call; 468 while (cnode) { 469 Node* recv = cnode->in(TypeFunc::Parms)->uncast(); 470 if (recv->is_Proj()) { 471 recv = recv->in(0); 472 } 473 cnode = recv->isa_CallStaticJava(); 474 if (cnode == NULL) { 475 alloc = recv->isa_Allocate(); 476 if (alloc == NULL) { 477 break; 478 } 479 // Find the constructor call 480 Node* result = alloc->result_cast(); 481 if (result == NULL || !result->is_CheckCastPP() || alloc->in(TypeFunc::Memory)->is_top()) { 482 // strange looking allocation 483 #ifndef PRODUCT 484 if (PrintOptimizeStringConcat) { 485 tty->print("giving up because allocation looks strange "); 486 alloc->jvms()->dump_spec(tty); tty->cr(); 487 } 488 #endif 489 break; 490 } 491 Node* constructor = NULL; 492 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 493 CallStaticJavaNode *use = i.get()->isa_CallStaticJava(); 494 if (use != NULL && 495 use->method() != NULL && 496 !use->method()->is_static() && 497 use->method()->name() == ciSymbols::object_initializer_name() && 498 use->method()->holder() == m->holder()) { 499 // Matched the constructor. 500 ciSymbol* sig = use->method()->signature()->as_symbol(); 501 if (sig == ciSymbols::void_method_signature() || 502 sig == ciSymbols::int_void_signature() || 503 sig == ciSymbols::string_void_signature()) { 504 if (sig == ciSymbols::string_void_signature()) { 505 // StringBuilder(String) so pick this up as the first argument 506 assert(use->in(TypeFunc::Parms + 1) != NULL, "what?"); 507 const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1)); 508 if (type == TypePtr::NULL_PTR) { 509 // StringBuilder(null) throws exception. 510 #ifndef PRODUCT 511 if (PrintOptimizeStringConcat) { 512 tty->print("giving up because StringBuilder(null) throws exception"); 513 alloc->jvms()->dump_spec(tty); 514 tty->cr(); 515 } 516 #endif 517 return NULL; 518 } 519 // StringBuilder(str) argument needs null check. 520 sc->push_string_null_check(use->in(TypeFunc::Parms + 1)); 521 } else if (sig == ciSymbols::int_void_signature()) { 522 // StringBuilder(int) case. 523 Node* parm = use->in(TypeFunc::Parms + 1); 524 assert(parm != NULL, "must exist"); 525 const TypeInt* type = _gvn->type(parm)->is_int(); 526 if (type->_hi < 0) { 527 // Initial capacity argument is always negative in which case StringBuilder(int) throws 528 // a NegativeArraySizeException. Bail out from string opts. 529 #ifndef PRODUCT 530 if (PrintOptimizeStringConcat) { 531 tty->print("giving up because a negative argument is passed to StringBuilder(int) which " 532 "throws a NegativeArraySizeException"); 533 alloc->jvms()->dump_spec(tty); 534 tty->cr(); 535 } 536 #endif 537 return NULL; 538 } else if (type->_lo < 0) { 539 // Argument could be negative: We need a runtime check to throw NegativeArraySizeException in that case. 540 sc->push_negative_int_check(parm); 541 } 542 } 543 // The int variant takes an initial size for the backing 544 // array so just treat it like the void version. 545 constructor = use; 546 } else { 547 #ifndef PRODUCT 548 if (PrintOptimizeStringConcat) { 549 tty->print("unexpected constructor signature: %s", sig->as_utf8()); 550 } 551 #endif 552 } 553 break; 554 } 555 } 556 if (constructor == NULL) { 557 // couldn't find constructor 558 #ifndef PRODUCT 559 if (PrintOptimizeStringConcat) { 560 tty->print("giving up because couldn't find constructor "); 561 alloc->jvms()->dump_spec(tty); tty->cr(); 562 } 563 #endif 564 break; 565 } 566 567 // Walked all the way back and found the constructor call so see 568 // if this call converted into a direct string concatenation. 569 sc->add_control(call); 570 sc->add_control(constructor); 571 sc->add_control(alloc); 572 sc->set_allocation(alloc); 573 sc->add_constructor(constructor); 574 if (sc->validate_control_flow() && sc->validate_mem_flow()) { 575 return sc; 576 } else { 577 return NULL; 578 } 579 } else if (cnode->method() == NULL) { 580 break; 581 } else if (!cnode->method()->is_static() && 582 cnode->method()->holder() == m->holder() && 583 cnode->method()->name() == ciSymbols::append_name() && 584 (cnode->method()->signature()->as_symbol() == string_sig || 585 cnode->method()->signature()->as_symbol() == char_sig || 586 cnode->method()->signature()->as_symbol() == int_sig)) { 587 sc->add_control(cnode); 588 Node* arg = cnode->in(TypeFunc::Parms + 1); 589 if (arg == NULL || arg->is_top()) { 590 #ifndef PRODUCT 591 if (PrintOptimizeStringConcat) { 592 tty->print("giving up because the call is effectively dead"); 593 cnode->jvms()->dump_spec(tty); tty->cr(); 594 } 595 #endif 596 break; 597 } 598 if (cnode->method()->signature()->as_symbol() == int_sig) { 599 sc->push_int(arg); 600 } else if (cnode->method()->signature()->as_symbol() == char_sig) { 601 sc->push_char(arg); 602 } else { 603 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) { 604 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 605 if (csj->method() != NULL && 606 csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString && 607 arg->outcnt() == 1) { 608 // _control is the list of StringBuilder calls nodes which 609 // will be replaced by new String code after this optimization. 610 // Integer::toString() call is not part of StringBuilder calls 611 // chain. It could be eliminated only if its result is used 612 // only by this SB calls chain. 613 // Another limitation: it should be used only once because 614 // it is unknown that it is used only by this SB calls chain 615 // until all related SB calls nodes are collected. 616 assert(arg->unique_out() == cnode, "sanity"); 617 sc->add_control(csj); 618 sc->push_int(csj->in(TypeFunc::Parms)); 619 continue; 620 } 621 } 622 sc->push_string(arg); 623 } 624 continue; 625 } else { 626 // some unhandled signature 627 #ifndef PRODUCT 628 if (PrintOptimizeStringConcat) { 629 tty->print("giving up because encountered unexpected signature "); 630 cnode->tf()->dump(); tty->cr(); 631 cnode->in(TypeFunc::Parms + 1)->dump(); 632 } 633 #endif 634 break; 635 } 636 } 637 return NULL; 638 } 639 640 641 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*): 642 Phase(StringOpts), 643 _gvn(gvn) { 644 645 assert(OptimizeStringConcat, "shouldn't be here"); 646 647 size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"), 648 ciSymbols::int_array_signature(), true); 649 if (size_table_field == NULL) { 650 // Something wrong so give up. 651 assert(false, "why can't we find Integer.sizeTable?"); 652 return; 653 } 654 655 // Collect the types needed to talk about the various slices of memory 656 byte_adr_idx = C->get_alias_index(TypeAryPtr::BYTES); 657 658 // For each locally allocated StringBuffer see if the usages can be 659 // collapsed into a single String construction. 660 661 // Run through the list of allocation looking for SB.toString to see 662 // if it's possible to fuse the usage of the SB into a single String 663 // construction. 664 GrowableArray<StringConcat*> concats; 665 Node_List toStrings = collect_toString_calls(); 666 while (toStrings.size() > 0) { 667 StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava()); 668 if (sc != NULL) { 669 concats.push(sc); 670 } 671 } 672 673 // try to coalesce separate concats 674 restart: 675 for (int c = 0; c < concats.length(); c++) { 676 StringConcat* sc = concats.at(c); 677 for (int i = 0; i < sc->num_arguments(); i++) { 678 Node* arg = sc->argument_uncast(i); 679 if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) { 680 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava(); 681 for (int o = 0; o < concats.length(); o++) { 682 if (c == o) continue; 683 StringConcat* other = concats.at(o); 684 if (other->end() == csj) { 685 #ifndef PRODUCT 686 if (PrintOptimizeStringConcat) { 687 tty->print_cr("considering stacked concats"); 688 } 689 #endif 690 691 StringConcat* merged = sc->merge(other, arg); 692 if (merged->validate_control_flow() && merged->validate_mem_flow()) { 693 #ifndef PRODUCT 694 if (PrintOptimizeStringConcat) { 695 tty->print_cr("stacking would succeed"); 696 } 697 #endif 698 if (c < o) { 699 concats.remove_at(o); 700 concats.at_put(c, merged); 701 } else { 702 concats.remove_at(c); 703 concats.at_put(o, merged); 704 } 705 goto restart; 706 } else { 707 #ifndef PRODUCT 708 if (PrintOptimizeStringConcat) { 709 tty->print_cr("stacking would fail"); 710 } 711 #endif 712 } 713 } 714 } 715 } 716 } 717 } 718 719 720 for (int c = 0; c < concats.length(); c++) { 721 StringConcat* sc = concats.at(c); 722 replace_string_concat(sc); 723 } 724 725 remove_dead_nodes(); 726 } 727 728 void PhaseStringOpts::record_dead_node(Node* dead) { 729 dead_worklist.push(dead); 730 } 731 732 void PhaseStringOpts::remove_dead_nodes() { 733 // Delete any dead nodes to make things clean enough that escape 734 // analysis doesn't get unhappy. 735 while (dead_worklist.size() > 0) { 736 Node* use = dead_worklist.pop(); 737 int opc = use->Opcode(); 738 switch (opc) { 739 case Op_Region: { 740 uint i = 1; 741 for (i = 1; i < use->req(); i++) { 742 if (use->in(i) != C->top()) { 743 break; 744 } 745 } 746 if (i >= use->req()) { 747 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 748 Node* m = i.get(); 749 if (m->is_Phi()) { 750 dead_worklist.push(m); 751 } 752 } 753 C->gvn_replace_by(use, C->top()); 754 } 755 break; 756 } 757 case Op_AddP: 758 case Op_CreateEx: { 759 // Recurisvely clean up references to CreateEx so EA doesn't 760 // get unhappy about the partially collapsed graph. 761 for (SimpleDUIterator i(use); i.has_next(); i.next()) { 762 Node* m = i.get(); 763 if (m->is_AddP()) { 764 dead_worklist.push(m); 765 } 766 } 767 C->gvn_replace_by(use, C->top()); 768 break; 769 } 770 case Op_Phi: 771 if (use->in(0) == C->top()) { 772 C->gvn_replace_by(use, C->top()); 773 } 774 break; 775 } 776 } 777 } 778 779 780 bool StringConcat::validate_mem_flow() { 781 Compile* C = _stringopts->C; 782 783 for (uint i = 0; i < _control.size(); i++) { 784 #ifndef PRODUCT 785 Node_List path; 786 #endif 787 Node* curr = _control.at(i); 788 if (curr->is_Call() && curr != _begin) { // For all calls except the first allocation 789 // Now here's the main invariant in our case: 790 // For memory between the constructor, and appends, and toString we should only see bottom memory, 791 // produced by the previous call we know about. 792 if (!_constructors.contains(curr)) { 793 NOT_PRODUCT(path.push(curr);) 794 Node* mem = curr->in(TypeFunc::Memory); 795 assert(mem != NULL, "calls should have memory edge"); 796 assert(!mem->is_Phi(), "should be handled by control flow validation"); 797 NOT_PRODUCT(path.push(mem);) 798 while (mem->is_MergeMem()) { 799 for (uint i = 1; i < mem->req(); i++) { 800 if (i != Compile::AliasIdxBot && mem->in(i) != C->top()) { 801 #ifndef PRODUCT 802 if (PrintOptimizeStringConcat) { 803 tty->print("fusion has incorrect memory flow (side effects) for "); 804 _begin->jvms()->dump_spec(tty); tty->cr(); 805 path.dump(); 806 } 807 #endif 808 return false; 809 } 810 } 811 // skip through a potential MergeMem chain, linked through Bot 812 mem = mem->in(Compile::AliasIdxBot); 813 NOT_PRODUCT(path.push(mem);) 814 } 815 // now let it fall through, and see if we have a projection 816 if (mem->is_Proj()) { 817 // Should point to a previous known call 818 Node *prev = mem->in(0); 819 NOT_PRODUCT(path.push(prev);) 820 if (!prev->is_Call() || !_control.contains(prev)) { 821 #ifndef PRODUCT 822 if (PrintOptimizeStringConcat) { 823 tty->print("fusion has incorrect memory flow (unknown call) for "); 824 _begin->jvms()->dump_spec(tty); tty->cr(); 825 path.dump(); 826 } 827 #endif 828 return false; 829 } 830 } else { 831 assert(mem->is_Store() || mem->is_LoadStore(), "unexpected node type: %s", mem->Name()); 832 #ifndef PRODUCT 833 if (PrintOptimizeStringConcat) { 834 tty->print("fusion has incorrect memory flow (unexpected source) for "); 835 _begin->jvms()->dump_spec(tty); tty->cr(); 836 path.dump(); 837 } 838 #endif 839 return false; 840 } 841 } else { 842 // For memory that feeds into constructors it's more complicated. 843 // However the advantage is that any side effect that happens between the Allocate/Initialize and 844 // the constructor will have to be control-dependent on Initialize. 845 // So we actually don't have to do anything, since it's going to be caught by the control flow 846 // analysis. 847 #ifdef ASSERT 848 // Do a quick verification of the control pattern between the constructor and the initialize node 849 assert(curr->is_Call(), "constructor should be a call"); 850 // Go up the control starting from the constructor call 851 Node* ctrl = curr->in(0); 852 IfNode* iff = NULL; 853 RegionNode* copy = NULL; 854 855 while (true) { 856 // skip known check patterns 857 if (ctrl->is_Region()) { 858 if (ctrl->as_Region()->is_copy()) { 859 copy = ctrl->as_Region(); 860 ctrl = copy->is_copy(); 861 } else { // a cast 862 assert(ctrl->req() == 3 && 863 ctrl->in(1) != NULL && ctrl->in(1)->is_Proj() && 864 ctrl->in(2) != NULL && ctrl->in(2)->is_Proj() && 865 ctrl->in(1)->in(0) == ctrl->in(2)->in(0) && 866 ctrl->in(1)->in(0) != NULL && ctrl->in(1)->in(0)->is_If(), 867 "must be a simple diamond"); 868 Node* true_proj = ctrl->in(1)->is_IfTrue() ? ctrl->in(1) : ctrl->in(2); 869 for (SimpleDUIterator i(true_proj); i.has_next(); i.next()) { 870 Node* use = i.get(); 871 assert(use == ctrl || use->is_ConstraintCast(), 872 "unexpected user: %s", use->Name()); 873 } 874 875 iff = ctrl->in(1)->in(0)->as_If(); 876 ctrl = iff->in(0); 877 } 878 } else if (ctrl->is_IfTrue()) { // null checks, class checks 879 iff = ctrl->in(0)->as_If(); 880 // Verify that the other arm is an uncommon trap 881 Node* otherproj = iff->proj_out(1 - ctrl->as_Proj()->_con); 882 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 883 assert(strcmp(call->_name, "uncommon_trap") == 0, "must be uncommon trap"); 884 ctrl = iff->in(0); 885 } else { 886 break; 887 } 888 } 889 890 assert(ctrl->is_Proj(), "must be a projection"); 891 assert(ctrl->in(0)->is_Initialize(), "should be initialize"); 892 for (SimpleDUIterator i(ctrl); i.has_next(); i.next()) { 893 Node* use = i.get(); 894 assert(use == copy || use == iff || use == curr || use->is_CheckCastPP() || use->is_Load(), 895 "unexpected user: %s", use->Name()); 896 } 897 #endif // ASSERT 898 } 899 } 900 } 901 902 #ifndef PRODUCT 903 if (PrintOptimizeStringConcat) { 904 tty->print("fusion has correct memory flow for "); 905 _begin->jvms()->dump_spec(tty); tty->cr(); 906 tty->cr(); 907 } 908 #endif 909 return true; 910 } 911 912 bool StringConcat::validate_control_flow() { 913 // We found all the calls and arguments now lets see if it's 914 // safe to transform the graph as we would expect. 915 916 // Check to see if this resulted in too many uncommon traps previously 917 if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(), 918 Deoptimization::Reason_intrinsic)) { 919 return false; 920 } 921 922 // Walk backwards over the control flow from toString to the 923 // allocation and make sure all the control flow is ok. This 924 // means it's either going to be eliminated once the calls are 925 // removed or it can safely be transformed into an uncommon 926 // trap. 927 928 int null_check_count = 0; 929 Unique_Node_List ctrl_path; 930 931 assert(_control.contains(_begin), "missing"); 932 assert(_control.contains(_end), "missing"); 933 934 // Collect the nodes that we know about and will eliminate into ctrl_path 935 for (uint i = 0; i < _control.size(); i++) { 936 // Push the call and it's control projection 937 Node* n = _control.at(i); 938 if (n->is_Allocate()) { 939 AllocateNode* an = n->as_Allocate(); 940 InitializeNode* init = an->initialization(); 941 ctrl_path.push(init); 942 ctrl_path.push(init->as_Multi()->proj_out(0)); 943 } 944 if (n->is_Call()) { 945 CallNode* cn = n->as_Call(); 946 ctrl_path.push(cn); 947 ctrl_path.push(cn->proj_out(0)); 948 ctrl_path.push(cn->proj_out(0)->unique_out()); 949 Node* catchproj = cn->proj_out(0)->unique_out()->as_Catch()->proj_out_or_null(0); 950 if (catchproj != NULL) { 951 ctrl_path.push(catchproj); 952 } 953 } else { 954 ShouldNotReachHere(); 955 } 956 } 957 958 // Skip backwards through the control checking for unexpected control flow 959 Node* ptr = _end; 960 bool fail = false; 961 while (ptr != _begin) { 962 if (ptr->is_Call() && ctrl_path.member(ptr)) { 963 ptr = ptr->in(0); 964 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) { 965 ptr = ptr->in(0)->in(0)->in(0); 966 assert(ctrl_path.member(ptr), "should be a known piece of control"); 967 } else if (ptr->is_IfTrue()) { 968 IfNode* iff = ptr->in(0)->as_If(); 969 BoolNode* b = iff->in(1)->isa_Bool(); 970 971 if (b == NULL) { 972 #ifndef PRODUCT 973 if (PrintOptimizeStringConcat) { 974 tty->print_cr("unexpected input to IfNode"); 975 iff->in(1)->dump(); 976 tty->cr(); 977 } 978 #endif 979 fail = true; 980 break; 981 } 982 983 Node* cmp = b->in(1); 984 Node* v1 = cmp->in(1); 985 Node* v2 = cmp->in(2); 986 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con); 987 988 // Null check of the return of append which can simply be eliminated 989 if (b->_test._test == BoolTest::ne && 990 v2->bottom_type() == TypePtr::NULL_PTR && 991 v1->is_Proj() && ctrl_path.member(v1->in(0))) { 992 // NULL check of the return value of the append 993 null_check_count++; 994 if (otherproj->outcnt() == 1) { 995 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 996 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 997 ctrl_path.push(call); 998 } 999 } 1000 _control.push(ptr); 1001 ptr = ptr->in(0)->in(0); 1002 continue; 1003 } 1004 1005 // A test which leads to an uncommon trap which should be safe. 1006 // Later this trap will be converted into a trap that restarts 1007 // at the beginning. 1008 if (otherproj->outcnt() == 1) { 1009 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava(); 1010 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) { 1011 // control flow leads to uct so should be ok 1012 _uncommon_traps.push(call); 1013 ctrl_path.push(call); 1014 ptr = ptr->in(0)->in(0); 1015 continue; 1016 } 1017 } 1018 1019 #ifndef PRODUCT 1020 // Some unexpected control flow we don't know how to handle. 1021 if (PrintOptimizeStringConcat) { 1022 tty->print_cr("failing with unknown test"); 1023 b->dump(); 1024 cmp->dump(); 1025 v1->dump(); 1026 v2->dump(); 1027 tty->cr(); 1028 } 1029 #endif 1030 fail = true; 1031 break; 1032 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) { 1033 ptr = ptr->in(0)->in(0); 1034 } else if (ptr->is_Region()) { 1035 Node* copy = ptr->as_Region()->is_copy(); 1036 if (copy != NULL) { 1037 ptr = copy; 1038 continue; 1039 } 1040 if (ptr->req() == 3 && 1041 ptr->in(1) != NULL && ptr->in(1)->is_Proj() && 1042 ptr->in(2) != NULL && ptr->in(2)->is_Proj() && 1043 ptr->in(1)->in(0) == ptr->in(2)->in(0) && 1044 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) { 1045 // Simple diamond. 1046 // XXX should check for possibly merging stores. simple data merges are ok. 1047 // The IGVN will make this simple diamond go away when it 1048 // transforms the Region. Make sure it sees it. 1049 Compile::current()->record_for_igvn(ptr); 1050 _control.push(ptr); 1051 ptr = ptr->in(1)->in(0)->in(0); 1052 continue; 1053 } 1054 #ifndef PRODUCT 1055 if (PrintOptimizeStringConcat) { 1056 tty->print_cr("fusion would fail for region"); 1057 _begin->dump(); 1058 ptr->dump(2); 1059 } 1060 #endif 1061 fail = true; 1062 break; 1063 } else { 1064 // other unknown control 1065 if (!fail) { 1066 #ifndef PRODUCT 1067 if (PrintOptimizeStringConcat) { 1068 tty->print_cr("fusion would fail for"); 1069 _begin->dump(); 1070 } 1071 #endif 1072 fail = true; 1073 } 1074 #ifndef PRODUCT 1075 if (PrintOptimizeStringConcat) { 1076 ptr->dump(); 1077 } 1078 #endif 1079 ptr = ptr->in(0); 1080 } 1081 } 1082 #ifndef PRODUCT 1083 if (PrintOptimizeStringConcat && fail) { 1084 tty->cr(); 1085 } 1086 #endif 1087 if (fail) return !fail; 1088 1089 // Validate that all these results produced are contained within 1090 // this cluster of objects. First collect all the results produced 1091 // by calls in the region. 1092 _stringopts->_visited.clear(); 1093 Node_List worklist; 1094 Node* final_result = _end->proj_out_or_null(TypeFunc::Parms); 1095 for (uint i = 0; i < _control.size(); i++) { 1096 CallNode* cnode = _control.at(i)->isa_Call(); 1097 if (cnode != NULL) { 1098 _stringopts->_visited.test_set(cnode->_idx); 1099 } 1100 Node* result = cnode != NULL ? cnode->proj_out_or_null(TypeFunc::Parms) : NULL; 1101 if (result != NULL && result != final_result) { 1102 worklist.push(result); 1103 } 1104 } 1105 1106 Node* last_result = NULL; 1107 while (worklist.size() > 0) { 1108 Node* result = worklist.pop(); 1109 if (_stringopts->_visited.test_set(result->_idx)) 1110 continue; 1111 for (SimpleDUIterator i(result); i.has_next(); i.next()) { 1112 Node *use = i.get(); 1113 if (ctrl_path.member(use)) { 1114 // already checked this 1115 continue; 1116 } 1117 int opc = use->Opcode(); 1118 if (opc == Op_CmpP || opc == Op_Node) { 1119 ctrl_path.push(use); 1120 continue; 1121 } 1122 if (opc == Op_CastPP || opc == Op_CheckCastPP) { 1123 for (SimpleDUIterator j(use); j.has_next(); j.next()) { 1124 worklist.push(j.get()); 1125 } 1126 worklist.push(use->in(1)); 1127 ctrl_path.push(use); 1128 continue; 1129 } 1130 #ifndef PRODUCT 1131 if (PrintOptimizeStringConcat) { 1132 if (result != last_result) { 1133 last_result = result; 1134 tty->print_cr("extra uses for result:"); 1135 last_result->dump(); 1136 } 1137 use->dump(); 1138 } 1139 #endif 1140 fail = true; 1141 break; 1142 } 1143 } 1144 1145 #ifndef PRODUCT 1146 if (PrintOptimizeStringConcat && !fail) { 1147 ttyLocker ttyl; 1148 tty->cr(); 1149 tty->print("fusion has correct control flow (%d %d) for ", null_check_count, _uncommon_traps.size()); 1150 _begin->jvms()->dump_spec(tty); tty->cr(); 1151 for (int i = 0; i < num_arguments(); i++) { 1152 argument(i)->dump(); 1153 } 1154 _control.dump(); 1155 tty->cr(); 1156 } 1157 #endif 1158 1159 return !fail; 1160 } 1161 1162 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) { 1163 const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror()); 1164 Node* klass_node = __ makecon(mirror_type); 1165 BasicType bt = field->layout_type(); 1166 ciType* field_klass = field->type(); 1167 1168 const Type *type; 1169 if( bt == T_OBJECT ) { 1170 if (!field->type()->is_loaded()) { 1171 type = TypeInstPtr::BOTTOM; 1172 } else if (field->is_static_constant()) { 1173 // This can happen if the constant oop is non-perm. 1174 ciObject* con = field->constant_value().as_object(); 1175 // Do not "join" in the previous type; it doesn't add value, 1176 // and may yield a vacuous result if the field is of interface type. 1177 type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr(); 1178 assert(type != NULL, "field singleton type must be consistent"); 1179 return __ makecon(type); 1180 } else { 1181 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 1182 } 1183 } else { 1184 type = Type::get_const_basic_type(bt); 1185 } 1186 1187 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()), 1188 type, T_OBJECT, 1189 C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())), 1190 MemNode::unordered); 1191 } 1192 1193 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) { 1194 if (arg->is_Con()) { 1195 // Constant integer. Compute constant length using Integer.sizeTable 1196 int arg_val = arg->get_int(); 1197 int count = 1; 1198 if (arg_val < 0) { 1199 // Special case for min_jint - it can't be negated. 1200 if (arg_val == min_jint) { 1201 return __ intcon(11); 1202 } 1203 1204 arg_val = -arg_val; 1205 count++; 1206 } 1207 1208 ciArray* size_table = (ciArray*)size_table_field->constant_value().as_object(); 1209 for (int i = 0; i < size_table->length(); i++) { 1210 if (arg_val <= size_table->element_value(i).as_int()) { 1211 count += i; 1212 break; 1213 } 1214 } 1215 return __ intcon(count); 1216 } 1217 1218 RegionNode *final_merge = new RegionNode(3); 1219 kit.gvn().set_type(final_merge, Type::CONTROL); 1220 Node* final_size = new PhiNode(final_merge, TypeInt::INT); 1221 kit.gvn().set_type(final_size, TypeInt::INT); 1222 1223 IfNode* iff = kit.create_and_map_if(kit.control(), 1224 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 1225 PROB_FAIR, COUNT_UNKNOWN); 1226 Node* is_min = __ IfFalse(iff); 1227 final_merge->init_req(1, is_min); 1228 final_size->init_req(1, __ intcon(11)); 1229 1230 kit.set_control(__ IfTrue(iff)); 1231 if (kit.stopped()) { 1232 final_merge->init_req(2, C->top()); 1233 final_size->init_req(2, C->top()); 1234 } else { 1235 1236 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 1237 RegionNode *r = new RegionNode(3); 1238 kit.gvn().set_type(r, Type::CONTROL); 1239 Node *phi = new PhiNode(r, TypeInt::INT); 1240 kit.gvn().set_type(phi, TypeInt::INT); 1241 Node *size = new PhiNode(r, TypeInt::INT); 1242 kit.gvn().set_type(size, TypeInt::INT); 1243 Node* chk = __ CmpI(arg, __ intcon(0)); 1244 Node* p = __ Bool(chk, BoolTest::lt); 1245 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN); 1246 Node* lessthan = __ IfTrue(iff); 1247 Node* greaterequal = __ IfFalse(iff); 1248 r->init_req(1, lessthan); 1249 phi->init_req(1, __ SubI(__ intcon(0), arg)); 1250 size->init_req(1, __ intcon(1)); 1251 r->init_req(2, greaterequal); 1252 phi->init_req(2, arg); 1253 size->init_req(2, __ intcon(0)); 1254 kit.set_control(r); 1255 C->record_for_igvn(r); 1256 C->record_for_igvn(phi); 1257 C->record_for_igvn(size); 1258 1259 // for (int i=0; ; i++) 1260 // if (x <= sizeTable[i]) 1261 // return i+1; 1262 1263 // Add loop predicate first. 1264 kit.add_empty_predicates(); 1265 C->set_has_loops(true); 1266 1267 RegionNode *loop = new RegionNode(3); 1268 loop->init_req(1, kit.control()); 1269 kit.gvn().set_type(loop, Type::CONTROL); 1270 1271 Node *index = new PhiNode(loop, TypeInt::INT); 1272 index->init_req(1, __ intcon(0)); 1273 kit.gvn().set_type(index, TypeInt::INT); 1274 kit.set_control(loop); 1275 Node* sizeTable = fetch_static_field(kit, size_table_field); 1276 1277 Node* value = kit.load_array_element(sizeTable, index, TypeAryPtr::INTS, /* set_ctrl */ false); 1278 C->record_for_igvn(value); 1279 Node* limit = __ CmpI(phi, value); 1280 Node* limitb = __ Bool(limit, BoolTest::le); 1281 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN); 1282 Node* lessEqual = __ IfTrue(iff2); 1283 Node* greater = __ IfFalse(iff2); 1284 1285 loop->init_req(2, greater); 1286 index->init_req(2, __ AddI(index, __ intcon(1))); 1287 1288 kit.set_control(lessEqual); 1289 C->record_for_igvn(loop); 1290 C->record_for_igvn(index); 1291 1292 final_merge->init_req(2, kit.control()); 1293 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1))); 1294 } 1295 1296 kit.set_control(final_merge); 1297 C->record_for_igvn(final_merge); 1298 C->record_for_igvn(final_size); 1299 1300 return final_size; 1301 } 1302 1303 // Simplified version of Integer.getChars 1304 void PhaseStringOpts::getChars(GraphKit& kit, Node* arg, Node* dst_array, BasicType bt, Node* end, Node* final_merge, Node* final_mem, int merge_index) { 1305 // if (i < 0) { 1306 // sign = '-'; 1307 // i = -i; 1308 // } 1309 IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt), 1310 PROB_FAIR, COUNT_UNKNOWN); 1311 1312 RegionNode* merge = new RegionNode(3); 1313 kit.gvn().set_type(merge, Type::CONTROL); 1314 Node* i = new PhiNode(merge, TypeInt::INT); 1315 kit.gvn().set_type(i, TypeInt::INT); 1316 Node* sign = new PhiNode(merge, TypeInt::INT); 1317 kit.gvn().set_type(sign, TypeInt::INT); 1318 1319 merge->init_req(1, __ IfTrue(iff)); 1320 i->init_req(1, __ SubI(__ intcon(0), arg)); 1321 sign->init_req(1, __ intcon('-')); 1322 merge->init_req(2, __ IfFalse(iff)); 1323 i->init_req(2, arg); 1324 sign->init_req(2, __ intcon(0)); 1325 1326 kit.set_control(merge); 1327 1328 C->record_for_igvn(merge); 1329 C->record_for_igvn(i); 1330 C->record_for_igvn(sign); 1331 1332 // for (;;) { 1333 // q = i / 10; 1334 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 1335 // buf [--charPos] = digits [r]; 1336 // i = q; 1337 // if (i == 0) break; 1338 // } 1339 1340 // Add loop predicate first. 1341 kit.add_empty_predicates(); 1342 1343 C->set_has_loops(true); 1344 RegionNode* head = new RegionNode(3); 1345 head->init_req(1, kit.control()); 1346 1347 kit.gvn().set_type(head, Type::CONTROL); 1348 Node* i_phi = new PhiNode(head, TypeInt::INT); 1349 i_phi->init_req(1, i); 1350 kit.gvn().set_type(i_phi, TypeInt::INT); 1351 Node* charPos = new PhiNode(head, TypeInt::INT); 1352 charPos->init_req(1, end); 1353 kit.gvn().set_type(charPos, TypeInt::INT); 1354 Node* mem = PhiNode::make(head, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES); 1355 kit.gvn().set_type(mem, Type::MEMORY); 1356 1357 kit.set_control(head); 1358 kit.set_memory(mem, byte_adr_idx); 1359 1360 Node* q = __ DivI(kit.null(), i_phi, __ intcon(10)); 1361 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)), 1362 __ LShiftI(q, __ intcon(1)))); 1363 Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2)); 1364 Node* ch = __ AddI(r, __ intcon('0')); 1365 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE), 1366 ch, bt, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */, 1367 false /* unaligned */, (bt != T_BYTE) /* mismatched */); 1368 1369 iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne), 1370 PROB_FAIR, COUNT_UNKNOWN); 1371 Node* ne = __ IfTrue(iff); 1372 Node* eq = __ IfFalse(iff); 1373 1374 head->init_req(2, ne); 1375 mem->init_req(2, st); 1376 1377 i_phi->init_req(2, q); 1378 charPos->init_req(2, index); 1379 charPos = index; 1380 1381 kit.set_control(eq); 1382 kit.set_memory(st, byte_adr_idx); 1383 1384 C->record_for_igvn(head); 1385 C->record_for_igvn(mem); 1386 C->record_for_igvn(i_phi); 1387 C->record_for_igvn(charPos); 1388 1389 // if (sign != 0) { 1390 // buf [--charPos] = sign; 1391 // } 1392 iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne), 1393 PROB_FAIR, COUNT_UNKNOWN); 1394 1395 final_merge->init_req(merge_index + 2, __ IfFalse(iff)); 1396 final_mem->init_req(merge_index + 2, kit.memory(byte_adr_idx)); 1397 1398 kit.set_control(__ IfTrue(iff)); 1399 if (kit.stopped()) { 1400 final_merge->init_req(merge_index + 1, C->top()); 1401 final_mem->init_req(merge_index + 1, C->top()); 1402 } else { 1403 Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2)); 1404 st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE), 1405 sign, bt, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */, 1406 false /* unaligned */, (bt != T_BYTE) /* mismatched */); 1407 1408 final_merge->init_req(merge_index + 1, kit.control()); 1409 final_mem->init_req(merge_index + 1, st); 1410 } 1411 } 1412 1413 // Copy the characters representing arg into dst_array starting at start 1414 Node* PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* dst_array, Node* dst_coder, Node* start, Node* size) { 1415 bool dcon = dst_coder->is_Con(); 1416 bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false; 1417 Node* end = __ AddI(start, __ LShiftI(size, dst_coder)); 1418 1419 // The final_merge node has 4 entries in case the encoding is known: 1420 // (0) Control, (1) result w/ sign, (2) result w/o sign, (3) result for Integer.min_value 1421 // or 6 entries in case the encoding is not known: 1422 // (0) Control, (1) Latin1 w/ sign, (2) Latin1 w/o sign, (3) min_value, (4) UTF16 w/ sign, (5) UTF16 w/o sign 1423 RegionNode* final_merge = new RegionNode(dcon ? 4 : 6); 1424 kit.gvn().set_type(final_merge, Type::CONTROL); 1425 1426 Node* final_mem = PhiNode::make(final_merge, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES); 1427 kit.gvn().set_type(final_mem, Type::MEMORY); 1428 1429 // need to handle arg == Integer.MIN_VALUE specially because negating doesn't make it positive 1430 IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne), 1431 PROB_FAIR, COUNT_UNKNOWN); 1432 1433 Node* old_mem = kit.memory(byte_adr_idx); 1434 1435 kit.set_control(__ IfFalse(iff)); 1436 if (kit.stopped()) { 1437 // Statically not equal to MIN_VALUE so this path is dead 1438 final_merge->init_req(3, kit.control()); 1439 } else { 1440 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())), 1441 dst_array, dst_coder, start); 1442 final_merge->init_req(3, kit.control()); 1443 final_mem->init_req(3, kit.memory(byte_adr_idx)); 1444 } 1445 1446 kit.set_control(__ IfTrue(iff)); 1447 kit.set_memory(old_mem, byte_adr_idx); 1448 1449 if (!dcon) { 1450 // Check encoding of destination 1451 iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(dst_coder, __ intcon(0)), BoolTest::eq), 1452 PROB_FAIR, COUNT_UNKNOWN); 1453 old_mem = kit.memory(byte_adr_idx); 1454 } 1455 if (!dcon || dbyte) { 1456 // Destination is Latin1, 1457 if (!dcon) { 1458 kit.set_control(__ IfTrue(iff)); 1459 } 1460 getChars(kit, arg, dst_array, T_BYTE, end, final_merge, final_mem); 1461 } 1462 if (!dcon || !dbyte) { 1463 // Destination is UTF16 1464 int merge_index = 0; 1465 if (!dcon) { 1466 kit.set_control(__ IfFalse(iff)); 1467 kit.set_memory(old_mem, byte_adr_idx); 1468 merge_index = 3; // Account for Latin1 case 1469 } 1470 getChars(kit, arg, dst_array, T_CHAR, end, final_merge, final_mem, merge_index); 1471 } 1472 1473 // Final merge point for Latin1 and UTF16 case 1474 kit.set_control(final_merge); 1475 kit.set_memory(final_mem, byte_adr_idx); 1476 1477 C->record_for_igvn(final_merge); 1478 C->record_for_igvn(final_mem); 1479 return end; 1480 } 1481 1482 // Copy 'count' bytes/chars from src_array to dst_array starting at index start 1483 void PhaseStringOpts::arraycopy(GraphKit& kit, IdealKit& ideal, Node* src_array, Node* dst_array, BasicType elembt, Node* start, Node* count) { 1484 assert(elembt == T_BYTE || elembt == T_CHAR, "Invalid type for arraycopy"); 1485 1486 if (elembt == T_CHAR) { 1487 // Get number of chars 1488 count = __ RShiftI(count, __ intcon(1)); 1489 } 1490 1491 Node* extra = NULL; 1492 #ifdef _LP64 1493 count = __ ConvI2L(count); 1494 extra = C->top(); 1495 #endif 1496 1497 Node* src_ptr = __ array_element_address(src_array, __ intcon(0), T_BYTE); 1498 Node* dst_ptr = __ array_element_address(dst_array, start, T_BYTE); 1499 // Check if destination address is aligned to HeapWordSize 1500 const TypeInt* tdst = __ gvn().type(start)->is_int(); 1501 bool aligned = tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0); 1502 // Figure out which arraycopy runtime method to call (disjoint, uninitialized). 1503 const char* copyfunc_name = "arraycopy"; 1504 address copyfunc_addr = StubRoutines::select_arraycopy_function(elembt, aligned, true, copyfunc_name, true); 1505 ideal.make_leaf_call_no_fp(OptoRuntime::fast_arraycopy_Type(), copyfunc_addr, copyfunc_name, 1506 TypeAryPtr::BYTES, src_ptr, dst_ptr, count, extra); 1507 } 1508 1509 #undef __ 1510 #define __ ideal. 1511 1512 // Copy contents of a Latin1 encoded string from src_array to dst_array 1513 void PhaseStringOpts::copy_latin1_string(GraphKit& kit, IdealKit& ideal, Node* src_array, IdealVariable& count, 1514 Node* dst_array, Node* dst_coder, Node* start) { 1515 bool dcon = dst_coder->is_Con(); 1516 bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false; 1517 1518 if (!dcon) { 1519 __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); 1520 } 1521 if (!dcon || dbyte) { 1522 // Destination is Latin1. Simply emit a byte arraycopy. 1523 arraycopy(kit, ideal, src_array, dst_array, T_BYTE, start, __ value(count)); 1524 } 1525 if (!dcon) { 1526 __ else_(); 1527 } 1528 if (!dcon || !dbyte) { 1529 // Destination is UTF16. Inflate src_array into dst_array. 1530 kit.sync_kit(ideal); 1531 if (Matcher::match_rule_supported(Op_StrInflatedCopy)) { 1532 // Use fast intrinsic 1533 Node* src = kit.array_element_address(src_array, kit.intcon(0), T_BYTE); 1534 Node* dst = kit.array_element_address(dst_array, start, T_BYTE); 1535 kit.inflate_string(src, dst, TypeAryPtr::BYTES, __ value(count)); 1536 } else { 1537 // No intrinsic available, use slow method 1538 kit.inflate_string_slow(src_array, dst_array, start, __ value(count)); 1539 } 1540 ideal.sync_kit(&kit); 1541 // Multiply count by two since we now need two bytes per char 1542 __ set(count, __ LShiftI(__ value(count), __ ConI(1))); 1543 } 1544 if (!dcon) { 1545 __ end_if(); 1546 } 1547 } 1548 1549 // Read two bytes from index and index+1 and convert them to a char 1550 static jchar readChar(ciTypeArray* array, int index) { 1551 int shift_high, shift_low; 1552 #ifdef VM_LITTLE_ENDIAN 1553 shift_high = 0; 1554 shift_low = 8; 1555 #else 1556 shift_high = 8; 1557 shift_low = 0; 1558 #endif 1559 1560 jchar b1 = ((jchar) array->byte_at(index)) & 0xff; 1561 jchar b2 = ((jchar) array->byte_at(index+1)) & 0xff; 1562 return (b1 << shift_high) | (b2 << shift_low); 1563 } 1564 1565 // Copy contents of constant src_array to dst_array by emitting individual stores 1566 void PhaseStringOpts::copy_constant_string(GraphKit& kit, IdealKit& ideal, ciTypeArray* src_array, IdealVariable& count, 1567 bool src_is_byte, Node* dst_array, Node* dst_coder, Node* start) { 1568 bool dcon = dst_coder->is_Con(); 1569 bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false; 1570 int length = src_array->length(); 1571 1572 if (!dcon) { 1573 __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); 1574 } 1575 if (!dcon || dbyte) { 1576 // Destination is Latin1. Copy each byte of src_array into dst_array. 1577 Node* index = start; 1578 for (int i = 0; i < length; i++) { 1579 Node* adr = kit.array_element_address(dst_array, index, T_BYTE); 1580 Node* val = __ ConI(src_array->byte_at(i)); 1581 __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered); 1582 index = __ AddI(index, __ ConI(1)); 1583 } 1584 } 1585 if (!dcon) { 1586 __ else_(); 1587 } 1588 if (!dcon || !dbyte) { 1589 // Destination is UTF16. Copy each char of src_array into dst_array. 1590 Node* index = start; 1591 for (int i = 0; i < length; i++) { 1592 Node* adr = kit.array_element_address(dst_array, index, T_BYTE); 1593 jchar val; 1594 if (src_is_byte) { 1595 val = src_array->byte_at(i) & 0xff; 1596 } else { 1597 val = readChar(src_array, i++); 1598 } 1599 __ store(__ ctrl(), adr, __ ConI(val), T_CHAR, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */, 1600 true /* mismatched */); 1601 index = __ AddI(index, __ ConI(2)); 1602 } 1603 if (src_is_byte) { 1604 // Multiply count by two since we now need two bytes per char 1605 __ set(count, __ ConI(2 * length)); 1606 } 1607 } 1608 if (!dcon) { 1609 __ end_if(); 1610 } 1611 } 1612 1613 // Compress copy contents of the byte/char String str into dst_array starting at index start. 1614 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* dst_array, Node* dst_coder, Node* start) { 1615 Node* src_array = kit.load_String_value(str, true); 1616 1617 IdealKit ideal(&kit, true, true); 1618 IdealVariable count(ideal); __ declarations_done(); 1619 1620 if (str->is_Con()) { 1621 // Constant source string 1622 ciTypeArray* src_array_type = get_constant_value(kit, str); 1623 1624 // Check encoding of constant string 1625 bool src_is_byte = (get_constant_coder(kit, str) == java_lang_String::CODER_LATIN1); 1626 1627 // For small constant strings just emit individual stores. 1628 // A length of 6 seems like a good space/speed tradeof. 1629 __ set(count, __ ConI(src_array_type->length())); 1630 int src_len = src_array_type->length() / (src_is_byte ? 1 : 2); 1631 if (src_len < unroll_string_copy_length) { 1632 // Small constant string 1633 copy_constant_string(kit, ideal, src_array_type, count, src_is_byte, dst_array, dst_coder, start); 1634 } else if (src_is_byte) { 1635 // Source is Latin1 1636 copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start); 1637 } else { 1638 // Source is UTF16 (destination too). Simply emit a char arraycopy. 1639 arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count)); 1640 } 1641 } else { 1642 Node* size = kit.load_array_length(src_array); 1643 __ set(count, size); 1644 // Non-constant source string 1645 if (CompactStrings) { 1646 // Emit runtime check for coder 1647 Node* coder = kit.load_String_coder(str, true); 1648 __ if_then(coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); { 1649 // Source is Latin1 1650 copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start); 1651 } __ else_(); 1652 } 1653 // Source is UTF16 (destination too). Simply emit a char arraycopy. 1654 arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count)); 1655 1656 if (CompactStrings) { 1657 __ end_if(); 1658 } 1659 } 1660 1661 // Finally sync IdealKit and GraphKit. 1662 kit.sync_kit(ideal); 1663 return __ AddI(start, __ value(count)); 1664 } 1665 1666 // Compress copy the char into dst_array at index start. 1667 Node* PhaseStringOpts::copy_char(GraphKit& kit, Node* val, Node* dst_array, Node* dst_coder, Node* start) { 1668 bool dcon = (dst_coder != NULL) && dst_coder->is_Con(); 1669 bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false; 1670 1671 IdealKit ideal(&kit, true, true); 1672 IdealVariable end(ideal); __ declarations_done(); 1673 Node* adr = kit.array_element_address(dst_array, start, T_BYTE); 1674 if (!dcon){ 1675 __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); 1676 } 1677 if (!dcon || dbyte) { 1678 // Destination is Latin1. Store a byte. 1679 __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered); 1680 __ set(end, __ AddI(start, __ ConI(1))); 1681 } 1682 if (!dcon) { 1683 __ else_(); 1684 } 1685 if (!dcon || !dbyte) { 1686 // Destination is UTF16. Store a char. 1687 __ store(__ ctrl(), adr, val, T_CHAR, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */, 1688 true /* mismatched */); 1689 __ set(end, __ AddI(start, __ ConI(2))); 1690 } 1691 if (!dcon) { 1692 __ end_if(); 1693 } 1694 // Finally sync IdealKit and GraphKit. 1695 kit.sync_kit(ideal); 1696 return __ value(end); 1697 } 1698 1699 #undef __ 1700 #define __ kit. 1701 1702 // Allocate a byte array of specified length. 1703 Node* PhaseStringOpts::allocate_byte_array(GraphKit& kit, IdealKit* ideal, Node* length) { 1704 if (ideal != NULL) { 1705 // Sync IdealKit and graphKit. 1706 kit.sync_kit(*ideal); 1707 } 1708 Node* byte_array = NULL; 1709 { 1710 PreserveReexecuteState preexecs(&kit); 1711 // The original jvms is for an allocation of either a String or 1712 // StringBuffer so no stack adjustment is necessary for proper 1713 // reexecution. If we deoptimize in the slow path the bytecode 1714 // will be reexecuted and the char[] allocation will be thrown away. 1715 kit.jvms()->set_should_reexecute(true); 1716 byte_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_BYTE))), 1717 length, 1); 1718 } 1719 1720 // Mark the allocation so that zeroing is skipped since the code 1721 // below will overwrite the entire array 1722 AllocateArrayNode* byte_alloc = AllocateArrayNode::Ideal_array_allocation(byte_array, _gvn); 1723 byte_alloc->maybe_set_complete(_gvn); 1724 1725 if (ideal != NULL) { 1726 // Sync IdealKit and graphKit. 1727 ideal->sync_kit(&kit); 1728 } 1729 return byte_array; 1730 } 1731 1732 jbyte PhaseStringOpts::get_constant_coder(GraphKit& kit, Node* str) { 1733 assert(str->is_Con(), "String must be constant"); 1734 const TypeOopPtr* str_type = kit.gvn().type(str)->isa_oopptr(); 1735 ciInstance* str_instance = str_type->const_oop()->as_instance(); 1736 jbyte coder = str_instance->field_value_by_offset(java_lang_String::coder_offset()).as_byte(); 1737 assert(CompactStrings || (coder == java_lang_String::CODER_UTF16), "Strings must be UTF16 encoded"); 1738 return coder; 1739 } 1740 1741 int PhaseStringOpts::get_constant_length(GraphKit& kit, Node* str) { 1742 assert(str->is_Con(), "String must be constant"); 1743 return get_constant_value(kit, str)->length(); 1744 } 1745 1746 ciTypeArray* PhaseStringOpts::get_constant_value(GraphKit& kit, Node* str) { 1747 assert(str->is_Con(), "String must be constant"); 1748 const TypeOopPtr* str_type = kit.gvn().type(str)->isa_oopptr(); 1749 ciInstance* str_instance = str_type->const_oop()->as_instance(); 1750 ciObject* src_array = str_instance->field_value_by_offset(java_lang_String::value_offset()).as_object(); 1751 return src_array->as_type_array(); 1752 } 1753 1754 void PhaseStringOpts::replace_string_concat(StringConcat* sc) { 1755 // Log a little info about the transformation 1756 sc->maybe_log_transform(); 1757 1758 // pull the JVMState of the allocation into a SafePointNode to serve as 1759 // as a shim for the insertion of the new code. 1760 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C); 1761 uint size = sc->begin()->req(); 1762 SafePointNode* map = new SafePointNode(size, jvms); 1763 1764 // copy the control and memory state from the final call into our 1765 // new starting state. This allows any preceeding tests to feed 1766 // into the new section of code. 1767 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) { 1768 map->init_req(i1, sc->end()->in(i1)); 1769 } 1770 // blow away old allocation arguments 1771 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) { 1772 map->init_req(i1, C->top()); 1773 } 1774 // Copy the rest of the inputs for the JVMState 1775 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) { 1776 map->init_req(i1, sc->begin()->in(i1)); 1777 } 1778 // Make sure the memory state is a MergeMem for parsing. 1779 if (!map->in(TypeFunc::Memory)->is_MergeMem()) { 1780 map->set_req(TypeFunc::Memory, MergeMemNode::make(map->in(TypeFunc::Memory))); 1781 } 1782 1783 jvms->set_map(map); 1784 map->ensure_stack(jvms, jvms->method()->max_stack()); 1785 1786 // disconnect all the old StringBuilder calls from the graph 1787 sc->eliminate_unneeded_control(); 1788 1789 // At this point all the old work has been completely removed from 1790 // the graph and the saved JVMState exists at the point where the 1791 // final toString call used to be. 1792 GraphKit kit(jvms); 1793 1794 // There may be uncommon traps which are still using the 1795 // intermediate states and these need to be rewritten to point at 1796 // the JVMState at the beginning of the transformation. 1797 sc->convert_uncommon_traps(kit, jvms); 1798 1799 // Now insert the logic to compute the size of the string followed 1800 // by all the logic to construct array and resulting string. 1801 1802 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string())); 1803 1804 // Create a region for the overflow checks to merge into. 1805 int args = MAX2(sc->num_arguments(), 1); 1806 RegionNode* overflow = new RegionNode(args); 1807 kit.gvn().set_type(overflow, Type::CONTROL); 1808 1809 // Create a hook node to hold onto the individual sizes since they 1810 // are need for the copying phase. 1811 Node* string_sizes = new Node(args); 1812 1813 Node* coder = __ intcon(0); 1814 Node* length = __ intcon(0); 1815 // If at least one argument is UTF16 encoded, we can fix the encoding. 1816 bool coder_fixed = false; 1817 1818 if (!CompactStrings) { 1819 // Fix encoding of result string to UTF16 1820 coder_fixed = true; 1821 coder = __ intcon(java_lang_String::CODER_UTF16); 1822 } 1823 1824 for (int argi = 0; argi < sc->num_arguments(); argi++) { 1825 Node* arg = sc->argument(argi); 1826 switch (sc->mode(argi)) { 1827 case StringConcat::NegativeIntCheckMode: { 1828 // Initial capacity argument might be negative in which case StringBuilder(int) throws 1829 // a NegativeArraySizeException. Insert a runtime check with an uncommon trap. 1830 const TypeInt* type = kit.gvn().type(arg)->is_int(); 1831 assert(type->_hi >= 0 && type->_lo < 0, "no runtime int check needed"); 1832 Node* p = __ Bool(__ CmpI(arg, kit.intcon(0)), BoolTest::ge); 1833 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1834 { 1835 // Negative int -> uncommon trap. 1836 PreserveJVMState pjvms(&kit); 1837 kit.set_control(__ IfFalse(iff)); 1838 kit.uncommon_trap(Deoptimization::Reason_intrinsic, 1839 Deoptimization::Action_maybe_recompile); 1840 } 1841 kit.set_control(__ IfTrue(iff)); 1842 break; 1843 } 1844 case StringConcat::IntMode: { 1845 Node* string_size = int_stringSize(kit, arg); 1846 1847 // accumulate total 1848 length = __ AddI(length, string_size); 1849 1850 // Cache this value for the use by int_toString 1851 string_sizes->init_req(argi, string_size); 1852 break; 1853 } 1854 case StringConcat::StringNullCheckMode: { 1855 const Type* type = kit.gvn().type(arg); 1856 assert(type != TypePtr::NULL_PTR, "missing check"); 1857 if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1858 // Null check with uncommon trap since 1859 // StringBuilder(null) throws exception. 1860 // Use special uncommon trap instead of 1861 // calling normal do_null_check(). 1862 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1863 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1864 overflow->add_req(__ IfFalse(iff)); 1865 Node* notnull = __ IfTrue(iff); 1866 kit.set_control(notnull); // set control for the cast_not_null 1867 arg = kit.cast_not_null(arg, false); 1868 sc->set_argument(argi, arg); 1869 } 1870 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity"); 1871 // Fallthrough to add string length. 1872 } 1873 case StringConcat::StringMode: { 1874 const Type* type = kit.gvn().type(arg); 1875 Node* count = NULL; 1876 Node* arg_coder = NULL; 1877 if (type == TypePtr::NULL_PTR) { 1878 // replace the argument with the null checked version 1879 arg = null_string; 1880 sc->set_argument(argi, arg); 1881 count = kit.load_String_length(arg, true); 1882 arg_coder = kit.load_String_coder(arg, true); 1883 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) { 1884 // s = s != null ? s : "null"; 1885 // length = length + (s.count - s.offset); 1886 RegionNode *r = new RegionNode(3); 1887 kit.gvn().set_type(r, Type::CONTROL); 1888 Node *phi = new PhiNode(r, type); 1889 kit.gvn().set_type(phi, phi->bottom_type()); 1890 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne); 1891 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN); 1892 Node* notnull = __ IfTrue(iff); 1893 Node* isnull = __ IfFalse(iff); 1894 kit.set_control(notnull); // set control for the cast_not_null 1895 r->init_req(1, notnull); 1896 phi->init_req(1, kit.cast_not_null(arg, false)); 1897 r->init_req(2, isnull); 1898 phi->init_req(2, null_string); 1899 kit.set_control(r); 1900 C->record_for_igvn(r); 1901 C->record_for_igvn(phi); 1902 // replace the argument with the null checked version 1903 arg = phi; 1904 sc->set_argument(argi, arg); 1905 count = kit.load_String_length(arg, true); 1906 arg_coder = kit.load_String_coder(arg, true); 1907 } else { 1908 // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP 1909 // kit.control might be a different test, that can be hoisted above the actual nullcheck 1910 // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck. 1911 count = kit.load_String_length(arg, false); 1912 arg_coder = kit.load_String_coder(arg, false); 1913 } 1914 if (arg->is_Con()) { 1915 // Constant string. Get constant coder and length. 1916 jbyte const_coder = get_constant_coder(kit, arg); 1917 int const_length = get_constant_length(kit, arg); 1918 if (const_coder == java_lang_String::CODER_LATIN1) { 1919 // Can be latin1 encoded 1920 arg_coder = __ intcon(const_coder); 1921 count = __ intcon(const_length); 1922 } else { 1923 // Found UTF16 encoded string. Fix result array encoding to UTF16. 1924 coder_fixed = true; 1925 coder = __ intcon(const_coder); 1926 count = __ intcon(const_length / 2); 1927 } 1928 } 1929 1930 if (!coder_fixed) { 1931 coder = __ OrI(coder, arg_coder); 1932 } 1933 length = __ AddI(length, count); 1934 string_sizes->init_req(argi, NULL); 1935 break; 1936 } 1937 case StringConcat::CharMode: { 1938 // one character only 1939 const TypeInt* t = kit.gvn().type(arg)->is_int(); 1940 if (!coder_fixed && t->is_con()) { 1941 // Constant char 1942 if (t->get_con() <= 255) { 1943 // Can be latin1 encoded 1944 coder = __ OrI(coder, __ intcon(java_lang_String::CODER_LATIN1)); 1945 } else { 1946 // Must be UTF16 encoded. Fix result array encoding to UTF16. 1947 coder_fixed = true; 1948 coder = __ intcon(java_lang_String::CODER_UTF16); 1949 } 1950 } else if (!coder_fixed) { 1951 // Not constant 1952 #undef __ 1953 #define __ ideal. 1954 IdealKit ideal(&kit, true, true); 1955 IdealVariable char_coder(ideal); __ declarations_done(); 1956 // Check if character can be latin1 encoded 1957 __ if_then(arg, BoolTest::le, __ ConI(0xFF)); 1958 __ set(char_coder, __ ConI(java_lang_String::CODER_LATIN1)); 1959 __ else_(); 1960 __ set(char_coder, __ ConI(java_lang_String::CODER_UTF16)); 1961 __ end_if(); 1962 kit.sync_kit(ideal); 1963 coder = __ OrI(coder, __ value(char_coder)); 1964 #undef __ 1965 #define __ kit. 1966 } 1967 length = __ AddI(length, __ intcon(1)); 1968 break; 1969 } 1970 default: 1971 ShouldNotReachHere(); 1972 } 1973 if (argi > 0) { 1974 // Check that the sum hasn't overflowed 1975 IfNode* iff = kit.create_and_map_if(kit.control(), 1976 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt), 1977 PROB_MIN, COUNT_UNKNOWN); 1978 kit.set_control(__ IfFalse(iff)); 1979 overflow->set_req(argi, __ IfTrue(iff)); 1980 } 1981 } 1982 1983 { 1984 // Hook 1985 PreserveJVMState pjvms(&kit); 1986 kit.set_control(overflow); 1987 C->record_for_igvn(overflow); 1988 kit.uncommon_trap(Deoptimization::Reason_intrinsic, 1989 Deoptimization::Action_make_not_entrant); 1990 } 1991 1992 Node* result; 1993 if (!kit.stopped()) { 1994 assert(CompactStrings || (coder->is_Con() && coder->get_int() == java_lang_String::CODER_UTF16), 1995 "Result string must be UTF16 encoded if CompactStrings is disabled"); 1996 1997 Node* dst_array = NULL; 1998 if (sc->num_arguments() == 1 && 1999 (sc->mode(0) == StringConcat::StringMode || 2000 sc->mode(0) == StringConcat::StringNullCheckMode)) { 2001 // Handle the case when there is only a single String argument. 2002 // In this case, we can just pull the value from the String itself. 2003 dst_array = kit.load_String_value(sc->argument(0), true); 2004 } else { 2005 // Allocate destination byte array according to coder 2006 dst_array = allocate_byte_array(kit, NULL, __ LShiftI(length, coder)); 2007 2008 // Now copy the string representations into the final byte[] 2009 Node* start = __ intcon(0); 2010 for (int argi = 0; argi < sc->num_arguments(); argi++) { 2011 Node* arg = sc->argument(argi); 2012 switch (sc->mode(argi)) { 2013 case StringConcat::NegativeIntCheckMode: 2014 break; // Nothing to do, was only needed to add a runtime check earlier. 2015 case StringConcat::IntMode: { 2016 start = int_getChars(kit, arg, dst_array, coder, start, string_sizes->in(argi)); 2017 break; 2018 } 2019 case StringConcat::StringNullCheckMode: 2020 case StringConcat::StringMode: { 2021 start = copy_string(kit, arg, dst_array, coder, start); 2022 break; 2023 } 2024 case StringConcat::CharMode: { 2025 start = copy_char(kit, arg, dst_array, coder, start); 2026 break; 2027 } 2028 default: 2029 ShouldNotReachHere(); 2030 } 2031 } 2032 } 2033 2034 // If we're not reusing an existing String allocation then allocate one here. 2035 result = sc->string_alloc(); 2036 if (result == NULL) { 2037 PreserveReexecuteState preexecs(&kit); 2038 // The original jvms is for an allocation of either a String or 2039 // StringBuffer so no stack adjustment is necessary for proper 2040 // reexecution. 2041 kit.jvms()->set_should_reexecute(true); 2042 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass()))); 2043 } 2044 2045 // Initialize the string 2046 kit.store_String_value(result, dst_array); 2047 kit.store_String_coder(result, coder); 2048 2049 // The value field is final. Emit a barrier here to ensure that the effect 2050 // of the initialization is committed to memory before any code publishes 2051 // a reference to the newly constructed object (see Parse::do_exits()). 2052 assert(AllocateNode::Ideal_allocation(result, _gvn) != NULL, "should be newly allocated"); 2053 kit.insert_mem_bar(Op_MemBarRelease, result); 2054 } else { 2055 result = C->top(); 2056 } 2057 // hook up the outgoing control and result 2058 kit.replace_call(sc->end(), result); 2059 2060 // Unhook any hook nodes 2061 string_sizes->disconnect_inputs(C); 2062 sc->cleanup(); 2063 }