annotate src/share/vm/opto/stringopts.cpp @ 13549:5ab7a67bc155

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