src/share/vm/opto/stringopts.cpp

Tue, 12 Jun 2012 14:31:44 -0700

author
twisti
date
Tue, 12 Jun 2012 14:31:44 -0700
changeset 3846
8b0a4867acf0
parent 3760
8f972594effc
child 3889
751bd303aa45
permissions
-rw-r--r--

7174218: remove AtomicLongCSImpl intrinsics
Reviewed-by: kvn, twisti
Contributed-by: Krystal Mok <sajia@taobao.com>

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

mercurial