1080N/A/*
3726N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
1080N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1080N/A *
1080N/A * This code is free software; you can redistribute it and/or modify it
1080N/A * under the terms of the GNU General Public License version 2 only, as
1080N/A * published by the Free Software Foundation.
1080N/A *
1080N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1080N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1080N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1080N/A * version 2 for more details (a copy is included in the LICENSE file that
1080N/A * accompanied this code).
1080N/A *
1080N/A * You should have received a copy of the GNU General Public License version
1080N/A * 2 along with this work; if not, write to the Free Software Foundation,
1080N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1080N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
1080N/A *
1080N/A */
1080N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "compiler/compileLog.hpp"
1879N/A#include "opto/addnode.hpp"
1879N/A#include "opto/callGenerator.hpp"
1879N/A#include "opto/callnode.hpp"
1879N/A#include "opto/divnode.hpp"
1879N/A#include "opto/graphKit.hpp"
1879N/A#include "opto/idealKit.hpp"
1879N/A#include "opto/rootnode.hpp"
1879N/A#include "opto/runtime.hpp"
1879N/A#include "opto/stringopts.hpp"
1879N/A#include "opto/subnode.hpp"
1080N/A
1080N/A#define __ kit.
1080N/A
1080N/Aclass StringConcat : public ResourceObj {
1080N/A private:
1080N/A PhaseStringOpts* _stringopts;
1080N/A Node* _string_alloc;
1080N/A AllocateNode* _begin; // The allocation the begins the pattern
1080N/A CallStaticJavaNode* _end; // The final call of the pattern. Will either be
1080N/A // SB.toString or or String.<init>(SB.toString)
1080N/A bool _multiple; // indicates this is a fusion of two or more
1080N/A // separate StringBuilders
1080N/A
1080N/A Node* _arguments; // The list of arguments to be concatenated
1080N/A GrowableArray<int> _mode; // into a String along with a mode flag
1080N/A // indicating how to treat the value.
1080N/A
1080N/A Node_List _control; // List of control nodes that will be deleted
1080N/A Node_List _uncommon_traps; // Uncommon traps that needs to be rewritten
1080N/A // to restart at the initial JVMState.
1080N/A public:
1080N/A // Mode for converting arguments to Strings
1080N/A enum {
1080N/A StringMode,
1080N/A IntMode,
1978N/A CharMode,
1978N/A StringNullCheckMode
1080N/A };
1080N/A
1080N/A StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end):
1080N/A _end(end),
1080N/A _begin(NULL),
1080N/A _multiple(false),
1080N/A _string_alloc(NULL),
1080N/A _stringopts(stringopts) {
4022N/A _arguments = new (_stringopts->C) Node(1);
1080N/A _arguments->del_req(0);
1080N/A }
1080N/A
1080N/A bool validate_control_flow();
1080N/A
1080N/A void merge_add() {
1080N/A#if 0
1080N/A // XXX This is place holder code for reusing an existing String
1080N/A // allocation but the logic for checking the state safety is
1080N/A // probably inadequate at the moment.
1080N/A CallProjections endprojs;
1080N/A sc->end()->extract_projections(&endprojs, false);
1080N/A if (endprojs.resproj != NULL) {
1080N/A for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) {
1080N/A CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
1080N/A if (use != NULL && use->method() != NULL &&
1737N/A use->method()->intrinsic_id() == vmIntrinsics::_String_String &&
1080N/A use->in(TypeFunc::Parms + 1) == endprojs.resproj) {
1080N/A // Found useless new String(sb.toString()) so reuse the newly allocated String
1080N/A // when creating the result instead of allocating a new one.
1080N/A sc->set_string_alloc(use->in(TypeFunc::Parms));
1080N/A sc->set_end(use);
1080N/A }
1080N/A }
1080N/A }
1080N/A#endif
1080N/A }
1080N/A
1080N/A StringConcat* merge(StringConcat* other, Node* arg);
1080N/A
1080N/A void set_allocation(AllocateNode* alloc) {
1080N/A _begin = alloc;
1080N/A }
1080N/A
1080N/A void append(Node* value, int mode) {
1080N/A _arguments->add_req(value);
1080N/A _mode.append(mode);
1080N/A }
1080N/A void push(Node* value, int mode) {
1080N/A _arguments->ins_req(0, value);
1080N/A _mode.insert_before(0, mode);
1080N/A }
3852N/A
1080N/A void push_string(Node* value) {
1080N/A push(value, StringMode);
1080N/A }
1978N/A void push_string_null_check(Node* value) {
1978N/A push(value, StringNullCheckMode);
1978N/A }
1080N/A void push_int(Node* value) {
1080N/A push(value, IntMode);
1080N/A }
1080N/A void push_char(Node* value) {
1080N/A push(value, CharMode);
1080N/A }
1080N/A
3852N/A static bool is_SB_toString(Node* call) {
3852N/A if (call->is_CallStaticJava()) {
3852N/A CallStaticJavaNode* csj = call->as_CallStaticJava();
3852N/A ciMethod* m = csj->method();
3852N/A if (m != NULL &&
3852N/A (m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString ||
3852N/A m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) {
3852N/A return true;
3852N/A }
3852N/A }
3852N/A return false;
3852N/A }
3852N/A
3852N/A static Node* skip_string_null_check(Node* value) {
3852N/A // Look for a diamond shaped Null check of toString() result
3852N/A // (could be code from String.valueOf()):
3852N/A // (Proj == NULL) ? "null":"CastPP(Proj)#NotNULL
3852N/A if (value->is_Phi()) {
3852N/A int true_path = value->as_Phi()->is_diamond_phi();
3852N/A if (true_path != 0) {
3852N/A // phi->region->if_proj->ifnode->bool
3852N/A BoolNode* b = value->in(0)->in(1)->in(0)->in(1)->as_Bool();
3852N/A Node* cmp = b->in(1);
3852N/A Node* v1 = cmp->in(1);
3852N/A Node* v2 = cmp->in(2);
3852N/A // Null check of the return of toString which can simply be skipped.
3852N/A if (b->_test._test == BoolTest::ne &&
3852N/A v2->bottom_type() == TypePtr::NULL_PTR &&
3852N/A value->in(true_path)->Opcode() == Op_CastPP &&
3852N/A value->in(true_path)->in(1) == v1 &&
3852N/A v1->is_Proj() && is_SB_toString(v1->in(0))) {
3852N/A return v1;
3852N/A }
3852N/A }
3852N/A }
3852N/A return value;
3852N/A }
3852N/A
1080N/A Node* argument(int i) {
1080N/A return _arguments->in(i);
1080N/A }
3852N/A Node* argument_uncast(int i) {
3852N/A Node* arg = argument(i);
3852N/A int amode = mode(i);
3852N/A if (amode == StringConcat::StringMode ||
3852N/A amode == StringConcat::StringNullCheckMode) {
3852N/A arg = skip_string_null_check(arg);
3852N/A }
3852N/A return arg;
3852N/A }
1080N/A void set_argument(int i, Node* value) {
1080N/A _arguments->set_req(i, value);
1080N/A }
1080N/A int num_arguments() {
1080N/A return _mode.length();
1080N/A }
1080N/A int mode(int i) {
1080N/A return _mode.at(i);
1080N/A }
1080N/A void add_control(Node* ctrl) {
1080N/A assert(!_control.contains(ctrl), "only push once");
1080N/A _control.push(ctrl);
1080N/A }
1080N/A CallStaticJavaNode* end() { return _end; }
1080N/A AllocateNode* begin() { return _begin; }
1080N/A Node* string_alloc() { return _string_alloc; }
1080N/A
1080N/A void eliminate_unneeded_control();
1080N/A void eliminate_initialize(InitializeNode* init);
1080N/A void eliminate_call(CallNode* call);
1080N/A
1080N/A void maybe_log_transform() {
1080N/A CompileLog* log = _stringopts->C->log();
1080N/A if (log != NULL) {
1080N/A log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'",
1080N/A num_arguments(),
1080N/A _string_alloc != NULL,
1080N/A _multiple);
1080N/A JVMState* p = _begin->jvms();
1080N/A while (p != NULL) {
1080N/A log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1080N/A p = p->caller();
1080N/A }
1080N/A log->tail("replace_string_concat");
1080N/A }
1080N/A }
1080N/A
1080N/A void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) {
1080N/A for (uint u = 0; u < _uncommon_traps.size(); u++) {
1080N/A Node* uct = _uncommon_traps.at(u);
1080N/A
1080N/A // Build a new call using the jvms state of the allocate
1668N/A address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();
1080N/A const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type();
1080N/A const TypePtr* no_memory_effects = NULL;
1080N/A Compile* C = _stringopts->C;
4022N/A CallStaticJavaNode* call = new (C) CallStaticJavaNode(call_type, call_addr, "uncommon_trap",
4022N/A jvms->bci(), no_memory_effects);
1080N/A for (int e = 0; e < TypeFunc::Parms; e++) {
1080N/A call->init_req(e, uct->in(e));
1080N/A }
1080N/A // Set the trap request to record intrinsic failure if this trap
1080N/A // is taken too many times. Ideally we would handle then traps by
1080N/A // doing the original bookkeeping in the MDO so that if it caused
1080N/A // the code to be thrown out we could still recompile and use the
1080N/A // optimization. Failing the uncommon traps doesn't really mean
1080N/A // that the optimization is a bad idea but there's no other way to
1080N/A // do the MDO updates currently.
1080N/A int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic,
1080N/A Deoptimization::Action_make_not_entrant);
1080N/A call->init_req(TypeFunc::Parms, __ intcon(trap_request));
1080N/A kit.add_safepoint_edges(call);
1080N/A
1080N/A _stringopts->gvn()->transform(call);
1080N/A C->gvn_replace_by(uct, call);
4123N/A uct->disconnect_inputs(NULL, C);
1080N/A }
1080N/A }
1080N/A
1080N/A void cleanup() {
1080N/A // disconnect the hook node
4123N/A _arguments->disconnect_inputs(NULL, _stringopts->C);
1080N/A }
1080N/A};
1080N/A
1080N/A
1080N/Avoid StringConcat::eliminate_unneeded_control() {
1080N/A for (uint i = 0; i < _control.size(); i++) {
1080N/A Node* n = _control.at(i);
3852N/A if (n->is_Allocate()) {
3852N/A eliminate_initialize(n->as_Allocate()->initialization());
3852N/A }
1080N/A if (n->is_Call()) {
1080N/A if (n != _end) {
1080N/A eliminate_call(n->as_Call());
1080N/A }
1080N/A } else if (n->is_IfTrue()) {
1080N/A Compile* C = _stringopts->C;
1080N/A C->gvn_replace_by(n, n->in(0)->in(0));
4132N/A // get rid of the other projection
4132N/A C->gvn_replace_by(n->in(0)->as_If()->proj_out(false), C->top());
1080N/A }
1080N/A }
1080N/A}
1080N/A
1080N/A
1080N/AStringConcat* StringConcat::merge(StringConcat* other, Node* arg) {
1080N/A StringConcat* result = new StringConcat(_stringopts, _end);
1080N/A for (uint x = 0; x < _control.size(); x++) {
1080N/A Node* n = _control.at(x);
1080N/A if (n->is_Call()) {
1080N/A result->_control.push(n);
1080N/A }
1080N/A }
1080N/A for (uint x = 0; x < other->_control.size(); x++) {
1080N/A Node* n = other->_control.at(x);
1080N/A if (n->is_Call()) {
1080N/A result->_control.push(n);
1080N/A }
1080N/A }
1080N/A assert(result->_control.contains(other->_end), "what?");
1080N/A assert(result->_control.contains(_begin), "what?");
1080N/A for (int x = 0; x < num_arguments(); x++) {
3852N/A Node* argx = argument_uncast(x);
3852N/A if (argx == arg) {
1080N/A // replace the toString result with the all the arguments that
1080N/A // made up the other StringConcat
1080N/A for (int y = 0; y < other->num_arguments(); y++) {
1080N/A result->append(other->argument(y), other->mode(y));
1080N/A }
1080N/A } else {
3852N/A result->append(argx, mode(x));
1080N/A }
1080N/A }
1080N/A result->set_allocation(other->_begin);
1080N/A result->_multiple = true;
1080N/A return result;
1080N/A}
1080N/A
1080N/A
1080N/Avoid StringConcat::eliminate_call(CallNode* call) {
1080N/A Compile* C = _stringopts->C;
1080N/A CallProjections projs;
1080N/A call->extract_projections(&projs, false);
1080N/A if (projs.fallthrough_catchproj != NULL) {
1080N/A C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control));
1080N/A }
1080N/A if (projs.fallthrough_memproj != NULL) {
1080N/A C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory));
1080N/A }
1080N/A if (projs.catchall_memproj != NULL) {
1080N/A C->gvn_replace_by(projs.catchall_memproj, C->top());
1080N/A }
1080N/A if (projs.fallthrough_ioproj != NULL) {
1080N/A C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O));
1080N/A }
1080N/A if (projs.catchall_ioproj != NULL) {
1080N/A C->gvn_replace_by(projs.catchall_ioproj, C->top());
1080N/A }
1080N/A if (projs.catchall_catchproj != NULL) {
1080N/A // EA can't cope with the partially collapsed graph this
1080N/A // creates so put it on the worklist to be collapsed later.
1080N/A for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) {
1080N/A Node *use = i.get();
1080N/A int opc = use->Opcode();
1080N/A if (opc == Op_CreateEx || opc == Op_Region) {
1080N/A _stringopts->record_dead_node(use);
1080N/A }
1080N/A }
1080N/A C->gvn_replace_by(projs.catchall_catchproj, C->top());
1080N/A }
1080N/A if (projs.resproj != NULL) {
1080N/A C->gvn_replace_by(projs.resproj, C->top());
1080N/A }
1080N/A C->gvn_replace_by(call, C->top());
1080N/A}
1080N/A
1080N/Avoid StringConcat::eliminate_initialize(InitializeNode* init) {
1080N/A Compile* C = _stringopts->C;
1080N/A
1080N/A // Eliminate Initialize node.
1080N/A assert(init->outcnt() <= 2, "only a control and memory projection expected");
1080N/A assert(init->req() <= InitializeNode::RawStores, "no pending inits");
1080N/A Node *ctrl_proj = init->proj_out(TypeFunc::Control);
1080N/A if (ctrl_proj != NULL) {
1080N/A C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control));
1080N/A }
1080N/A Node *mem_proj = init->proj_out(TypeFunc::Memory);
1080N/A if (mem_proj != NULL) {
1080N/A Node *mem = init->in(TypeFunc::Memory);
1080N/A C->gvn_replace_by(mem_proj, mem);
1080N/A }
1080N/A C->gvn_replace_by(init, C->top());
4123N/A init->disconnect_inputs(NULL, C);
1080N/A}
1080N/A
1080N/ANode_List PhaseStringOpts::collect_toString_calls() {
1080N/A Node_List string_calls;
1080N/A Node_List worklist;
1080N/A
1080N/A _visited.Clear();
1080N/A
1080N/A // Prime the worklist
1080N/A for (uint i = 1; i < C->root()->len(); i++) {
1080N/A Node* n = C->root()->in(i);
1080N/A if (n != NULL && !_visited.test_set(n->_idx)) {
1080N/A worklist.push(n);
1080N/A }
1080N/A }
1080N/A
1080N/A while (worklist.size() > 0) {
1080N/A Node* ctrl = worklist.pop();
3852N/A if (StringConcat::is_SB_toString(ctrl)) {
1080N/A CallStaticJavaNode* csj = ctrl->as_CallStaticJava();
3852N/A string_calls.push(csj);
1080N/A }
1080N/A if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) {
1080N/A worklist.push(ctrl->in(0));
1080N/A }
1080N/A if (ctrl->is_Region()) {
1080N/A for (uint i = 1; i < ctrl->len(); i++) {
1080N/A if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) {
1080N/A worklist.push(ctrl->in(i));
1080N/A }
1080N/A }
1080N/A }
1080N/A }
1080N/A return string_calls;
1080N/A}
1080N/A
1080N/A
1080N/AStringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) {
1080N/A ciMethod* m = call->method();
1080N/A ciSymbol* string_sig;
1080N/A ciSymbol* int_sig;
1080N/A ciSymbol* char_sig;
1080N/A if (m->holder() == C->env()->StringBuilder_klass()) {
1080N/A string_sig = ciSymbol::String_StringBuilder_signature();
1080N/A int_sig = ciSymbol::int_StringBuilder_signature();
1080N/A char_sig = ciSymbol::char_StringBuilder_signature();
1080N/A } else if (m->holder() == C->env()->StringBuffer_klass()) {
1080N/A string_sig = ciSymbol::String_StringBuffer_signature();
1080N/A int_sig = ciSymbol::int_StringBuffer_signature();
1080N/A char_sig = ciSymbol::char_StringBuffer_signature();
1080N/A } else {
1080N/A return NULL;
1080N/A }
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print("considering toString call in ");
1080N/A call->jvms()->dump_spec(tty); tty->cr();
1080N/A }
1080N/A#endif
1080N/A
1080N/A StringConcat* sc = new StringConcat(this, call);
1080N/A
1080N/A AllocateNode* alloc = NULL;
1080N/A InitializeNode* init = NULL;
1080N/A
1080N/A // possible opportunity for StringBuilder fusion
1080N/A CallStaticJavaNode* cnode = call;
1080N/A while (cnode) {
1080N/A Node* recv = cnode->in(TypeFunc::Parms)->uncast();
1080N/A if (recv->is_Proj()) {
1080N/A recv = recv->in(0);
1080N/A }
1080N/A cnode = recv->isa_CallStaticJava();
1080N/A if (cnode == NULL) {
1080N/A alloc = recv->isa_Allocate();
1080N/A if (alloc == NULL) {
1080N/A break;
1080N/A }
1080N/A // Find the constructor call
1080N/A Node* result = alloc->result_cast();
4132N/A if (result == NULL || !result->is_CheckCastPP() || alloc->in(TypeFunc::Memory)->is_top()) {
1080N/A // strange looking allocation
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print("giving up because allocation looks strange ");
1080N/A alloc->jvms()->dump_spec(tty); tty->cr();
1080N/A }
1080N/A#endif
1080N/A break;
1080N/A }
1080N/A Node* constructor = NULL;
1080N/A for (SimpleDUIterator i(result); i.has_next(); i.next()) {
1080N/A CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
1737N/A if (use != NULL &&
1737N/A use->method() != NULL &&
1737N/A !use->method()->is_static() &&
1080N/A use->method()->name() == ciSymbol::object_initializer_name() &&
1080N/A use->method()->holder() == m->holder()) {
1080N/A // Matched the constructor.
1080N/A ciSymbol* sig = use->method()->signature()->as_symbol();
1080N/A if (sig == ciSymbol::void_method_signature() ||
1080N/A sig == ciSymbol::int_void_signature() ||
1080N/A sig == ciSymbol::string_void_signature()) {
1080N/A if (sig == ciSymbol::string_void_signature()) {
1080N/A // StringBuilder(String) so pick this up as the first argument
1080N/A assert(use->in(TypeFunc::Parms + 1) != NULL, "what?");
1978N/A const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1));
1978N/A if (type == TypePtr::NULL_PTR) {
1978N/A // StringBuilder(null) throws exception.
1978N/A#ifndef PRODUCT
1978N/A if (PrintOptimizeStringConcat) {
1978N/A tty->print("giving up because StringBuilder(null) throws exception");
1978N/A alloc->jvms()->dump_spec(tty); tty->cr();
1978N/A }
1978N/A#endif
1978N/A return NULL;
1978N/A }
1978N/A // StringBuilder(str) argument needs null check.
1978N/A sc->push_string_null_check(use->in(TypeFunc::Parms + 1));
1080N/A }
1080N/A // The int variant takes an initial size for the backing
1080N/A // array so just treat it like the void version.
1080N/A constructor = use;
1080N/A } else {
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print("unexpected constructor signature: %s", sig->as_utf8());
1080N/A }
1080N/A#endif
1080N/A }
1080N/A break;
1080N/A }
1080N/A }
1080N/A if (constructor == NULL) {
1080N/A // couldn't find constructor
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print("giving up because couldn't find constructor ");
1978N/A alloc->jvms()->dump_spec(tty); tty->cr();
1080N/A }
1080N/A#endif
1080N/A break;
1080N/A }
1080N/A
1080N/A // Walked all the way back and found the constructor call so see
1080N/A // if this call converted into a direct string concatenation.
1080N/A sc->add_control(call);
1080N/A sc->add_control(constructor);
1080N/A sc->add_control(alloc);
1080N/A sc->set_allocation(alloc);
1080N/A if (sc->validate_control_flow()) {
1080N/A return sc;
1080N/A } else {
1080N/A return NULL;
1080N/A }
1080N/A } else if (cnode->method() == NULL) {
1080N/A break;
1737N/A } else if (!cnode->method()->is_static() &&
1737N/A cnode->method()->holder() == m->holder() &&
1080N/A cnode->method()->name() == ciSymbol::append_name() &&
1080N/A (cnode->method()->signature()->as_symbol() == string_sig ||
1080N/A cnode->method()->signature()->as_symbol() == char_sig ||
1080N/A cnode->method()->signature()->as_symbol() == int_sig)) {
1080N/A sc->add_control(cnode);
1080N/A Node* arg = cnode->in(TypeFunc::Parms + 1);
1080N/A if (cnode->method()->signature()->as_symbol() == int_sig) {
1080N/A sc->push_int(arg);
1080N/A } else if (cnode->method()->signature()->as_symbol() == char_sig) {
1080N/A sc->push_char(arg);
1080N/A } else {
1080N/A if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {
1080N/A CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
1080N/A if (csj->method() != NULL &&
3889N/A csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString &&
3889N/A arg->outcnt() == 1) {
3889N/A // _control is the list of StringBuilder calls nodes which
3889N/A // will be replaced by new String code after this optimization.
3889N/A // Integer::toString() call is not part of StringBuilder calls
3889N/A // chain. It could be eliminated only if its result is used
3889N/A // only by this SB calls chain.
3889N/A // Another limitation: it should be used only once because
3889N/A // it is unknown that it is used only by this SB calls chain
3889N/A // until all related SB calls nodes are collected.
3889N/A assert(arg->unique_out() == cnode, "sanity");
1080N/A sc->add_control(csj);
1080N/A sc->push_int(csj->in(TypeFunc::Parms));
1080N/A continue;
1080N/A }
1080N/A }
1080N/A sc->push_string(arg);
1080N/A }
1080N/A continue;
1080N/A } else {
1080N/A // some unhandled signature
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print("giving up because encountered unexpected signature ");
1080N/A cnode->tf()->dump(); tty->cr();
1080N/A cnode->in(TypeFunc::Parms + 1)->dump();
1080N/A }
1080N/A#endif
1080N/A break;
1080N/A }
1080N/A }
1080N/A return NULL;
1080N/A}
1080N/A
1080N/A
1080N/APhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*):
1080N/A Phase(StringOpts),
1080N/A _gvn(gvn),
1080N/A _visited(Thread::current()->resource_area()) {
1080N/A
1080N/A assert(OptimizeStringConcat, "shouldn't be here");
1080N/A
1080N/A size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"),
1080N/A ciSymbol::make("[I"), true);
1080N/A if (size_table_field == NULL) {
1080N/A // Something wrong so give up.
1080N/A assert(false, "why can't we find Integer.sizeTable?");
1080N/A return;
1080N/A }
1080N/A
1080N/A // Collect the types needed to talk about the various slices of memory
1080N/A char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS);
1080N/A
1080N/A // For each locally allocated StringBuffer see if the usages can be
1080N/A // collapsed into a single String construction.
1080N/A
1080N/A // Run through the list of allocation looking for SB.toString to see
1080N/A // if it's possible to fuse the usage of the SB into a single String
1080N/A // construction.
1080N/A GrowableArray<StringConcat*> concats;
1080N/A Node_List toStrings = collect_toString_calls();
1080N/A while (toStrings.size() > 0) {
1080N/A StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava());
1080N/A if (sc != NULL) {
1080N/A concats.push(sc);
1080N/A }
1080N/A }
1080N/A
1080N/A // try to coalesce separate concats
1080N/A restart:
1080N/A for (int c = 0; c < concats.length(); c++) {
1080N/A StringConcat* sc = concats.at(c);
1080N/A for (int i = 0; i < sc->num_arguments(); i++) {
3852N/A Node* arg = sc->argument_uncast(i);
3852N/A if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) {
1080N/A CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
3852N/A for (int o = 0; o < concats.length(); o++) {
3852N/A if (c == o) continue;
3852N/A StringConcat* other = concats.at(o);
3852N/A if (other->end() == csj) {
1080N/A#ifndef PRODUCT
3852N/A if (PrintOptimizeStringConcat) {
3852N/A tty->print_cr("considering stacked concats");
3852N/A }
1080N/A#endif
1080N/A
3852N/A StringConcat* merged = sc->merge(other, arg);
3852N/A if (merged->validate_control_flow()) {
1080N/A#ifndef PRODUCT
3852N/A if (PrintOptimizeStringConcat) {
3852N/A tty->print_cr("stacking would succeed");
3852N/A }
1080N/A#endif
3852N/A if (c < o) {
3852N/A concats.remove_at(o);
3852N/A concats.at_put(c, merged);
1080N/A } else {
3852N/A concats.remove_at(c);
3852N/A concats.at_put(o, merged);
3852N/A }
3852N/A goto restart;
3852N/A } else {
1080N/A#ifndef PRODUCT
3852N/A if (PrintOptimizeStringConcat) {
3852N/A tty->print_cr("stacking would fail");
3852N/A }
1080N/A#endif
1080N/A }
1080N/A }
1080N/A }
1080N/A }
1080N/A }
1080N/A }
1080N/A
1080N/A
1080N/A for (int c = 0; c < concats.length(); c++) {
1080N/A StringConcat* sc = concats.at(c);
1080N/A replace_string_concat(sc);
1080N/A }
1080N/A
1080N/A remove_dead_nodes();
1080N/A}
1080N/A
1080N/Avoid PhaseStringOpts::record_dead_node(Node* dead) {
1080N/A dead_worklist.push(dead);
1080N/A}
1080N/A
1080N/Avoid PhaseStringOpts::remove_dead_nodes() {
1080N/A // Delete any dead nodes to make things clean enough that escape
1080N/A // analysis doesn't get unhappy.
1080N/A while (dead_worklist.size() > 0) {
1080N/A Node* use = dead_worklist.pop();
1080N/A int opc = use->Opcode();
1080N/A switch (opc) {
1080N/A case Op_Region: {
1080N/A uint i = 1;
1080N/A for (i = 1; i < use->req(); i++) {
1080N/A if (use->in(i) != C->top()) {
1080N/A break;
1080N/A }
1080N/A }
1080N/A if (i >= use->req()) {
1080N/A for (SimpleDUIterator i(use); i.has_next(); i.next()) {
1080N/A Node* m = i.get();
1080N/A if (m->is_Phi()) {
1080N/A dead_worklist.push(m);
1080N/A }
1080N/A }
1080N/A C->gvn_replace_by(use, C->top());
1080N/A }
1080N/A break;
1080N/A }
1080N/A case Op_AddP:
1080N/A case Op_CreateEx: {
1080N/A // Recurisvely clean up references to CreateEx so EA doesn't
1080N/A // get unhappy about the partially collapsed graph.
1080N/A for (SimpleDUIterator i(use); i.has_next(); i.next()) {
1080N/A Node* m = i.get();
1080N/A if (m->is_AddP()) {
1080N/A dead_worklist.push(m);
1080N/A }
1080N/A }
1080N/A C->gvn_replace_by(use, C->top());
1080N/A break;
1080N/A }
1080N/A case Op_Phi:
1080N/A if (use->in(0) == C->top()) {
1080N/A C->gvn_replace_by(use, C->top());
1080N/A }
1080N/A break;
1080N/A }
1080N/A }
1080N/A}
1080N/A
1080N/A
1080N/Abool StringConcat::validate_control_flow() {
1080N/A // We found all the calls and arguments now lets see if it's
1080N/A // safe to transform the graph as we would expect.
1080N/A
1080N/A // Check to see if this resulted in too many uncommon traps previously
1080N/A if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(),
1080N/A Deoptimization::Reason_intrinsic)) {
1080N/A return false;
1080N/A }
1080N/A
1080N/A // Walk backwards over the control flow from toString to the
1080N/A // allocation and make sure all the control flow is ok. This
1080N/A // means it's either going to be eliminated once the calls are
1080N/A // removed or it can safely be transformed into an uncommon
1080N/A // trap.
1080N/A
1080N/A int null_check_count = 0;
1080N/A Unique_Node_List ctrl_path;
1080N/A
1080N/A assert(_control.contains(_begin), "missing");
1080N/A assert(_control.contains(_end), "missing");
1080N/A
1080N/A // Collect the nodes that we know about and will eliminate into ctrl_path
1080N/A for (uint i = 0; i < _control.size(); i++) {
1080N/A // Push the call and it's control projection
1080N/A Node* n = _control.at(i);
1080N/A if (n->is_Allocate()) {
1080N/A AllocateNode* an = n->as_Allocate();
1080N/A InitializeNode* init = an->initialization();
1080N/A ctrl_path.push(init);
1080N/A ctrl_path.push(init->as_Multi()->proj_out(0));
1080N/A }
1080N/A if (n->is_Call()) {
1080N/A CallNode* cn = n->as_Call();
1080N/A ctrl_path.push(cn);
1080N/A ctrl_path.push(cn->proj_out(0));
1080N/A ctrl_path.push(cn->proj_out(0)->unique_out());
4127N/A if (cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0) != NULL) {
4127N/A ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0));
4127N/A }
1080N/A } else {
1080N/A ShouldNotReachHere();
1080N/A }
1080N/A }
1080N/A
1080N/A // Skip backwards through the control checking for unexpected contro flow
1080N/A Node* ptr = _end;
1080N/A bool fail = false;
1080N/A while (ptr != _begin) {
1080N/A if (ptr->is_Call() && ctrl_path.member(ptr)) {
1080N/A ptr = ptr->in(0);
1080N/A } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
1080N/A ptr = ptr->in(0)->in(0)->in(0);
1080N/A assert(ctrl_path.member(ptr), "should be a known piece of control");
1080N/A } else if (ptr->is_IfTrue()) {
1080N/A IfNode* iff = ptr->in(0)->as_If();
1080N/A BoolNode* b = iff->in(1)->isa_Bool();
4127N/A
4127N/A if (b == NULL) {
4127N/A fail = true;
4127N/A break;
4127N/A }
4127N/A
1080N/A Node* cmp = b->in(1);
1080N/A Node* v1 = cmp->in(1);
1080N/A Node* v2 = cmp->in(2);
1080N/A Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con);
1080N/A
1080N/A // Null check of the return of append which can simply be eliminated
1080N/A if (b->_test._test == BoolTest::ne &&
1080N/A v2->bottom_type() == TypePtr::NULL_PTR &&
1080N/A v1->is_Proj() && ctrl_path.member(v1->in(0))) {
1080N/A // NULL check of the return value of the append
1080N/A null_check_count++;
1080N/A if (otherproj->outcnt() == 1) {
1080N/A CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
1080N/A if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
1080N/A ctrl_path.push(call);
1080N/A }
1080N/A }
1080N/A _control.push(ptr);
1080N/A ptr = ptr->in(0)->in(0);
1080N/A continue;
1080N/A }
1080N/A
1080N/A // A test which leads to an uncommon trap which should be safe.
1080N/A // Later this trap will be converted into a trap that restarts
1080N/A // at the beginning.
1080N/A if (otherproj->outcnt() == 1) {
1080N/A CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
1080N/A if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
1080N/A // control flow leads to uct so should be ok
1080N/A _uncommon_traps.push(call);
1080N/A ctrl_path.push(call);
1080N/A ptr = ptr->in(0)->in(0);
1080N/A continue;
1080N/A }
1080N/A }
1080N/A
1080N/A#ifndef PRODUCT
1080N/A // Some unexpected control flow we don't know how to handle.
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print_cr("failing with unknown test");
1080N/A b->dump();
1080N/A cmp->dump();
1080N/A v1->dump();
1080N/A v2->dump();
1080N/A tty->cr();
1080N/A }
1080N/A#endif
2493N/A fail = true;
1080N/A break;
1080N/A } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) {
1080N/A ptr = ptr->in(0)->in(0);
1080N/A } else if (ptr->is_Region()) {
1080N/A Node* copy = ptr->as_Region()->is_copy();
1080N/A if (copy != NULL) {
1080N/A ptr = copy;
1080N/A continue;
1080N/A }
1080N/A if (ptr->req() == 3 &&
1080N/A ptr->in(1) != NULL && ptr->in(1)->is_Proj() &&
1080N/A ptr->in(2) != NULL && ptr->in(2)->is_Proj() &&
1080N/A ptr->in(1)->in(0) == ptr->in(2)->in(0) &&
1080N/A ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) {
1080N/A // Simple diamond.
1080N/A // XXX should check for possibly merging stores. simple data merges are ok.
4132N/A // The IGVN will make this simple diamond go away when it
4132N/A // transforms the Region. Make sure it sees it.
4132N/A Compile::current()->record_for_igvn(ptr);
1080N/A ptr = ptr->in(1)->in(0)->in(0);
1080N/A continue;
1080N/A }
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print_cr("fusion would fail for region");
1080N/A _begin->dump();
1080N/A ptr->dump(2);
1080N/A }
1080N/A#endif
1080N/A fail = true;
1080N/A break;
1080N/A } else {
1080N/A // other unknown control
1080N/A if (!fail) {
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A tty->print_cr("fusion would fail for");
1080N/A _begin->dump();
1080N/A }
1080N/A#endif
1080N/A fail = true;
1080N/A }
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A ptr->dump();
1080N/A }
1080N/A#endif
1080N/A ptr = ptr->in(0);
1080N/A }
1080N/A }
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat && fail) {
1080N/A tty->cr();
1080N/A }
1080N/A#endif
1080N/A if (fail) return !fail;
1080N/A
1080N/A // Validate that all these results produced are contained within
1080N/A // this cluster of objects. First collect all the results produced
1080N/A // by calls in the region.
1080N/A _stringopts->_visited.Clear();
1080N/A Node_List worklist;
1080N/A Node* final_result = _end->proj_out(TypeFunc::Parms);
1080N/A for (uint i = 0; i < _control.size(); i++) {
1080N/A CallNode* cnode = _control.at(i)->isa_Call();
1080N/A if (cnode != NULL) {
1080N/A _stringopts->_visited.test_set(cnode->_idx);
1080N/A }
1080N/A Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL;
1080N/A if (result != NULL && result != final_result) {
1080N/A worklist.push(result);
1080N/A }
1080N/A }
1080N/A
1080N/A Node* last_result = NULL;
1080N/A while (worklist.size() > 0) {
1080N/A Node* result = worklist.pop();
1080N/A if (_stringopts->_visited.test_set(result->_idx))
1080N/A continue;
1080N/A for (SimpleDUIterator i(result); i.has_next(); i.next()) {
1080N/A Node *use = i.get();
1080N/A if (ctrl_path.member(use)) {
1080N/A // already checked this
1080N/A continue;
1080N/A }
1080N/A int opc = use->Opcode();
1080N/A if (opc == Op_CmpP || opc == Op_Node) {
1080N/A ctrl_path.push(use);
1080N/A continue;
1080N/A }
1080N/A if (opc == Op_CastPP || opc == Op_CheckCastPP) {
1080N/A for (SimpleDUIterator j(use); j.has_next(); j.next()) {
1080N/A worklist.push(j.get());
1080N/A }
1080N/A worklist.push(use->in(1));
1080N/A ctrl_path.push(use);
1080N/A continue;
1080N/A }
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat) {
1080N/A if (result != last_result) {
1080N/A last_result = result;
1080N/A tty->print_cr("extra uses for result:");
1080N/A last_result->dump();
1080N/A }
1080N/A use->dump();
1080N/A }
1080N/A#endif
1080N/A fail = true;
1080N/A break;
1080N/A }
1080N/A }
1080N/A
1080N/A#ifndef PRODUCT
1080N/A if (PrintOptimizeStringConcat && !fail) {
1080N/A ttyLocker ttyl;
1080N/A tty->cr();
1080N/A tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size());
1080N/A _begin->jvms()->dump_spec(tty); tty->cr();
1080N/A for (int i = 0; i < num_arguments(); i++) {
1080N/A argument(i)->dump();
1080N/A }
1080N/A _control.dump();
1080N/A tty->cr();
1080N/A }
1080N/A#endif
1080N/A
1080N/A return !fail;
1080N/A}
1080N/A
1080N/ANode* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
3712N/A const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror());
3712N/A Node* klass_node = __ makecon(mirror_type);
1080N/A BasicType bt = field->layout_type();
1080N/A ciType* field_klass = field->type();
1080N/A
1080N/A const Type *type;
1080N/A if( bt == T_OBJECT ) {
1080N/A if (!field->type()->is_loaded()) {
1080N/A type = TypeInstPtr::BOTTOM;
1080N/A } else if (field->is_constant()) {
1080N/A // This can happen if the constant oop is non-perm.
1080N/A ciObject* con = field->constant_value().as_object();
1080N/A // Do not "join" in the previous type; it doesn't add value,
1080N/A // and may yield a vacuous result if the field is of interface type.
2226N/A type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr();
1080N/A assert(type != NULL, "field singleton type must be consistent");
3712N/A return __ makecon(type);
1080N/A } else {
1080N/A type = TypeOopPtr::make_from_klass(field_klass->as_klass());
1080N/A }
1080N/A } else {
1080N/A type = Type::get_const_basic_type(bt);
1080N/A }
1080N/A
1080N/A return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
1080N/A type, T_OBJECT,
3712N/A C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())));
1080N/A}
1080N/A
1080N/ANode* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
4022N/A RegionNode *final_merge = new (C) RegionNode(3);
1080N/A kit.gvn().set_type(final_merge, Type::CONTROL);
4022N/A Node* final_size = new (C) PhiNode(final_merge, TypeInt::INT);
1080N/A kit.gvn().set_type(final_size, TypeInt::INT);
1080N/A
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(),
1080N/A __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1080N/A PROB_FAIR, COUNT_UNKNOWN);
1080N/A Node* is_min = __ IfFalse(iff);
1080N/A final_merge->init_req(1, is_min);
1080N/A final_size->init_req(1, __ intcon(11));
1080N/A
1080N/A kit.set_control(__ IfTrue(iff));
1080N/A if (kit.stopped()) {
1080N/A final_merge->init_req(2, C->top());
1080N/A final_size->init_req(2, C->top());
1080N/A } else {
1080N/A
1080N/A // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
4022N/A RegionNode *r = new (C) RegionNode(3);
1080N/A kit.gvn().set_type(r, Type::CONTROL);
4022N/A Node *phi = new (C) PhiNode(r, TypeInt::INT);
1080N/A kit.gvn().set_type(phi, TypeInt::INT);
4022N/A Node *size = new (C) PhiNode(r, TypeInt::INT);
1080N/A kit.gvn().set_type(size, TypeInt::INT);
1080N/A Node* chk = __ CmpI(arg, __ intcon(0));
1080N/A Node* p = __ Bool(chk, BoolTest::lt);
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN);
1080N/A Node* lessthan = __ IfTrue(iff);
1080N/A Node* greaterequal = __ IfFalse(iff);
1080N/A r->init_req(1, lessthan);
1080N/A phi->init_req(1, __ SubI(__ intcon(0), arg));
1080N/A size->init_req(1, __ intcon(1));
1080N/A r->init_req(2, greaterequal);
1080N/A phi->init_req(2, arg);
1080N/A size->init_req(2, __ intcon(0));
1080N/A kit.set_control(r);
1080N/A C->record_for_igvn(r);
1080N/A C->record_for_igvn(phi);
1080N/A C->record_for_igvn(size);
1080N/A
1080N/A // for (int i=0; ; i++)
1080N/A // if (x <= sizeTable[i])
1080N/A // return i+1;
2230N/A
2230N/A // Add loop predicate first.
2230N/A kit.add_predicate();
2230N/A
4022N/A RegionNode *loop = new (C) RegionNode(3);
1080N/A loop->init_req(1, kit.control());
1080N/A kit.gvn().set_type(loop, Type::CONTROL);
1080N/A
4022N/A Node *index = new (C) PhiNode(loop, TypeInt::INT);
1080N/A index->init_req(1, __ intcon(0));
1080N/A kit.gvn().set_type(index, TypeInt::INT);
1080N/A kit.set_control(loop);
1080N/A Node* sizeTable = fetch_static_field(kit, size_table_field);
1080N/A
1080N/A Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS);
1080N/A C->record_for_igvn(value);
1080N/A Node* limit = __ CmpI(phi, value);
1080N/A Node* limitb = __ Bool(limit, BoolTest::le);
1080N/A IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN);
1080N/A Node* lessEqual = __ IfTrue(iff2);
1080N/A Node* greater = __ IfFalse(iff2);
1080N/A
1080N/A loop->init_req(2, greater);
1080N/A index->init_req(2, __ AddI(index, __ intcon(1)));
1080N/A
1080N/A kit.set_control(lessEqual);
1080N/A C->record_for_igvn(loop);
1080N/A C->record_for_igvn(index);
1080N/A
1080N/A final_merge->init_req(2, kit.control());
1080N/A final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
1080N/A }
1080N/A
1080N/A kit.set_control(final_merge);
1080N/A C->record_for_igvn(final_merge);
1080N/A C->record_for_igvn(final_size);
1080N/A
1080N/A return final_size;
1080N/A}
1080N/A
1080N/Avoid PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) {
4022N/A RegionNode *final_merge = new (C) RegionNode(4);
1080N/A kit.gvn().set_type(final_merge, Type::CONTROL);
1080N/A Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1080N/A kit.gvn().set_type(final_mem, Type::MEMORY);
1080N/A
1080N/A // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive
1080N/A {
1080N/A // i == MIN_VALUE
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(),
1080N/A __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
1080N/A PROB_FAIR, COUNT_UNKNOWN);
1080N/A
1080N/A Node* old_mem = kit.memory(char_adr_idx);
1080N/A
1080N/A kit.set_control(__ IfFalse(iff));
1080N/A if (kit.stopped()) {
1080N/A // Statically not equal to MIN_VALUE so this path is dead
1080N/A final_merge->init_req(3, kit.control());
1080N/A } else {
1080N/A copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
1080N/A char_array, start);
1080N/A final_merge->init_req(3, kit.control());
1080N/A final_mem->init_req(3, kit.memory(char_adr_idx));
1080N/A }
1080N/A
1080N/A kit.set_control(__ IfTrue(iff));
1080N/A kit.set_memory(old_mem, char_adr_idx);
1080N/A }
1080N/A
1080N/A
1080N/A // Simplified version of Integer.getChars
1080N/A
1080N/A // int q, r;
1080N/A // int charPos = index;
1080N/A Node* charPos = end;
1080N/A
1080N/A // char sign = 0;
1080N/A
1080N/A Node* i = arg;
1080N/A Node* sign = __ intcon(0);
1080N/A
1080N/A // if (i < 0) {
1080N/A // sign = '-';
1080N/A // i = -i;
1080N/A // }
1080N/A {
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(),
1080N/A __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
1080N/A PROB_FAIR, COUNT_UNKNOWN);
1080N/A
4022N/A RegionNode *merge = new (C) RegionNode(3);
1080N/A kit.gvn().set_type(merge, Type::CONTROL);
4022N/A i = new (C) PhiNode(merge, TypeInt::INT);
1080N/A kit.gvn().set_type(i, TypeInt::INT);
4022N/A sign = new (C) PhiNode(merge, TypeInt::INT);
1080N/A kit.gvn().set_type(sign, TypeInt::INT);
1080N/A
1080N/A merge->init_req(1, __ IfTrue(iff));
1080N/A i->init_req(1, __ SubI(__ intcon(0), arg));
1080N/A sign->init_req(1, __ intcon('-'));
1080N/A merge->init_req(2, __ IfFalse(iff));
1080N/A i->init_req(2, arg);
1080N/A sign->init_req(2, __ intcon(0));
1080N/A
1080N/A kit.set_control(merge);
1080N/A
1080N/A C->record_for_igvn(merge);
1080N/A C->record_for_igvn(i);
1080N/A C->record_for_igvn(sign);
1080N/A }
1080N/A
1080N/A // for (;;) {
1080N/A // q = i / 10;
1080N/A // r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
1080N/A // buf [--charPos] = digits [r];
1080N/A // i = q;
1080N/A // if (i == 0) break;
1080N/A // }
1080N/A
1080N/A {
2230N/A // Add loop predicate first.
2230N/A kit.add_predicate();
2230N/A
4022N/A RegionNode *head = new (C) RegionNode(3);
1080N/A head->init_req(1, kit.control());
1080N/A kit.gvn().set_type(head, Type::CONTROL);
4022N/A Node *i_phi = new (C) PhiNode(head, TypeInt::INT);
1080N/A i_phi->init_req(1, i);
1080N/A kit.gvn().set_type(i_phi, TypeInt::INT);
1080N/A charPos = PhiNode::make(head, charPos);
1080N/A kit.gvn().set_type(charPos, TypeInt::INT);
1080N/A Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
1080N/A kit.gvn().set_type(mem, Type::MEMORY);
1080N/A kit.set_control(head);
1080N/A kit.set_memory(mem, char_adr_idx);
1080N/A
1250N/A Node* q = __ DivI(NULL, i_phi, __ intcon(10));
1080N/A Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
1080N/A __ LShiftI(q, __ intcon(1))));
1080N/A Node* m1 = __ SubI(charPos, __ intcon(1));
1080N/A Node* ch = __ AddI(r, __ intcon('0'));
1080N/A
1080N/A Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1080N/A ch, T_CHAR, char_adr_idx);
1080N/A
1080N/A
1080N/A IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
1080N/A PROB_FAIR, COUNT_UNKNOWN);
1080N/A Node* ne = __ IfTrue(iff);
1080N/A Node* eq = __ IfFalse(iff);
1080N/A
1080N/A head->init_req(2, ne);
1080N/A mem->init_req(2, st);
1080N/A i_phi->init_req(2, q);
1080N/A charPos->init_req(2, m1);
1080N/A
1080N/A charPos = m1;
1080N/A
1080N/A kit.set_control(eq);
1080N/A kit.set_memory(st, char_adr_idx);
1080N/A
1080N/A C->record_for_igvn(head);
1080N/A C->record_for_igvn(mem);
1080N/A C->record_for_igvn(i_phi);
1080N/A C->record_for_igvn(charPos);
1080N/A }
1080N/A
1080N/A {
1080N/A // if (sign != 0) {
1080N/A // buf [--charPos] = sign;
1080N/A // }
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(),
1080N/A __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
1080N/A PROB_FAIR, COUNT_UNKNOWN);
1080N/A
1080N/A final_merge->init_req(2, __ IfFalse(iff));
1080N/A final_mem->init_req(2, kit.memory(char_adr_idx));
1080N/A
1080N/A kit.set_control(__ IfTrue(iff));
1080N/A if (kit.stopped()) {
1080N/A final_merge->init_req(1, C->top());
1080N/A final_mem->init_req(1, C->top());
1080N/A } else {
1080N/A Node* m1 = __ SubI(charPos, __ intcon(1));
1080N/A Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
1080N/A sign, T_CHAR, char_adr_idx);
1080N/A
1080N/A final_merge->init_req(1, kit.control());
1080N/A final_mem->init_req(1, st);
1080N/A }
1080N/A
1080N/A kit.set_control(final_merge);
1080N/A kit.set_memory(final_mem, char_adr_idx);
1080N/A
1080N/A C->record_for_igvn(final_merge);
1080N/A C->record_for_igvn(final_mem);
1080N/A }
1080N/A}
1080N/A
1080N/A
1080N/ANode* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) {
1080N/A Node* string = str;
3726N/A Node* offset = kit.load_String_offset(kit.control(), string);
3726N/A Node* count = kit.load_String_length(kit.control(), string);
3726N/A Node* value = kit.load_String_value (kit.control(), string);
1080N/A
1080N/A // copy the contents
1080N/A if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) {
1080N/A // For small constant strings just emit individual stores.
1080N/A // A length of 6 seems like a good space/speed tradeof.
1080N/A int c = count->get_int();
1080N/A int o = offset->get_int();
1080N/A const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr();
1080N/A ciTypeArray* value_array = t->const_oop()->as_type_array();
1080N/A for (int e = 0; e < c; e++) {
1080N/A __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
1080N/A __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx);
1080N/A start = __ AddI(start, __ intcon(1));
1080N/A }
1080N/A } else {
1080N/A Node* src_ptr = kit.array_element_address(value, offset, T_CHAR);
1080N/A Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR);
1080N/A Node* c = count;
1080N/A Node* extra = NULL;
1080N/A#ifdef _LP64
1080N/A c = __ ConvI2L(c);
1080N/A extra = C->top();
1080N/A#endif
1080N/A Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP,
1080N/A OptoRuntime::fast_arraycopy_Type(),
1080N/A CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()),
1080N/A "jshort_disjoint_arraycopy", TypeAryPtr::CHARS,
1080N/A src_ptr, dst_ptr, c, extra);
1080N/A start = __ AddI(start, count);
1080N/A }
1080N/A return start;
1080N/A}
1080N/A
1080N/A
1080N/Avoid PhaseStringOpts::replace_string_concat(StringConcat* sc) {
1080N/A // Log a little info about the transformation
1080N/A sc->maybe_log_transform();
1080N/A
1080N/A // pull the JVMState of the allocation into a SafePointNode to serve as
1080N/A // as a shim for the insertion of the new code.
1080N/A JVMState* jvms = sc->begin()->jvms()->clone_shallow(C);
1080N/A uint size = sc->begin()->req();
4022N/A SafePointNode* map = new (C) SafePointNode(size, jvms);
1080N/A
1080N/A // copy the control and memory state from the final call into our
1080N/A // new starting state. This allows any preceeding tests to feed
1080N/A // into the new section of code.
1080N/A for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
1080N/A map->init_req(i1, sc->end()->in(i1));
1080N/A }
1080N/A // blow away old allocation arguments
1080N/A for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
1080N/A map->init_req(i1, C->top());
1080N/A }
1080N/A // Copy the rest of the inputs for the JVMState
1080N/A for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
1080N/A map->init_req(i1, sc->begin()->in(i1));
1080N/A }
1080N/A // Make sure the memory state is a MergeMem for parsing.
1080N/A if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
1080N/A map->set_req(TypeFunc::Memory, MergeMemNode::make(C, map->in(TypeFunc::Memory)));
1080N/A }
1080N/A
1080N/A jvms->set_map(map);
1080N/A map->ensure_stack(jvms, jvms->method()->max_stack());
1080N/A
1080N/A
1080N/A // disconnect all the old StringBuilder calls from the graph
1080N/A sc->eliminate_unneeded_control();
1080N/A
1080N/A // At this point all the old work has been completely removed from
1080N/A // the graph and the saved JVMState exists at the point where the
1080N/A // final toString call used to be.
1080N/A GraphKit kit(jvms);
1080N/A
1080N/A // There may be uncommon traps which are still using the
1080N/A // intermediate states and these need to be rewritten to point at
1080N/A // the JVMState at the beginning of the transformation.
1080N/A sc->convert_uncommon_traps(kit, jvms);
1080N/A
1080N/A // Now insert the logic to compute the size of the string followed
1080N/A // by all the logic to construct array and resulting string.
1080N/A
1080N/A Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
1080N/A
1080N/A // Create a region for the overflow checks to merge into.
1080N/A int args = MAX2(sc->num_arguments(), 1);
4022N/A RegionNode* overflow = new (C) RegionNode(args);
1080N/A kit.gvn().set_type(overflow, Type::CONTROL);
1080N/A
1080N/A // Create a hook node to hold onto the individual sizes since they
1080N/A // are need for the copying phase.
4022N/A Node* string_sizes = new (C) Node(args);
1080N/A
1080N/A Node* length = __ intcon(0);
1080N/A for (int argi = 0; argi < sc->num_arguments(); argi++) {
1080N/A Node* arg = sc->argument(argi);
1080N/A switch (sc->mode(argi)) {
1080N/A case StringConcat::IntMode: {
1080N/A Node* string_size = int_stringSize(kit, arg);
1080N/A
1080N/A // accumulate total
1080N/A length = __ AddI(length, string_size);
1080N/A
1080N/A // Cache this value for the use by int_toString
1080N/A string_sizes->init_req(argi, string_size);
1080N/A break;
1080N/A }
1978N/A case StringConcat::StringNullCheckMode: {
1978N/A const Type* type = kit.gvn().type(arg);
1978N/A assert(type != TypePtr::NULL_PTR, "missing check");
1978N/A if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1978N/A // Null check with uncommont trap since
1978N/A // StringBuilder(null) throws exception.
1978N/A // Use special uncommon trap instead of
1978N/A // calling normal do_null_check().
1978N/A Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1978N/A IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1978N/A overflow->add_req(__ IfFalse(iff));
1978N/A Node* notnull = __ IfTrue(iff);
1978N/A kit.set_control(notnull); // set control for the cast_not_null
1978N/A arg = kit.cast_not_null(arg, false);
1978N/A sc->set_argument(argi, arg);
1978N/A }
1978N/A assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
1978N/A // Fallthrough to add string length.
1978N/A }
1080N/A case StringConcat::StringMode: {
1080N/A const Type* type = kit.gvn().type(arg);
1080N/A if (type == TypePtr::NULL_PTR) {
1080N/A // replace the argument with the null checked version
1080N/A arg = null_string;
1080N/A sc->set_argument(argi, arg);
1080N/A } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
1080N/A // s = s != null ? s : "null";
1080N/A // length = length + (s.count - s.offset);
4022N/A RegionNode *r = new (C) RegionNode(3);
1080N/A kit.gvn().set_type(r, Type::CONTROL);
4022N/A Node *phi = new (C) PhiNode(r, type);
1080N/A kit.gvn().set_type(phi, phi->bottom_type());
1080N/A Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
1080N/A Node* notnull = __ IfTrue(iff);
1080N/A Node* isnull = __ IfFalse(iff);
1250N/A kit.set_control(notnull); // set control for the cast_not_null
1080N/A r->init_req(1, notnull);
1250N/A phi->init_req(1, kit.cast_not_null(arg, false));
1080N/A r->init_req(2, isnull);
1080N/A phi->init_req(2, null_string);
1080N/A kit.set_control(r);
1080N/A C->record_for_igvn(r);
1080N/A C->record_for_igvn(phi);
1080N/A // replace the argument with the null checked version
1080N/A arg = phi;
1080N/A sc->set_argument(argi, arg);
1080N/A }
3726N/A
3726N/A Node* count = kit.load_String_length(kit.control(), arg);
3726N/A
1080N/A length = __ AddI(length, count);
1080N/A string_sizes->init_req(argi, NULL);
1080N/A break;
1080N/A }
1080N/A case StringConcat::CharMode: {
1080N/A // one character only
1080N/A length = __ AddI(length, __ intcon(1));
1080N/A break;
1080N/A }
1080N/A default:
1080N/A ShouldNotReachHere();
1080N/A }
1080N/A if (argi > 0) {
1080N/A // Check that the sum hasn't overflowed
1080N/A IfNode* iff = kit.create_and_map_if(kit.control(),
1080N/A __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
1080N/A PROB_MIN, COUNT_UNKNOWN);
1080N/A kit.set_control(__ IfFalse(iff));
1080N/A overflow->set_req(argi, __ IfTrue(iff));
1080N/A }
1080N/A }
1080N/A
1080N/A {
1080N/A // Hook
1080N/A PreserveJVMState pjvms(&kit);
1080N/A kit.set_control(overflow);
1978N/A C->record_for_igvn(overflow);
1080N/A kit.uncommon_trap(Deoptimization::Reason_intrinsic,
1080N/A Deoptimization::Action_make_not_entrant);
1080N/A }
1080N/A
4127N/A Node* result;
4127N/A if (!kit.stopped()) {
4127N/A
4127N/A // length now contains the number of characters needed for the
4127N/A // char[] so create a new AllocateArray for the char[]
4127N/A Node* char_array = NULL;
4127N/A {
4127N/A PreserveReexecuteState preexecs(&kit);
4127N/A // The original jvms is for an allocation of either a String or
4127N/A // StringBuffer so no stack adjustment is necessary for proper
4127N/A // reexecution. If we deoptimize in the slow path the bytecode
4127N/A // will be reexecuted and the char[] allocation will be thrown away.
4127N/A kit.jvms()->set_should_reexecute(true);
4127N/A char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))),
4127N/A length, 1);
4127N/A }
4127N/A
4127N/A // Mark the allocation so that zeroing is skipped since the code
4127N/A // below will overwrite the entire array
4127N/A AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn);
4127N/A char_alloc->maybe_set_complete(_gvn);
1080N/A
4127N/A // Now copy the string representations into the final char[]
4127N/A Node* start = __ intcon(0);
4127N/A for (int argi = 0; argi < sc->num_arguments(); argi++) {
4127N/A Node* arg = sc->argument(argi);
4127N/A switch (sc->mode(argi)) {
4127N/A case StringConcat::IntMode: {
4127N/A Node* end = __ AddI(start, string_sizes->in(argi));
4127N/A // getChars words backwards so pass the ending point as well as the start
4127N/A int_getChars(kit, arg, char_array, start, end);
4127N/A start = end;
4127N/A break;
4127N/A }
4127N/A case StringConcat::StringNullCheckMode:
4127N/A case StringConcat::StringMode: {
4127N/A start = copy_string(kit, arg, char_array, start);
4127N/A break;
4127N/A }
4127N/A case StringConcat::CharMode: {
4127N/A __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
4127N/A arg, T_CHAR, char_adr_idx);
4127N/A start = __ AddI(start, __ intcon(1));
4127N/A break;
4127N/A }
4127N/A default:
4127N/A ShouldNotReachHere();
1080N/A }
1080N/A }
1080N/A
4127N/A // If we're not reusing an existing String allocation then allocate one here.
4127N/A result = sc->string_alloc();
4127N/A if (result == NULL) {
4127N/A PreserveReexecuteState preexecs(&kit);
4127N/A // The original jvms is for an allocation of either a String or
4127N/A // StringBuffer so no stack adjustment is necessary for proper
4127N/A // reexecution.
4127N/A kit.jvms()->set_should_reexecute(true);
4127N/A result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
4127N/A }
4127N/A
4127N/A // Intialize the string
4127N/A if (java_lang_String::has_offset_field()) {
4127N/A kit.store_String_offset(kit.control(), result, __ intcon(0));
4127N/A kit.store_String_length(kit.control(), result, length);
4127N/A }
4127N/A kit.store_String_value(kit.control(), result, char_array);
4127N/A } else {
4127N/A result = C->top();
1080N/A }
1080N/A // hook up the outgoing control and result
1080N/A kit.replace_call(sc->end(), result);
1080N/A
1080N/A // Unhook any hook nodes
4123N/A string_sizes->disconnect_inputs(NULL, C);
1080N/A sc->cleanup();
1080N/A}