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