hotspot/src/share/vm/opto/stringopts.cpp
author iveresov
Thu, 22 Jan 2015 11:25:23 -0800
changeset 28723 0a36120cb225
parent 28642 a42fefc69922
child 29081 c61eb4914428
permissions -rw-r--r--
8071302: assert(!_reg_node[reg_lo] || edge_from_to(_reg_node[reg_lo], def)) failed: after block local Summary: Add merge nodes to node to block mapping Reviewed-by: kvn, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     1
/*
25930
eae8b7490d2c 8054033: Remove unused references to Compile*
thartmann
parents: 24923
diff changeset
     2
 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     4
 *
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     8
 *
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    13
 * accompanied this code).
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    14
 *
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4891
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4891
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4891
diff changeset
    21
 * questions.
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    22
 *
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    23
 */
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    26
#include "compiler/compileLog.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    27
#include "opto/addnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    28
#include "opto/callGenerator.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    29
#include "opto/callnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    30
#include "opto/divnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    31
#include "opto/graphKit.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    32
#include "opto/idealKit.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    33
#include "opto/rootnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    34
#include "opto/runtime.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    35
#include "opto/stringopts.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6743
diff changeset
    36
#include "opto/subnode.hpp"
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    37
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    38
#define __ kit.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    39
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    40
class StringConcat : public ResourceObj {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    41
 private:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    42
  PhaseStringOpts*    _stringopts;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    43
  Node*               _string_alloc;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    44
  AllocateNode*       _begin;          // The allocation the begins the pattern
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    45
  CallStaticJavaNode* _end;            // The final call of the pattern.  Will either be
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    46
                                       // SB.toString or or String.<init>(SB.toString)
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    47
  bool                _multiple;       // indicates this is a fusion of two or more
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    48
                                       // separate StringBuilders
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    49
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    50
  Node*               _arguments;      // The list of arguments to be concatenated
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    51
  GrowableArray<int>  _mode;           // into a String along with a mode flag
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    52
                                       // indicating how to treat the value.
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
    53
  Node_List           _constructors;   // List of constructors (many in case of stacked concat)
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    54
  Node_List           _control;        // List of control nodes that will be deleted
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    55
  Node_List           _uncommon_traps; // Uncommon traps that needs to be rewritten
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    56
                                       // to restart at the initial JVMState.
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
    57
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    58
 public:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    59
  // Mode for converting arguments to Strings
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    60
  enum {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    61
    StringMode,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    62
    IntMode,
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
    63
    CharMode,
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
    64
    StringNullCheckMode
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    65
  };
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    66
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    67
  StringConcat(PhaseStringOpts* stringopts, CallStaticJavaNode* end):
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    68
    _end(end),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    69
    _begin(NULL),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    70
    _multiple(false),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    71
    _string_alloc(NULL),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    72
    _stringopts(stringopts) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
    73
    _arguments = new Node(1);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    74
    _arguments->del_req(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    75
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    76
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
    77
  bool validate_mem_flow();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    78
  bool validate_control_flow();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    79
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    80
  void merge_add() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    81
#if 0
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    82
    // XXX This is place holder code for reusing an existing String
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    83
    // allocation but the logic for checking the state safety is
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    84
    // probably inadequate at the moment.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    85
    CallProjections endprojs;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    86
    sc->end()->extract_projections(&endprojs, false);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    87
    if (endprojs.resproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    88
      for (SimpleDUIterator i(endprojs.resproj); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    89
        CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    90
        if (use != NULL && use->method() != NULL &&
6743
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
    91
            use->method()->intrinsic_id() == vmIntrinsics::_String_String &&
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    92
            use->in(TypeFunc::Parms + 1) == endprojs.resproj) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    93
          // Found useless new String(sb.toString()) so reuse the newly allocated String
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    94
          // when creating the result instead of allocating a new one.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    95
          sc->set_string_alloc(use->in(TypeFunc::Parms));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    96
          sc->set_end(use);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    97
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    98
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
    99
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   100
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   101
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   102
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   103
  StringConcat* merge(StringConcat* other, Node* arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   104
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   105
  void set_allocation(AllocateNode* alloc) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   106
    _begin = alloc;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   107
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   108
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   109
  void append(Node* value, int mode) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   110
    _arguments->add_req(value);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   111
    _mode.append(mode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   112
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   113
  void push(Node* value, int mode) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   114
    _arguments->ins_req(0, value);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   115
    _mode.insert_before(0, mode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   116
  }
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   117
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   118
  void push_string(Node* value) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   119
    push(value, StringMode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   120
  }
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   121
  void push_string_null_check(Node* value) {
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   122
    push(value, StringNullCheckMode);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   123
  }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   124
  void push_int(Node* value) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   125
    push(value, IntMode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   126
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   127
  void push_char(Node* value) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   128
    push(value, CharMode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   129
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   130
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   131
  static bool is_SB_toString(Node* call) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   132
    if (call->is_CallStaticJava()) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   133
      CallStaticJavaNode* csj = call->as_CallStaticJava();
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   134
      ciMethod* m = csj->method();
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   135
      if (m != NULL &&
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   136
          (m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString ||
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   137
           m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString)) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   138
        return true;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   139
      }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   140
    }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   141
    return false;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   142
  }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   143
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   144
  static Node* skip_string_null_check(Node* value) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   145
    // Look for a diamond shaped Null check of toString() result
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   146
    // (could be code from String.valueOf()):
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   147
    // (Proj == NULL) ? "null":"CastPP(Proj)#NotNULL
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   148
    if (value->is_Phi()) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   149
      int true_path = value->as_Phi()->is_diamond_phi();
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   150
      if (true_path != 0) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   151
        // phi->region->if_proj->ifnode->bool
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   152
        BoolNode* b = value->in(0)->in(1)->in(0)->in(1)->as_Bool();
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   153
        Node* cmp = b->in(1);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   154
        Node* v1 = cmp->in(1);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   155
        Node* v2 = cmp->in(2);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   156
        // Null check of the return of toString which can simply be skipped.
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   157
        if (b->_test._test == BoolTest::ne &&
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   158
            v2->bottom_type() == TypePtr::NULL_PTR &&
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   159
            value->in(true_path)->Opcode() == Op_CastPP &&
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   160
            value->in(true_path)->in(1) == v1 &&
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   161
            v1->is_Proj() && is_SB_toString(v1->in(0))) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   162
          return v1;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   163
        }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   164
      }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   165
    }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   166
    return value;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   167
  }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   168
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   169
  Node* argument(int i) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   170
    return _arguments->in(i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   171
  }
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   172
  Node* argument_uncast(int i) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   173
    Node* arg = argument(i);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   174
    int amode = mode(i);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   175
    if (amode == StringConcat::StringMode ||
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   176
        amode == StringConcat::StringNullCheckMode) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   177
      arg = skip_string_null_check(arg);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   178
    }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   179
    return arg;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   180
  }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   181
  void set_argument(int i, Node* value) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   182
    _arguments->set_req(i, value);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   183
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   184
  int num_arguments() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   185
    return _mode.length();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   186
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   187
  int mode(int i) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   188
    return _mode.at(i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   189
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   190
  void add_control(Node* ctrl) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   191
    assert(!_control.contains(ctrl), "only push once");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   192
    _control.push(ctrl);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   193
  }
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   194
  void add_constructor(Node* init) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   195
    assert(!_constructors.contains(init), "only push once");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   196
    _constructors.push(init);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   197
  }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   198
  CallStaticJavaNode* end() { return _end; }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   199
  AllocateNode* begin() { return _begin; }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   200
  Node* string_alloc() { return _string_alloc; }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   201
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   202
  void eliminate_unneeded_control();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   203
  void eliminate_initialize(InitializeNode* init);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   204
  void eliminate_call(CallNode* call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   205
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   206
  void maybe_log_transform() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   207
    CompileLog* log = _stringopts->C->log();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   208
    if (log != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   209
      log->head("replace_string_concat arguments='%d' string_alloc='%d' multiple='%d'",
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   210
                num_arguments(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   211
                _string_alloc != NULL,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   212
                _multiple);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   213
      JVMState* p = _begin->jvms();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   214
      while (p != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   215
        log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   216
        p = p->caller();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   217
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   218
      log->tail("replace_string_concat");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   219
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   220
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   221
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   222
  void convert_uncommon_traps(GraphKit& kit, const JVMState* jvms) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   223
    for (uint u = 0; u < _uncommon_traps.size(); u++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   224
      Node* uct = _uncommon_traps.at(u);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   225
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   226
      // Build a new call using the jvms state of the allocate
6418
6671edbd230e 6978355: renaming for 6961697
twisti
parents: 5547
diff changeset
   227
      address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   228
      const TypeFunc* call_type = OptoRuntime::uncommon_trap_Type();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   229
      const TypePtr* no_memory_effects = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   230
      Compile* C = _stringopts->C;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
   231
      CallStaticJavaNode* call = new CallStaticJavaNode(call_type, call_addr, "uncommon_trap",
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
   232
                                                        jvms->bci(), no_memory_effects);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   233
      for (int e = 0; e < TypeFunc::Parms; e++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   234
        call->init_req(e, uct->in(e));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   235
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   236
      // Set the trap request to record intrinsic failure if this trap
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   237
      // is taken too many times.  Ideally we would handle then traps by
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   238
      // doing the original bookkeeping in the MDO so that if it caused
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   239
      // the code to be thrown out we could still recompile and use the
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   240
      // optimization.  Failing the uncommon traps doesn't really mean
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   241
      // that the optimization is a bad idea but there's no other way to
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   242
      // do the MDO updates currently.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   243
      int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_intrinsic,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   244
                                                           Deoptimization::Action_make_not_entrant);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   245
      call->init_req(TypeFunc::Parms, __ intcon(trap_request));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   246
      kit.add_safepoint_edges(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   247
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   248
      _stringopts->gvn()->transform(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   249
      C->gvn_replace_by(uct, call);
14623
70c4c1be0a14 7092905: C2: Keep track of the number of dead nodes
bharadwaj
parents: 13895
diff changeset
   250
      uct->disconnect_inputs(NULL, C);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   251
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   252
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   253
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   254
  void cleanup() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   255
    // disconnect the hook node
14623
70c4c1be0a14 7092905: C2: Keep track of the number of dead nodes
bharadwaj
parents: 13895
diff changeset
   256
    _arguments->disconnect_inputs(NULL, _stringopts->C);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   257
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   258
};
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   259
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   260
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   261
void StringConcat::eliminate_unneeded_control() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   262
  for (uint i = 0; i < _control.size(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   263
    Node* n = _control.at(i);
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   264
    if (n->is_Allocate()) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   265
      eliminate_initialize(n->as_Allocate()->initialization());
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   266
    }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   267
    if (n->is_Call()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   268
      if (n != _end) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   269
        eliminate_call(n->as_Call());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   270
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   271
    } else if (n->is_IfTrue()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   272
      Compile* C = _stringopts->C;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   273
      C->gvn_replace_by(n, n->in(0)->in(0));
15113
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   274
      // get rid of the other projection
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   275
      C->gvn_replace_by(n->in(0)->as_If()->proj_out(false), C->top());
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   276
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   277
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   278
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   279
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   280
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   281
StringConcat* StringConcat::merge(StringConcat* other, Node* arg) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   282
  StringConcat* result = new StringConcat(_stringopts, _end);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   283
  for (uint x = 0; x < _control.size(); x++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   284
    Node* n = _control.at(x);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   285
    if (n->is_Call()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   286
      result->_control.push(n);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   287
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   288
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   289
  for (uint x = 0; x < other->_control.size(); x++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   290
    Node* n = other->_control.at(x);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   291
    if (n->is_Call()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   292
      result->_control.push(n);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   293
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   294
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   295
  assert(result->_control.contains(other->_end), "what?");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   296
  assert(result->_control.contains(_begin), "what?");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   297
  for (int x = 0; x < num_arguments(); x++) {
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   298
    Node* argx = argument_uncast(x);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   299
    if (argx == arg) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   300
      // replace the toString result with the all the arguments that
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   301
      // made up the other StringConcat
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   302
      for (int y = 0; y < other->num_arguments(); y++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   303
        result->append(other->argument(y), other->mode(y));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   304
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   305
    } else {
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   306
      result->append(argx, mode(x));
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   307
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   308
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   309
  result->set_allocation(other->_begin);
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   310
  for (uint i = 0; i < _constructors.size(); i++) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   311
    result->add_constructor(_constructors.at(i));
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   312
  }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   313
  for (uint i = 0; i < other->_constructors.size(); i++) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   314
    result->add_constructor(other->_constructors.at(i));
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   315
  }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   316
  result->_multiple = true;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   317
  return result;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   318
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   319
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   320
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   321
void StringConcat::eliminate_call(CallNode* call) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   322
  Compile* C = _stringopts->C;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   323
  CallProjections projs;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   324
  call->extract_projections(&projs, false);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   325
  if (projs.fallthrough_catchproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   326
    C->gvn_replace_by(projs.fallthrough_catchproj, call->in(TypeFunc::Control));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   327
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   328
  if (projs.fallthrough_memproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   329
    C->gvn_replace_by(projs.fallthrough_memproj, call->in(TypeFunc::Memory));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   330
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   331
  if (projs.catchall_memproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   332
    C->gvn_replace_by(projs.catchall_memproj, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   333
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   334
  if (projs.fallthrough_ioproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   335
    C->gvn_replace_by(projs.fallthrough_ioproj, call->in(TypeFunc::I_O));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   336
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   337
  if (projs.catchall_ioproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   338
    C->gvn_replace_by(projs.catchall_ioproj, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   339
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   340
  if (projs.catchall_catchproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   341
    // EA can't cope with the partially collapsed graph this
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   342
    // creates so put it on the worklist to be collapsed later.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   343
    for (SimpleDUIterator i(projs.catchall_catchproj); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   344
      Node *use = i.get();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   345
      int opc = use->Opcode();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   346
      if (opc == Op_CreateEx || opc == Op_Region) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   347
        _stringopts->record_dead_node(use);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   348
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   349
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   350
    C->gvn_replace_by(projs.catchall_catchproj, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   351
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   352
  if (projs.resproj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   353
    C->gvn_replace_by(projs.resproj, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   354
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   355
  C->gvn_replace_by(call, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   356
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   357
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   358
void StringConcat::eliminate_initialize(InitializeNode* init) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   359
  Compile* C = _stringopts->C;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   360
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   361
  // Eliminate Initialize node.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   362
  assert(init->outcnt() <= 2, "only a control and memory projection expected");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   363
  assert(init->req() <= InitializeNode::RawStores, "no pending inits");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   364
  Node *ctrl_proj = init->proj_out(TypeFunc::Control);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   365
  if (ctrl_proj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   366
    C->gvn_replace_by(ctrl_proj, init->in(TypeFunc::Control));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   367
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   368
  Node *mem_proj = init->proj_out(TypeFunc::Memory);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   369
  if (mem_proj != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   370
    Node *mem = init->in(TypeFunc::Memory);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   371
    C->gvn_replace_by(mem_proj, mem);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   372
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   373
  C->gvn_replace_by(init, C->top());
14623
70c4c1be0a14 7092905: C2: Keep track of the number of dead nodes
bharadwaj
parents: 13895
diff changeset
   374
  init->disconnect_inputs(NULL, C);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   375
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   376
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   377
Node_List PhaseStringOpts::collect_toString_calls() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   378
  Node_List string_calls;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   379
  Node_List worklist;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   380
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   381
  _visited.Clear();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   382
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   383
  // Prime the worklist
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   384
  for (uint i = 1; i < C->root()->len(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   385
    Node* n = C->root()->in(i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   386
    if (n != NULL && !_visited.test_set(n->_idx)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   387
      worklist.push(n);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   388
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   389
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   390
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   391
  while (worklist.size() > 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   392
    Node* ctrl = worklist.pop();
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   393
    if (StringConcat::is_SB_toString(ctrl)) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   394
      CallStaticJavaNode* csj = ctrl->as_CallStaticJava();
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   395
      string_calls.push(csj);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   396
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   397
    if (ctrl->in(0) != NULL && !_visited.test_set(ctrl->in(0)->_idx)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   398
      worklist.push(ctrl->in(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   399
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   400
    if (ctrl->is_Region()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   401
      for (uint i = 1; i < ctrl->len(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   402
        if (ctrl->in(i) != NULL && !_visited.test_set(ctrl->in(i)->_idx)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   403
          worklist.push(ctrl->in(i));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   404
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   405
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   406
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   407
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   408
  return string_calls;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   409
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   410
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   411
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   412
StringConcat* PhaseStringOpts::build_candidate(CallStaticJavaNode* call) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   413
  ciMethod* m = call->method();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   414
  ciSymbol* string_sig;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   415
  ciSymbol* int_sig;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   416
  ciSymbol* char_sig;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   417
  if (m->holder() == C->env()->StringBuilder_klass()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   418
    string_sig = ciSymbol::String_StringBuilder_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   419
    int_sig = ciSymbol::int_StringBuilder_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   420
    char_sig = ciSymbol::char_StringBuilder_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   421
  } else if (m->holder() == C->env()->StringBuffer_klass()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   422
    string_sig = ciSymbol::String_StringBuffer_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   423
    int_sig = ciSymbol::int_StringBuffer_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   424
    char_sig = ciSymbol::char_StringBuffer_signature();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   425
  } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   426
    return NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   427
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   428
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   429
  if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   430
    tty->print("considering toString call in ");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   431
    call->jvms()->dump_spec(tty); tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   432
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   433
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   434
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   435
  StringConcat* sc = new StringConcat(this, call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   436
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   437
  AllocateNode* alloc = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   438
  InitializeNode* init = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   439
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   440
  // possible opportunity for StringBuilder fusion
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   441
  CallStaticJavaNode* cnode = call;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   442
  while (cnode) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   443
    Node* recv = cnode->in(TypeFunc::Parms)->uncast();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   444
    if (recv->is_Proj()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   445
      recv = recv->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   446
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   447
    cnode = recv->isa_CallStaticJava();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   448
    if (cnode == NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   449
      alloc = recv->isa_Allocate();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   450
      if (alloc == NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   451
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   452
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   453
      // Find the constructor call
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   454
      Node* result = alloc->result_cast();
15113
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   455
      if (result == NULL || !result->is_CheckCastPP() || alloc->in(TypeFunc::Memory)->is_top()) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   456
        // strange looking allocation
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   457
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   458
        if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   459
          tty->print("giving up because allocation looks strange ");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   460
          alloc->jvms()->dump_spec(tty); tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   461
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   462
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   463
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   464
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   465
      Node* constructor = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   466
      for (SimpleDUIterator i(result); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   467
        CallStaticJavaNode *use = i.get()->isa_CallStaticJava();
6743
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
   468
        if (use != NULL &&
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
   469
            use->method() != NULL &&
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
   470
            !use->method()->is_static() &&
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   471
            use->method()->name() == ciSymbol::object_initializer_name() &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   472
            use->method()->holder() == m->holder()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   473
          // Matched the constructor.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   474
          ciSymbol* sig = use->method()->signature()->as_symbol();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   475
          if (sig == ciSymbol::void_method_signature() ||
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   476
              sig == ciSymbol::int_void_signature() ||
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   477
              sig == ciSymbol::string_void_signature()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   478
            if (sig == ciSymbol::string_void_signature()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   479
              // StringBuilder(String) so pick this up as the first argument
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   480
              assert(use->in(TypeFunc::Parms + 1) != NULL, "what?");
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   481
              const Type* type = _gvn->type(use->in(TypeFunc::Parms + 1));
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   482
              if (type == TypePtr::NULL_PTR) {
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   483
                // StringBuilder(null) throws exception.
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   484
#ifndef PRODUCT
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   485
                if (PrintOptimizeStringConcat) {
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   486
                  tty->print("giving up because StringBuilder(null) throws exception");
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   487
                  alloc->jvms()->dump_spec(tty); tty->cr();
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   488
                }
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   489
#endif
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   490
                return NULL;
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   491
              }
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   492
              // StringBuilder(str) argument needs null check.
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   493
              sc->push_string_null_check(use->in(TypeFunc::Parms + 1));
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   494
            }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   495
            // The int variant takes an initial size for the backing
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   496
            // array so just treat it like the void version.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   497
            constructor = use;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   498
          } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   499
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   500
            if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   501
              tty->print("unexpected constructor signature: %s", sig->as_utf8());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   502
            }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   503
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   504
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   505
          break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   506
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   507
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   508
      if (constructor == NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   509
        // couldn't find constructor
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   510
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   511
        if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   512
          tty->print("giving up because couldn't find constructor ");
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
   513
          alloc->jvms()->dump_spec(tty); tty->cr();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   514
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   515
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   516
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   517
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   518
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   519
      // Walked all the way back and found the constructor call so see
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   520
      // if this call converted into a direct string concatenation.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   521
      sc->add_control(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   522
      sc->add_control(constructor);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   523
      sc->add_control(alloc);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   524
      sc->set_allocation(alloc);
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   525
      sc->add_constructor(constructor);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   526
      if (sc->validate_control_flow() && sc->validate_mem_flow()) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   527
        return sc;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   528
      } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   529
        return NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   530
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   531
    } else if (cnode->method() == NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   532
      break;
6743
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
   533
    } else if (!cnode->method()->is_static() &&
ef1795cd50a7 6986028: assert(_base == Int) failed: Not an Int in CmpINode::sub
never
parents: 6418
diff changeset
   534
               cnode->method()->holder() == m->holder() &&
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   535
               cnode->method()->name() == ciSymbol::append_name() &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   536
               (cnode->method()->signature()->as_symbol() == string_sig ||
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   537
                cnode->method()->signature()->as_symbol() == char_sig ||
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   538
                cnode->method()->signature()->as_symbol() == int_sig)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   539
      sc->add_control(cnode);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   540
      Node* arg = cnode->in(TypeFunc::Parms + 1);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   541
      if (cnode->method()->signature()->as_symbol() == int_sig) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   542
        sc->push_int(arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   543
      } else if (cnode->method()->signature()->as_symbol() == char_sig) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   544
        sc->push_char(arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   545
      } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   546
        if (arg->is_Proj() && arg->in(0)->is_CallStaticJava()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   547
          CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   548
          if (csj->method() != NULL &&
13292
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   549
              csj->method()->intrinsic_id() == vmIntrinsics::_Integer_toString &&
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   550
              arg->outcnt() == 1) {
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   551
            // _control is the list of StringBuilder calls nodes which
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   552
            // will be replaced by new String code after this optimization.
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   553
            // Integer::toString() call is not part of StringBuilder calls
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   554
            // chain. It could be eliminated only if its result is used
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   555
            // only by this SB calls chain.
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   556
            // Another limitation: it should be used only once because
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   557
            // it is unknown that it is used only by this SB calls chain
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   558
            // until all related SB calls nodes are collected.
587bb173cf43 7123926: Some CTW test crash: !_control.contains(ctrl)
kvn
parents: 13111
diff changeset
   559
            assert(arg->unique_out() == cnode, "sanity");
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   560
            sc->add_control(csj);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   561
            sc->push_int(csj->in(TypeFunc::Parms));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   562
            continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   563
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   564
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   565
        sc->push_string(arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   566
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   567
      continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   568
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   569
      // some unhandled signature
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   570
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   571
      if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   572
        tty->print("giving up because encountered unexpected signature ");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   573
        cnode->tf()->dump(); tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   574
        cnode->in(TypeFunc::Parms + 1)->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   575
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   576
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   577
      break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   578
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   579
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   580
  return NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   581
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   582
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   583
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   584
PhaseStringOpts::PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List*):
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   585
  Phase(StringOpts),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   586
  _gvn(gvn),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   587
  _visited(Thread::current()->resource_area()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   588
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   589
  assert(OptimizeStringConcat, "shouldn't be here");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   590
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   591
  size_table_field = C->env()->Integer_klass()->get_field_by_name(ciSymbol::make("sizeTable"),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   592
                                                                  ciSymbol::make("[I"), true);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   593
  if (size_table_field == NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   594
    // Something wrong so give up.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   595
    assert(false, "why can't we find Integer.sizeTable?");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   596
    return;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   597
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   598
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   599
  // Collect the types needed to talk about the various slices of memory
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   600
  char_adr_idx = C->get_alias_index(TypeAryPtr::CHARS);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   601
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   602
  // For each locally allocated StringBuffer see if the usages can be
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   603
  // collapsed into a single String construction.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   604
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   605
  // Run through the list of allocation looking for SB.toString to see
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   606
  // if it's possible to fuse the usage of the SB into a single String
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   607
  // construction.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   608
  GrowableArray<StringConcat*> concats;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   609
  Node_List toStrings = collect_toString_calls();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   610
  while (toStrings.size() > 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   611
    StringConcat* sc = build_candidate(toStrings.pop()->as_CallStaticJava());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   612
    if (sc != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   613
      concats.push(sc);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   614
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   615
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   616
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   617
  // try to coalesce separate concats
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   618
 restart:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   619
  for (int c = 0; c < concats.length(); c++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   620
    StringConcat* sc = concats.at(c);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   621
    for (int i = 0; i < sc->num_arguments(); i++) {
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   622
      Node* arg = sc->argument_uncast(i);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   623
      if (arg->is_Proj() && StringConcat::is_SB_toString(arg->in(0))) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   624
        CallStaticJavaNode* csj = arg->in(0)->as_CallStaticJava();
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   625
        for (int o = 0; o < concats.length(); o++) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   626
          if (c == o) continue;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   627
          StringConcat* other = concats.at(o);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   628
          if (other->end() == csj) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   629
#ifndef PRODUCT
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   630
            if (PrintOptimizeStringConcat) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   631
              tty->print_cr("considering stacked concats");
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   632
            }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   633
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   634
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   635
            StringConcat* merged = sc->merge(other, arg);
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   636
            if (merged->validate_control_flow() && merged->validate_mem_flow()) {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   637
#ifndef PRODUCT
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   638
              if (PrintOptimizeStringConcat) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   639
                tty->print_cr("stacking would succeed");
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   640
              }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   641
#endif
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   642
              if (c < o) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   643
                concats.remove_at(o);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   644
                concats.at_put(c, merged);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   645
              } else {
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   646
                concats.remove_at(c);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   647
                concats.at_put(o, merged);
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   648
              }
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   649
              goto restart;
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   650
            } else {
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   651
#ifndef PRODUCT
13111
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   652
              if (PrintOptimizeStringConcat) {
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   653
                tty->print_cr("stacking would fail");
e8a578a9b20a 7179138: Incorrect result with String concatenation optimization
kvn
parents: 12623
diff changeset
   654
              }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   655
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   656
            }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   657
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   658
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   659
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   660
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   661
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   662
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   663
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   664
  for (int c = 0; c < concats.length(); c++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   665
    StringConcat* sc = concats.at(c);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   666
    replace_string_concat(sc);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   667
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   668
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   669
  remove_dead_nodes();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   670
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   671
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   672
void PhaseStringOpts::record_dead_node(Node* dead) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   673
  dead_worklist.push(dead);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   674
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   675
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   676
void PhaseStringOpts::remove_dead_nodes() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   677
  // Delete any dead nodes to make things clean enough that escape
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   678
  // analysis doesn't get unhappy.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   679
  while (dead_worklist.size() > 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   680
    Node* use = dead_worklist.pop();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   681
    int opc = use->Opcode();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   682
    switch (opc) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   683
      case Op_Region: {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   684
        uint i = 1;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   685
        for (i = 1; i < use->req(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   686
          if (use->in(i) != C->top()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   687
            break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   688
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   689
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   690
        if (i >= use->req()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   691
          for (SimpleDUIterator i(use); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   692
            Node* m = i.get();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   693
            if (m->is_Phi()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   694
              dead_worklist.push(m);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   695
            }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   696
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   697
          C->gvn_replace_by(use, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   698
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   699
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   700
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   701
      case Op_AddP:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   702
      case Op_CreateEx: {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   703
        // Recurisvely clean up references to CreateEx so EA doesn't
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   704
        // get unhappy about the partially collapsed graph.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   705
        for (SimpleDUIterator i(use); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   706
          Node* m = i.get();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   707
          if (m->is_AddP()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   708
            dead_worklist.push(m);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   709
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   710
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   711
        C->gvn_replace_by(use, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   712
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   713
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   714
      case Op_Phi:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   715
        if (use->in(0) == C->top()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   716
          C->gvn_replace_by(use, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   717
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   718
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   719
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   720
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   721
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   722
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   723
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   724
bool StringConcat::validate_mem_flow() {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   725
  Compile* C = _stringopts->C;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   726
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   727
  for (uint i = 0; i < _control.size(); i++) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   728
#ifndef PRODUCT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   729
    Node_List path;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   730
#endif
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   731
    Node* curr = _control.at(i);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   732
    if (curr->is_Call() && curr != _begin) { // For all calls except the first allocation
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   733
      // Now here's the main invariant in our case:
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   734
      // For memory between the constructor, and appends, and toString we should only see bottom memory,
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   735
      // produced by the previous call we know about.
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   736
      if (!_constructors.contains(curr)) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   737
        NOT_PRODUCT(path.push(curr);)
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   738
        Node* mem = curr->in(TypeFunc::Memory);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   739
        assert(mem != NULL, "calls should have memory edge");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   740
        assert(!mem->is_Phi(), "should be handled by control flow validation");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   741
        NOT_PRODUCT(path.push(mem);)
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   742
        while (mem->is_MergeMem()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   743
          for (uint i = 1; i < mem->req(); i++) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   744
            if (i != Compile::AliasIdxBot && mem->in(i) != C->top()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   745
#ifndef PRODUCT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   746
              if (PrintOptimizeStringConcat) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   747
                tty->print("fusion has incorrect memory flow (side effects) for ");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   748
                _begin->jvms()->dump_spec(tty); tty->cr();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   749
                path.dump();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   750
              }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   751
#endif
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   752
              return false;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   753
            }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   754
          }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   755
          // skip through a potential MergeMem chain, linked through Bot
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   756
          mem = mem->in(Compile::AliasIdxBot);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   757
          NOT_PRODUCT(path.push(mem);)
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   758
        }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   759
        // now let it fall through, and see if we have a projection
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   760
        if (mem->is_Proj()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   761
          // Should point to a previous known call
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   762
          Node *prev = mem->in(0);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   763
          NOT_PRODUCT(path.push(prev);)
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   764
          if (!prev->is_Call() || !_control.contains(prev)) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   765
#ifndef PRODUCT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   766
            if (PrintOptimizeStringConcat) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   767
              tty->print("fusion has incorrect memory flow (unknown call) for ");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   768
              _begin->jvms()->dump_spec(tty); tty->cr();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   769
              path.dump();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   770
            }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   771
#endif
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   772
            return false;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   773
          }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   774
        } else {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   775
          assert(mem->is_Store() || mem->is_LoadStore(), err_msg_res("unexpected node type: %s", mem->Name()));
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   776
#ifndef PRODUCT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   777
          if (PrintOptimizeStringConcat) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   778
            tty->print("fusion has incorrect memory flow (unexpected source) for ");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   779
            _begin->jvms()->dump_spec(tty); tty->cr();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   780
            path.dump();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   781
          }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   782
#endif
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   783
          return false;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   784
        }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   785
      } else {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   786
        // For memory that feeds into constructors it's more complicated.
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   787
        // However the advantage is that any side effect that happens between the Allocate/Initialize and
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   788
        // the constructor will have to be control-dependent on Initialize.
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   789
        // So we actually don't have to do anything, since it's going to be caught by the control flow
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   790
        // analysis.
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   791
#ifdef ASSERT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   792
        // Do a quick verification of the control pattern between the constructor and the initialize node
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   793
        assert(curr->is_Call(), "constructor should be a call");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   794
        // Go up the control starting from the constructor call
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   795
        Node* ctrl = curr->in(0);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   796
        IfNode* iff = NULL;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   797
        RegionNode* copy = NULL;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   798
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   799
        while (true) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   800
          // skip known check patterns
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   801
          if (ctrl->is_Region()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   802
            if (ctrl->as_Region()->is_copy()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   803
              copy = ctrl->as_Region();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   804
              ctrl = copy->is_copy();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   805
            } else { // a cast
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   806
              assert(ctrl->req() == 3 &&
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   807
                     ctrl->in(1) != NULL && ctrl->in(1)->is_Proj() &&
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   808
                     ctrl->in(2) != NULL && ctrl->in(2)->is_Proj() &&
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   809
                     ctrl->in(1)->in(0) == ctrl->in(2)->in(0) &&
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   810
                     ctrl->in(1)->in(0) != NULL && ctrl->in(1)->in(0)->is_If(),
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   811
                     "must be a simple diamond");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   812
              Node* true_proj = ctrl->in(1)->is_IfTrue() ? ctrl->in(1) : ctrl->in(2);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   813
              for (SimpleDUIterator i(true_proj); i.has_next(); i.next()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   814
                Node* use = i.get();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   815
                assert(use == ctrl || use->is_ConstraintCast(),
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   816
                       err_msg_res("unexpected user: %s", use->Name()));
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   817
              }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   818
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   819
              iff = ctrl->in(1)->in(0)->as_If();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   820
              ctrl = iff->in(0);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   821
            }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   822
          } else if (ctrl->is_IfTrue()) { // null checks, class checks
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   823
            iff = ctrl->in(0)->as_If();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   824
            assert(iff->is_If(), "must be if");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   825
            // Verify that the other arm is an uncommon trap
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   826
            Node* otherproj = iff->proj_out(1 - ctrl->as_Proj()->_con);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   827
            CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   828
            assert(strcmp(call->_name, "uncommon_trap") == 0, "must be uncommond trap");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   829
            ctrl = iff->in(0);
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   830
          } else {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   831
            break;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   832
          }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   833
        }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   834
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   835
        assert(ctrl->is_Proj(), "must be a projection");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   836
        assert(ctrl->in(0)->is_Initialize(), "should be initialize");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   837
        for (SimpleDUIterator i(ctrl); i.has_next(); i.next()) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   838
          Node* use = i.get();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   839
          assert(use == copy || use == iff || use == curr || use->is_CheckCastPP() || use->is_Load(),
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   840
                 err_msg_res("unexpected user: %s", use->Name()));
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   841
        }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   842
#endif // ASSERT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   843
      }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   844
    }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   845
  }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   846
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   847
#ifndef PRODUCT
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   848
  if (PrintOptimizeStringConcat) {
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   849
    tty->print("fusion has correct memory flow for ");
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   850
    _begin->jvms()->dump_spec(tty); tty->cr();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   851
    tty->cr();
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   852
  }
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   853
#endif
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   854
  return true;
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   855
}
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   856
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   857
bool StringConcat::validate_control_flow() {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   858
  // We found all the calls and arguments now lets see if it's
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   859
  // safe to transform the graph as we would expect.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   860
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   861
  // Check to see if this resulted in too many uncommon traps previously
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   862
  if (Compile::current()->too_many_traps(_begin->jvms()->method(), _begin->jvms()->bci(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   863
                        Deoptimization::Reason_intrinsic)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   864
    return false;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   865
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   866
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   867
  // Walk backwards over the control flow from toString to the
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   868
  // allocation and make sure all the control flow is ok.  This
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   869
  // means it's either going to be eliminated once the calls are
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   870
  // removed or it can safely be transformed into an uncommon
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   871
  // trap.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   872
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   873
  int null_check_count = 0;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   874
  Unique_Node_List ctrl_path;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   875
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   876
  assert(_control.contains(_begin), "missing");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   877
  assert(_control.contains(_end), "missing");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   878
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   879
  // Collect the nodes that we know about and will eliminate into ctrl_path
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   880
  for (uint i = 0; i < _control.size(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   881
    // Push the call and it's control projection
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   882
    Node* n = _control.at(i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   883
    if (n->is_Allocate()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   884
      AllocateNode* an = n->as_Allocate();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   885
      InitializeNode* init = an->initialization();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   886
      ctrl_path.push(init);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   887
      ctrl_path.push(init->as_Multi()->proj_out(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   888
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   889
    if (n->is_Call()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   890
      CallNode* cn = n->as_Call();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   891
      ctrl_path.push(cn);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   892
      ctrl_path.push(cn->proj_out(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   893
      ctrl_path.push(cn->proj_out(0)->unique_out());
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   894
      if (cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0) != NULL) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   895
        ctrl_path.push(cn->proj_out(0)->unique_out()->as_Catch()->proj_out(0));
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   896
      }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   897
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   898
      ShouldNotReachHere();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   899
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   900
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   901
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
   902
  // Skip backwards through the control checking for unexpected control flow
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   903
  Node* ptr = _end;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   904
  bool fail = false;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   905
  while (ptr != _begin) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   906
    if (ptr->is_Call() && ctrl_path.member(ptr)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   907
      ptr = ptr->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   908
    } else if (ptr->is_CatchProj() && ctrl_path.member(ptr)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   909
      ptr = ptr->in(0)->in(0)->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   910
      assert(ctrl_path.member(ptr), "should be a known piece of control");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   911
    } else if (ptr->is_IfTrue()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   912
      IfNode* iff = ptr->in(0)->as_If();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   913
      BoolNode* b = iff->in(1)->isa_Bool();
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   914
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   915
      if (b == NULL) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   916
        fail = true;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   917
        break;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   918
      }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
   919
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   920
      Node* cmp = b->in(1);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   921
      Node* v1 = cmp->in(1);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   922
      Node* v2 = cmp->in(2);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   923
      Node* otherproj = iff->proj_out(1 - ptr->as_Proj()->_con);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   924
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   925
      // Null check of the return of append which can simply be eliminated
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   926
      if (b->_test._test == BoolTest::ne &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   927
          v2->bottom_type() == TypePtr::NULL_PTR &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   928
          v1->is_Proj() && ctrl_path.member(v1->in(0))) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   929
        // NULL check of the return value of the append
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   930
        null_check_count++;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   931
        if (otherproj->outcnt() == 1) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   932
          CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   933
          if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   934
            ctrl_path.push(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   935
          }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   936
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   937
        _control.push(ptr);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   938
        ptr = ptr->in(0)->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   939
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   940
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   941
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   942
      // A test which leads to an uncommon trap which should be safe.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   943
      // Later this trap will be converted into a trap that restarts
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   944
      // at the beginning.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   945
      if (otherproj->outcnt() == 1) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   946
        CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   947
        if (call != NULL && call->_name != NULL && strcmp(call->_name, "uncommon_trap") == 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   948
          // control flow leads to uct so should be ok
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   949
          _uncommon_traps.push(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   950
          ctrl_path.push(call);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   951
          ptr = ptr->in(0)->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   952
          continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   953
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   954
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   955
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   956
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   957
      // Some unexpected control flow we don't know how to handle.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   958
      if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   959
        tty->print_cr("failing with unknown test");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   960
        b->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   961
        cmp->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   962
        v1->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   963
        v2->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   964
        tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   965
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   966
#endif
9953
a0f68ceb2f77 7046096: SEGV IN C2 WITH 6U25
kvn
parents: 9332
diff changeset
   967
      fail = true;
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   968
      break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   969
    } else if (ptr->is_Proj() && ptr->in(0)->is_Initialize()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   970
      ptr = ptr->in(0)->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   971
    } else if (ptr->is_Region()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   972
      Node* copy = ptr->as_Region()->is_copy();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   973
      if (copy != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   974
        ptr = copy;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   975
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   976
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   977
      if (ptr->req() == 3 &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   978
          ptr->in(1) != NULL && ptr->in(1)->is_Proj() &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   979
          ptr->in(2) != NULL && ptr->in(2)->is_Proj() &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   980
          ptr->in(1)->in(0) == ptr->in(2)->in(0) &&
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   981
          ptr->in(1)->in(0) != NULL && ptr->in(1)->in(0)->is_If()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   982
        // Simple diamond.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   983
        // XXX should check for possibly merging stores.  simple data merges are ok.
15113
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   984
        // The IGVN will make this simple diamond go away when it
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   985
        // transforms the Region. Make sure it sees it.
823590505eb4 8005071: Incremental inlining for JSR 292
roland
parents: 14828
diff changeset
   986
        Compile::current()->record_for_igvn(ptr);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   987
        ptr = ptr->in(1)->in(0)->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   988
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   989
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   990
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   991
      if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   992
        tty->print_cr("fusion would fail for region");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   993
        _begin->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   994
        ptr->dump(2);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   995
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   996
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   997
      fail = true;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   998
      break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
   999
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1000
      // other unknown control
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1001
      if (!fail) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1002
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1003
        if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1004
          tty->print_cr("fusion would fail for");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1005
          _begin->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1006
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1007
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1008
        fail = true;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1009
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1010
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1011
      if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1012
        ptr->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1013
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1014
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1015
      ptr = ptr->in(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1016
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1017
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1018
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1019
  if (PrintOptimizeStringConcat && fail) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1020
    tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1021
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1022
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1023
  if (fail) return !fail;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1024
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1025
  // Validate that all these results produced are contained within
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1026
  // this cluster of objects.  First collect all the results produced
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1027
  // by calls in the region.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1028
  _stringopts->_visited.Clear();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1029
  Node_List worklist;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1030
  Node* final_result = _end->proj_out(TypeFunc::Parms);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1031
  for (uint i = 0; i < _control.size(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1032
    CallNode* cnode = _control.at(i)->isa_Call();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1033
    if (cnode != NULL) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1034
      _stringopts->_visited.test_set(cnode->_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1035
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1036
    Node* result = cnode != NULL ? cnode->proj_out(TypeFunc::Parms) : NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1037
    if (result != NULL && result != final_result) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1038
      worklist.push(result);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1039
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1040
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1041
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1042
  Node* last_result = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1043
  while (worklist.size() > 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1044
    Node* result = worklist.pop();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1045
    if (_stringopts->_visited.test_set(result->_idx))
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1046
      continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1047
    for (SimpleDUIterator i(result); i.has_next(); i.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1048
      Node *use = i.get();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1049
      if (ctrl_path.member(use)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1050
        // already checked this
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1051
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1052
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1053
      int opc = use->Opcode();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1054
      if (opc == Op_CmpP || opc == Op_Node) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1055
        ctrl_path.push(use);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1056
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1057
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1058
      if (opc == Op_CastPP || opc == Op_CheckCastPP) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1059
        for (SimpleDUIterator j(use); j.has_next(); j.next()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1060
          worklist.push(j.get());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1061
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1062
        worklist.push(use->in(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1063
        ctrl_path.push(use);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1064
        continue;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1065
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1066
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1067
      if (PrintOptimizeStringConcat) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1068
        if (result != last_result) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1069
          last_result = result;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1070
          tty->print_cr("extra uses for result:");
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1071
          last_result->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1072
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1073
        use->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1074
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1075
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1076
      fail = true;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1077
      break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1078
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1079
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1080
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1081
#ifndef PRODUCT
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1082
  if (PrintOptimizeStringConcat && !fail) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1083
    ttyLocker ttyl;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1084
    tty->cr();
20716
5093ad743df4 8009303: Tiered: incorrect results in VM tests stringconcat with -Xcomp -XX:+DeoptimizeALot on solaris-amd64
iveresov
parents: 15113
diff changeset
  1085
    tty->print("fusion has correct control flow (%d %d) for ", null_check_count, _uncommon_traps.size());
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1086
    _begin->jvms()->dump_spec(tty); tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1087
    for (int i = 0; i < num_arguments(); i++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1088
      argument(i)->dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1089
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1090
    _control.dump();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1091
    tty->cr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1092
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1093
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1094
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1095
  return !fail;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1096
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1097
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1098
Node* PhaseStringOpts::fetch_static_field(GraphKit& kit, ciField* field) {
12592
6d69203a4cb0 7161796: PhaseStringOpts::fetch_static_field tries to fetch field from the Klass instead of the mirror
never
parents: 9953
diff changeset
  1099
  const TypeInstPtr* mirror_type = TypeInstPtr::make(field->holder()->java_mirror());
6d69203a4cb0 7161796: PhaseStringOpts::fetch_static_field tries to fetch field from the Klass instead of the mirror
never
parents: 9953
diff changeset
  1100
  Node* klass_node = __ makecon(mirror_type);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1101
  BasicType bt = field->layout_type();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1102
  ciType* field_klass = field->type();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1103
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1104
  const Type *type;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1105
  if( bt == T_OBJECT ) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1106
    if (!field->type()->is_loaded()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1107
      type = TypeInstPtr::BOTTOM;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1108
    } else if (field->is_constant()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1109
      // This can happen if the constant oop is non-perm.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1110
      ciObject* con = field->constant_value().as_object();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1111
      // Do not "join" in the previous type; it doesn't add value,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1112
      // and may yield a vacuous result if the field is of interface type.
8728
3f1bcd33068e 6962931: move interned strings out of the perm gen
jcoomes
parents: 7714
diff changeset
  1113
      type = TypeOopPtr::make_from_constant(con, true)->isa_oopptr();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1114
      assert(type != NULL, "field singleton type must be consistent");
12592
6d69203a4cb0 7161796: PhaseStringOpts::fetch_static_field tries to fetch field from the Klass instead of the mirror
never
parents: 9953
diff changeset
  1115
      return __ makecon(type);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1116
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1117
      type = TypeOopPtr::make_from_klass(field_klass->as_klass());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1118
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1119
  } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1120
    type = Type::get_const_basic_type(bt);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1121
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1122
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1123
  return kit.make_load(NULL, kit.basic_plus_adr(klass_node, field->offset_in_bytes()),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1124
                       type, T_OBJECT,
22845
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1125
                       C->get_alias_index(mirror_type->add_offset(field->offset_in_bytes())),
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1126
                       MemNode::unordered);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1127
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1128
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1129
Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1130
  RegionNode *final_merge = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1131
  kit.gvn().set_type(final_merge, Type::CONTROL);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1132
  Node* final_size = new PhiNode(final_merge, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1133
  kit.gvn().set_type(final_size, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1134
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1135
  IfNode* iff = kit.create_and_map_if(kit.control(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1136
                                      __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1137
                                      PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1138
  Node* is_min = __ IfFalse(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1139
  final_merge->init_req(1, is_min);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1140
  final_size->init_req(1, __ intcon(11));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1141
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1142
  kit.set_control(__ IfTrue(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1143
  if (kit.stopped()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1144
    final_merge->init_req(2, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1145
    final_size->init_req(2, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1146
  } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1147
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1148
    // int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1149
    RegionNode *r = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1150
    kit.gvn().set_type(r, Type::CONTROL);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1151
    Node *phi = new PhiNode(r, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1152
    kit.gvn().set_type(phi, TypeInt::INT);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1153
    Node *size = new PhiNode(r, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1154
    kit.gvn().set_type(size, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1155
    Node* chk = __ CmpI(arg, __ intcon(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1156
    Node* p = __ Bool(chk, BoolTest::lt);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1157
    IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1158
    Node* lessthan = __ IfTrue(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1159
    Node* greaterequal = __ IfFalse(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1160
    r->init_req(1, lessthan);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1161
    phi->init_req(1, __ SubI(__ intcon(0), arg));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1162
    size->init_req(1, __ intcon(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1163
    r->init_req(2, greaterequal);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1164
    phi->init_req(2, arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1165
    size->init_req(2, __ intcon(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1166
    kit.set_control(r);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1167
    C->record_for_igvn(r);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1168
    C->record_for_igvn(phi);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1169
    C->record_for_igvn(size);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1170
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1171
    // for (int i=0; ; i++)
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1172
    //   if (x <= sizeTable[i])
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1173
    //     return i+1;
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1174
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1175
    // Add loop predicate first.
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1176
    kit.add_predicate();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1177
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1178
    RegionNode *loop = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1179
    loop->init_req(1, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1180
    kit.gvn().set_type(loop, Type::CONTROL);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1181
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1182
    Node *index = new PhiNode(loop, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1183
    index->init_req(1, __ intcon(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1184
    kit.gvn().set_type(index, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1185
    kit.set_control(loop);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1186
    Node* sizeTable = fetch_static_field(kit, size_table_field);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1187
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1188
    Node* value = kit.load_array_element(NULL, sizeTable, index, TypeAryPtr::INTS);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1189
    C->record_for_igvn(value);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1190
    Node* limit = __ CmpI(phi, value);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1191
    Node* limitb = __ Bool(limit, BoolTest::le);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1192
    IfNode* iff2 = kit.create_and_map_if(kit.control(), limitb, PROB_MIN, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1193
    Node* lessEqual = __ IfTrue(iff2);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1194
    Node* greater = __ IfFalse(iff2);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1195
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1196
    loop->init_req(2, greater);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1197
    index->init_req(2, __ AddI(index, __ intcon(1)));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1198
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1199
    kit.set_control(lessEqual);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1200
    C->record_for_igvn(loop);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1201
    C->record_for_igvn(index);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1202
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1203
    final_merge->init_req(2, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1204
    final_size->init_req(2, __ AddI(__ AddI(index, size), __ intcon(1)));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1205
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1206
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1207
  kit.set_control(final_merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1208
  C->record_for_igvn(final_merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1209
  C->record_for_igvn(final_size);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1210
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1211
  return final_size;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1212
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1213
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1214
void PhaseStringOpts::int_getChars(GraphKit& kit, Node* arg, Node* char_array, Node* start, Node* end) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1215
  RegionNode *final_merge = new RegionNode(4);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1216
  kit.gvn().set_type(final_merge, Type::CONTROL);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1217
  Node *final_mem = PhiNode::make(final_merge, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1218
  kit.gvn().set_type(final_mem, Type::MEMORY);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1219
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1220
  // need to handle Integer.MIN_VALUE specially because negating doesn't make it positive
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1221
  {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1222
    // i == MIN_VALUE
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1223
    IfNode* iff = kit.create_and_map_if(kit.control(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1224
                                        __ Bool(__ CmpI(arg, __ intcon(0x80000000)), BoolTest::ne),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1225
                                        PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1226
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1227
    Node* old_mem = kit.memory(char_adr_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1228
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1229
    kit.set_control(__ IfFalse(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1230
    if (kit.stopped()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1231
      // Statically not equal to MIN_VALUE so this path is dead
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1232
      final_merge->init_req(3, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1233
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1234
      copy_string(kit, __ makecon(TypeInstPtr::make(C->env()->the_min_jint_string())),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1235
                  char_array, start);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1236
      final_merge->init_req(3, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1237
      final_mem->init_req(3, kit.memory(char_adr_idx));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1238
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1239
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1240
    kit.set_control(__ IfTrue(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1241
    kit.set_memory(old_mem, char_adr_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1242
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1243
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1244
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1245
  // Simplified version of Integer.getChars
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1246
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1247
  // int q, r;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1248
  // int charPos = index;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1249
  Node* charPos = end;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1250
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1251
  // char sign = 0;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1252
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1253
  Node* i = arg;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1254
  Node* sign = __ intcon(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1255
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1256
  // if (i < 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1257
  //     sign = '-';
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1258
  //     i = -i;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1259
  // }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1260
  {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1261
    IfNode* iff = kit.create_and_map_if(kit.control(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1262
                                        __ Bool(__ CmpI(arg, __ intcon(0)), BoolTest::lt),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1263
                                        PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1264
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1265
    RegionNode *merge = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1266
    kit.gvn().set_type(merge, Type::CONTROL);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1267
    i = new PhiNode(merge, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1268
    kit.gvn().set_type(i, TypeInt::INT);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1269
    sign = new PhiNode(merge, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1270
    kit.gvn().set_type(sign, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1271
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1272
    merge->init_req(1, __ IfTrue(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1273
    i->init_req(1, __ SubI(__ intcon(0), arg));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1274
    sign->init_req(1, __ intcon('-'));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1275
    merge->init_req(2, __ IfFalse(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1276
    i->init_req(2, arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1277
    sign->init_req(2, __ intcon(0));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1278
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1279
    kit.set_control(merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1280
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1281
    C->record_for_igvn(merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1282
    C->record_for_igvn(i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1283
    C->record_for_igvn(sign);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1284
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1285
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1286
  // for (;;) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1287
  //     q = i / 10;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1288
  //     r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1289
  //     buf [--charPos] = digits [r];
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1290
  //     i = q;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1291
  //     if (i == 0) break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1292
  // }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1293
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1294
  {
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1295
    // Add loop predicate first.
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1296
    kit.add_predicate();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7714
diff changeset
  1297
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1298
    RegionNode *head = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1299
    head->init_req(1, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1300
    kit.gvn().set_type(head, Type::CONTROL);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1301
    Node *i_phi = new PhiNode(head, TypeInt::INT);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1302
    i_phi->init_req(1, i);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1303
    kit.gvn().set_type(i_phi, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1304
    charPos = PhiNode::make(head, charPos);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1305
    kit.gvn().set_type(charPos, TypeInt::INT);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1306
    Node *mem = PhiNode::make(head, kit.memory(char_adr_idx), Type::MEMORY, TypeAryPtr::CHARS);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1307
    kit.gvn().set_type(mem, Type::MEMORY);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1308
    kit.set_control(head);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1309
    kit.set_memory(mem, char_adr_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1310
4891
7c8755dd5bb2 6920293: OptimizeStringConcat causing core dumps
never
parents: 4450
diff changeset
  1311
    Node* q = __ DivI(NULL, i_phi, __ intcon(10));
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1312
    Node* r = __ SubI(i_phi, __ AddI(__ LShiftI(q, __ intcon(3)),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1313
                                     __ LShiftI(q, __ intcon(1))));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1314
    Node* m1 = __ SubI(charPos, __ intcon(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1315
    Node* ch = __ AddI(r, __ intcon('0'));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1316
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1317
    Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
22845
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1318
                                  ch, T_CHAR, char_adr_idx, MemNode::unordered);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1319
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1320
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1321
    IfNode* iff = kit.create_and_map_if(head, __ Bool(__ CmpI(q, __ intcon(0)), BoolTest::ne),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1322
                                        PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1323
    Node* ne = __ IfTrue(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1324
    Node* eq = __ IfFalse(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1325
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1326
    head->init_req(2, ne);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1327
    mem->init_req(2, st);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1328
    i_phi->init_req(2, q);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1329
    charPos->init_req(2, m1);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1330
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1331
    charPos = m1;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1332
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1333
    kit.set_control(eq);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1334
    kit.set_memory(st, char_adr_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1335
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1336
    C->record_for_igvn(head);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1337
    C->record_for_igvn(mem);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1338
    C->record_for_igvn(i_phi);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1339
    C->record_for_igvn(charPos);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1340
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1341
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1342
  {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1343
    // if (sign != 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1344
    //     buf [--charPos] = sign;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1345
    // }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1346
    IfNode* iff = kit.create_and_map_if(kit.control(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1347
                                        __ Bool(__ CmpI(sign, __ intcon(0)), BoolTest::ne),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1348
                                        PROB_FAIR, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1349
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1350
    final_merge->init_req(2, __ IfFalse(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1351
    final_mem->init_req(2, kit.memory(char_adr_idx));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1352
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1353
    kit.set_control(__ IfTrue(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1354
    if (kit.stopped()) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1355
      final_merge->init_req(1, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1356
      final_mem->init_req(1, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1357
    } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1358
      Node* m1 = __ SubI(charPos, __ intcon(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1359
      Node* st = __ store_to_memory(kit.control(), kit.array_element_address(char_array, m1, T_CHAR),
22845
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1360
                                    sign, T_CHAR, char_adr_idx, MemNode::unordered);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1361
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1362
      final_merge->init_req(1, kit.control());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1363
      final_mem->init_req(1, st);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1364
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1365
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1366
    kit.set_control(final_merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1367
    kit.set_memory(final_mem, char_adr_idx);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1368
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1369
    C->record_for_igvn(final_merge);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1370
    C->record_for_igvn(final_mem);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1371
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1372
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1373
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1374
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1375
Node* PhaseStringOpts::copy_string(GraphKit& kit, Node* str, Node* char_array, Node* start) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1376
  Node* string = str;
12623
09fcb0dc71ad 6924259: Remove String.count/String.offset
kvn
parents: 12592
diff changeset
  1377
  Node* offset = kit.load_String_offset(kit.control(), string);
09fcb0dc71ad 6924259: Remove String.count/String.offset
kvn
parents: 12592
diff changeset
  1378
  Node* count  = kit.load_String_length(kit.control(), string);
09fcb0dc71ad 6924259: Remove String.count/String.offset
kvn
parents: 12592
diff changeset
  1379
  Node* value  = kit.load_String_value (kit.control(), string);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1380
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1381
  // copy the contents
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1382
  if (offset->is_Con() && count->is_Con() && value->is_Con() && count->get_int() < unroll_string_copy_length) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1383
    // For small constant strings just emit individual stores.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1384
    // A length of 6 seems like a good space/speed tradeof.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1385
    int c = count->get_int();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1386
    int o = offset->get_int();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1387
    const TypeOopPtr* t = kit.gvn().type(value)->isa_oopptr();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1388
    ciTypeArray* value_array = t->const_oop()->as_type_array();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1389
    for (int e = 0; e < c; e++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1390
      __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
22845
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1391
                         __ intcon(value_array->char_at(o + e)), T_CHAR, char_adr_idx,
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1392
                         MemNode::unordered);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1393
      start = __ AddI(start, __ intcon(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1394
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1395
  } else {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1396
    Node* src_ptr = kit.array_element_address(value, offset, T_CHAR);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1397
    Node* dst_ptr = kit.array_element_address(char_array, start, T_CHAR);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1398
    Node* c = count;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1399
    Node* extra = NULL;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1400
#ifdef _LP64
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1401
    c = __ ConvI2L(c);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1402
    extra = C->top();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1403
#endif
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1404
    Node* call = kit.make_runtime_call(GraphKit::RC_LEAF|GraphKit::RC_NO_FP,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1405
                                       OptoRuntime::fast_arraycopy_Type(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1406
                                       CAST_FROM_FN_PTR(address, StubRoutines::jshort_disjoint_arraycopy()),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1407
                                       "jshort_disjoint_arraycopy", TypeAryPtr::CHARS,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1408
                                       src_ptr, dst_ptr, c, extra);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1409
    start = __ AddI(start, count);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1410
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1411
  return start;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1412
}
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1413
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1414
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1415
void PhaseStringOpts::replace_string_concat(StringConcat* sc) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1416
  // Log a little info about the transformation
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1417
  sc->maybe_log_transform();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1418
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1419
  // pull the JVMState of the allocation into a SafePointNode to serve as
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1420
  // as a shim for the insertion of the new code.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1421
  JVMState* jvms     = sc->begin()->jvms()->clone_shallow(C);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1422
  uint size = sc->begin()->req();
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1423
  SafePointNode* map = new SafePointNode(size, jvms);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1424
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1425
  // copy the control and memory state from the final call into our
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1426
  // new starting state.  This allows any preceeding tests to feed
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1427
  // into the new section of code.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1428
  for (uint i1 = 0; i1 < TypeFunc::Parms; i1++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1429
    map->init_req(i1, sc->end()->in(i1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1430
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1431
  // blow away old allocation arguments
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1432
  for (uint i1 = TypeFunc::Parms; i1 < jvms->debug_start(); i1++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1433
    map->init_req(i1, C->top());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1434
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1435
  // Copy the rest of the inputs for the JVMState
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1436
  for (uint i1 = jvms->debug_start(); i1 < sc->begin()->req(); i1++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1437
    map->init_req(i1, sc->begin()->in(i1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1438
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1439
  // Make sure the memory state is a MergeMem for parsing.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1440
  if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
25930
eae8b7490d2c 8054033: Remove unused references to Compile*
thartmann
parents: 24923
diff changeset
  1441
    map->set_req(TypeFunc::Memory, MergeMemNode::make(map->in(TypeFunc::Memory)));
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1442
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1443
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1444
  jvms->set_map(map);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1445
  map->ensure_stack(jvms, jvms->method()->max_stack());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1446
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1447
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1448
  // disconnect all the old StringBuilder calls from the graph
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1449
  sc->eliminate_unneeded_control();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1451
  // At this point all the old work has been completely removed from
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1452
  // the graph and the saved JVMState exists at the point where the
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1453
  // final toString call used to be.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1454
  GraphKit kit(jvms);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1455
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1456
  // There may be uncommon traps which are still using the
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1457
  // intermediate states and these need to be rewritten to point at
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1458
  // the JVMState at the beginning of the transformation.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1459
  sc->convert_uncommon_traps(kit, jvms);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1460
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1461
  // Now insert the logic to compute the size of the string followed
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1462
  // by all the logic to construct array and resulting string.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1463
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1464
  Node* null_string = __ makecon(TypeInstPtr::make(C->env()->the_null_string()));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1465
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1466
  // Create a region for the overflow checks to merge into.
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1467
  int args = MAX2(sc->num_arguments(), 1);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1468
  RegionNode* overflow = new RegionNode(args);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1469
  kit.gvn().set_type(overflow, Type::CONTROL);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1470
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1471
  // Create a hook node to hold onto the individual sizes since they
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1472
  // are need for the copying phase.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1473
  Node* string_sizes = new Node(args);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1474
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1475
  Node* length = __ intcon(0);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1476
  for (int argi = 0; argi < sc->num_arguments(); argi++) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1477
    Node* arg = sc->argument(argi);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1478
    switch (sc->mode(argi)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1479
      case StringConcat::IntMode: {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1480
        Node* string_size = int_stringSize(kit, arg);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1481
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1482
        // accumulate total
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1483
        length = __ AddI(length, string_size);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1484
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1485
        // Cache this value for the use by int_toString
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1486
        string_sizes->init_req(argi, string_size);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1487
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1488
      }
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1489
      case StringConcat::StringNullCheckMode: {
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1490
        const Type* type = kit.gvn().type(arg);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1491
        assert(type != TypePtr::NULL_PTR, "missing check");
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1492
        if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1493
          // Null check with uncommont trap since
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1494
          // StringBuilder(null) throws exception.
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1495
          // Use special uncommon trap instead of
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1496
          // calling normal do_null_check().
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1497
          Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1498
          IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1499
          overflow->add_req(__ IfFalse(iff));
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1500
          Node* notnull = __ IfTrue(iff);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1501
          kit.set_control(notnull); // set control for the cast_not_null
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1502
          arg = kit.cast_not_null(arg, false);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1503
          sc->set_argument(argi, arg);
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1504
        }
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1505
        assert(kit.gvn().type(arg)->higher_equal(TypeInstPtr::NOTNULL), "sanity");
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1506
        // Fallthrough to add string length.
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1507
      }
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1508
      case StringConcat::StringMode: {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1509
        const Type* type = kit.gvn().type(arg);
28642
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1510
        Node* count = NULL;
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1511
        if (type == TypePtr::NULL_PTR) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1512
          // replace the argument with the null checked version
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1513
          arg = null_string;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1514
          sc->set_argument(argi, arg);
28642
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1515
          count = kit.load_String_length(kit.control(), arg);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1516
        } else if (!type->higher_equal(TypeInstPtr::NOTNULL)) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1517
          // s = s != null ? s : "null";
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1518
          // length = length + (s.count - s.offset);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1519
          RegionNode *r = new RegionNode(3);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1520
          kit.gvn().set_type(r, Type::CONTROL);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 22845
diff changeset
  1521
          Node *phi = new PhiNode(r, type);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1522
          kit.gvn().set_type(phi, phi->bottom_type());
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1523
          Node* p = __ Bool(__ CmpP(arg, kit.null()), BoolTest::ne);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1524
          IfNode* iff = kit.create_and_map_if(kit.control(), p, PROB_MIN, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1525
          Node* notnull = __ IfTrue(iff);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1526
          Node* isnull =  __ IfFalse(iff);
4891
7c8755dd5bb2 6920293: OptimizeStringConcat causing core dumps
never
parents: 4450
diff changeset
  1527
          kit.set_control(notnull); // set control for the cast_not_null
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1528
          r->init_req(1, notnull);
4891
7c8755dd5bb2 6920293: OptimizeStringConcat causing core dumps
never
parents: 4450
diff changeset
  1529
          phi->init_req(1, kit.cast_not_null(arg, false));
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1530
          r->init_req(2, isnull);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1531
          phi->init_req(2, null_string);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1532
          kit.set_control(r);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1533
          C->record_for_igvn(r);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1534
          C->record_for_igvn(phi);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1535
          // replace the argument with the null checked version
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1536
          arg = phi;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1537
          sc->set_argument(argi, arg);
28642
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1538
          count = kit.load_String_length(kit.control(), arg);
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1539
        } else {
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1540
          // A corresponding nullcheck will be connected during IGVN MemNode::Ideal_common_DU_postCCP
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1541
          // kit.control might be a different test, that can be hoisted above the actual nullcheck
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1542
          // in case, that the control input is not null, Ideal_common_DU_postCCP will not look for a nullcheck.
a42fefc69922 8068909: SIGSEGV in c2 compiled code with OptimizeStringConcat
asiebenborn
parents: 25930
diff changeset
  1543
          count = kit.load_String_length(NULL, arg);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1544
        }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1545
        length = __ AddI(length, count);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1546
        string_sizes->init_req(argi, NULL);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1547
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1548
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1549
      case StringConcat::CharMode: {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1550
        // one character only
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1551
        length = __ AddI(length, __ intcon(1));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1552
        break;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1553
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1554
      default:
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1555
        ShouldNotReachHere();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1556
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1557
    if (argi > 0) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1558
      // Check that the sum hasn't overflowed
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1559
      IfNode* iff = kit.create_and_map_if(kit.control(),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1560
                                          __ Bool(__ CmpI(length, __ intcon(0)), BoolTest::lt),
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1561
                                          PROB_MIN, COUNT_UNKNOWN);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1562
      kit.set_control(__ IfFalse(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1563
      overflow->set_req(argi, __ IfTrue(iff));
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1564
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1565
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1566
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1567
  {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1568
    // Hook
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1569
    PreserveJVMState pjvms(&kit);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1570
    kit.set_control(overflow);
7714
f078cdefa674 7009359: HS with -XX:+AggressiveOpts optimize new StringBuffer(null) so it does not throw NPE as expected
kvn
parents: 7397
diff changeset
  1571
    C->record_for_igvn(overflow);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1572
    kit.uncommon_trap(Deoptimization::Reason_intrinsic,
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1573
                      Deoptimization::Action_make_not_entrant);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1574
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1575
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1576
  Node* result;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1577
  if (!kit.stopped()) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1578
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1579
    // length now contains the number of characters needed for the
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1580
    // char[] so create a new AllocateArray for the char[]
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1581
    Node* char_array = NULL;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1582
    {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1583
      PreserveReexecuteState preexecs(&kit);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1584
      // The original jvms is for an allocation of either a String or
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1585
      // StringBuffer so no stack adjustment is necessary for proper
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1586
      // reexecution.  If we deoptimize in the slow path the bytecode
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1587
      // will be reexecuted and the char[] allocation will be thrown away.
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1588
      kit.jvms()->set_should_reexecute(true);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1589
      char_array = kit.new_array(__ makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_CHAR))),
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1590
                                 length, 1);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1591
    }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1592
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1593
    // Mark the allocation so that zeroing is skipped since the code
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1594
    // below will overwrite the entire array
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1595
    AllocateArrayNode* char_alloc = AllocateArrayNode::Ideal_array_allocation(char_array, _gvn);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1596
    char_alloc->maybe_set_complete(_gvn);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1597
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1598
    // Now copy the string representations into the final char[]
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1599
    Node* start = __ intcon(0);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1600
    for (int argi = 0; argi < sc->num_arguments(); argi++) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1601
      Node* arg = sc->argument(argi);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1602
      switch (sc->mode(argi)) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1603
        case StringConcat::IntMode: {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1604
          Node* end = __ AddI(start, string_sizes->in(argi));
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1605
          // getChars words backwards so pass the ending point as well as the start
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1606
          int_getChars(kit, arg, char_array, start, end);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1607
          start = end;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1608
          break;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1609
        }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1610
        case StringConcat::StringNullCheckMode:
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1611
        case StringConcat::StringMode: {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1612
          start = copy_string(kit, arg, char_array, start);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1613
          break;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1614
        }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1615
        case StringConcat::CharMode: {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1616
          __ store_to_memory(kit.control(), kit.array_element_address(char_array, start, T_CHAR),
22845
d8812d0ff387 8024921: PPC64 (part 113): Extend Load and Store nodes to know about memory ordering
goetz
parents: 20716
diff changeset
  1617
                             arg, T_CHAR, char_adr_idx, MemNode::unordered);
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1618
          start = __ AddI(start, __ intcon(1));
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1619
          break;
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1620
        }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1621
        default:
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1622
          ShouldNotReachHere();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1623
      }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1624
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1625
14828
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1626
    // If we're not reusing an existing String allocation then allocate one here.
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1627
    result = sc->string_alloc();
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1628
    if (result == NULL) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1629
      PreserveReexecuteState preexecs(&kit);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1630
      // The original jvms is for an allocation of either a String or
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1631
      // StringBuffer so no stack adjustment is necessary for proper
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1632
      // reexecution.
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1633
      kit.jvms()->set_should_reexecute(true);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1634
      result = kit.new_instance(__ makecon(TypeKlassPtr::make(C->env()->String_klass())));
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1635
    }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1636
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1637
    // Intialize the string
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1638
    if (java_lang_String::has_offset_field()) {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1639
      kit.store_String_offset(kit.control(), result, __ intcon(0));
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1640
      kit.store_String_length(kit.control(), result, length);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1641
    }
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1642
    kit.store_String_value(kit.control(), result, char_array);
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1643
  } else {
bb9dffedf46c 8005031: Some cleanup in c2 to prepare for incremental inlining support
roland
parents: 14623
diff changeset
  1644
    result = C->top();
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1645
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1646
  // hook up the outgoing control and result
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1647
  kit.replace_call(sc->end(), result);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1648
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1649
  // Unhook any hook nodes
14623
70c4c1be0a14 7092905: C2: Keep track of the number of dead nodes
bharadwaj
parents: 13895
diff changeset
  1650
  string_sizes->disconnect_inputs(NULL, C);
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1651
  sc->cleanup();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents:
diff changeset
  1652
}