src/share/vm/opto/stringopts.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot-comp Sdiff src/share/vm/opto

src/share/vm/opto/stringopts.cpp

Print this page


   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  *


  33 #include "opto/rootnode.hpp"
  34 #include "opto/runtime.hpp"
  35 #include "opto/stringopts.hpp"
  36 #include "opto/subnode.hpp"
  37 
  38 #define __ kit.
  39 
  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
  49 
  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.
  53 
  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   };
  65 
  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) Node(1);
  73     _arguments->del_req(0);
  74   }
  75 

  76   bool validate_control_flow();
  77 
  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         }


 172     int amode = mode(i);
 173     if (amode == StringConcat::StringMode ||
 174         amode == StringConcat::StringNullCheckMode) {
 175       arg = skip_string_null_check(arg);
 176     }
 177     return arg;
 178   }
 179   void set_argument(int i, Node* value) {
 180     _arguments->set_req(i, value);
 181   }
 182   int num_arguments() {
 183     return _mode.length();
 184   }
 185   int mode(int i) {
 186     return _mode.at(i);
 187   }
 188   void add_control(Node* ctrl) {
 189     assert(!_control.contains(ctrl), "only push once");
 190     _control.push(ctrl);
 191   }




 192   CallStaticJavaNode* end() { return _end; }
 193   AllocateNode* begin() { return _begin; }
 194   Node* string_alloc() { return _string_alloc; }
 195 
 196   void eliminate_unneeded_control();
 197   void eliminate_initialize(InitializeNode* init);
 198   void eliminate_call(CallNode* call);
 199 
 200   void maybe_log_transform() {
 201     CompileLog* log = _stringopts->C->log();
 202     if (log != NULL) {
 203       log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'",
 204                 num_arguments(),
 205                 _string_alloc != NULL,
 206                 _multiple);
 207       JVMState* p = _begin->jvms();
 208       while (p != NULL) {
 209         log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
 210         p = p->caller();
 211       }


 284     Node* n = other->_control.at(x);
 285     if (n->is_Call()) {
 286       result->_control.push(n);
 287     }
 288   }
 289   assert(result->_control.contains(other->_end), "what?");
 290   assert(result->_control.contains(_begin), "what?");
 291   for (int x = 0; x < num_arguments(); x++) {
 292     Node* argx = argument_uncast(x);
 293     if (argx == arg) {
 294       // replace the toString result with the all the arguments that
 295       // made up the other StringConcat
 296       for (int y = 0; y < other->num_arguments(); y++) {
 297         result->append(other->argument(y), other->mode(y));
 298       }
 299     } else {
 300       result->append(argx, mode(x));
 301     }
 302   }
 303   result->set_allocation(other->_begin);






 304   result->_multiple = true;
 305   return result;
 306 }
 307 
 308 
 309 void StringConcat::eliminate_call(CallNode* call) {
 310   Compile* C = _stringopts->C;
 311   CallProjections projs;
 312   call->extract_projections(&projs, false);
 313   if (projs.fallthrough_catchproj != NULL) {
 314     C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control));
 315   }
 316   if (projs.fallthrough_memproj != NULL) {
 317     C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory));
 318   }
 319   if (projs.catchall_memproj != NULL) {
 320     C->gvn_replace_by(projs.catchall_memproj, C->top());
 321   }
 322   if (projs.fallthrough_ioproj != NULL) {
 323     C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O));


 493           break;
 494         }
 495       }
 496       if (constructor == NULL) {
 497         // couldn't find constructor
 498 #ifndef PRODUCT
 499         if (PrintOptimizeStringConcat) {
 500           tty->print("giving up because couldn't find constructor ");
 501           alloc->jvms()->dump_spec(tty); tty->cr();
 502         }
 503 #endif
 504         break;
 505       }
 506 
 507       // Walked all the way back and found the constructor call so see
 508       // if this call converted into a direct string concatenation.
 509       sc->add_control(call);
 510       sc->add_control(constructor);
 511       sc->add_control(alloc);
 512       sc->set_allocation(alloc);
 513       if (sc->validate_control_flow()) {

 514         return sc;
 515       } else {
 516         return NULL;
 517       }
 518     } else if (cnode->method() == NULL) {
 519       break;
 520     } else if (!cnode->method()->is_static() &&
 521                cnode->method()->holder() == m->holder() &&
 522                cnode->method()->name() == ciSymbol::append_name() &&
 523                (cnode->method()->signature()->as_symbol() == string_sig ||
 524                 cnode->method()->signature()->as_symbol() == char_sig ||
 525                 cnode->method()->signature()->as_symbol() == int_sig)) {
 526       sc->add_control(cnode);
 527       Node* arg = cnode->in(TypeFunc::Parms + 1);
 528       if (cnode->method()->signature()->as_symbol() == int_sig) {
 529         sc->push_int(arg);
 530       } else if (cnode->method()->signature()->as_symbol() == char_sig) {
 531         sc->push_char(arg);
 532       } else {
 533         if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {


 603 
 604   // try to coalesce separate concats
 605  restart:
 606   for (int c = 0; c < concats.length(); c++) {
 607     StringConcat* sc = concats.at(c);
 608     for (int i = 0; i < sc->num_arguments(); i++) {
 609       Node* arg = sc->argument_uncast(i);
 610       if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) {
 611         CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
 612         for (int o = 0; o < concats.length(); o++) {
 613           if (c == o) continue;
 614           StringConcat* other = concats.at(o);
 615           if (other->end() == csj) {
 616 #ifndef PRODUCT
 617             if (PrintOptimizeStringConcat) {
 618               tty->print_cr("considering stacked concats");
 619             }
 620 #endif
 621 
 622             StringConcat* merged = sc->merge(other, arg);
 623             if (merged->validate_control_flow()) {
 624 #ifndef PRODUCT
 625               if (PrintOptimizeStringConcat) {
 626                 tty->print_cr("stacking would succeed");
 627               }
 628 #endif
 629               if (c < o) {
 630                 concats.remove_at(o);
 631                 concats.at_put(c, merged);
 632               } else {
 633                 concats.remove_at(c);
 634                 concats.at_put(o, merged);
 635               }
 636               goto restart;
 637             } else {
 638 #ifndef PRODUCT
 639               if (PrintOptimizeStringConcat) {
 640                 tty->print_cr("stacking would fail");
 641               }
 642 #endif
 643             }


 691         // get unhappy about the partially collapsed graph.
 692         for (SimpleDUIterator i(use); i.has_next(); i.next()) {
 693           Node* m = i.get();
 694           if (m->is_AddP()) {
 695             dead_worklist.push(m);
 696           }
 697         }
 698         C->gvn_replace_by(use, C->top());
 699         break;
 700       }
 701       case Op_Phi:
 702         if (use->in(0) == C->top()) {
 703           C->gvn_replace_by(use, C->top());
 704         }
 705         break;
 706     }
 707   }
 708 }
 709 
 710 













































































 711 bool StringConcat::validate_control_flow() {
 712   // We found all the calls and arguments now lets see if it's
 713   // safe to transform the graph as we would expect.
 714 
 715   // Check to see if this resulted in too many uncommon traps previously
 716   if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(),
 717                         Deoptimization::Reason_intrinsic)) {
 718     return false;
 719   }
 720 
 721   // Walk backwards over the control flow from toString to the
 722   // allocation and make sure all the control flow is ok.  This
 723   // means it's either going to be eliminated once the calls are
 724   // removed or it can safely be transformed into an uncommon
 725   // trap.
 726 
 727   int null_check_count = 0;
 728   Unique_Node_List ctrl_path;
 729 
 730   assert(_control.contains(_begin), "missing");


 736     Node* n = _control.at(i);
 737     if (n->is_Allocate()) {
 738       AllocateNode* an = n->as_Allocate();
 739       InitializeNode* init = an->initialization();
 740       ctrl_path.push(init);
 741       ctrl_path.push(init->as_Multi()->proj_out(0));
 742     }
 743     if (n->is_Call()) {
 744       CallNode* cn = n->as_Call();
 745       ctrl_path.push(cn);
 746       ctrl_path.push(cn->proj_out(0));
 747       ctrl_path.push(cn->proj_out(0)->unique_out());
 748       if (cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0) != NULL) {
 749         ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0));
 750       }
 751     } else {
 752       ShouldNotReachHere();
 753     }
 754   }
 755 
 756   // Skip backwards through the control checking for unexpected contro flow
 757   Node* ptr = _end;
 758   bool fail = false;
 759   while (ptr != _begin) {
 760     if (ptr->is_Call() && ctrl_path.member(ptr)) {
 761       ptr = ptr->in(0);
 762     } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
 763       ptr = ptr->in(0)->in(0)->in(0);
 764       assert(ctrl_path.member(ptr), "should be a known piece of control");
 765     } else if (ptr->is_IfTrue()) {
 766       IfNode* iff = ptr->in(0)->as_If();
 767       BoolNode* b = iff->in(1)->isa_Bool();
 768 
 769       if (b == NULL) {
 770         fail = true;
 771         break;
 772       }
 773 
 774       Node* cmp = b->in(1);
 775       Node* v1 = cmp->in(1);
 776       Node* v2 = cmp->in(2);


 919       }
 920 #ifndef PRODUCT
 921       if (PrintOptimizeStringConcat) {
 922         if (result != last_result) {
 923           last_result = result;
 924           tty->print_cr("extra uses for result:");
 925           last_result->dump();
 926         }
 927         use->dump();
 928       }
 929 #endif
 930       fail = true;
 931       break;
 932     }
 933   }
 934 
 935 #ifndef PRODUCT
 936   if (PrintOptimizeStringConcat && !fail) {
 937     ttyLocker ttyl;
 938     tty->cr();
 939     tty->print("fusion would succeed (%d %d) for ", null_check_count, _uncommon_traps.size());
 940     _begin->jvms()->dump_spec(tty); tty->cr();
 941     for (int i = 0; i < num_arguments(); i++) {
 942       argument(i)->dump();
 943     }
 944     _control.dump();
 945     tty->cr();
 946   }
 947 #endif
 948 
 949   return !fail;
 950 }
 951 
 952 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
 953   const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror());
 954   Node* klass_node = __ makecon(mirror_type);
 955   BasicType bt = field->layout_type();
 956   ciType* field_klass = field->type();
 957 
 958   const Type *type;
 959   if( bt == T_OBJECT ) {


   1 /*
   2  * Copyright (c) 2009, 2013, 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  *


  33 #include "opto/rootnode.hpp"
  34 #include "opto/runtime.hpp"
  35 #include "opto/stringopts.hpp"
  36 #include "opto/subnode.hpp"
  37 
  38 #define __ kit.
  39 
  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
  49 
  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.
  53   Node_List           _constructors;   // List of constructors (many in case of stacked concat)
  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 
  58  public:
  59   // Mode for converting arguments to Strings
  60   enum {
  61     StringMode,
  62     IntMode,
  63     CharMode,
  64     StringNullCheckMode
  65   };
  66 
  67   StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end):
  68     _end(end),
  69     _begin(NULL),
  70     _multiple(false),
  71     _string_alloc(NULL),
  72     _stringopts(stringopts) {
  73     _arguments = new (_stringopts->C) Node(1);
  74     _arguments->del_req(0);
  75   }
  76 
  77   bool validate_mem_flow();
  78   bool validate_control_flow();
  79 
  80   void merge_add() {
  81 #if 0
  82     // XXX This is place holder code for reusing an existing String
  83     // allocation but the logic for checking the state safety is
  84     // probably inadequate at the moment.
  85     CallProjections endprojs;
  86     sc->end()->extract_projections(&endprojs, false);
  87     if (endprojs.resproj != NULL) {
  88       for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) {
  89         CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
  90         if (use != NULL && use->method() != NULL &&
  91             use->method()->intrinsic_id() == vmIntrinsics::_String_String &&
  92             use->in(TypeFunc::Parms + 1) == endprojs.resproj) {
  93           // Found useless new String(sb.toString()) so reuse the newly allocated String
  94           // when creating the result instead of allocating a new one.
  95           sc->set_string_alloc(use->in(TypeFunc::Parms));
  96           sc->set_end(use);
  97         }


 174     int amode = mode(i);
 175     if (amode == StringConcat::StringMode ||
 176         amode == StringConcat::StringNullCheckMode) {
 177       arg = skip_string_null_check(arg);
 178     }
 179     return arg;
 180   }
 181   void set_argument(int i, Node* value) {
 182     _arguments->set_req(i, value);
 183   }
 184   int num_arguments() {
 185     return _mode.length();
 186   }
 187   int mode(int i) {
 188     return _mode.at(i);
 189   }
 190   void add_control(Node* ctrl) {
 191     assert(!_control.contains(ctrl), "only push once");
 192     _control.push(ctrl);
 193   }
 194   void add_constructor(Node* init) {
 195     assert(!_constructors.contains(init), "only push once");
 196     _constructors.push(init);
 197   }
 198   CallStaticJavaNode* end() { return _end; }
 199   AllocateNode* begin() { return _begin; }
 200   Node* string_alloc() { return _string_alloc; }
 201 
 202   void eliminate_unneeded_control();
 203   void eliminate_initialize(InitializeNode* init);
 204   void eliminate_call(CallNode* call);
 205 
 206   void maybe_log_transform() {
 207     CompileLog* log = _stringopts->C->log();
 208     if (log != NULL) {
 209       log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'",
 210                 num_arguments(),
 211                 _string_alloc != NULL,
 212                 _multiple);
 213       JVMState* p = _begin->jvms();
 214       while (p != NULL) {
 215         log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
 216         p = p->caller();
 217       }


 290     Node* n = other->_control.at(x);
 291     if (n->is_Call()) {
 292       result->_control.push(n);
 293     }
 294   }
 295   assert(result->_control.contains(other->_end), "what?");
 296   assert(result->_control.contains(_begin), "what?");
 297   for (int x = 0; x < num_arguments(); x++) {
 298     Node* argx = argument_uncast(x);
 299     if (argx == arg) {
 300       // replace the toString result with the all the arguments that
 301       // made up the other StringConcat
 302       for (int y = 0; y < other->num_arguments(); y++) {
 303         result->append(other->argument(y), other->mode(y));
 304       }
 305     } else {
 306       result->append(argx, mode(x));
 307     }
 308   }
 309   result->set_allocation(other->_begin);
 310   for (uint i = 0; i < _constructors.size(); i++) {
 311     result->add_constructor(_constructors.at(i));
 312   }
 313   for (uint i = 0; i < other->_constructors.size(); i++) {
 314     result->add_constructor(other->_constructors.at(i));
 315   }
 316   result->_multiple = true;
 317   return result;
 318 }
 319 
 320 
 321 void StringConcat::eliminate_call(CallNode* call) {
 322   Compile* C = _stringopts->C;
 323   CallProjections projs;
 324   call->extract_projections(&projs, false);
 325   if (projs.fallthrough_catchproj != NULL) {
 326     C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control));
 327   }
 328   if (projs.fallthrough_memproj != NULL) {
 329     C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory));
 330   }
 331   if (projs.catchall_memproj != NULL) {
 332     C->gvn_replace_by(projs.catchall_memproj, C->top());
 333   }
 334   if (projs.fallthrough_ioproj != NULL) {
 335     C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O));


 505           break;
 506         }
 507       }
 508       if (constructor == NULL) {
 509         // couldn't find constructor
 510 #ifndef PRODUCT
 511         if (PrintOptimizeStringConcat) {
 512           tty->print("giving up because couldn't find constructor ");
 513           alloc->jvms()->dump_spec(tty); tty->cr();
 514         }
 515 #endif
 516         break;
 517       }
 518 
 519       // Walked all the way back and found the constructor call so see
 520       // if this call converted into a direct string concatenation.
 521       sc->add_control(call);
 522       sc->add_control(constructor);
 523       sc->add_control(alloc);
 524       sc->set_allocation(alloc);
 525       sc->add_constructor(constructor);
 526       if (sc->validate_control_flow() && sc->validate_mem_flow()) {
 527         return sc;
 528       } else {
 529         return NULL;
 530       }
 531     } else if (cnode->method() == NULL) {
 532       break;
 533     } else if (!cnode->method()->is_static() &&
 534                cnode->method()->holder() == m->holder() &&
 535                cnode->method()->name() == ciSymbol::append_name() &&
 536                (cnode->method()->signature()->as_symbol() == string_sig ||
 537                 cnode->method()->signature()->as_symbol() == char_sig ||
 538                 cnode->method()->signature()->as_symbol() == int_sig)) {
 539       sc->add_control(cnode);
 540       Node* arg = cnode->in(TypeFunc::Parms + 1);
 541       if (cnode->method()->signature()->as_symbol() == int_sig) {
 542         sc->push_int(arg);
 543       } else if (cnode->method()->signature()->as_symbol() == char_sig) {
 544         sc->push_char(arg);
 545       } else {
 546         if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {


 616 
 617   // try to coalesce separate concats
 618  restart:
 619   for (int c = 0; c < concats.length(); c++) {
 620     StringConcat* sc = concats.at(c);
 621     for (int i = 0; i < sc->num_arguments(); i++) {
 622       Node* arg = sc->argument_uncast(i);
 623       if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) {
 624         CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
 625         for (int o = 0; o < concats.length(); o++) {
 626           if (c == o) continue;
 627           StringConcat* other = concats.at(o);
 628           if (other->end() == csj) {
 629 #ifndef PRODUCT
 630             if (PrintOptimizeStringConcat) {
 631               tty->print_cr("considering stacked concats");
 632             }
 633 #endif
 634 
 635             StringConcat* merged = sc->merge(other, arg);
 636             if (merged->validate_control_flow() && merged->validate_mem_flow()) {
 637 #ifndef PRODUCT
 638               if (PrintOptimizeStringConcat) {
 639                 tty->print_cr("stacking would succeed");
 640               }
 641 #endif
 642               if (c < o) {
 643                 concats.remove_at(o);
 644                 concats.at_put(c, merged);
 645               } else {
 646                 concats.remove_at(c);
 647                 concats.at_put(o, merged);
 648               }
 649               goto restart;
 650             } else {
 651 #ifndef PRODUCT
 652               if (PrintOptimizeStringConcat) {
 653                 tty->print_cr("stacking would fail");
 654               }
 655 #endif
 656             }


 704         // get unhappy about the partially collapsed graph.
 705         for (SimpleDUIterator i(use); i.has_next(); i.next()) {
 706           Node* m = i.get();
 707           if (m->is_AddP()) {
 708             dead_worklist.push(m);
 709           }
 710         }
 711         C->gvn_replace_by(use, C->top());
 712         break;
 713       }
 714       case Op_Phi:
 715         if (use->in(0) == C->top()) {
 716           C->gvn_replace_by(use, C->top());
 717         }
 718         break;
 719     }
 720   }
 721 }
 722 
 723 
 724 bool StringConcat::validate_mem_flow() {
 725   Compile* C = _stringopts->C;
 726 
 727   for (uint i = 0; i < _control.size(); i++) {
 728 #ifndef PRODUCT
 729     Node_List path;
 730 #endif
 731     Node* curr = _control.at(i);
 732     if (curr->is_Call() && curr != _begin) { // For all calls except the first allocation
 733       // Now here's the main invariant in our case:
 734       // For memory between the constructor, and appends, and toString we should only see bottom memory,
 735       // produced by the previous call we know about.
 736       if (!_constructors.contains(curr)) {
 737         NOT_PRODUCT(path.push(curr);)
 738         Node* mem = curr->in(TypeFunc::Memory);
 739         assert(mem != NULL, "calls should have memory edge");
 740         assert(!mem->is_Phi(), "should be handled by control flow validation");
 741         NOT_PRODUCT(path.push(mem);)
 742         if (mem->is_MergeMem()) {
 743           while (mem->is_MergeMem()) {
 744             for (uint i = 1; i < mem->req(); i++) {
 745               if (i != Compile::AliasIdxBot && mem->in(i) != C->top()) {
 746 #ifndef PRODUCT
 747                 tty->print("fusion has incorrect memory flow (side effects) for ");
 748                 _begin->jvms()->dump_spec(tty); tty->cr();
 749                 path.dump();
 750 #endif
 751                 return false;
 752               }
 753             }
 754             // skip through a potential MergeMem chain, linked through Bot
 755             mem = mem->in(Compile::AliasIdxBot);
 756             NOT_PRODUCT(path.push(mem);)
 757           }
 758           // now let it fall though, and see if the have a projection
 759         }
 760         if (mem->is_Proj()) {
 761           // Should point to a previous known call
 762           Node *prev = mem->in(0);
 763           NOT_PRODUCT(path.push(prev);)
 764           if (!prev->is_Call() || !_control.contains(prev)) {
 765 #ifndef PRODUCT
 766             tty->print("fusion has incorrect memory flow (unknown call) for ");
 767             _begin->jvms()->dump_spec(tty); tty->cr();
 768             path.dump();
 769 #endif
 770             return false;
 771           }
 772         } else {
 773           assert(mem->is_Store() || mem->is_LoadStore(), err_msg_res("Unexpected node type: %s", mem->Name()));
 774 #ifndef PRODUCT
 775           tty->print("fusion has incorrect memory flow (unexpected source) for ");
 776           _begin->jvms()->dump_spec(tty); tty->cr();
 777           path.dump();
 778 #endif
 779           return false;
 780         }
 781       } else {
 782         // For memory that feeds into constructors it's more complicated.
 783         // However the advantage is that any side effect that happens between the Allocate/Initialize and
 784         // the constructor will have to be control-dependent on Initialize.
 785         // So we actually don't have to do anything, since it's going to be caught by the control flow
 786         // analysis.
 787       }
 788     }
 789   }
 790 
 791 #ifndef PRODUCT
 792   if (PrintOptimizeStringConcat) {
 793     tty->print("fusion has correct memory flow for ");
 794     _begin->jvms()->dump_spec(tty); tty->cr();
 795     tty->cr();
 796   }
 797 #endif
 798   return true;
 799 }
 800 
 801 bool StringConcat::validate_control_flow() {
 802   // We found all the calls and arguments now lets see if it's
 803   // safe to transform the graph as we would expect.
 804 
 805   // Check to see if this resulted in too many uncommon traps previously
 806   if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(),
 807                         Deoptimization::Reason_intrinsic)) {
 808     return false;
 809   }
 810 
 811   // Walk backwards over the control flow from toString to the
 812   // allocation and make sure all the control flow is ok.  This
 813   // means it's either going to be eliminated once the calls are
 814   // removed or it can safely be transformed into an uncommon
 815   // trap.
 816 
 817   int null_check_count = 0;
 818   Unique_Node_List ctrl_path;
 819 
 820   assert(_control.contains(_begin), "missing");


 826     Node* n = _control.at(i);
 827     if (n->is_Allocate()) {
 828       AllocateNode* an = n->as_Allocate();
 829       InitializeNode* init = an->initialization();
 830       ctrl_path.push(init);
 831       ctrl_path.push(init->as_Multi()->proj_out(0));
 832     }
 833     if (n->is_Call()) {
 834       CallNode* cn = n->as_Call();
 835       ctrl_path.push(cn);
 836       ctrl_path.push(cn->proj_out(0));
 837       ctrl_path.push(cn->proj_out(0)->unique_out());
 838       if (cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0) != NULL) {
 839         ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0));
 840       }
 841     } else {
 842       ShouldNotReachHere();
 843     }
 844   }
 845 
 846   // Skip backwards through the control checking for unexpected control flow
 847   Node* ptr = _end;
 848   bool fail = false;
 849   while (ptr != _begin) {
 850     if (ptr->is_Call() && ctrl_path.member(ptr)) {
 851       ptr = ptr->in(0);
 852     } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
 853       ptr = ptr->in(0)->in(0)->in(0);
 854       assert(ctrl_path.member(ptr), "should be a known piece of control");
 855     } else if (ptr->is_IfTrue()) {
 856       IfNode* iff = ptr->in(0)->as_If();
 857       BoolNode* b = iff->in(1)->isa_Bool();
 858 
 859       if (b == NULL) {
 860         fail = true;
 861         break;
 862       }
 863 
 864       Node* cmp = b->in(1);
 865       Node* v1 = cmp->in(1);
 866       Node* v2 = cmp->in(2);


1009       }
1010 #ifndef PRODUCT
1011       if (PrintOptimizeStringConcat) {
1012         if (result != last_result) {
1013           last_result = result;
1014           tty->print_cr("extra uses for result:");
1015           last_result->dump();
1016         }
1017         use->dump();
1018       }
1019 #endif
1020       fail = true;
1021       break;
1022     }
1023   }
1024 
1025 #ifndef PRODUCT
1026   if (PrintOptimizeStringConcat && !fail) {
1027     ttyLocker ttyl;
1028     tty->cr();
1029     tty->print("fusion has correct control flow (%d %d) for ", null_check_count, _uncommon_traps.size());
1030     _begin->jvms()->dump_spec(tty); tty->cr();
1031     for (int i = 0; i < num_arguments(); i++) {
1032       argument(i)->dump();
1033     }
1034     _control.dump();
1035     tty->cr();
1036   }
1037 #endif
1038 
1039   return !fail;
1040 }
1041 
1042 Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
1043   const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror());
1044   Node* klass_node = __ makecon(mirror_type);
1045   BasicType bt = field->layout_type();
1046   ciType* field_klass = field->type();
1047 
1048   const Type *type;
1049   if( bt == T_OBJECT ) {


src/share/vm/opto/stringopts.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File