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 = call->extract_projections(false);
 345   if (projs->fallthrough_catchproj != nullptr) {
 346     C->gvn_replace_by(projs->fallthrough_catchproj, call->in(TypeFunc::Control));
 347   }
 348   if (projs->fallthrough_memproj != nullptr) {
 349     C->gvn_replace_by(projs->fallthrough_memproj, call->in(TypeFunc::Memory));
 350   }
 351   if (projs->catchall_memproj != nullptr) {
 352     C->gvn_replace_by(projs->catchall_memproj, C->top());
 353   }
 354   if (projs->fallthrough_ioproj != nullptr) {
 355     C->gvn_replace_by(projs->fallthrough_ioproj, call->in(TypeFunc::I_O));
 356   }
 357   if (projs->catchall_ioproj != nullptr) {
 358     C->gvn_replace_by(projs->catchall_ioproj, C->top());
 359   }
 360   if (projs->catchall_catchproj != nullptr) {
 361     // EA can't cope with the partially collapsed graph this
 362     // creates so put it on the worklist to be collapsed later.
 363     for (SimpleDUIterator i(projs->catchall_catchproj); i.has_next(); i.next()) {
 364       Node *use = i.get();
 365       int opc = use->Opcode();
 366       if (opc == Op_CreateEx || opc == Op_Region) {
 367         _stringopts->record_dead_node(use);
 368       }
 369     }
 370     C->gvn_replace_by(projs->catchall_catchproj, C->top());
 371   }
 372   if (projs->resproj[0] != nullptr) {
 373     assert(projs->nb_resproj == 1, "unexpected number of results");
 374     C->gvn_replace_by(projs->resproj[0], 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         if (opc == Op_CheckCastPP) {
1160           worklist.push(use);
1161         }
1162         for (SimpleDUIterator j(use); j.has_next(); j.next()) {
1163           worklist.push(j.get());
1164         }
1165         worklist.push(use->in(1));
1166         ctrl_path.push(use);
1167         continue;
1168       }
1169 #ifndef PRODUCT
1170       if (PrintOptimizeStringConcat) {
1171         if (result != last_result) {
1172           last_result = result;
1173           tty->print_cr("extra uses for result:");
1174           last_result->dump();
1175         }
1176         use->dump();
1177       }
1178 #endif
1179       fail = true;
1180       break;
1181     }
1182   }
1183 
1184 #ifndef PRODUCT
1185   if (PrintOptimizeStringConcat && !fail) {
1186     ttyLocker ttyl;
1187     tty->cr();
1188     tty->print("fusion has correct control flow (%d %d) for ", null_check_count, _uncommon_traps.size());
1189     _begin->jvms()->dump_spec(tty); tty->cr();
1190     for (int i = 0; i < num_arguments(); i++) {
1191       argument(i)->dump();
1192     }
1193     _control.dump();
1194     tty->cr();
1195   }
1196 #endif
1197 
1198   return !fail;
1199 }
1200 
1201 // Mirror of Integer.stringSize() method, return the count of digits in integer,
1202 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
1203   if (arg->is_Con()) {
1204     // Constant integer. Compute constant length
1205     jint arg_val = arg->get_int();
1206     jint d = 1;
1207     if (arg_val >= 0) {
1208       d = 0;
1209       arg_val = -arg_val;
1210     }
1211     jint p = -10;
1212     for (int i = 1; i < 10; i++) {
1213       if (arg_val > p) {
1214         return __ intcon(i + d);
1215       }
1216       p = java_multiply(10, p);
1217     }
1218     return __ intcon(10 + d);
1219   }
1220 
1221   // int d = 1;
1222   // if (x >= 0) {
1223   //     d = 0;
1224   //     x = -x;
1225   // }
1226   RegionNode* sign_merge = new RegionNode(3);
1227   kit.gvn().set_type(sign_merge, Type::CONTROL);
1228   Node* digit_cnt = new PhiNode(sign_merge, TypeInt::INT);
1229   kit.gvn().set_type(digit_cnt, TypeInt::INT);
1230   Node* val = new PhiNode(sign_merge, TypeInt::INT);
1231   kit.gvn().set_type(val, TypeInt::INT);
1232 
1233   IfNode* iff = kit.create_and_map_if(kit.control(),
1234                                       __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::ge),
1235                                       PROB_FAIR, COUNT_UNKNOWN);
1236   sign_merge->init_req(1, __ IfTrue(iff));
1237   sign_merge->init_req(2, __ IfFalse(iff));
1238   digit_cnt->init_req(1, __ intcon(0));
1239   digit_cnt->init_req(2, __ intcon(1));
1240   val->init_req(1, __ SubI(__ intcon(0), arg));
1241   val->init_req(2, arg);
1242   kit.set_control(sign_merge);
1243 
1244   // int p = -10;
1245   // for (int i = 1; i < 10; i++) {
1246   //     if (x > p)
1247   //         return i + d;
1248   //     p = 10 * p;
1249   // }
1250   RegionNode* final_merge = new RegionNode(3);
1251   kit.gvn().set_type(final_merge, Type::CONTROL);
1252   Node* final_size = new PhiNode(final_merge, TypeInt::INT);
1253   kit.gvn().set_type(final_size, TypeInt::INT);
1254 
1255   kit.add_parse_predicates();
1256   C->set_has_loops(true);
1257 
1258   RegionNode* loop = new RegionNode(3);
1259   kit.gvn().set_type(loop, Type::CONTROL);
1260   Node* index = new PhiNode(loop, TypeInt::INT);
1261   kit.gvn().set_type(index, TypeInt::INT);
1262   Node* temp = new PhiNode(loop, TypeInt::INT);
1263   kit.gvn().set_type(temp, TypeInt::INT);
1264 
1265   loop->init_req(1, kit.control());
1266   index->init_req(1, __ intcon(1));
1267   temp->init_req(1, __ intcon(-10));
1268   kit.set_control(loop);
1269 
1270   Node* limit = __ CmpI(index, __ intcon(10));
1271   Node* limitb = __ Bool(limit, BoolTest::lt);
1272   IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN);
1273   Node* limit_less = __ IfTrue(iff2);
1274   kit.set_control(limit_less);
1275 
1276   Node* cmp = __ CmpI(val, temp);
1277   Node* cmpb = __ Bool(cmp, BoolTest::gt);
1278   IfNode* iff3 = kit.create_and_map_if(kit.control(), cmpb, PROB_MIN, COUNT_UNKNOWN);
1279   Node* cmp_le = __ IfFalse(iff3);
1280   kit.set_control(cmp_le);
1281 
1282   loop->init_req(2, kit.control());
1283   index->init_req(2, __ AddI(index, __ intcon(1)));
1284   temp->init_req(2, __ MulI(temp, __ intcon(10)));
1285 
1286   final_merge->init_req(1, __ IfFalse(iff2));
1287   final_merge->init_req(2, __ IfTrue(iff3));
1288   final_size->init_req(1, __ AddI(digit_cnt, __ intcon(10)));
1289   final_size->init_req(2, __ AddI(digit_cnt, index));
1290   kit.set_control(final_merge);
1291 
1292   C->record_for_igvn(sign_merge);
1293   C->record_for_igvn(digit_cnt);
1294   C->record_for_igvn(val);
1295   C->record_for_igvn(final_merge);
1296   C->record_for_igvn(final_size);
1297   C->record_for_igvn(loop);
1298   C->record_for_igvn(index);
1299   C->record_for_igvn(temp);
1300   return final_size;
1301 }
1302 
1303 // Simplified version of Integer.getChars
1304 void PhaseStringOpts::getChars(GraphKit& kit, Node* arg, Node* dst_array, BasicType bt, Node* end, Node* final_merge, Node* final_mem, int merge_index) {
1305   // if (i < 0) {
1306   //     sign = '-';
1307   //     i = -i;
1308   // }
1309   IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
1310                                       PROB_FAIR, COUNT_UNKNOWN);
1311 
1312   RegionNode* merge = new RegionNode(3);
1313   kit.gvn().set_type(merge, Type::CONTROL);
1314   Node* i = new PhiNode(merge, TypeInt::INT);
1315   kit.gvn().set_type(i, TypeInt::INT);
1316   Node* sign = new PhiNode(merge, TypeInt::INT);
1317   kit.gvn().set_type(sign, TypeInt::INT);
1318 
1319   merge->init_req(1, __ IfTrue(iff));
1320   i->init_req(1, __ SubI(__ intcon(0), arg));
1321   sign->init_req(1, __ intcon('-'));
1322   merge->init_req(2, __ IfFalse(iff));
1323   i->init_req(2, arg);
1324   sign->init_req(2, __ intcon(0));
1325 
1326   kit.set_control(merge);
1327 
1328   C->record_for_igvn(merge);
1329   C->record_for_igvn(i);
1330   C->record_for_igvn(sign);
1331 
1332   // for (;;) {
1333   //     q = i / 10;
1334   //     r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
1335   //     buf [--charPos] = digits [r];
1336   //     i = q;
1337   //     if (i == 0) break;
1338   // }
1339 
1340   // Add Parse Predicates first.
1341   kit.add_parse_predicates();
1342 
1343   C->set_has_loops(true);
1344   RegionNode* head = new RegionNode(3);
1345   head->init_req(1, kit.control());
1346 
1347   kit.gvn().set_type(head, Type::CONTROL);
1348   Node* i_phi = new PhiNode(head, TypeInt::INT);
1349   i_phi->init_req(1, i);
1350   kit.gvn().set_type(i_phi, TypeInt::INT);
1351   Node* charPos = new PhiNode(head, TypeInt::INT);
1352   charPos->init_req(1, end);
1353   kit.gvn().set_type(charPos, TypeInt::INT);
1354   Node* mem = PhiNode::make(head, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES);
1355   kit.gvn().set_type(mem, Type::MEMORY);
1356 
1357   kit.set_control(head);
1358   kit.set_memory(mem, byte_adr_idx);
1359 
1360   Node* q = __ DivI(kit.null(), i_phi, __ intcon(10));
1361   Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
1362                                    __ LShiftI(q, __ intcon(1))));
1363   Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2));
1364   Node* ch = __ AddI(r, __ intcon('0'));
1365   Node* st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE),
1366                                 ch, bt, MemNode::unordered, false /* require_atomic_access */,
1367                                 false /* unaligned */, (bt != T_BYTE) /* mismatched */);
1368 
1369   iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
1370                               PROB_FAIR, COUNT_UNKNOWN);
1371   Node* ne = __ IfTrue(iff);
1372   Node* eq = __ IfFalse(iff);
1373 
1374   head->init_req(2, ne);
1375   mem->init_req(2, st);
1376 
1377   i_phi->init_req(2, q);
1378   charPos->init_req(2, index);
1379   charPos = index;
1380 
1381   kit.set_control(eq);
1382   kit.set_memory(st, byte_adr_idx);
1383 
1384   C->record_for_igvn(head);
1385   C->record_for_igvn(mem);
1386   C->record_for_igvn(i_phi);
1387   C->record_for_igvn(charPos);
1388 
1389   // if (sign != 0) {
1390   //     buf [--charPos] = sign;
1391   // }
1392   iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
1393                               PROB_FAIR, COUNT_UNKNOWN);
1394 
1395   final_merge->init_req(merge_index + 2, __ IfFalse(iff));
1396   final_mem->init_req(merge_index + 2, kit.memory(byte_adr_idx));
1397 
1398   kit.set_control(__ IfTrue(iff));
1399   if (kit.stopped()) {
1400     final_merge->init_req(merge_index + 1, C->top());
1401     final_mem->init_req(merge_index + 1, C->top());
1402   } else {
1403     Node* index = __ SubI(charPos, __ intcon((bt == T_BYTE) ? 1 : 2));
1404     st = __ store_to_memory(kit.control(), kit.array_element_address(dst_array, index, T_BYTE),
1405                             sign, bt, MemNode::unordered, false /* require_atomic_access */, false /* unaligned */,
1406                             (bt != T_BYTE) /* mismatched */);
1407 
1408     final_merge->init_req(merge_index + 1, kit.control());
1409     final_mem->init_req(merge_index + 1, st);
1410   }
1411 }
1412 
1413 // Copy the characters representing arg into dst_array starting at start
1414 Node* PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* dst_array, Node* dst_coder, Node* start, Node* size) {
1415   bool dcon = dst_coder->is_Con();
1416   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1417   Node* end = __ AddI(start, __ LShiftI(size, dst_coder));
1418 
1419   // The final_merge node has 4 entries in case the encoding is known:
1420   // (0) Control, (1) result w/ sign, (2) result w/o sign, (3) result for Integer.min_value
1421   // or 6 entries in case the encoding is not known:
1422   // (0) Control, (1) Latin1 w/ sign, (2) Latin1 w/o sign, (3) min_value, (4) UTF16 w/ sign, (5) UTF16 w/o sign
1423   RegionNode* final_merge = new RegionNode(dcon ? 4 : 6);
1424   kit.gvn().set_type(final_merge, Type::CONTROL);
1425 
1426   Node* final_mem = PhiNode::make(final_merge, kit.memory(byte_adr_idx), Type::MEMORY, TypeAryPtr::BYTES);
1427   kit.gvn().set_type(final_mem, Type::MEMORY);
1428 
1429   // need to handle arg == Integer.MIN_VALUE specially because negating doesn't make it positive
1430   IfNode* iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1431                                       PROB_FAIR, COUNT_UNKNOWN);
1432 
1433   Node* old_mem = kit.memory(byte_adr_idx);
1434 
1435   kit.set_control(__ IfFalse(iff));
1436   if (kit.stopped()) {
1437     // Statically not equal to MIN_VALUE so this path is dead
1438     final_merge->init_req(3, kit.control());
1439   } else {
1440     copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
1441                 dst_array, dst_coder, start);
1442     final_merge->init_req(3, kit.control());
1443     final_mem->init_req(3, kit.memory(byte_adr_idx));
1444   }
1445 
1446   kit.set_control(__ IfTrue(iff));
1447   kit.set_memory(old_mem, byte_adr_idx);
1448 
1449   if (!dcon) {
1450     // Check encoding of destination
1451     iff = kit.create_and_map_if(kit.control(), __ Bool(__ CmpI(dst_coder, __ intcon(0)), BoolTest::eq),
1452                                 PROB_FAIR, COUNT_UNKNOWN);
1453     old_mem = kit.memory(byte_adr_idx);
1454   }
1455   if (!dcon || dbyte) {
1456     // Destination is Latin1,
1457     if (!dcon) {
1458       kit.set_control(__ IfTrue(iff));
1459     }
1460     getChars(kit, arg, dst_array, T_BYTE, end, final_merge, final_mem);
1461   }
1462   if (!dcon || !dbyte) {
1463     // Destination is UTF16
1464     int merge_index = 0;
1465     if (!dcon) {
1466       kit.set_control(__ IfFalse(iff));
1467       kit.set_memory(old_mem, byte_adr_idx);
1468       merge_index = 3; // Account for Latin1 case
1469     }
1470     getChars(kit, arg, dst_array, T_CHAR, end, final_merge, final_mem, merge_index);
1471   }
1472 
1473   // Final merge point for Latin1 and UTF16 case
1474   kit.set_control(final_merge);
1475   kit.set_memory(final_mem, byte_adr_idx);
1476 
1477   C->record_for_igvn(final_merge);
1478   C->record_for_igvn(final_mem);
1479   return end;
1480 }
1481 
1482 // Copy 'count' bytes/chars from src_array to dst_array starting at index start
1483 void PhaseStringOpts::arraycopy(GraphKit& kit, IdealKit& ideal, Node* src_array, Node* dst_array, BasicType elembt, Node* start, Node* count) {
1484   assert(elembt == T_BYTE || elembt == T_CHAR, "Invalid type for arraycopy");
1485 
1486   if (elembt == T_CHAR) {
1487     // Get number of chars
1488     count = __ RShiftI(count, __ intcon(1));
1489   }
1490 
1491   Node* extra = nullptr;
1492 #ifdef _LP64
1493   count = __ ConvI2L(count);
1494   extra = C->top();
1495 #endif
1496 
1497   Node* src_ptr = __ array_element_address(src_array, __ intcon(0), T_BYTE);
1498   Node* dst_ptr = __ array_element_address(dst_array, start, T_BYTE);
1499   // Check if src array address is aligned to HeapWordSize
1500   bool aligned = (arrayOopDesc::base_offset_in_bytes(T_BYTE) % HeapWordSize == 0);
1501   // If true, then check if dst array address is aligned to HeapWordSize
1502   if (aligned) {
1503     const TypeInt* tdst = __ gvn().type(start)->is_int();
1504     aligned = tdst->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_BYTE) +
1505                                   tdst->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0);
1506   }
1507   // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1508   const char* copyfunc_name = "arraycopy";
1509   address     copyfunc_addr = StubRoutines::select_arraycopy_function(elembt, aligned, true, copyfunc_name, true);
1510   ideal.make_leaf_call_no_fp(OptoRuntime::fast_arraycopy_Type(), copyfunc_addr, copyfunc_name,
1511                              TypeAryPtr::BYTES, src_ptr, dst_ptr, count, extra);
1512 }
1513 
1514 #undef __
1515 #define __ ideal.
1516 
1517 // Copy contents of a Latin1 encoded string from src_array to dst_array
1518 void PhaseStringOpts::copy_latin1_string(GraphKit& kit, IdealKit& ideal, Node* src_array, IdealVariable& count,
1519                                          Node* dst_array, Node* dst_coder, Node* start) {
1520   bool dcon = dst_coder->is_Con();
1521   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1522 
1523   if (!dcon) {
1524     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1525   }
1526   if (!dcon || dbyte) {
1527     // Destination is Latin1. Simply emit a byte arraycopy.
1528     arraycopy(kit, ideal, src_array, dst_array, T_BYTE, start, __ value(count));
1529   }
1530   if (!dcon) {
1531     __ else_();
1532   }
1533   if (!dcon || !dbyte) {
1534     // Destination is UTF16. Inflate src_array into dst_array.
1535     kit.sync_kit(ideal);
1536     if (Matcher::match_rule_supported(Op_StrInflatedCopy)) {
1537       // Use fast intrinsic
1538       Node* src = kit.array_element_address(src_array, kit.intcon(0), T_BYTE);
1539       Node* dst = kit.array_element_address(dst_array, start, T_BYTE);
1540       kit.inflate_string(src, dst, TypeAryPtr::BYTES, __ value(count));
1541     } else {
1542       // No intrinsic available, use slow method
1543       kit.inflate_string_slow(src_array, dst_array, start, __ value(count));
1544     }
1545     ideal.sync_kit(&kit);
1546     // Multiply count by two since we now need two bytes per char
1547     __ set(count, __ LShiftI(__ value(count), __ ConI(1)));
1548   }
1549   if (!dcon) {
1550     __ end_if();
1551   }
1552 }
1553 
1554 // Read two bytes from index and index+1 and convert them to a char
1555 static jchar readChar(ciTypeArray* array, int index) {
1556   int shift_high, shift_low;
1557 #ifdef VM_LITTLE_ENDIAN
1558     shift_high = 0;
1559     shift_low = 8;
1560 #else
1561     shift_high = 8;
1562     shift_low = 0;
1563 #endif
1564 
1565   jchar b1 = ((jchar) array->byte_at(index)) & 0xff;
1566   jchar b2 = ((jchar) array->byte_at(index+1)) & 0xff;
1567   return (b1 << shift_high) | (b2 << shift_low);
1568 }
1569 
1570 // Copy contents of constant src_array to dst_array by emitting individual stores
1571 void PhaseStringOpts::copy_constant_string(GraphKit& kit, IdealKit& ideal, ciTypeArray* src_array, IdealVariable& count,
1572                                            bool src_is_byte, Node* dst_array, Node* dst_coder, Node* start) {
1573   bool dcon = dst_coder->is_Con();
1574   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1575   int length = src_array->length();
1576 
1577   if (!dcon) {
1578     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1579   }
1580   if (!dcon || dbyte) {
1581     // Destination is Latin1. Copy each byte of src_array into dst_array.
1582     Node* index = start;
1583     for (int i = 0; i < length; i++) {
1584       Node* adr = kit.array_element_address(dst_array, index, T_BYTE);
1585       Node* val = __ ConI(src_array->byte_at(i));
1586       __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered);
1587       index = __ AddI(index, __ ConI(1));
1588     }
1589   }
1590   if (!dcon) {
1591     __ else_();
1592   }
1593   if (!dcon || !dbyte) {
1594     // Destination is UTF16. Copy each char of src_array into dst_array.
1595     Node* index = start;
1596     for (int i = 0; i < length; i++) {
1597       Node* adr = kit.array_element_address(dst_array, index, T_BYTE);
1598       jchar val;
1599       if (src_is_byte) {
1600         val = src_array->byte_at(i) & 0xff;
1601       } else {
1602         val = readChar(src_array, i++);
1603       }
1604       __ store(__ ctrl(), adr, __ ConI(val), T_CHAR, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */,
1605                true /* mismatched */);
1606       index = __ AddI(index, __ ConI(2));
1607     }
1608     if (src_is_byte) {
1609       // Multiply count by two since we now need two bytes per char
1610       __ set(count, __ ConI(2 * length));
1611     }
1612   }
1613   if (!dcon) {
1614     __ end_if();
1615   }
1616 }
1617 
1618 // Compress copy contents of the byte/char String str into dst_array starting at index start.
1619 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* dst_array, Node* dst_coder, Node* start) {
1620   Node* src_array = kit.load_String_value(str, true);
1621 
1622   IdealKit ideal(&kit, true, true);
1623   IdealVariable count(ideal); __ declarations_done();
1624 
1625   if (str->is_Con()) {
1626     // Constant source string
1627     ciTypeArray* src_array_type = get_constant_value(kit, str);
1628 
1629     // Check encoding of constant string
1630     bool src_is_byte = (get_constant_coder(kit, str) == java_lang_String::CODER_LATIN1);
1631 
1632     // For small constant strings just emit individual stores.
1633     // A length of 6 seems like a good space/speed tradeof.
1634     __ set(count, __ ConI(src_array_type->length()));
1635     int src_len = src_array_type->length() / (src_is_byte ? 1 : 2);
1636     if (src_len < unroll_string_copy_length) {
1637       // Small constant string
1638       copy_constant_string(kit, ideal, src_array_type, count, src_is_byte, dst_array, dst_coder, start);
1639     } else if (src_is_byte) {
1640       // Source is Latin1
1641       copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start);
1642     } else {
1643       // Source is UTF16 (destination too). Simply emit a char arraycopy.
1644       arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count));
1645     }
1646   } else {
1647     Node* size = kit.load_array_length(src_array);
1648     __ set(count, size);
1649     // Non-constant source string
1650     if (CompactStrings) {
1651       // Emit runtime check for coder
1652       Node* coder = kit.load_String_coder(str, true);
1653       __ if_then(coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1)); {
1654         // Source is Latin1
1655         copy_latin1_string(kit, ideal, src_array, count, dst_array, dst_coder, start);
1656       } __ else_();
1657     }
1658     // Source is UTF16 (destination too). Simply emit a char arraycopy.
1659     arraycopy(kit, ideal, src_array, dst_array, T_CHAR, start, __ value(count));
1660 
1661     if (CompactStrings) {
1662       __ end_if();
1663     }
1664   }
1665 
1666   // Finally sync IdealKit and GraphKit.
1667   kit.sync_kit(ideal);
1668   return __ AddI(start, __ value(count));
1669 }
1670 
1671 // Compress copy the char into dst_array at index start.
1672 Node* PhaseStringOpts::copy_char(GraphKit& kit, Node* val, Node* dst_array, Node* dst_coder, Node* start) {
1673   bool dcon = (dst_coder != nullptr) && dst_coder->is_Con();
1674   bool dbyte = dcon ? (dst_coder->get_int() == java_lang_String::CODER_LATIN1) : false;
1675 
1676   IdealKit ideal(&kit, true, true);
1677   IdealVariable end(ideal); __ declarations_done();
1678   Node* adr = kit.array_element_address(dst_array, start, T_BYTE);
1679   if (!dcon){
1680     __ if_then(dst_coder, BoolTest::eq, __ ConI(java_lang_String::CODER_LATIN1));
1681   }
1682   if (!dcon || dbyte) {
1683     // Destination is Latin1. Store a byte.
1684     __ store(__ ctrl(), adr, val, T_BYTE, byte_adr_idx, MemNode::unordered);
1685     __ set(end, __ AddI(start, __ ConI(1)));
1686   }
1687   if (!dcon) {
1688     __ else_();
1689   }
1690   if (!dcon || !dbyte) {
1691     // Destination is UTF16. Store a char.
1692     __ store(__ ctrl(), adr, val, T_CHAR, byte_adr_idx, MemNode::unordered, false /* require_atomic_access */,
1693              true /* mismatched */);
1694     __ set(end, __ AddI(start, __ ConI(2)));
1695   }
1696   if (!dcon) {
1697     __ end_if();
1698   }
1699   // Finally sync IdealKit and GraphKit.
1700   kit.sync_kit(ideal);
1701   return __ value(end);
1702 }
1703 
1704 #undef __
1705 #define __ kit.
1706 
1707 // Allocate a byte array of specified length.
1708 Node* PhaseStringOpts::allocate_byte_array(GraphKit& kit, IdealKit* ideal, Node* length) {
1709   if (ideal != nullptr) {
1710     // Sync IdealKit and graphKit.
1711     kit.sync_kit(*ideal);
1712   }
1713   Node* byte_array = nullptr;
1714   {
1715     PreserveReexecuteState preexecs(&kit);
1716     // The original jvms is for an allocation of either a String or
1717     // StringBuffer so no stack adjustment is necessary for proper
1718     // reexecution.  If we deoptimize in the slow path the bytecode
1719     // will be reexecuted and the char[] allocation will be thrown away.
1720     kit.jvms()->set_should_reexecute(true);
1721     byte_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_BYTE))),
1722                                length, 1);
1723   }
1724 
1725   // Mark the allocation so that zeroing is skipped since the code
1726   // below will overwrite the entire array
1727   AllocateArrayNode* byte_alloc = AllocateArrayNode::Ideal_array_allocation(byte_array);
1728   byte_alloc->maybe_set_complete(_gvn);
1729 
1730   if (ideal != nullptr) {
1731     // Sync IdealKit and graphKit.
1732     ideal->sync_kit(&kit);
1733   }
1734   return byte_array;
1735 }
1736 
1737 jbyte PhaseStringOpts::get_constant_coder(GraphKit& kit, Node* str) {
1738   assert(str->is_Con(), "String must be constant");
1739   const TypeOopPtr* str_type = kit.gvn().type(str)->isa_oopptr();
1740   ciInstance* str_instance = str_type->const_oop()->as_instance();
1741   jbyte coder = str_instance->field_value_by_offset(java_lang_String::coder_offset()).as_byte();
1742   assert(CompactStrings || (coder == java_lang_String::CODER_UTF16), "Strings must be UTF16 encoded");
1743   return coder;
1744 }
1745 
1746 int PhaseStringOpts::get_constant_length(GraphKit& kit, Node* str) {
1747   assert(str->is_Con(), "String must be constant");
1748   return get_constant_value(kit, str)->length();
1749 }
1750 
1751 ciTypeArray* PhaseStringOpts::get_constant_value(GraphKit& kit, Node* str) {
1752   assert(str->is_Con(), "String must be constant");
1753   const TypeOopPtr* str_type = kit.gvn().type(str)->isa_oopptr();
1754   ciInstance* str_instance = str_type->const_oop()->as_instance();
1755   ciObject* src_array = str_instance->field_value_by_offset(java_lang_String::value_offset()).as_object();
1756   return src_array->as_type_array();
1757 }
1758 
1759 void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
1760   // Log a little info about the transformation
1761   sc->maybe_log_transform();
1762 
1763   // pull the JVMState of the allocation into a SafePointNode to serve as
1764   // as a shim for the insertion of the new code.
1765   JVMState* jvms     = sc->begin()->jvms()->clone_shallow(C);
1766   uint size = sc->begin()->req();
1767   SafePointNode* map = new SafePointNode(size, jvms);
1768 
1769   // copy the control and memory state from the final call into our
1770   // new starting state.  This allows any preceding tests to feed
1771   // into the new section of code.
1772   for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
1773     map->init_req(i1, sc->end()->in(i1));
1774   }
1775   // blow away old allocation arguments
1776   for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
1777     map->init_req(i1, C->top());
1778   }
1779   // Copy the rest of the inputs for the JVMState
1780   for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
1781     map->init_req(i1, sc->begin()->in(i1));
1782   }
1783   // Make sure the memory state is a MergeMem for parsing.
1784   if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
1785     map->set_req(TypeFunc::Memory, MergeMemNode::make(map->in(TypeFunc::Memory)));
1786   }
1787 
1788   jvms->set_map(map);
1789   map->ensure_stack(jvms, jvms->method()->max_stack());
1790 
1791   // disconnect all the old StringBuilder calls from the graph
1792   sc->eliminate_unneeded_control();
1793 
1794   // At this point all the old work has been completely removed from
1795   // the graph and the saved JVMState exists at the point where the
1796   // final toString call used to be.
1797   GraphKit kit(jvms);
1798 
1799   // There may be uncommon traps which are still using the
1800   // intermediate states and these need to be rewritten to point at
1801   // the JVMState at the beginning of the transformation.
1802   sc->convert_uncommon_traps(kit, jvms);
1803 
1804   // Now insert the logic to compute the size of the string followed
1805   // by all the logic to construct array and resulting string.
1806 
1807   Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
1808 
1809   // Create a region for the overflow checks to merge into.
1810   int args = MAX2(sc->num_arguments(), 1);
1811   RegionNode* overflow = new RegionNode(args);
1812   kit.gvn().set_type(overflow, Type::CONTROL);
1813 
1814   // Create a hook node to hold onto the individual sizes since they
1815   // are need for the copying phase.
1816   Node* string_sizes = new Node(args);
1817 
1818   Node* coder = __ intcon(0);
1819   Node* length = __ intcon(0);
1820   // If at least one argument is UTF16 encoded, we can fix the encoding.
1821   bool coder_fixed = false;
1822 
1823   if (!CompactStrings) {
1824     // Fix encoding of result string to UTF16
1825     coder_fixed = true;
1826     coder = __ intcon(java_lang_String::CODER_UTF16);
1827   }
1828 
1829   for (int argi = 0; argi < sc->num_arguments(); argi++) {
1830     Node* arg = sc->argument(argi);
1831     switch (sc->mode(argi)) {
1832       case StringConcat::NegativeIntCheckMode: {
1833         // Initial capacity argument might be negative in which case StringBuilder(int) throws
1834         // a NegativeArraySizeException. Insert a runtime check with an uncommon trap.
1835         const TypeInt* type = kit.gvn().type(arg)->is_int();
1836         assert(type->_hi >= 0 && type->_lo < 0, "no runtime int check needed");
1837         Node* p = __ Bool(__ CmpI(arg, kit.intcon(0)), BoolTest::ge);
1838         IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1839         {
1840           // Negative int -> uncommon trap.
1841           PreserveJVMState pjvms(&kit);
1842           kit.set_control(__ IfFalse(iff));
1843           kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1844                             Deoptimization::Action_maybe_recompile);
1845         }
1846         kit.set_control(__ IfTrue(iff));
1847         break;
1848       }
1849       case StringConcat::IntMode: {
1850         Node* string_size = int_stringSize(kit, arg);
1851 
1852         // accumulate total
1853         length = __ AddI(length, string_size);
1854 
1855         // Cache this value for the use by int_toString
1856         string_sizes->init_req(argi, string_size);
1857         break;
1858       }
1859       case StringConcat::StringNullCheckMode: {
1860         const Type* type = kit.gvn().type(arg);
1861         assert(type != TypePtr::NULL_PTR, "missing check");
1862         if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1863           // Null check with uncommon trap since
1864           // StringBuilder(null) throws exception.
1865           // Use special uncommon trap instead of
1866           // calling normal do_null_check().
1867           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1868           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1869           overflow->add_req(__ IfFalse(iff));
1870           Node* notnull = __ IfTrue(iff);
1871           kit.set_control(notnull); // set control for the cast_not_null
1872           arg = kit.cast_not_null(arg, false);
1873           sc->set_argument(argi, arg);
1874         }
1875         assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
1876         // Fallthrough to add string length.
1877       }
1878       case StringConcat::StringMode: {
1879         const Type* type = kit.gvn().type(arg);
1880         Node* count = nullptr;
1881         Node* arg_coder = nullptr;
1882         if (type == TypePtr::NULL_PTR) {
1883           // replace the argument with the null checked version
1884           arg = null_string;
1885           sc->set_argument(argi, arg);
1886           count = kit.load_String_length(arg, true);
1887           arg_coder = kit.load_String_coder(arg, true);
1888         } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1889           // s = s != null ? s : "null";
1890           // length = length + (s.count - s.offset);
1891           RegionNode *r = new RegionNode(3);
1892           kit.gvn().set_type(r, Type::CONTROL);
1893           Node *phi = new PhiNode(r, type);
1894           kit.gvn().set_type(phi, phi->bottom_type());
1895           Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1896           IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1897           Node* notnull = __ IfTrue(iff);
1898           Node* isnull =  __ IfFalse(iff);
1899           kit.set_control(notnull); // set control for the cast_not_null
1900           r->init_req(1, notnull);
1901           phi->init_req(1, kit.cast_not_null(arg, false));
1902           r->init_req(2, isnull);
1903           phi->init_req(2, null_string);
1904           kit.set_control(r);
1905           C->record_for_igvn(r);
1906           C->record_for_igvn(phi);
1907           // replace the argument with the null checked version
1908           arg = phi;
1909           sc->set_argument(argi, arg);
1910           count = kit.load_String_length(arg, true);
1911           arg_coder = kit.load_String_coder(arg, true);
1912         } else {
1913           // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP
1914           // kit.control might be a different test, that can be hoisted above the actual nullcheck
1915           // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck.
1916           count = kit.load_String_length(arg, false);
1917           arg_coder = kit.load_String_coder(arg, false);
1918         }
1919         if (arg->is_Con()) {
1920           // Constant string. Get constant coder and length.
1921           jbyte const_coder = get_constant_coder(kit, arg);
1922           int const_length = get_constant_length(kit, arg);
1923           if (const_coder == java_lang_String::CODER_LATIN1) {
1924             // Can be latin1 encoded
1925             arg_coder = __ intcon(const_coder);
1926             count = __ intcon(const_length);
1927           } else {
1928             // Found UTF16 encoded string. Fix result array encoding to UTF16.
1929             coder_fixed = true;
1930             coder = __ intcon(const_coder);
1931             count = __ intcon(const_length / 2);
1932           }
1933         }
1934 
1935         if (!coder_fixed) {
1936           coder = __ OrI(coder, arg_coder);
1937         }
1938         length = __ AddI(length, count);
1939         string_sizes->init_req(argi, nullptr);
1940         break;
1941       }
1942       case StringConcat::CharMode: {
1943         // one character only
1944         const TypeInt* t = kit.gvn().type(arg)->is_int();
1945         if (!coder_fixed && t->is_con()) {
1946           // Constant char
1947           if (t->get_con() <= 255) {
1948             // Can be latin1 encoded
1949             coder = __ OrI(coder, __ intcon(java_lang_String::CODER_LATIN1));
1950           } else {
1951             // Must be UTF16 encoded. Fix result array encoding to UTF16.
1952             coder_fixed = true;
1953             coder = __ intcon(java_lang_String::CODER_UTF16);
1954           }
1955         } else if (!coder_fixed) {
1956           // Not constant
1957 #undef __
1958 #define __ ideal.
1959           IdealKit ideal(&kit, true, true);
1960           IdealVariable char_coder(ideal); __ declarations_done();
1961           // Check if character can be latin1 encoded
1962           __ if_then(arg, BoolTest::le, __ ConI(0xFF));
1963             __ set(char_coder, __ ConI(java_lang_String::CODER_LATIN1));
1964           __ else_();
1965             __ set(char_coder, __ ConI(java_lang_String::CODER_UTF16));
1966           __ end_if();
1967           kit.sync_kit(ideal);
1968           coder = __ OrI(coder, __ value(char_coder));
1969 #undef __
1970 #define __ kit.
1971         }
1972         length = __ AddI(length, __ intcon(1));
1973         break;
1974       }
1975       default:
1976         ShouldNotReachHere();
1977     }
1978     if (argi > 0) {
1979       // Check that the sum hasn't overflowed
1980       IfNode* iff = kit.create_and_map_if(kit.control(),
1981                                           __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
1982                                           PROB_MIN, COUNT_UNKNOWN);
1983       kit.set_control(__ IfFalse(iff));
1984       overflow->set_req(argi, __ IfTrue(iff));
1985     }
1986   }
1987 
1988   {
1989     // Hook
1990     PreserveJVMState pjvms(&kit);
1991     kit.set_control(overflow);
1992     C->record_for_igvn(overflow);
1993     kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1994                       Deoptimization::Action_make_not_entrant);
1995   }
1996 
1997   Node* result;
1998   if (!kit.stopped()) {
1999     assert(CompactStrings || (coder->is_Con() && coder->get_int() == java_lang_String::CODER_UTF16),
2000            "Result string must be UTF16 encoded if CompactStrings is disabled");
2001 
2002     Node* dst_array = nullptr;
2003     if (sc->num_arguments() == 1 &&
2004         (sc->mode(0) == StringConcat::StringMode ||
2005          sc->mode(0) == StringConcat::StringNullCheckMode)) {
2006       // Handle the case when there is only a single String argument.
2007       // In this case, we can just pull the value from the String itself.
2008       dst_array = kit.load_String_value(sc->argument(0), true);
2009     } else {
2010       // Allocate destination byte array according to coder
2011       dst_array = allocate_byte_array(kit, nullptr, __ LShiftI(length, coder));
2012 
2013       // Now copy the string representations into the final byte[]
2014       Node* start = __ intcon(0);
2015       for (int argi = 0; argi < sc->num_arguments(); argi++) {
2016         Node* arg = sc->argument(argi);
2017         switch (sc->mode(argi)) {
2018           case StringConcat::NegativeIntCheckMode:
2019             break; // Nothing to do, was only needed to add a runtime check earlier.
2020           case StringConcat::IntMode: {
2021             start = int_getChars(kit, arg, dst_array, coder, start, string_sizes->in(argi));
2022             break;
2023           }
2024           case StringConcat::StringNullCheckMode:
2025           case StringConcat::StringMode: {
2026             start = copy_string(kit, arg, dst_array, coder, start);
2027             break;
2028           }
2029           case StringConcat::CharMode: {
2030             start = copy_char(kit, arg, dst_array, coder, start);
2031           break;
2032           }
2033           default:
2034             ShouldNotReachHere();
2035         }
2036       }
2037     }
2038 
2039     {
2040       PreserveReexecuteState preexecs(&kit);
2041       // The original jvms is for an allocation of either a String or
2042       // StringBuffer so no stack adjustment is necessary for proper
2043       // reexecution.
2044       kit.jvms()->set_should_reexecute(true);
2045       result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
2046     }
2047 
2048     // Initialize the string
2049     kit.store_String_value(result, dst_array);
2050     kit.store_String_coder(result, coder);
2051 
2052     // The value field is final. Emit a barrier here to ensure that the effect
2053     // of the initialization is committed to memory before any code publishes
2054     // a reference to the newly constructed object (see Parse::do_exits()).
2055     assert(AllocateNode::Ideal_allocation(result) != nullptr, "should be newly allocated");
2056     kit.insert_mem_bar(UseStoreStoreForCtor ? Op_MemBarStoreStore : Op_MemBarRelease, result);
2057   } else {
2058     result = C->top();
2059   }
2060   // hook up the outgoing control and result
2061   kit.replace_call(sc->end(), result);
2062 
2063   // Unhook any hook nodes
2064   string_sizes->disconnect_inputs(C);
2065   sc->cleanup();
2066 #ifndef PRODUCT
2067   AtomicAccess::inc(&_stropts_replaced);
2068 #endif
2069 }
2070 
2071 #ifndef PRODUCT
2072 uint PhaseStringOpts::_stropts_replaced = 0;
2073 uint PhaseStringOpts::_stropts_merged = 0;
2074 uint PhaseStringOpts::_stropts_total = 0;
2075 
2076 void PhaseStringOpts::print_statistics() {
2077   tty->print_cr("StringConcat: %4d/%4d/%4d(replaced/merged/total)", _stropts_replaced, _stropts_merged, _stropts_total);
2078 }
2079 #endif