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