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