src/share/vm/opto/stringopts.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial