annotate src/share/vm/opto/stringopts.cpp @ 7535:dae2d83e0ec2

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