src/share/vm/opto/stringopts.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

aoqi@0 1 /*
thartmann@8443 2 * Copyright (c) 2009, 2016, 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());
rraghavan@8777 894 Node* catchproj = cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0);
rraghavan@8777 895 if (catchproj != NULL) {
rraghavan@8777 896 ctrl_path.push(catchproj);
aoqi@0 897 }
aoqi@0 898 } else {
aoqi@0 899 ShouldNotReachHere();
aoqi@0 900 }
aoqi@0 901 }
aoqi@0 902
aoqi@0 903 // Skip backwards through the control checking for unexpected control flow
aoqi@0 904 Node* ptr = _end;
aoqi@0 905 bool fail = false;
aoqi@0 906 while (ptr != _begin) {
aoqi@0 907 if (ptr->is_Call() && ctrl_path.member(ptr)) {
aoqi@0 908 ptr = ptr->in(0);
aoqi@0 909 } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
aoqi@0 910 ptr = ptr->in(0)->in(0)->in(0);
aoqi@0 911 assert(ctrl_path.member(ptr), "should be a known piece of control");
aoqi@0 912 } else if (ptr->is_IfTrue()) {
aoqi@0 913 IfNode* iff = ptr->in(0)->as_If();
aoqi@0 914 BoolNode* b = iff->in(1)->isa_Bool();
aoqi@0 915
aoqi@0 916 if (b == NULL) {
aoqi@0 917 fail = true;
aoqi@0 918 break;
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 Node* cmp = b->in(1);
aoqi@0 922 Node* v1 = cmp->in(1);
aoqi@0 923 Node* v2 = cmp->in(2);
aoqi@0 924 Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con);
aoqi@0 925
aoqi@0 926 // Null check of the return of append which can simply be eliminated
aoqi@0 927 if (b->_test._test == BoolTest::ne &&
aoqi@0 928 v2->bottom_type() == TypePtr::NULL_PTR &&
aoqi@0 929 v1->is_Proj() && ctrl_path.member(v1->in(0))) {
aoqi@0 930 // NULL check of the return value of the append
aoqi@0 931 null_check_count++;
aoqi@0 932 if (otherproj->outcnt() == 1) {
aoqi@0 933 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
aoqi@0 934 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
aoqi@0 935 ctrl_path.push(call);
aoqi@0 936 }
aoqi@0 937 }
aoqi@0 938 _control.push(ptr);
aoqi@0 939 ptr = ptr->in(0)->in(0);
aoqi@0 940 continue;
aoqi@0 941 }
aoqi@0 942
aoqi@0 943 // A test which leads to an uncommon trap which should be safe.
aoqi@0 944 // Later this trap will be converted into a trap that restarts
aoqi@0 945 // at the beginning.
aoqi@0 946 if (otherproj->outcnt() == 1) {
aoqi@0 947 CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
aoqi@0 948 if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
aoqi@0 949 // control flow leads to uct so should be ok
aoqi@0 950 _uncommon_traps.push(call);
aoqi@0 951 ctrl_path.push(call);
aoqi@0 952 ptr = ptr->in(0)->in(0);
aoqi@0 953 continue;
aoqi@0 954 }
aoqi@0 955 }
aoqi@0 956
aoqi@0 957 #ifndef PRODUCT
aoqi@0 958 // Some unexpected control flow we don't know how to handle.
aoqi@0 959 if (PrintOptimizeStringConcat) {
aoqi@0 960 tty->print_cr("failing with unknown test");
aoqi@0 961 b->dump();
aoqi@0 962 cmp->dump();
aoqi@0 963 v1->dump();
aoqi@0 964 v2->dump();
aoqi@0 965 tty->cr();
aoqi@0 966 }
aoqi@0 967 #endif
aoqi@0 968 fail = true;
aoqi@0 969 break;
aoqi@0 970 } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) {
aoqi@0 971 ptr = ptr->in(0)->in(0);
aoqi@0 972 } else if (ptr->is_Region()) {
aoqi@0 973 Node* copy = ptr->as_Region()->is_copy();
aoqi@0 974 if (copy != NULL) {
aoqi@0 975 ptr = copy;
aoqi@0 976 continue;
aoqi@0 977 }
aoqi@0 978 if (ptr->req() == 3 &&
aoqi@0 979 ptr->in(1) != NULL && ptr->in(1)->is_Proj() &&
aoqi@0 980 ptr->in(2) != NULL && ptr->in(2)->is_Proj() &&
aoqi@0 981 ptr->in(1)->in(0) == ptr->in(2)->in(0) &&
aoqi@0 982 ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) {
aoqi@0 983 // Simple diamond.
aoqi@0 984 // XXX should check for possibly merging stores. simple data merges are ok.
aoqi@0 985 // The IGVN will make this simple diamond go away when it
aoqi@0 986 // transforms the Region. Make sure it sees it.
aoqi@0 987 Compile::current()->record_for_igvn(ptr);
aoqi@0 988 ptr = ptr->in(1)->in(0)->in(0);
aoqi@0 989 continue;
aoqi@0 990 }
aoqi@0 991 #ifndef PRODUCT
aoqi@0 992 if (PrintOptimizeStringConcat) {
aoqi@0 993 tty->print_cr("fusion would fail for region");
aoqi@0 994 _begin->dump();
aoqi@0 995 ptr->dump(2);
aoqi@0 996 }
aoqi@0 997 #endif
aoqi@0 998 fail = true;
aoqi@0 999 break;
aoqi@0 1000 } else {
aoqi@0 1001 // other unknown control
aoqi@0 1002 if (!fail) {
aoqi@0 1003 #ifndef PRODUCT
aoqi@0 1004 if (PrintOptimizeStringConcat) {
aoqi@0 1005 tty->print_cr("fusion would fail for");
aoqi@0 1006 _begin->dump();
aoqi@0 1007 }
aoqi@0 1008 #endif
aoqi@0 1009 fail = true;
aoqi@0 1010 }
aoqi@0 1011 #ifndef PRODUCT
aoqi@0 1012 if (PrintOptimizeStringConcat) {
aoqi@0 1013 ptr->dump();
aoqi@0 1014 }
aoqi@0 1015 #endif
aoqi@0 1016 ptr = ptr->in(0);
aoqi@0 1017 }
aoqi@0 1018 }
aoqi@0 1019 #ifndef PRODUCT
aoqi@0 1020 if (PrintOptimizeStringConcat && fail) {
aoqi@0 1021 tty->cr();
aoqi@0 1022 }
aoqi@0 1023 #endif
aoqi@0 1024 if (fail) return !fail;
aoqi@0 1025
aoqi@0 1026 // Validate that all these results produced are contained within
aoqi@0 1027 // this cluster of objects. First collect all the results produced
aoqi@0 1028 // by calls in the region.
aoqi@0 1029 _stringopts->_visited.Clear();
aoqi@0 1030 Node_List worklist;
aoqi@0 1031 Node* final_result = _end->proj_out(TypeFunc::Parms);
aoqi@0 1032 for (uint i = 0; i < _control.size(); i++) {
aoqi@0 1033 CallNode* cnode = _control.at(i)->isa_Call();
aoqi@0 1034 if (cnode != NULL) {
aoqi@0 1035 _stringopts->_visited.test_set(cnode->_idx);
aoqi@0 1036 }
aoqi@0 1037 Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL;
aoqi@0 1038 if (result != NULL && result != final_result) {
aoqi@0 1039 worklist.push(result);
aoqi@0 1040 }
aoqi@0 1041 }
aoqi@0 1042
aoqi@0 1043 Node* last_result = NULL;
aoqi@0 1044 while (worklist.size() > 0) {
aoqi@0 1045 Node* result = worklist.pop();
aoqi@0 1046 if (_stringopts->_visited.test_set(result->_idx))
aoqi@0 1047 continue;
aoqi@0 1048 for (SimpleDUIterator i(result); i.has_next(); i.next()) {
aoqi@0 1049 Node *use = i.get();
aoqi@0 1050 if (ctrl_path.member(use)) {
aoqi@0 1051 // already checked this
aoqi@0 1052 continue;
aoqi@0 1053 }
aoqi@0 1054 int opc = use->Opcode();
aoqi@0 1055 if (opc == Op_CmpP || opc == Op_Node) {
aoqi@0 1056 ctrl_path.push(use);
aoqi@0 1057 continue;
aoqi@0 1058 }
aoqi@0 1059 if (opc == Op_CastPP || opc == Op_CheckCastPP) {
aoqi@0 1060 for (SimpleDUIterator j(use); j.has_next(); j.next()) {
aoqi@0 1061 worklist.push(j.get());
aoqi@0 1062 }
aoqi@0 1063 worklist.push(use->in(1));
aoqi@0 1064 ctrl_path.push(use);
aoqi@0 1065 continue;
aoqi@0 1066 }
aoqi@0 1067 #ifndef PRODUCT
aoqi@0 1068 if (PrintOptimizeStringConcat) {
aoqi@0 1069 if (result != last_result) {
aoqi@0 1070 last_result = result;
aoqi@0 1071 tty->print_cr("extra uses for result:");
aoqi@0 1072 last_result->dump();
aoqi@0 1073 }
aoqi@0 1074 use->dump();
aoqi@0 1075 }
aoqi@0 1076 #endif
aoqi@0 1077 fail = true;
aoqi@0 1078 break;
aoqi@0 1079 }
aoqi@0 1080 }
aoqi@0 1081
aoqi@0 1082 #ifndef PRODUCT
aoqi@0 1083 if (PrintOptimizeStringConcat && !fail) {
aoqi@0 1084 ttyLocker ttyl;
aoqi@0 1085 tty->cr();
aoqi@0 1086 tty->print("fusion has correct control flow (%d %d) for ", null_check_count, _uncommon_traps.size());
aoqi@0 1087 _begin->jvms()->dump_spec(tty); tty->cr();
aoqi@0 1088 for (int i = 0; i < num_arguments(); i++) {
aoqi@0 1089 argument(i)->dump();
aoqi@0 1090 }
aoqi@0 1091 _control.dump();
aoqi@0 1092 tty->cr();
aoqi@0 1093 }
aoqi@0 1094 #endif
aoqi@0 1095
aoqi@0 1096 return !fail;
aoqi@0 1097 }
aoqi@0 1098
aoqi@0 1099 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
aoqi@0 1100 const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror());
aoqi@0 1101 Node* klass_node = __ makecon(mirror_type);
aoqi@0 1102 BasicType bt = field->layout_type();
aoqi@0 1103 ciType* field_klass = field->type();
aoqi@0 1104
aoqi@0 1105 const Type *type;
aoqi@0 1106 if( bt == T_OBJECT ) {
aoqi@0 1107 if (!field->type()->is_loaded()) {
aoqi@0 1108 type = TypeInstPtr::BOTTOM;
aoqi@0 1109 } else if (field->is_constant()) {
aoqi@0 1110 // This can happen if the constant oop is non-perm.
aoqi@0 1111 ciObject* con = field->constant_value().as_object();
aoqi@0 1112 // Do not "join" in the previous type; it doesn't add value,
aoqi@0 1113 // and may yield a vacuous result if the field is of interface type.
aoqi@0 1114 type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr();
aoqi@0 1115 assert(type != NULL, "field singleton type must be consistent");
aoqi@0 1116 return __ makecon(type);
aoqi@0 1117 } else {
aoqi@0 1118 type = TypeOopPtr::make_from_klass(field_klass->as_klass());
aoqi@0 1119 }
aoqi@0 1120 } else {
aoqi@0 1121 type = Type::get_const_basic_type(bt);
aoqi@0 1122 }
aoqi@0 1123
aoqi@0 1124 return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
aoqi@0 1125 type, T_OBJECT,
aoqi@0 1126 C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())),
aoqi@0 1127 MemNode::unordered);
aoqi@0 1128 }
aoqi@0 1129
aoqi@0 1130 Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
aoqi@0 1131 RegionNode *final_merge = new (C) RegionNode(3);
aoqi@0 1132 kit.gvn().set_type(final_merge, Type::CONTROL);
aoqi@0 1133 Node* final_size = new (C) PhiNode(final_merge, TypeInt::INT);
aoqi@0 1134 kit.gvn().set_type(final_size, TypeInt::INT);
aoqi@0 1135
aoqi@0 1136 IfNode* iff = kit.create_and_map_if(kit.control(),
aoqi@0 1137 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
aoqi@0 1138 PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1139 Node* is_min = __ IfFalse(iff);
aoqi@0 1140 final_merge->init_req(1, is_min);
aoqi@0 1141 final_size->init_req(1, __ intcon(11));
aoqi@0 1142
aoqi@0 1143 kit.set_control(__ IfTrue(iff));
aoqi@0 1144 if (kit.stopped()) {
aoqi@0 1145 final_merge->init_req(2, C->top());
aoqi@0 1146 final_size->init_req(2, C->top());
aoqi@0 1147 } else {
aoqi@0 1148
aoqi@0 1149 // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
aoqi@0 1150 RegionNode *r = new (C) RegionNode(3);
aoqi@0 1151 kit.gvn().set_type(r, Type::CONTROL);
aoqi@0 1152 Node *phi = new (C) PhiNode(r, TypeInt::INT);
aoqi@0 1153 kit.gvn().set_type(phi, TypeInt::INT);
aoqi@0 1154 Node *size = new (C) PhiNode(r, TypeInt::INT);
aoqi@0 1155 kit.gvn().set_type(size, TypeInt::INT);
aoqi@0 1156 Node* chk = __ CmpI(arg, __ intcon(0));
aoqi@0 1157 Node* p = __ Bool(chk, BoolTest::lt);
aoqi@0 1158 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1159 Node* lessthan = __ IfTrue(iff);
aoqi@0 1160 Node* greaterequal = __ IfFalse(iff);
aoqi@0 1161 r->init_req(1, lessthan);
aoqi@0 1162 phi->init_req(1, __ SubI(__ intcon(0), arg));
aoqi@0 1163 size->init_req(1, __ intcon(1));
aoqi@0 1164 r->init_req(2, greaterequal);
aoqi@0 1165 phi->init_req(2, arg);
aoqi@0 1166 size->init_req(2, __ intcon(0));
aoqi@0 1167 kit.set_control(r);
aoqi@0 1168 C->record_for_igvn(r);
aoqi@0 1169 C->record_for_igvn(phi);
aoqi@0 1170 C->record_for_igvn(size);
aoqi@0 1171
aoqi@0 1172 // for (int i=0; ; i++)
aoqi@0 1173 // if (x <= sizeTable[i])
aoqi@0 1174 // return i+1;
aoqi@0 1175
aoqi@0 1176 // Add loop predicate first.
aoqi@0 1177 kit.add_predicate();
aoqi@0 1178
aoqi@0 1179 RegionNode *loop = new (C) RegionNode(3);
aoqi@0 1180 loop->init_req(1, kit.control());
aoqi@0 1181 kit.gvn().set_type(loop, Type::CONTROL);
aoqi@0 1182
aoqi@0 1183 Node *index = new (C) PhiNode(loop, TypeInt::INT);
aoqi@0 1184 index->init_req(1, __ intcon(0));
aoqi@0 1185 kit.gvn().set_type(index, TypeInt::INT);
aoqi@0 1186 kit.set_control(loop);
aoqi@0 1187 Node* sizeTable = fetch_static_field(kit, size_table_field);
aoqi@0 1188
aoqi@0 1189 Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS);
aoqi@0 1190 C->record_for_igvn(value);
aoqi@0 1191 Node* limit = __ CmpI(phi, value);
aoqi@0 1192 Node* limitb = __ Bool(limit, BoolTest::le);
aoqi@0 1193 IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1194 Node* lessEqual = __ IfTrue(iff2);
aoqi@0 1195 Node* greater = __ IfFalse(iff2);
aoqi@0 1196
aoqi@0 1197 loop->init_req(2, greater);
aoqi@0 1198 index->init_req(2, __ AddI(index, __ intcon(1)));
aoqi@0 1199
aoqi@0 1200 kit.set_control(lessEqual);
aoqi@0 1201 C->record_for_igvn(loop);
aoqi@0 1202 C->record_for_igvn(index);
aoqi@0 1203
aoqi@0 1204 final_merge->init_req(2, kit.control());
aoqi@0 1205 final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
aoqi@0 1206 }
aoqi@0 1207
aoqi@0 1208 kit.set_control(final_merge);
aoqi@0 1209 C->record_for_igvn(final_merge);
aoqi@0 1210 C->record_for_igvn(final_size);
aoqi@0 1211
aoqi@0 1212 return final_size;
aoqi@0 1213 }
aoqi@0 1214
aoqi@0 1215 void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) {
aoqi@0 1216 RegionNode *final_merge = new (C) RegionNode(4);
aoqi@0 1217 kit.gvn().set_type(final_merge, Type::CONTROL);
aoqi@0 1218 Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
aoqi@0 1219 kit.gvn().set_type(final_mem, Type::MEMORY);
aoqi@0 1220
aoqi@0 1221 // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive
aoqi@0 1222 {
aoqi@0 1223 // i == MIN_VALUE
aoqi@0 1224 IfNode* iff = kit.create_and_map_if(kit.control(),
aoqi@0 1225 __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
aoqi@0 1226 PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1227
aoqi@0 1228 Node* old_mem = kit.memory(char_adr_idx);
aoqi@0 1229
aoqi@0 1230 kit.set_control(__ IfFalse(iff));
aoqi@0 1231 if (kit.stopped()) {
aoqi@0 1232 // Statically not equal to MIN_VALUE so this path is dead
aoqi@0 1233 final_merge->init_req(3, kit.control());
aoqi@0 1234 } else {
aoqi@0 1235 copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
aoqi@0 1236 char_array, start);
aoqi@0 1237 final_merge->init_req(3, kit.control());
aoqi@0 1238 final_mem->init_req(3, kit.memory(char_adr_idx));
aoqi@0 1239 }
aoqi@0 1240
aoqi@0 1241 kit.set_control(__ IfTrue(iff));
aoqi@0 1242 kit.set_memory(old_mem, char_adr_idx);
aoqi@0 1243 }
aoqi@0 1244
aoqi@0 1245
aoqi@0 1246 // Simplified version of Integer.getChars
aoqi@0 1247
aoqi@0 1248 // int q, r;
aoqi@0 1249 // int charPos = index;
aoqi@0 1250 Node* charPos = end;
aoqi@0 1251
aoqi@0 1252 // char sign = 0;
aoqi@0 1253
aoqi@0 1254 Node* i = arg;
aoqi@0 1255 Node* sign = __ intcon(0);
aoqi@0 1256
aoqi@0 1257 // if (i < 0) {
aoqi@0 1258 // sign = '-';
aoqi@0 1259 // i = -i;
aoqi@0 1260 // }
aoqi@0 1261 {
aoqi@0 1262 IfNode* iff = kit.create_and_map_if(kit.control(),
aoqi@0 1263 __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
aoqi@0 1264 PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1265
aoqi@0 1266 RegionNode *merge = new (C) RegionNode(3);
aoqi@0 1267 kit.gvn().set_type(merge, Type::CONTROL);
aoqi@0 1268 i = new (C) PhiNode(merge, TypeInt::INT);
aoqi@0 1269 kit.gvn().set_type(i, TypeInt::INT);
aoqi@0 1270 sign = new (C) PhiNode(merge, TypeInt::INT);
aoqi@0 1271 kit.gvn().set_type(sign, TypeInt::INT);
aoqi@0 1272
aoqi@0 1273 merge->init_req(1, __ IfTrue(iff));
aoqi@0 1274 i->init_req(1, __ SubI(__ intcon(0), arg));
aoqi@0 1275 sign->init_req(1, __ intcon('-'));
aoqi@0 1276 merge->init_req(2, __ IfFalse(iff));
aoqi@0 1277 i->init_req(2, arg);
aoqi@0 1278 sign->init_req(2, __ intcon(0));
aoqi@0 1279
aoqi@0 1280 kit.set_control(merge);
aoqi@0 1281
aoqi@0 1282 C->record_for_igvn(merge);
aoqi@0 1283 C->record_for_igvn(i);
aoqi@0 1284 C->record_for_igvn(sign);
aoqi@0 1285 }
aoqi@0 1286
aoqi@0 1287 // for (;;) {
aoqi@0 1288 // q = i / 10;
aoqi@0 1289 // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
aoqi@0 1290 // buf [--charPos] = digits [r];
aoqi@0 1291 // i = q;
aoqi@0 1292 // if (i == 0) break;
aoqi@0 1293 // }
aoqi@0 1294
aoqi@0 1295 {
aoqi@0 1296 // Add loop predicate first.
aoqi@0 1297 kit.add_predicate();
aoqi@0 1298
aoqi@0 1299 RegionNode *head = new (C) RegionNode(3);
aoqi@0 1300 head->init_req(1, kit.control());
aoqi@0 1301 kit.gvn().set_type(head, Type::CONTROL);
aoqi@0 1302 Node *i_phi = new (C) PhiNode(head, TypeInt::INT);
aoqi@0 1303 i_phi->init_req(1, i);
aoqi@0 1304 kit.gvn().set_type(i_phi, TypeInt::INT);
aoqi@0 1305 charPos = PhiNode::make(head, charPos);
aoqi@0 1306 kit.gvn().set_type(charPos, TypeInt::INT);
aoqi@0 1307 Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
aoqi@0 1308 kit.gvn().set_type(mem, Type::MEMORY);
aoqi@0 1309 kit.set_control(head);
aoqi@0 1310 kit.set_memory(mem, char_adr_idx);
aoqi@0 1311
aoqi@0 1312 Node* q = __ DivI(NULL, i_phi, __ intcon(10));
aoqi@0 1313 Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
aoqi@0 1314 __ LShiftI(q, __ intcon(1))));
aoqi@0 1315 Node* m1 = __ SubI(charPos, __ intcon(1));
aoqi@0 1316 Node* ch = __ AddI(r, __ intcon('0'));
aoqi@0 1317
aoqi@0 1318 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
aoqi@0 1319 ch, T_CHAR, char_adr_idx, MemNode::unordered);
aoqi@0 1320
aoqi@0 1321
aoqi@0 1322 IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
aoqi@0 1323 PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1324 Node* ne = __ IfTrue(iff);
aoqi@0 1325 Node* eq = __ IfFalse(iff);
aoqi@0 1326
aoqi@0 1327 head->init_req(2, ne);
aoqi@0 1328 mem->init_req(2, st);
aoqi@0 1329 i_phi->init_req(2, q);
aoqi@0 1330 charPos->init_req(2, m1);
aoqi@0 1331
aoqi@0 1332 charPos = m1;
aoqi@0 1333
aoqi@0 1334 kit.set_control(eq);
aoqi@0 1335 kit.set_memory(st, char_adr_idx);
aoqi@0 1336
aoqi@0 1337 C->record_for_igvn(head);
aoqi@0 1338 C->record_for_igvn(mem);
aoqi@0 1339 C->record_for_igvn(i_phi);
aoqi@0 1340 C->record_for_igvn(charPos);
aoqi@0 1341 }
aoqi@0 1342
aoqi@0 1343 {
aoqi@0 1344 // if (sign != 0) {
aoqi@0 1345 // buf [--charPos] = sign;
aoqi@0 1346 // }
aoqi@0 1347 IfNode* iff = kit.create_and_map_if(kit.control(),
aoqi@0 1348 __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
aoqi@0 1349 PROB_FAIR, COUNT_UNKNOWN);
aoqi@0 1350
aoqi@0 1351 final_merge->init_req(2, __ IfFalse(iff));
aoqi@0 1352 final_mem->init_req(2, kit.memory(char_adr_idx));
aoqi@0 1353
aoqi@0 1354 kit.set_control(__ IfTrue(iff));
aoqi@0 1355 if (kit.stopped()) {
aoqi@0 1356 final_merge->init_req(1, C->top());
aoqi@0 1357 final_mem->init_req(1, C->top());
aoqi@0 1358 } else {
aoqi@0 1359 Node* m1 = __ SubI(charPos, __ intcon(1));
aoqi@0 1360 Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
aoqi@0 1361 sign, T_CHAR, char_adr_idx, MemNode::unordered);
aoqi@0 1362
aoqi@0 1363 final_merge->init_req(1, kit.control());
aoqi@0 1364 final_mem->init_req(1, st);
aoqi@0 1365 }
aoqi@0 1366
aoqi@0 1367 kit.set_control(final_merge);
aoqi@0 1368 kit.set_memory(final_mem, char_adr_idx);
aoqi@0 1369
aoqi@0 1370 C->record_for_igvn(final_merge);
aoqi@0 1371 C->record_for_igvn(final_mem);
aoqi@0 1372 }
aoqi@0 1373 }
aoqi@0 1374
aoqi@0 1375
aoqi@0 1376 Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) {
aoqi@0 1377 Node* string = str;
aoqi@0 1378 Node* offset = kit.load_String_offset(kit.control(), string);
aoqi@0 1379 Node* count = kit.load_String_length(kit.control(), string);
aoqi@0 1380 Node* value = kit.load_String_value (kit.control(), string);
aoqi@0 1381
aoqi@0 1382 // copy the contents
aoqi@0 1383 if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) {
aoqi@0 1384 // For small constant strings just emit individual stores.
aoqi@0 1385 // A length of 6 seems like a good space/speed tradeof.
aoqi@0 1386 int c = count->get_int();
aoqi@0 1387 int o = offset->get_int();
aoqi@0 1388 const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr();
aoqi@0 1389 ciTypeArray* value_array = t->const_oop()->as_type_array();
aoqi@0 1390 for (int e = 0; e < c; e++) {
aoqi@0 1391 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
aoqi@0 1392 __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx,
aoqi@0 1393 MemNode::unordered);
aoqi@0 1394 start = __ AddI(start, __ intcon(1));
aoqi@0 1395 }
aoqi@0 1396 } else {
aoqi@0 1397 Node* src_ptr = kit.array_element_address(value, offset, T_CHAR);
aoqi@0 1398 Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR);
aoqi@0 1399 Node* c = count;
aoqi@0 1400 Node* extra = NULL;
aoqi@0 1401 #ifdef _LP64
aoqi@0 1402 c = __ ConvI2L(c);
aoqi@0 1403 extra = C->top();
aoqi@0 1404 #endif
aoqi@0 1405 Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP,
aoqi@0 1406 OptoRuntime::fast_arraycopy_Type(),
aoqi@0 1407 CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()),
aoqi@0 1408 "jshort_disjoint_arraycopy", TypeAryPtr::CHARS,
aoqi@0 1409 src_ptr, dst_ptr, c, extra);
aoqi@0 1410 start = __ AddI(start, count);
aoqi@0 1411 }
aoqi@0 1412 return start;
aoqi@0 1413 }
aoqi@0 1414
aoqi@0 1415
aoqi@0 1416 void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
aoqi@0 1417 // Log a little info about the transformation
aoqi@0 1418 sc->maybe_log_transform();
aoqi@0 1419
aoqi@0 1420 // pull the JVMState of the allocation into a SafePointNode to serve as
aoqi@0 1421 // as a shim for the insertion of the new code.
aoqi@0 1422 JVMState* jvms = sc->begin()->jvms()->clone_shallow(C);
aoqi@0 1423 uint size = sc->begin()->req();
aoqi@0 1424 SafePointNode* map = new (C) SafePointNode(size, jvms);
aoqi@0 1425
aoqi@0 1426 // copy the control and memory state from the final call into our
aoqi@0 1427 // new starting state. This allows any preceeding tests to feed
aoqi@0 1428 // into the new section of code.
aoqi@0 1429 for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
aoqi@0 1430 map->init_req(i1, sc->end()->in(i1));
aoqi@0 1431 }
aoqi@0 1432 // blow away old allocation arguments
aoqi@0 1433 for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
aoqi@0 1434 map->init_req(i1, C->top());
aoqi@0 1435 }
aoqi@0 1436 // Copy the rest of the inputs for the JVMState
aoqi@0 1437 for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
aoqi@0 1438 map->init_req(i1, sc->begin()->in(i1));
aoqi@0 1439 }
aoqi@0 1440 // Make sure the memory state is a MergeMem for parsing.
aoqi@0 1441 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
aoqi@0 1442 map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory)));
aoqi@0 1443 }
aoqi@0 1444
aoqi@0 1445 jvms->set_map(map);
aoqi@0 1446 map->ensure_stack(jvms, jvms->method()->max_stack());
aoqi@0 1447
aoqi@0 1448
aoqi@0 1449 // disconnect all the old StringBuilder calls from the graph
aoqi@0 1450 sc->eliminate_unneeded_control();
aoqi@0 1451
aoqi@0 1452 // At this point all the old work has been completely removed from
aoqi@0 1453 // the graph and the saved JVMState exists at the point where the
aoqi@0 1454 // final toString call used to be.
aoqi@0 1455 GraphKit kit(jvms);
aoqi@0 1456
aoqi@0 1457 // There may be uncommon traps which are still using the
aoqi@0 1458 // intermediate states and these need to be rewritten to point at
aoqi@0 1459 // the JVMState at the beginning of the transformation.
aoqi@0 1460 sc->convert_uncommon_traps(kit, jvms);
aoqi@0 1461
aoqi@0 1462 // Now insert the logic to compute the size of the string followed
aoqi@0 1463 // by all the logic to construct array and resulting string.
aoqi@0 1464
aoqi@0 1465 Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
aoqi@0 1466
aoqi@0 1467 // Create a region for the overflow checks to merge into.
aoqi@0 1468 int args = MAX2(sc->num_arguments(), 1);
aoqi@0 1469 RegionNode* overflow = new (C) RegionNode(args);
aoqi@0 1470 kit.gvn().set_type(overflow, Type::CONTROL);
aoqi@0 1471
aoqi@0 1472 // Create a hook node to hold onto the individual sizes since they
aoqi@0 1473 // are need for the copying phase.
aoqi@0 1474 Node* string_sizes = new (C) Node(args);
aoqi@0 1475
aoqi@0 1476 Node* length = __ intcon(0);
aoqi@0 1477 for (int argi = 0; argi < sc->num_arguments(); argi++) {
aoqi@0 1478 Node* arg = sc->argument(argi);
aoqi@0 1479 switch (sc->mode(argi)) {
aoqi@0 1480 case StringConcat::IntMode: {
aoqi@0 1481 Node* string_size = int_stringSize(kit, arg);
aoqi@0 1482
aoqi@0 1483 // accumulate total
aoqi@0 1484 length = __ AddI(length, string_size);
aoqi@0 1485
aoqi@0 1486 // Cache this value for the use by int_toString
aoqi@0 1487 string_sizes->init_req(argi, string_size);
aoqi@0 1488 break;
aoqi@0 1489 }
aoqi@0 1490 case StringConcat::StringNullCheckMode: {
aoqi@0 1491 const Type* type = kit.gvn().type(arg);
aoqi@0 1492 assert(type != TypePtr::NULL_PTR, "missing check");
aoqi@0 1493 if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
aoqi@0 1494 // Null check with uncommont trap since
aoqi@0 1495 // StringBuilder(null) throws exception.
aoqi@0 1496 // Use special uncommon trap instead of
aoqi@0 1497 // calling normal do_null_check().
aoqi@0 1498 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
aoqi@0 1499 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1500 overflow->add_req(__ IfFalse(iff));
aoqi@0 1501 Node* notnull = __ IfTrue(iff);
aoqi@0 1502 kit.set_control(notnull); // set control for the cast_not_null
aoqi@0 1503 arg = kit.cast_not_null(arg, false);
aoqi@0 1504 sc->set_argument(argi, arg);
aoqi@0 1505 }
aoqi@0 1506 assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
aoqi@0 1507 // Fallthrough to add string length.
aoqi@0 1508 }
aoqi@0 1509 case StringConcat::StringMode: {
aoqi@0 1510 const Type* type = kit.gvn().type(arg);
asiebenborn@7561 1511 Node* count = NULL;
aoqi@0 1512 if (type == TypePtr::NULL_PTR) {
aoqi@0 1513 // replace the argument with the null checked version
aoqi@0 1514 arg = null_string;
aoqi@0 1515 sc->set_argument(argi, arg);
asiebenborn@7561 1516 count = kit.load_String_length(kit.control(), arg);
aoqi@0 1517 } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
aoqi@0 1518 // s = s != null ? s : "null";
aoqi@0 1519 // length = length + (s.count - s.offset);
aoqi@0 1520 RegionNode *r = new (C) RegionNode(3);
aoqi@0 1521 kit.gvn().set_type(r, Type::CONTROL);
aoqi@0 1522 Node *phi = new (C) PhiNode(r, type);
aoqi@0 1523 kit.gvn().set_type(phi, phi->bottom_type());
aoqi@0 1524 Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
aoqi@0 1525 IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1526 Node* notnull = __ IfTrue(iff);
aoqi@0 1527 Node* isnull = __ IfFalse(iff);
aoqi@0 1528 kit.set_control(notnull); // set control for the cast_not_null
aoqi@0 1529 r->init_req(1, notnull);
aoqi@0 1530 phi->init_req(1, kit.cast_not_null(arg, false));
aoqi@0 1531 r->init_req(2, isnull);
aoqi@0 1532 phi->init_req(2, null_string);
aoqi@0 1533 kit.set_control(r);
aoqi@0 1534 C->record_for_igvn(r);
aoqi@0 1535 C->record_for_igvn(phi);
aoqi@0 1536 // replace the argument with the null checked version
aoqi@0 1537 arg = phi;
aoqi@0 1538 sc->set_argument(argi, arg);
asiebenborn@7561 1539 count = kit.load_String_length(kit.control(), arg);
asiebenborn@7561 1540 } else {
asiebenborn@7561 1541 // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP
asiebenborn@7561 1542 // kit.control might be a different test, that can be hoisted above the actual nullcheck
asiebenborn@7561 1543 // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck.
asiebenborn@7561 1544 count = kit.load_String_length(NULL, arg);
aoqi@0 1545 }
aoqi@0 1546 length = __ AddI(length, count);
aoqi@0 1547 string_sizes->init_req(argi, NULL);
aoqi@0 1548 break;
aoqi@0 1549 }
aoqi@0 1550 case StringConcat::CharMode: {
aoqi@0 1551 // one character only
aoqi@0 1552 length = __ AddI(length, __ intcon(1));
aoqi@0 1553 break;
aoqi@0 1554 }
aoqi@0 1555 default:
aoqi@0 1556 ShouldNotReachHere();
aoqi@0 1557 }
aoqi@0 1558 if (argi > 0) {
aoqi@0 1559 // Check that the sum hasn't overflowed
aoqi@0 1560 IfNode* iff = kit.create_and_map_if(kit.control(),
aoqi@0 1561 __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
aoqi@0 1562 PROB_MIN, COUNT_UNKNOWN);
aoqi@0 1563 kit.set_control(__ IfFalse(iff));
aoqi@0 1564 overflow->set_req(argi, __ IfTrue(iff));
aoqi@0 1565 }
aoqi@0 1566 }
aoqi@0 1567
aoqi@0 1568 {
aoqi@0 1569 // Hook
aoqi@0 1570 PreserveJVMState pjvms(&kit);
aoqi@0 1571 kit.set_control(overflow);
aoqi@0 1572 C->record_for_igvn(overflow);
aoqi@0 1573 kit.uncommon_trap(Deoptimization::Reason_intrinsic,
aoqi@0 1574 Deoptimization::Action_make_not_entrant);
aoqi@0 1575 }
aoqi@0 1576
aoqi@0 1577 Node* result;
aoqi@0 1578 if (!kit.stopped()) {
aoqi@0 1579
aoqi@0 1580 // length now contains the number of characters needed for the
aoqi@0 1581 // char[] so create a new AllocateArray for the char[]
aoqi@0 1582 Node* char_array = NULL;
aoqi@0 1583 {
aoqi@0 1584 PreserveReexecuteState preexecs(&kit);
aoqi@0 1585 // The original jvms is for an allocation of either a String or
aoqi@0 1586 // StringBuffer so no stack adjustment is necessary for proper
aoqi@0 1587 // reexecution. If we deoptimize in the slow path the bytecode
aoqi@0 1588 // will be reexecuted and the char[] allocation will be thrown away.
aoqi@0 1589 kit.jvms()->set_should_reexecute(true);
aoqi@0 1590 char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))),
aoqi@0 1591 length, 1);
aoqi@0 1592 }
aoqi@0 1593
aoqi@0 1594 // Mark the allocation so that zeroing is skipped since the code
aoqi@0 1595 // below will overwrite the entire array
aoqi@0 1596 AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn);
aoqi@0 1597 char_alloc->maybe_set_complete(_gvn);
aoqi@0 1598
aoqi@0 1599 // Now copy the string representations into the final char[]
aoqi@0 1600 Node* start = __ intcon(0);
aoqi@0 1601 for (int argi = 0; argi < sc->num_arguments(); argi++) {
aoqi@0 1602 Node* arg = sc->argument(argi);
aoqi@0 1603 switch (sc->mode(argi)) {
aoqi@0 1604 case StringConcat::IntMode: {
aoqi@0 1605 Node* end = __ AddI(start, string_sizes->in(argi));
aoqi@0 1606 // getChars words backwards so pass the ending point as well as the start
aoqi@0 1607 int_getChars(kit, arg, char_array, start, end);
aoqi@0 1608 start = end;
aoqi@0 1609 break;
aoqi@0 1610 }
aoqi@0 1611 case StringConcat::StringNullCheckMode:
aoqi@0 1612 case StringConcat::StringMode: {
aoqi@0 1613 start = copy_string(kit, arg, char_array, start);
aoqi@0 1614 break;
aoqi@0 1615 }
aoqi@0 1616 case StringConcat::CharMode: {
aoqi@0 1617 __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
aoqi@0 1618 arg, T_CHAR, char_adr_idx, MemNode::unordered);
aoqi@0 1619 start = __ AddI(start, __ intcon(1));
aoqi@0 1620 break;
aoqi@0 1621 }
aoqi@0 1622 default:
aoqi@0 1623 ShouldNotReachHere();
aoqi@0 1624 }
aoqi@0 1625 }
aoqi@0 1626
aoqi@0 1627 // If we're not reusing an existing String allocation then allocate one here.
aoqi@0 1628 result = sc->string_alloc();
aoqi@0 1629 if (result == NULL) {
aoqi@0 1630 PreserveReexecuteState preexecs(&kit);
aoqi@0 1631 // The original jvms is for an allocation of either a String or
aoqi@0 1632 // StringBuffer so no stack adjustment is necessary for proper
aoqi@0 1633 // reexecution.
aoqi@0 1634 kit.jvms()->set_should_reexecute(true);
aoqi@0 1635 result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
aoqi@0 1636 }
aoqi@0 1637
aoqi@0 1638 // Intialize the string
aoqi@0 1639 if (java_lang_String::has_offset_field()) {
aoqi@0 1640 kit.store_String_offset(kit.control(), result, __ intcon(0));
aoqi@0 1641 kit.store_String_length(kit.control(), result, length);
aoqi@0 1642 }
aoqi@0 1643 kit.store_String_value(kit.control(), result, char_array);
thartmann@8443 1644
thartmann@8450 1645 // The value field is final. Emit a barrier here to ensure that the effect
thartmann@8450 1646 // of the initialization is committed to memory before any code publishes
thartmann@8450 1647 // a reference to the newly constructed object (see Parse::do_exits()).
thartmann@8450 1648 assert(AllocateNode::Ideal_allocation(result, _gvn) != NULL, "should be newly allocated");
thartmann@8450 1649 kit.insert_mem_bar(Op_MemBarRelease, result);
aoqi@0 1650 } else {
aoqi@0 1651 result = C->top();
aoqi@0 1652 }
aoqi@0 1653 // hook up the outgoing control and result
aoqi@0 1654 kit.replace_call(sc->end(), result);
aoqi@0 1655
aoqi@0 1656 // Unhook any hook nodes
aoqi@0 1657 string_sizes->disconnect_inputs(NULL, C);
aoqi@0 1658 sc->cleanup();
aoqi@0 1659 }

mercurial