Fri, 11 Mar 2011 07:50:51 -0800
7026631: field _klass is incorrectly set for dual type of TypeAryPtr::OOPS
Summary: add missing check this->dual() != TypeAryPtr::OOPS into TypeAryPtr::klass().
Reviewed-by: never
1 /*
2 * Copyright (c) 2009, 2010, 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 */
25 #include "precompiled.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "opto/addnode.hpp"
28 #include "opto/callGenerator.hpp"
29 #include "opto/callnode.hpp"
30 #include "opto/divnode.hpp"
31 #include "opto/graphKit.hpp"
32 #include "opto/idealKit.hpp"
33 #include "opto/rootnode.hpp"
34 #include "opto/runtime.hpp"
35 #include "opto/stringopts.hpp"
36 #include "opto/subnode.hpp"
38 #define __ kit.
40 class StringConcat : public ResourceObj {
41 private:
42 PhaseStringOpts* _stringopts;
43 Node* _string_alloc;
44 AllocateNode* _begin; // The allocation the begins the pattern
45 CallStaticJavaNode* _end; // The final call of the pattern. Will either be
46 // SB.toString or or String.<init>(SB.toString)
47 bool _multiple; // indicates this is a fusion of two or more
48 // separate StringBuilders
50 Node* _arguments; // The list of arguments to be concatenated
51 GrowableArray<int> _mode; // into a String along with a mode flag
52 // indicating how to treat the value.
54 Node_List _control; // List of control nodes that will be deleted
55 Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten
56 // to restart at the initial JVMState.
57 public:
58 // Mode for converting arguments to Strings
59 enum {
60 StringMode,
61 IntMode,
62 CharMode,
63 StringNullCheckMode
64 };
66 StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end):
67 _end(end),
68 _begin(NULL),
69 _multiple(false),
70 _string_alloc(NULL),
71 _stringopts(stringopts) {
72 _arguments = new (_stringopts->C, 1) Node(1);
73 _arguments->del_req(0);
74 }
76 bool validate_control_flow();
78 void merge_add() {
79 #if 0
80 // XXX This is place holder code for reusing an existing String
81 // allocation but the logic for checking the state safety is
82 // probably inadequate at the moment.
83 CallProjections endprojs;
84 sc->end()->extract_projections(&endprojs, false);
85 if (endprojs.resproj != NULL) {
86 for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) {
87 CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
88 if (use != NULL && use->method() != NULL &&
89 use->method()->intrinsic_id() == vmIntrinsics::_String_String &&
90 use->in(TypeFunc::Parms + 1) == endprojs.resproj) {
91 // Found useless new String(sb.toString()) so reuse the newly allocated String
92 // when creating the result instead of allocating a new one.
93 sc->set_string_alloc(use->in(TypeFunc::Parms));
94 sc->set_end(use);
95 }
96 }
97 }
98 #endif
99 }
101 StringConcat* merge(StringConcat* other, Node* arg);
103 void set_allocation(AllocateNode* alloc) {
104 _begin = alloc;
105 }
107 void append(Node* value, int mode) {
108 _arguments->add_req(value);
109 _mode.append(mode);
110 }
111 void push(Node* value, int mode) {
112 _arguments->ins_req(0, value);
113 _mode.insert_before(0, mode);
114 }
115 void push_string(Node* value) {
116 push(value, StringMode);
117 }
118 void push_string_null_check(Node* value) {
119 push(value, StringNullCheckMode);
120 }
121 void push_int(Node* value) {
122 push(value, IntMode);
123 }
124 void push_char(Node* value) {
125 push(value, CharMode);
126 }
128 Node* argument(int i) {
129 return _arguments->in(i);
130 }
131 void set_argument(int i, Node* value) {
132 _arguments->set_req(i, value);
133 }
134 int num_arguments() {
135 return _mode.length();
136 }
137 int mode(int i) {
138 return _mode.at(i);
139 }
140 void add_control(Node* ctrl) {
141 assert(!_control.contains(ctrl), "only push once");
142 _control.push(ctrl);
143 }
144 CallStaticJavaNode* end() { return _end; }
145 AllocateNode* begin() { return _begin; }
146 Node* string_alloc() { return _string_alloc; }
148 void eliminate_unneeded_control();
149 void eliminate_initialize(InitializeNode* init);
150 void eliminate_call(CallNode* call);
152 void maybe_log_transform() {
153 CompileLog* log = _stringopts->C->log();
154 if (log != NULL) {
155 log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'",
156 num_arguments(),
157 _string_alloc != NULL,
158 _multiple);
159 JVMState* p = _begin->jvms();
160 while (p != NULL) {
161 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
162 p = p->caller();
163 }
164 log->tail("replace_string_concat");
165 }
166 }
168 void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) {
169 for (uint u = 0; u < _uncommon_traps.size(); u++) {
170 Node* uct = _uncommon_traps.at(u);
172 // Build a new call using the jvms state of the allocate
173 address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();
174 const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type();
175 int size = call_type->domain()->cnt();
176 const TypePtr* no_memory_effects = NULL;
177 Compile* C = _stringopts->C;
178 CallStaticJavaNode* call = new (C, size) CallStaticJavaNode(call_type, call_addr, "uncommon_trap",
179 jvms->bci(), no_memory_effects);
180 for (int e = 0; e < TypeFunc::Parms; e++) {
181 call->init_req(e, uct->in(e));
182 }
183 // Set the trap request to record intrinsic failure if this trap
184 // is taken too many times. Ideally we would handle then traps by
185 // doing the original bookkeeping in the MDO so that if it caused
186 // the code to be thrown out we could still recompile and use the
187 // optimization. Failing the uncommon traps doesn't really mean
188 // that the optimization is a bad idea but there's no other way to
189 // do the MDO updates currently.
190 int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic,
191 Deoptimization::Action_make_not_entrant);
192 call->init_req(TypeFunc::Parms, __ intcon(trap_request));
193 kit.add_safepoint_edges(call);
195 _stringopts->gvn()->transform(call);
196 C->gvn_replace_by(uct, call);
197 uct->disconnect_inputs(NULL);
198 }
199 }
201 void cleanup() {
202 // disconnect the hook node
203 _arguments->disconnect_inputs(NULL);
204 }
205 };
208 void StringConcat::eliminate_unneeded_control() {
209 eliminate_initialize(begin()->initialization());
210 for (uint i = 0; i < _control.size(); i++) {
211 Node* n = _control.at(i);
212 if (n->is_Call()) {
213 if (n != _end) {
214 eliminate_call(n->as_Call());
215 }
216 } else if (n->is_IfTrue()) {
217 Compile* C = _stringopts->C;
218 C->gvn_replace_by(n, n->in(0)->in(0));
219 C->gvn_replace_by(n->in(0), C->top());
220 }
221 }
222 }
225 StringConcat* StringConcat::merge(StringConcat* other, Node* arg) {
226 StringConcat* result = new StringConcat(_stringopts, _end);
227 for (uint x = 0; x < _control.size(); x++) {
228 Node* n = _control.at(x);
229 if (n->is_Call()) {
230 result->_control.push(n);
231 }
232 }
233 for (uint x = 0; x < other->_control.size(); x++) {
234 Node* n = other->_control.at(x);
235 if (n->is_Call()) {
236 result->_control.push(n);
237 }
238 }
239 assert(result->_control.contains(other->_end), "what?");
240 assert(result->_control.contains(_begin), "what?");
241 for (int x = 0; x < num_arguments(); x++) {
242 if (argument(x) == arg) {
243 // replace the toString result with the all the arguments that
244 // made up the other StringConcat
245 for (int y = 0; y < other->num_arguments(); y++) {
246 result->append(other->argument(y), other->mode(y));
247 }
248 } else {
249 result->append(argument(x), mode(x));
250 }
251 }
252 result->set_allocation(other->_begin);
253 result->_multiple = true;
254 return result;
255 }
258 void StringConcat::eliminate_call(CallNode* call) {
259 Compile* C = _stringopts->C;
260 CallProjections projs;
261 call->extract_projections(&projs, false);
262 if (projs.fallthrough_catchproj != NULL) {
263 C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control));
264 }
265 if (projs.fallthrough_memproj != NULL) {
266 C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory));
267 }
268 if (projs.catchall_memproj != NULL) {
269 C->gvn_replace_by(projs.catchall_memproj, C->top());
270 }
271 if (projs.fallthrough_ioproj != NULL) {
272 C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O));
273 }
274 if (projs.catchall_ioproj != NULL) {
275 C->gvn_replace_by(projs.catchall_ioproj, C->top());
276 }
277 if (projs.catchall_catchproj != NULL) {
278 // EA can't cope with the partially collapsed graph this
279 // creates so put it on the worklist to be collapsed later.
280 for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) {
281 Node *use = i.get();
282 int opc = use->Opcode();
283 if (opc == Op_CreateEx || opc == Op_Region) {
284 _stringopts->record_dead_node(use);
285 }
286 }
287 C->gvn_replace_by(projs.catchall_catchproj, C->top());
288 }
289 if (projs.resproj != NULL) {
290 C->gvn_replace_by(projs.resproj, C->top());
291 }
292 C->gvn_replace_by(call, C->top());
293 }
295 void StringConcat::eliminate_initialize(InitializeNode* init) {
296 Compile* C = _stringopts->C;
298 // Eliminate Initialize node.
299 assert(init->outcnt() <= 2, "only a control and memory projection expected");
300 assert(init->req() <= InitializeNode::RawStores, "no pending inits");
301 Node *ctrl_proj = init->proj_out(TypeFunc::Control);
302 if (ctrl_proj != NULL) {
303 C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control));
304 }
305 Node *mem_proj = init->proj_out(TypeFunc::Memory);
306 if (mem_proj != NULL) {
307 Node *mem = init->in(TypeFunc::Memory);
308 C->gvn_replace_by(mem_proj, mem);
309 }
310 C->gvn_replace_by(init, C->top());
311 init->disconnect_inputs(NULL);
312 }
314 Node_List PhaseStringOpts::collect_toString_calls() {
315 Node_List string_calls;
316 Node_List worklist;
318 _visited.Clear();
320 // Prime the worklist
321 for (uint i = 1; i < C->root()->len(); i++) {
322 Node* n = C->root()->in(i);
323 if (n != NULL && !_visited.test_set(n->_idx)) {
324 worklist.push(n);
325 }
326 }
328 while (worklist.size() > 0) {
329 Node* ctrl = worklist.pop();
330 if (ctrl->is_CallStaticJava()) {
331 CallStaticJavaNode* csj = ctrl->as_CallStaticJava();
332 ciMethod* m = csj->method();
333 if (m != NULL &&
334 (m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString ||
335 m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString)) {
336 string_calls.push(csj);
337 }
338 }
339 if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) {
340 worklist.push(ctrl->in(0));
341 }
342 if (ctrl->is_Region()) {
343 for (uint i = 1; i < ctrl->len(); i++) {
344 if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) {
345 worklist.push(ctrl->in(i));
346 }
347 }
348 }
349 }
350 return string_calls;
351 }
354 StringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) {
355 ciMethod* m = call->method();
356 ciSymbol* string_sig;
357 ciSymbol* int_sig;
358 ciSymbol* char_sig;
359 if (m->holder() == C->env()->StringBuilder_klass()) {
360 string_sig = ciSymbol::String_StringBuilder_signature();
361 int_sig = ciSymbol::int_StringBuilder_signature();
362 char_sig = ciSymbol::char_StringBuilder_signature();
363 } else if (m->holder() == C->env()->StringBuffer_klass()) {
364 string_sig = ciSymbol::String_StringBuffer_signature();
365 int_sig = ciSymbol::int_StringBuffer_signature();
366 char_sig = ciSymbol::char_StringBuffer_signature();
367 } else {
368 return NULL;
369 }
370 #ifndef PRODUCT
371 if (PrintOptimizeStringConcat) {
372 tty->print("considering toString call in ");
373 call->jvms()->dump_spec(tty); tty->cr();
374 }
375 #endif
377 StringConcat* sc = new StringConcat(this, call);
379 AllocateNode* alloc = NULL;
380 InitializeNode* init = NULL;
382 // possible opportunity for StringBuilder fusion
383 CallStaticJavaNode* cnode = call;
384 while (cnode) {
385 Node* recv = cnode->in(TypeFunc::Parms)->uncast();
386 if (recv->is_Proj()) {
387 recv = recv->in(0);
388 }
389 cnode = recv->isa_CallStaticJava();
390 if (cnode == NULL) {
391 alloc = recv->isa_Allocate();
392 if (alloc == NULL) {
393 break;
394 }
395 // Find the constructor call
396 Node* result = alloc->result_cast();
397 if (result == NULL || !result->is_CheckCastPP()) {
398 // strange looking allocation
399 #ifndef PRODUCT
400 if (PrintOptimizeStringConcat) {
401 tty->print("giving up because allocation looks strange ");
402 alloc->jvms()->dump_spec(tty); tty->cr();
403 }
404 #endif
405 break;
406 }
407 Node* constructor = NULL;
408 for (SimpleDUIterator i(result); i.has_next(); i.next()) {
409 CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
410 if (use != NULL &&
411 use->method() != NULL &&
412 !use->method()->is_static() &&
413 use->method()->name() == ciSymbol::object_initializer_name() &&
414 use->method()->holder() == m->holder()) {
415 // Matched the constructor.
416 ciSymbol* sig = use->method()->signature()->as_symbol();
417 if (sig == ciSymbol::void_method_signature() ||
418 sig == ciSymbol::int_void_signature() ||
419 sig == ciSymbol::string_void_signature()) {
420 if (sig == ciSymbol::string_void_signature()) {
421 // StringBuilder(String) so pick this up as the first argument
422 assert(use->in(TypeFunc::Parms + 1) != NULL, "what?");
423 const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1));
424 if (type == TypePtr::NULL_PTR) {
425 // StringBuilder(null) throws exception.
426 #ifndef PRODUCT
427 if (PrintOptimizeStringConcat) {
428 tty->print("giving up because StringBuilder(null) throws exception");
429 alloc->jvms()->dump_spec(tty); tty->cr();
430 }
431 #endif
432 return NULL;
433 }
434 // StringBuilder(str) argument needs null check.
435 sc->push_string_null_check(use->in(TypeFunc::Parms + 1));
436 }
437 // The int variant takes an initial size for the backing
438 // array so just treat it like the void version.
439 constructor = use;
440 } else {
441 #ifndef PRODUCT
442 if (PrintOptimizeStringConcat) {
443 tty->print("unexpected constructor signature: %s", sig->as_utf8());
444 }
445 #endif
446 }
447 break;
448 }
449 }
450 if (constructor == NULL) {
451 // couldn't find constructor
452 #ifndef PRODUCT
453 if (PrintOptimizeStringConcat) {
454 tty->print("giving up because couldn't find constructor ");
455 alloc->jvms()->dump_spec(tty); tty->cr();
456 }
457 #endif
458 break;
459 }
461 // Walked all the way back and found the constructor call so see
462 // if this call converted into a direct string concatenation.
463 sc->add_control(call);
464 sc->add_control(constructor);
465 sc->add_control(alloc);
466 sc->set_allocation(alloc);
467 if (sc->validate_control_flow()) {
468 return sc;
469 } else {
470 return NULL;
471 }
472 } else if (cnode->method() == NULL) {
473 break;
474 } else if (!cnode->method()->is_static() &&
475 cnode->method()->holder() == m->holder() &&
476 cnode->method()->name() == ciSymbol::append_name() &&
477 (cnode->method()->signature()->as_symbol() == string_sig ||
478 cnode->method()->signature()->as_symbol() == char_sig ||
479 cnode->method()->signature()->as_symbol() == int_sig)) {
480 sc->add_control(cnode);
481 Node* arg = cnode->in(TypeFunc::Parms + 1);
482 if (cnode->method()->signature()->as_symbol() == int_sig) {
483 sc->push_int(arg);
484 } else if (cnode->method()->signature()->as_symbol() == char_sig) {
485 sc->push_char(arg);
486 } else {
487 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {
488 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
489 if (csj->method() != NULL &&
490 csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString) {
491 sc->add_control(csj);
492 sc->push_int(csj->in(TypeFunc::Parms));
493 continue;
494 }
495 }
496 sc->push_string(arg);
497 }
498 continue;
499 } else {
500 // some unhandled signature
501 #ifndef PRODUCT
502 if (PrintOptimizeStringConcat) {
503 tty->print("giving up because encountered unexpected signature ");
504 cnode->tf()->dump(); tty->cr();
505 cnode->in(TypeFunc::Parms + 1)->dump();
506 }
507 #endif
508 break;
509 }
510 }
511 return NULL;
512 }
515 PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*):
516 Phase(StringOpts),
517 _gvn(gvn),
518 _visited(Thread::current()->resource_area()) {
520 assert(OptimizeStringConcat, "shouldn't be here");
522 size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"),
523 ciSymbol::make("[I"), true);
524 if (size_table_field == NULL) {
525 // Something wrong so give up.
526 assert(false, "why can't we find Integer.sizeTable?");
527 return;
528 }
530 // Collect the types needed to talk about the various slices of memory
531 const TypeInstPtr* string_type = TypeInstPtr::make(TypePtr::NotNull, C->env()->String_klass(),
532 false, NULL, 0);
534 const TypePtr* value_field_type = string_type->add_offset(java_lang_String::value_offset_in_bytes());
535 const TypePtr* offset_field_type = string_type->add_offset(java_lang_String::offset_offset_in_bytes());
536 const TypePtr* count_field_type = string_type->add_offset(java_lang_String::count_offset_in_bytes());
538 value_field_idx = C->get_alias_index(value_field_type);
539 count_field_idx = C->get_alias_index(count_field_type);
540 offset_field_idx = C->get_alias_index(offset_field_type);
541 char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS);
543 // For each locally allocated StringBuffer see if the usages can be
544 // collapsed into a single String construction.
546 // Run through the list of allocation looking for SB.toString to see
547 // if it's possible to fuse the usage of the SB into a single String
548 // construction.
549 GrowableArray<StringConcat*> concats;
550 Node_List toStrings = collect_toString_calls();
551 while (toStrings.size() > 0) {
552 StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava());
553 if (sc != NULL) {
554 concats.push(sc);
555 }
556 }
558 // try to coalesce separate concats
559 restart:
560 for (int c = 0; c < concats.length(); c++) {
561 StringConcat* sc = concats.at(c);
562 for (int i = 0; i < sc->num_arguments(); i++) {
563 Node* arg = sc->argument(i);
564 if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {
565 CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
566 if (csj->method() != NULL &&
567 (csj->method()->intrinsic_id() == vmIntrinsics::_StringBuilder_toString ||
568 csj->method()->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) {
569 for (int o = 0; o < concats.length(); o++) {
570 if (c == o) continue;
571 StringConcat* other = concats.at(o);
572 if (other->end() == csj) {
573 #ifndef PRODUCT
574 if (PrintOptimizeStringConcat) {
575 tty->print_cr("considering stacked concats");
576 }
577 #endif
579 StringConcat* merged = sc->merge(other, arg);
580 if (merged->validate_control_flow()) {
581 #ifndef PRODUCT
582 if (PrintOptimizeStringConcat) {
583 tty->print_cr("stacking would succeed");
584 }
585 #endif
586 if (c < o) {
587 concats.remove_at(o);
588 concats.at_put(c, merged);
589 } else {
590 concats.remove_at(c);
591 concats.at_put(o, merged);
592 }
593 goto restart;
594 } else {
595 #ifndef PRODUCT
596 if (PrintOptimizeStringConcat) {
597 tty->print_cr("stacking would fail");
598 }
599 #endif
600 }
601 }
602 }
603 }
604 }
605 }
606 }
609 for (int c = 0; c < concats.length(); c++) {
610 StringConcat* sc = concats.at(c);
611 replace_string_concat(sc);
612 }
614 remove_dead_nodes();
615 }
617 void PhaseStringOpts::record_dead_node(Node* dead) {
618 dead_worklist.push(dead);
619 }
621 void PhaseStringOpts::remove_dead_nodes() {
622 // Delete any dead nodes to make things clean enough that escape
623 // analysis doesn't get unhappy.
624 while (dead_worklist.size() > 0) {
625 Node* use = dead_worklist.pop();
626 int opc = use->Opcode();
627 switch (opc) {
628 case Op_Region: {
629 uint i = 1;
630 for (i = 1; i < use->req(); i++) {
631 if (use->in(i) != C->top()) {
632 break;
633 }
634 }
635 if (i >= use->req()) {
636 for (SimpleDUIterator i(use); i.has_next(); i.next()) {
637 Node* m = i.get();
638 if (m->is_Phi()) {
639 dead_worklist.push(m);
640 }
641 }
642 C->gvn_replace_by(use, C->top());
643 }
644 break;
645 }
646 case Op_AddP:
647 case Op_CreateEx: {
648 // Recurisvely clean up references to CreateEx so EA doesn't
649 // get unhappy about the partially collapsed graph.
650 for (SimpleDUIterator i(use); i.has_next(); i.next()) {
651 Node* m = i.get();
652 if (m->is_AddP()) {
653 dead_worklist.push(m);
654 }
655 }
656 C->gvn_replace_by(use, C->top());
657 break;
658 }
659 case Op_Phi:
660 if (use->in(0) == C->top()) {
661 C->gvn_replace_by(use, C->top());
662 }
663 break;
664 }
665 }
666 }
669 bool StringConcat::validate_control_flow() {
670 // We found all the calls and arguments now lets see if it's
671 // safe to transform the graph as we would expect.
673 // Check to see if this resulted in too many uncommon traps previously
674 if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(),
675 Deoptimization::Reason_intrinsic)) {
676 return false;
677 }
679 // Walk backwards over the control flow from toString to the
680 // allocation and make sure all the control flow is ok. This
681 // means it's either going to be eliminated once the calls are
682 // removed or it can safely be transformed into an uncommon
683 // trap.
685 int null_check_count = 0;
686 Unique_Node_List ctrl_path;
688 assert(_control.contains(_begin), "missing");
689 assert(_control.contains(_end), "missing");
691 // Collect the nodes that we know about and will eliminate into ctrl_path
692 for (uint i = 0; i < _control.size(); i++) {
693 // Push the call and it's control projection
694 Node* n = _control.at(i);
695 if (n->is_Allocate()) {
696 AllocateNode* an = n->as_Allocate();
697 InitializeNode* init = an->initialization();
698 ctrl_path.push(init);
699 ctrl_path.push(init->as_Multi()->proj_out(0));
700 }
701 if (n->is_Call()) {
702 CallNode* cn = n->as_Call();
703 ctrl_path.push(cn);
704 ctrl_path.push(cn->proj_out(0));
705 ctrl_path.push(cn->proj_out(0)->unique_out());
706 ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0));
707 } else {
708 ShouldNotReachHere();
709 }
710 }
712 // Skip backwards through the control checking for unexpected contro flow
713 Node* ptr = _end;
714 bool fail = false;
715 while (ptr != _begin) {
716 if (ptr->is_Call() && ctrl_path.member(ptr)) {
717 ptr = ptr->in(0);
718 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
719 ptr = ptr->in(0)->in(0)->in(0);
720 assert(ctrl_path.member(ptr), "should be a known piece of control");
721 } else if (ptr->is_IfTrue()) {
722 IfNode* iff = ptr->in(0)->as_If();
723 BoolNode* b = iff->in(1)->isa_Bool();
724 Node* cmp = b->in(1);
725 Node* v1 = cmp->in(1);
726 Node* v2 = cmp->in(2);
727 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con);
729 // Null check of the return of append which can simply be eliminated
730 if (b->_test._test == BoolTest::ne &&
731 v2->bottom_type() == TypePtr::NULL_PTR &&
732 v1->is_Proj() && ctrl_path.member(v1->in(0))) {
733 // NULL check of the return value of the append
734 null_check_count++;
735 if (otherproj->outcnt() == 1) {
736 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
737 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
738 ctrl_path.push(call);
739 }
740 }
741 _control.push(ptr);
742 ptr = ptr->in(0)->in(0);
743 continue;
744 }
746 // A test which leads to an uncommon trap which should be safe.
747 // Later this trap will be converted into a trap that restarts
748 // at the beginning.
749 if (otherproj->outcnt() == 1) {
750 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
751 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
752 // control flow leads to uct so should be ok
753 _uncommon_traps.push(call);
754 ctrl_path.push(call);
755 ptr = ptr->in(0)->in(0);
756 continue;
757 }
758 }
760 #ifndef PRODUCT
761 // Some unexpected control flow we don't know how to handle.
762 if (PrintOptimizeStringConcat) {
763 tty->print_cr("failing with unknown test");
764 b->dump();
765 cmp->dump();
766 v1->dump();
767 v2->dump();
768 tty->cr();
769 }
770 #endif
771 break;
772 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) {
773 ptr = ptr->in(0)->in(0);
774 } else if (ptr->is_Region()) {
775 Node* copy = ptr->as_Region()->is_copy();
776 if (copy != NULL) {
777 ptr = copy;
778 continue;
779 }
780 if (ptr->req() == 3 &&
781 ptr->in(1) != NULL && ptr->in(1)->is_Proj() &&
782 ptr->in(2) != NULL && ptr->in(2)->is_Proj() &&
783 ptr->in(1)->in(0) == ptr->in(2)->in(0) &&
784 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) {
785 // Simple diamond.
786 // XXX should check for possibly merging stores. simple data merges are ok.
787 ptr = ptr->in(1)->in(0)->in(0);
788 continue;
789 }
790 #ifndef PRODUCT
791 if (PrintOptimizeStringConcat) {
792 tty->print_cr("fusion would fail for region");
793 _begin->dump();
794 ptr->dump(2);
795 }
796 #endif
797 fail = true;
798 break;
799 } else {
800 // other unknown control
801 if (!fail) {
802 #ifndef PRODUCT
803 if (PrintOptimizeStringConcat) {
804 tty->print_cr("fusion would fail for");
805 _begin->dump();
806 }
807 #endif
808 fail = true;
809 }
810 #ifndef PRODUCT
811 if (PrintOptimizeStringConcat) {
812 ptr->dump();
813 }
814 #endif
815 ptr = ptr->in(0);
816 }
817 }
818 #ifndef PRODUCT
819 if (PrintOptimizeStringConcat && fail) {
820 tty->cr();
821 }
822 #endif
823 if (fail) return !fail;
825 // Validate that all these results produced are contained within
826 // this cluster of objects. First collect all the results produced
827 // by calls in the region.
828 _stringopts->_visited.Clear();
829 Node_List worklist;
830 Node* final_result = _end->proj_out(TypeFunc::Parms);
831 for (uint i = 0; i < _control.size(); i++) {
832 CallNode* cnode = _control.at(i)->isa_Call();
833 if (cnode != NULL) {
834 _stringopts->_visited.test_set(cnode->_idx);
835 }
836 Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL;
837 if (result != NULL && result != final_result) {
838 worklist.push(result);
839 }
840 }
842 Node* last_result = NULL;
843 while (worklist.size() > 0) {
844 Node* result = worklist.pop();
845 if (_stringopts->_visited.test_set(result->_idx))
846 continue;
847 for (SimpleDUIterator i(result); i.has_next(); i.next()) {
848 Node *use = i.get();
849 if (ctrl_path.member(use)) {
850 // already checked this
851 continue;
852 }
853 int opc = use->Opcode();
854 if (opc == Op_CmpP || opc == Op_Node) {
855 ctrl_path.push(use);
856 continue;
857 }
858 if (opc == Op_CastPP || opc == Op_CheckCastPP) {
859 for (SimpleDUIterator j(use); j.has_next(); j.next()) {
860 worklist.push(j.get());
861 }
862 worklist.push(use->in(1));
863 ctrl_path.push(use);
864 continue;
865 }
866 #ifndef PRODUCT
867 if (PrintOptimizeStringConcat) {
868 if (result != last_result) {
869 last_result = result;
870 tty->print_cr("extra uses for result:");
871 last_result->dump();
872 }
873 use->dump();
874 }
875 #endif
876 fail = true;
877 break;
878 }
879 }
881 #ifndef PRODUCT
882 if (PrintOptimizeStringConcat && !fail) {
883 ttyLocker ttyl;
884 tty->cr();
885 tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size());
886 _begin->jvms()->dump_spec(tty); tty->cr();
887 for (int i = 0; i < num_arguments(); i++) {
888 argument(i)->dump();
889 }
890 _control.dump();
891 tty->cr();
892 }
893 #endif
895 return !fail;
896 }
898 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
899 const TypeKlassPtr* klass_type = TypeKlassPtr::make(field->holder());
900 Node* klass_node = __ makecon(klass_type);
901 BasicType bt = field->layout_type();
902 ciType* field_klass = field->type();
904 const Type *type;
905 if( bt == T_OBJECT ) {
906 if (!field->type()->is_loaded()) {
907 type = TypeInstPtr::BOTTOM;
908 } else if (field->is_constant()) {
909 // This can happen if the constant oop is non-perm.
910 ciObject* con = field->constant_value().as_object();
911 // Do not "join" in the previous type; it doesn't add value,
912 // and may yield a vacuous result if the field is of interface type.
913 type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
914 assert(type != NULL, "field singleton type must be consistent");
915 } else {
916 type = TypeOopPtr::make_from_klass(field_klass->as_klass());
917 }
918 } else {
919 type = Type::get_const_basic_type(bt);
920 }
922 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
923 type, T_OBJECT,
924 C->get_alias_index(klass_type->add_offset(field->offset_in_bytes())));
925 }
927 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
928 RegionNode *final_merge = new (C, 3) RegionNode(3);
929 kit.gvn().set_type(final_merge, Type::CONTROL);
930 Node* final_size = new (C, 3) PhiNode(final_merge, TypeInt::INT);
931 kit.gvn().set_type(final_size, TypeInt::INT);
933 IfNode* iff = kit.create_and_map_if(kit.control(),
934 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
935 PROB_FAIR, COUNT_UNKNOWN);
936 Node* is_min = __ IfFalse(iff);
937 final_merge->init_req(1, is_min);
938 final_size->init_req(1, __ intcon(11));
940 kit.set_control(__ IfTrue(iff));
941 if (kit.stopped()) {
942 final_merge->init_req(2, C->top());
943 final_size->init_req(2, C->top());
944 } else {
946 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
947 RegionNode *r = new (C, 3) RegionNode(3);
948 kit.gvn().set_type(r, Type::CONTROL);
949 Node *phi = new (C, 3) PhiNode(r, TypeInt::INT);
950 kit.gvn().set_type(phi, TypeInt::INT);
951 Node *size = new (C, 3) PhiNode(r, TypeInt::INT);
952 kit.gvn().set_type(size, TypeInt::INT);
953 Node* chk = __ CmpI(arg, __ intcon(0));
954 Node* p = __ Bool(chk, BoolTest::lt);
955 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN);
956 Node* lessthan = __ IfTrue(iff);
957 Node* greaterequal = __ IfFalse(iff);
958 r->init_req(1, lessthan);
959 phi->init_req(1, __ SubI(__ intcon(0), arg));
960 size->init_req(1, __ intcon(1));
961 r->init_req(2, greaterequal);
962 phi->init_req(2, arg);
963 size->init_req(2, __ intcon(0));
964 kit.set_control(r);
965 C->record_for_igvn(r);
966 C->record_for_igvn(phi);
967 C->record_for_igvn(size);
969 // for (int i=0; ; i++)
970 // if (x <= sizeTable[i])
971 // return i+1;
972 RegionNode *loop = new (C, 3) RegionNode(3);
973 loop->init_req(1, kit.control());
974 kit.gvn().set_type(loop, Type::CONTROL);
976 Node *index = new (C, 3) PhiNode(loop, TypeInt::INT);
977 index->init_req(1, __ intcon(0));
978 kit.gvn().set_type(index, TypeInt::INT);
979 kit.set_control(loop);
980 Node* sizeTable = fetch_static_field(kit, size_table_field);
982 Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS);
983 C->record_for_igvn(value);
984 Node* limit = __ CmpI(phi, value);
985 Node* limitb = __ Bool(limit, BoolTest::le);
986 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN);
987 Node* lessEqual = __ IfTrue(iff2);
988 Node* greater = __ IfFalse(iff2);
990 loop->init_req(2, greater);
991 index->init_req(2, __ AddI(index, __ intcon(1)));
993 kit.set_control(lessEqual);
994 C->record_for_igvn(loop);
995 C->record_for_igvn(index);
997 final_merge->init_req(2, kit.control());
998 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
999 }
1001 kit.set_control(final_merge);
1002 C->record_for_igvn(final_merge);
1003 C->record_for_igvn(final_size);
1005 return final_size;
1006 }
1008 void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) {
1009 RegionNode *final_merge = new (C, 4) RegionNode(4);
1010 kit.gvn().set_type(final_merge, Type::CONTROL);
1011 Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1012 kit.gvn().set_type(final_mem, Type::MEMORY);
1014 // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive
1015 {
1016 // i == MIN_VALUE
1017 IfNode* iff = kit.create_and_map_if(kit.control(),
1018 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1019 PROB_FAIR, COUNT_UNKNOWN);
1021 Node* old_mem = kit.memory(char_adr_idx);
1023 kit.set_control(__ IfFalse(iff));
1024 if (kit.stopped()) {
1025 // Statically not equal to MIN_VALUE so this path is dead
1026 final_merge->init_req(3, kit.control());
1027 } else {
1028 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
1029 char_array, start);
1030 final_merge->init_req(3, kit.control());
1031 final_mem->init_req(3, kit.memory(char_adr_idx));
1032 }
1034 kit.set_control(__ IfTrue(iff));
1035 kit.set_memory(old_mem, char_adr_idx);
1036 }
1039 // Simplified version of Integer.getChars
1041 // int q, r;
1042 // int charPos = index;
1043 Node* charPos = end;
1045 // char sign = 0;
1047 Node* i = arg;
1048 Node* sign = __ intcon(0);
1050 // if (i < 0) {
1051 // sign = '-';
1052 // i = -i;
1053 // }
1054 {
1055 IfNode* iff = kit.create_and_map_if(kit.control(),
1056 __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
1057 PROB_FAIR, COUNT_UNKNOWN);
1059 RegionNode *merge = new (C, 3) RegionNode(3);
1060 kit.gvn().set_type(merge, Type::CONTROL);
1061 i = new (C, 3) PhiNode(merge, TypeInt::INT);
1062 kit.gvn().set_type(i, TypeInt::INT);
1063 sign = new (C, 3) PhiNode(merge, TypeInt::INT);
1064 kit.gvn().set_type(sign, TypeInt::INT);
1066 merge->init_req(1, __ IfTrue(iff));
1067 i->init_req(1, __ SubI(__ intcon(0), arg));
1068 sign->init_req(1, __ intcon('-'));
1069 merge->init_req(2, __ IfFalse(iff));
1070 i->init_req(2, arg);
1071 sign->init_req(2, __ intcon(0));
1073 kit.set_control(merge);
1075 C->record_for_igvn(merge);
1076 C->record_for_igvn(i);
1077 C->record_for_igvn(sign);
1078 }
1080 // for (;;) {
1081 // q = i / 10;
1082 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
1083 // buf [--charPos] = digits [r];
1084 // i = q;
1085 // if (i == 0) break;
1086 // }
1088 {
1089 RegionNode *head = new (C, 3) RegionNode(3);
1090 head->init_req(1, kit.control());
1091 kit.gvn().set_type(head, Type::CONTROL);
1092 Node *i_phi = new (C, 3) PhiNode(head, TypeInt::INT);
1093 i_phi->init_req(1, i);
1094 kit.gvn().set_type(i_phi, TypeInt::INT);
1095 charPos = PhiNode::make(head, charPos);
1096 kit.gvn().set_type(charPos, TypeInt::INT);
1097 Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1098 kit.gvn().set_type(mem, Type::MEMORY);
1099 kit.set_control(head);
1100 kit.set_memory(mem, char_adr_idx);
1102 Node* q = __ DivI(NULL, i_phi, __ intcon(10));
1103 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
1104 __ LShiftI(q, __ intcon(1))));
1105 Node* m1 = __ SubI(charPos, __ intcon(1));
1106 Node* ch = __ AddI(r, __ intcon('0'));
1108 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1109 ch, T_CHAR, char_adr_idx);
1112 IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
1113 PROB_FAIR, COUNT_UNKNOWN);
1114 Node* ne = __ IfTrue(iff);
1115 Node* eq = __ IfFalse(iff);
1117 head->init_req(2, ne);
1118 mem->init_req(2, st);
1119 i_phi->init_req(2, q);
1120 charPos->init_req(2, m1);
1122 charPos = m1;
1124 kit.set_control(eq);
1125 kit.set_memory(st, char_adr_idx);
1127 C->record_for_igvn(head);
1128 C->record_for_igvn(mem);
1129 C->record_for_igvn(i_phi);
1130 C->record_for_igvn(charPos);
1131 }
1133 {
1134 // if (sign != 0) {
1135 // buf [--charPos] = sign;
1136 // }
1137 IfNode* iff = kit.create_and_map_if(kit.control(),
1138 __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
1139 PROB_FAIR, COUNT_UNKNOWN);
1141 final_merge->init_req(2, __ IfFalse(iff));
1142 final_mem->init_req(2, kit.memory(char_adr_idx));
1144 kit.set_control(__ IfTrue(iff));
1145 if (kit.stopped()) {
1146 final_merge->init_req(1, C->top());
1147 final_mem->init_req(1, C->top());
1148 } else {
1149 Node* m1 = __ SubI(charPos, __ intcon(1));
1150 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1151 sign, T_CHAR, char_adr_idx);
1153 final_merge->init_req(1, kit.control());
1154 final_mem->init_req(1, st);
1155 }
1157 kit.set_control(final_merge);
1158 kit.set_memory(final_mem, char_adr_idx);
1160 C->record_for_igvn(final_merge);
1161 C->record_for_igvn(final_mem);
1162 }
1163 }
1166 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) {
1167 Node* string = str;
1168 Node* offset = kit.make_load(NULL,
1169 kit.basic_plus_adr(string, string, java_lang_String::offset_offset_in_bytes()),
1170 TypeInt::INT, T_INT, offset_field_idx);
1171 Node* count = kit.make_load(NULL,
1172 kit.basic_plus_adr(string, string, java_lang_String::count_offset_in_bytes()),
1173 TypeInt::INT, T_INT, count_field_idx);
1174 const TypeAryPtr* value_type = TypeAryPtr::make(TypePtr::NotNull,
1175 TypeAry::make(TypeInt::CHAR,TypeInt::POS),
1176 ciTypeArrayKlass::make(T_CHAR), true, 0);
1177 Node* value = kit.make_load(NULL,
1178 kit.basic_plus_adr(string, string, java_lang_String::value_offset_in_bytes()),
1179 value_type, T_OBJECT, value_field_idx);
1181 // copy the contents
1182 if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) {
1183 // For small constant strings just emit individual stores.
1184 // A length of 6 seems like a good space/speed tradeof.
1185 int c = count->get_int();
1186 int o = offset->get_int();
1187 const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr();
1188 ciTypeArray* value_array = t->const_oop()->as_type_array();
1189 for (int e = 0; e < c; e++) {
1190 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
1191 __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx);
1192 start = __ AddI(start, __ intcon(1));
1193 }
1194 } else {
1195 Node* src_ptr = kit.array_element_address(value, offset, T_CHAR);
1196 Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR);
1197 Node* c = count;
1198 Node* extra = NULL;
1199 #ifdef _LP64
1200 c = __ ConvI2L(c);
1201 extra = C->top();
1202 #endif
1203 Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP,
1204 OptoRuntime::fast_arraycopy_Type(),
1205 CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()),
1206 "jshort_disjoint_arraycopy", TypeAryPtr::CHARS,
1207 src_ptr, dst_ptr, c, extra);
1208 start = __ AddI(start, count);
1209 }
1210 return start;
1211 }
1214 void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
1215 // Log a little info about the transformation
1216 sc->maybe_log_transform();
1218 // pull the JVMState of the allocation into a SafePointNode to serve as
1219 // as a shim for the insertion of the new code.
1220 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C);
1221 uint size = sc->begin()->req();
1222 SafePointNode* map = new (C, size) SafePointNode(size, jvms);
1224 // copy the control and memory state from the final call into our
1225 // new starting state. This allows any preceeding tests to feed
1226 // into the new section of code.
1227 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
1228 map->init_req(i1, sc->end()->in(i1));
1229 }
1230 // blow away old allocation arguments
1231 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
1232 map->init_req(i1, C->top());
1233 }
1234 // Copy the rest of the inputs for the JVMState
1235 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
1236 map->init_req(i1, sc->begin()->in(i1));
1237 }
1238 // Make sure the memory state is a MergeMem for parsing.
1239 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
1240 map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory)));
1241 }
1243 jvms->set_map(map);
1244 map->ensure_stack(jvms, jvms->method()->max_stack());
1247 // disconnect all the old StringBuilder calls from the graph
1248 sc->eliminate_unneeded_control();
1250 // At this point all the old work has been completely removed from
1251 // the graph and the saved JVMState exists at the point where the
1252 // final toString call used to be.
1253 GraphKit kit(jvms);
1255 // There may be uncommon traps which are still using the
1256 // intermediate states and these need to be rewritten to point at
1257 // the JVMState at the beginning of the transformation.
1258 sc->convert_uncommon_traps(kit, jvms);
1260 // Now insert the logic to compute the size of the string followed
1261 // by all the logic to construct array and resulting string.
1263 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
1265 // Create a region for the overflow checks to merge into.
1266 int args = MAX2(sc->num_arguments(), 1);
1267 RegionNode* overflow = new (C, args) RegionNode(args);
1268 kit.gvn().set_type(overflow, Type::CONTROL);
1270 // Create a hook node to hold onto the individual sizes since they
1271 // are need for the copying phase.
1272 Node* string_sizes = new (C, args) Node(args);
1274 Node* length = __ intcon(0);
1275 for (int argi = 0; argi < sc->num_arguments(); argi++) {
1276 Node* arg = sc->argument(argi);
1277 switch (sc->mode(argi)) {
1278 case StringConcat::IntMode: {
1279 Node* string_size = int_stringSize(kit, arg);
1281 // accumulate total
1282 length = __ AddI(length, string_size);
1284 // Cache this value for the use by int_toString
1285 string_sizes->init_req(argi, string_size);
1286 break;
1287 }
1288 case StringConcat::StringNullCheckMode: {
1289 const Type* type = kit.gvn().type(arg);
1290 assert(type != TypePtr::NULL_PTR, "missing check");
1291 if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1292 // Null check with uncommont trap since
1293 // StringBuilder(null) throws exception.
1294 // Use special uncommon trap instead of
1295 // calling normal do_null_check().
1296 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1297 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1298 overflow->add_req(__ IfFalse(iff));
1299 Node* notnull = __ IfTrue(iff);
1300 kit.set_control(notnull); // set control for the cast_not_null
1301 arg = kit.cast_not_null(arg, false);
1302 sc->set_argument(argi, arg);
1303 }
1304 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
1305 // Fallthrough to add string length.
1306 }
1307 case StringConcat::StringMode: {
1308 const Type* type = kit.gvn().type(arg);
1309 if (type == TypePtr::NULL_PTR) {
1310 // replace the argument with the null checked version
1311 arg = null_string;
1312 sc->set_argument(argi, arg);
1313 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1314 // s = s != null ? s : "null";
1315 // length = length + (s.count - s.offset);
1316 RegionNode *r = new (C, 3) RegionNode(3);
1317 kit.gvn().set_type(r, Type::CONTROL);
1318 Node *phi = new (C, 3) PhiNode(r, type);
1319 kit.gvn().set_type(phi, phi->bottom_type());
1320 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1321 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1322 Node* notnull = __ IfTrue(iff);
1323 Node* isnull = __ IfFalse(iff);
1324 kit.set_control(notnull); // set control for the cast_not_null
1325 r->init_req(1, notnull);
1326 phi->init_req(1, kit.cast_not_null(arg, false));
1327 r->init_req(2, isnull);
1328 phi->init_req(2, null_string);
1329 kit.set_control(r);
1330 C->record_for_igvn(r);
1331 C->record_for_igvn(phi);
1332 // replace the argument with the null checked version
1333 arg = phi;
1334 sc->set_argument(argi, arg);
1335 }
1336 // Node* offset = kit.make_load(NULL, kit.basic_plus_adr(arg, arg, offset_offset),
1337 // TypeInt::INT, T_INT, offset_field_idx);
1338 Node* count = kit.make_load(NULL, kit.basic_plus_adr(arg, arg, java_lang_String::count_offset_in_bytes()),
1339 TypeInt::INT, T_INT, count_field_idx);
1340 length = __ AddI(length, count);
1341 string_sizes->init_req(argi, NULL);
1342 break;
1343 }
1344 case StringConcat::CharMode: {
1345 // one character only
1346 length = __ AddI(length, __ intcon(1));
1347 break;
1348 }
1349 default:
1350 ShouldNotReachHere();
1351 }
1352 if (argi > 0) {
1353 // Check that the sum hasn't overflowed
1354 IfNode* iff = kit.create_and_map_if(kit.control(),
1355 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
1356 PROB_MIN, COUNT_UNKNOWN);
1357 kit.set_control(__ IfFalse(iff));
1358 overflow->set_req(argi, __ IfTrue(iff));
1359 }
1360 }
1362 {
1363 // Hook
1364 PreserveJVMState pjvms(&kit);
1365 kit.set_control(overflow);
1366 C->record_for_igvn(overflow);
1367 kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1368 Deoptimization::Action_make_not_entrant);
1369 }
1371 // length now contains the number of characters needed for the
1372 // char[] so create a new AllocateArray for the char[]
1373 Node* char_array = NULL;
1374 {
1375 PreserveReexecuteState preexecs(&kit);
1376 // The original jvms is for an allocation of either a String or
1377 // StringBuffer so no stack adjustment is necessary for proper
1378 // reexecution. If we deoptimize in the slow path the bytecode
1379 // will be reexecuted and the char[] allocation will be thrown away.
1380 kit.jvms()->set_should_reexecute(true);
1381 char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))),
1382 length, 1);
1383 }
1385 // Mark the allocation so that zeroing is skipped since the code
1386 // below will overwrite the entire array
1387 AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn);
1388 char_alloc->maybe_set_complete(_gvn);
1390 // Now copy the string representations into the final char[]
1391 Node* start = __ intcon(0);
1392 for (int argi = 0; argi < sc->num_arguments(); argi++) {
1393 Node* arg = sc->argument(argi);
1394 switch (sc->mode(argi)) {
1395 case StringConcat::IntMode: {
1396 Node* end = __ AddI(start, string_sizes->in(argi));
1397 // getChars words backwards so pass the ending point as well as the start
1398 int_getChars(kit, arg, char_array, start, end);
1399 start = end;
1400 break;
1401 }
1402 case StringConcat::StringNullCheckMode:
1403 case StringConcat::StringMode: {
1404 start = copy_string(kit, arg, char_array, start);
1405 break;
1406 }
1407 case StringConcat::CharMode: {
1408 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
1409 arg, T_CHAR, char_adr_idx);
1410 start = __ AddI(start, __ intcon(1));
1411 break;
1412 }
1413 default:
1414 ShouldNotReachHere();
1415 }
1416 }
1418 // If we're not reusing an existing String allocation then allocate one here.
1419 Node* result = sc->string_alloc();
1420 if (result == NULL) {
1421 PreserveReexecuteState preexecs(&kit);
1422 // The original jvms is for an allocation of either a String or
1423 // StringBuffer so no stack adjustment is necessary for proper
1424 // reexecution.
1425 kit.jvms()->set_should_reexecute(true);
1426 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
1427 }
1429 // Intialize the string
1430 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::offset_offset_in_bytes()),
1431 __ intcon(0), T_INT, offset_field_idx);
1432 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::count_offset_in_bytes()),
1433 length, T_INT, count_field_idx);
1434 kit.store_to_memory(kit.control(), kit.basic_plus_adr(result, java_lang_String::value_offset_in_bytes()),
1435 char_array, T_OBJECT, value_field_idx);
1437 // hook up the outgoing control and result
1438 kit.replace_call(sc->end(), result);
1440 // Unhook any hook nodes
1441 string_sizes->disconnect_inputs(NULL);
1442 sc->cleanup();
1443 }