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