1 /*
2  * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_OPTO_STRINGOPTS_HPP
26 #define SHARE_OPTO_STRINGOPTS_HPP
27 
28 #include "opto/node.hpp"
29 #include "opto/phaseX.hpp"
30 
31 class StringConcat;
32 class IdealVariable;
33 
34 class PhaseStringOpts : public Phase {
35   friend class StringConcat;
36 
37  private:
38   PhaseGVN* _gvn;
39 
40   // List of dead nodes to clean up aggressively at the end
41   Unique_Node_List dead_worklist;
42 
43   // Memory slices needed for code gen
44   int byte_adr_idx;
45 
46   // Integer.sizeTable - used for int to String conversion
47   ciField* size_table_field;
48 
49   // A set for use by various stages
50   VectorSet _visited;
51 
52   // Collect a list of all SB.toString calls
53   Node_List collect_toString_calls();
54 
55   // Examine the use of the SB alloc to see if it can be replace with
56   // a single string construction.
57   StringConcat* build_candidate(CallStaticJavaNode* call);
58 
59   // Replace all the SB calls in concat with an optimization String allocation
60   void replace_string_concat(StringConcat* concat);
61 
62   // Load the value of a static field, performing any constant folding.
63   Node* fetch_static_field(GraphKit& kit, ciField* field);
64 
65   // Compute the number of characters required to represent the int value
66   Node* int_stringSize(GraphKit& kit, Node* value);
67 
68   // Simplified version of Integer.getChars
69   void getChars(GraphKit& kit, Node* arg, Node* dst_array, BasicType bt, Node* end, Node* final_merge, Node* final_mem, int merge_index = 0);
70 
71   // Copy the characters representing arg into dst_array starting at start
72   Node* int_getChars(GraphKit& kit, Node* arg, Node* dst_array, Node* dst_coder, Node* start, Node* size);
73 
74   // Copy contents of the String str into dst_array starting at index start.
75   Node* copy_string(GraphKit& kit, Node* str, Node* dst_array, Node* dst_coder, Node* start);
76 
77   // Copy 'count' bytes/chars from src_array to dst_array starting at index start
78   void arraycopy(GraphKit& kit, IdealKit& ideal, Node* src_array, Node* dst_array, BasicType elembt, Node* start, Node* count);
79 
80   // Copy contents of constant src_array to dst_array by emitting individual stores
81   void copy_constant_string(GraphKit& kit, IdealKit& ideal, ciTypeArray* src_array, IdealVariable& count,
82                             bool src_is_byte, Node* dst_array, Node* dst_coder, Node* start);
83 
84   // Copy contents of a Latin1 encoded string from src_array to dst_array
85   void copy_latin1_string(GraphKit& kit, IdealKit& ideal, Node* src_array, IdealVariable& count,
86                           Node* dst_array, Node* dst_coder, Node* start);
87 
88   // Copy the char into dst_array at index start.
89   Node* copy_char(GraphKit& kit, Node* val, Node* dst_array, Node* dst_coder, Node* start);
90 
91   // Allocate a byte array of specified length.
92   Node* allocate_byte_array(GraphKit& kit, IdealKit* ideal, Node* length);
93 
94   // Returns the coder of a constant string
95   jbyte get_constant_coder(GraphKit& kit, Node* str);
96 
97   // Returns the length of a constant string
98   int get_constant_length(GraphKit& kit, Node* str);
99 
100   // Returns the value array of a constant string
101   ciTypeArray* get_constant_value(GraphKit& kit, Node* str);
102 
103   // Clean up any leftover nodes
104   void record_dead_node(Node* node);
105   void remove_dead_nodes();
106 
gvn()107   PhaseGVN* gvn() { return _gvn; }
108 
109   enum {
110     // max length of constant string copy unrolling in copy_string
111     unroll_string_copy_length = 6
112   };
113 
114  public:
115   PhaseStringOpts(PhaseGVN* gvn, Unique_Node_List* worklist);
116 };
117 
118 #endif // SHARE_OPTO_STRINGOPTS_HPP
119