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