1 /*
  2  * Copyright (c) 2012, 2021, 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_PHASETYPE_HPP
 26 #define SHARE_OPTO_PHASETYPE_HPP
 27 
 28 #define COMPILER_PHASES(flags) \
 29   flags(BEFORE_STRINGOPTS,            "Before StringOpts") \
 30   flags(AFTER_STRINGOPTS,             "After StringOpts") \
 31   flags(BEFORE_REMOVEUSELESS,         "Before RemoveUseless") \
 32   flags(AFTER_PARSING,                "After Parsing") \
 33   flags(ITER_GVN1,                    "Iter GVN 1") \
 34   flags(EXPAND_VUNBOX,                "Expand VectorUnbox") \
 35   flags(SCALARIZE_VBOX,               "Scalarize VectorBox") \
 36   flags(INLINE_VECTOR_REBOX,          "Inline Vector Rebox Calls") \
 37   flags(EXPAND_VBOX,                  "Expand VectorBox") \
 38   flags(ELIMINATE_VBOX_ALLOC,         "Eliminate VectorBoxAllocate") \
 39   flags(PHASEIDEAL_BEFORE_EA,         "PhaseIdealLoop before EA") \
 40   flags(ITER_GVN_AFTER_VECTOR,        "Iter GVN after vector box elimination") \
 41   flags(ITER_GVN_BEFORE_EA,           "Iter GVN before EA") \
 42   flags(ITER_GVN_AFTER_EA,            "Iter GVN after EA") \
 43   flags(ITER_GVN_AFTER_ELIMINATION,   "Iter GVN after eliminating allocations and locks") \
 44   flags(PHASEIDEALLOOP1,              "PhaseIdealLoop 1") \
 45   flags(PHASEIDEALLOOP2,              "PhaseIdealLoop 2") \
 46   flags(PHASEIDEALLOOP3,              "PhaseIdealLoop 3") \
 47   flags(CCP1,                         "PhaseCCP 1") \
 48   flags(ITER_GVN2,                    "Iter GVN 2") \
 49   flags(PHASEIDEALLOOP_ITERATIONS,    "PhaseIdealLoop iterations") \
 50   flags(OPTIMIZE_FINISHED,            "Optimize finished") \
 51   flags(GLOBAL_CODE_MOTION,           "Global code motion") \
 52   flags(FINAL_CODE,                   "Final Code") \
 53   flags(AFTER_EA,                     "After Escape Analysis") \
 54   flags(BEFORE_CLOOPS,                "Before CountedLoop") \
 55   flags(AFTER_CLOOPS,                 "After CountedLoop") \
 56   flags(BEFORE_BEAUTIFY_LOOPS,        "Before beautify loops") \
 57   flags(AFTER_BEAUTIFY_LOOPS,         "After beautify loops") \
 58   flags(BEFORE_MATCHING,              "Before matching") \
 59   flags(MATCHING,                     "After matching") \
 60   flags(INCREMENTAL_INLINE,           "Incremental Inline") \
 61   flags(INCREMENTAL_INLINE_STEP,      "Incremental Inline Step") \
 62   flags(INCREMENTAL_INLINE_CLEANUP,   "Incremental Inline Cleanup") \
 63   flags(INCREMENTAL_BOXING_INLINE,    "Incremental Boxing Inline") \
 64   flags(CALL_CATCH_CLEANUP,           "Call catch cleanup") \
 65   flags(MACRO_EXPANSION,              "Macro expand") \
 66   flags(BARRIER_EXPANSION,            "Barrier expand") \
 67   flags(END,                          "End") \
 68   flags(FAILURE,                      "Failure") \
 69   flags(SPLIT_INLINES_ARRAY,          "Split inlines array") \
 70   flags(SPLIT_INLINES_ARRAY_IGVN,     "IGVN after split inlines array") \
 71   flags(DEBUG,                        "Debug")
 72 
 73 #define table_entry(name, description) PHASE_##name,
 74 enum CompilerPhaseType {
 75   COMPILER_PHASES(table_entry)
 76   PHASE_NUM_TYPES,
 77   PHASE_NONE
 78 };
 79 #undef table_entry
 80 
 81 static const char* phase_descriptions[] = {
 82 #define array_of_labels(name, description) description,
 83        COMPILER_PHASES(array_of_labels)
 84 #undef array_of_labels
 85 };
 86 
 87 static const char* phase_names[] = {
 88 #define array_of_labels(name, description) #name,
 89        COMPILER_PHASES(array_of_labels)
 90 #undef array_of_labels
 91 };
 92 
 93 class CompilerPhaseTypeHelper {
 94   public:
 95   static const char* to_name(CompilerPhaseType cpt) {
 96     return phase_names[cpt];
 97   }
 98   static const char* to_description(CompilerPhaseType cpt) {
 99     return phase_descriptions[cpt];
100   }
101   static int to_bitmask(CompilerPhaseType cpt) {
102     return (1 << cpt);
103   }
104 };
105 
106 static CompilerPhaseType find_phase(const char* str) {
107   for (int i = 0; i < PHASE_NUM_TYPES; i++) {
108     if (strcmp(phase_names[i], str) == 0) {
109       return (CompilerPhaseType)i;
110     }
111   }
112   return PHASE_NONE;
113 }
114 
115 class PhaseNameIter {
116  private:
117   char* _token;
118   char* _saved_ptr;
119   char* _list;
120 
121  public:
122   PhaseNameIter(ccstrlist option) {
123     _list = (char*) canonicalize(option);
124     _saved_ptr = _list;
125     _token = strtok_r(_saved_ptr, ",", &_saved_ptr);
126   }
127 
128   ~PhaseNameIter() {
129     FREE_C_HEAP_ARRAY(char, _list);
130   }
131 
132   const char* operator*() const { return _token; }
133 
134   PhaseNameIter& operator++() {
135     _token = strtok_r(NULL, ",", &_saved_ptr);
136     return *this;
137   }
138 
139   ccstrlist canonicalize(ccstrlist option_value) {
140     char* canonicalized_list = NEW_C_HEAP_ARRAY(char, strlen(option_value) + 1, mtCompiler);
141     int i = 0;
142     char current;
143     while ((current = option_value[i]) != '\0') {
144       if (current == '\n' || current == ' ') {
145         canonicalized_list[i] = ',';
146       } else {
147         canonicalized_list[i] = current;
148       }
149       i++;
150     }
151     canonicalized_list[i] = '\0';
152     return canonicalized_list;
153   }
154 };
155 
156 class PhaseNameValidator {
157  private:
158   bool _valid;
159   char* _bad;
160 
161  public:
162   PhaseNameValidator(ccstrlist option, uint64_t& mask) : _valid(true), _bad(nullptr) {
163     for (PhaseNameIter iter(option); *iter != NULL && _valid; ++iter) {
164 
165       CompilerPhaseType cpt = find_phase(*iter);
166       if (PHASE_NONE == cpt) {
167         const size_t len = MIN2<size_t>(strlen(*iter), 63) + 1;  // cap len to a value we know is enough for all phase descriptions
168         _bad = NEW_C_HEAP_ARRAY(char, len, mtCompiler);
169         // strncpy always writes len characters. If the source string is shorter, the function fills the remaining bytes with NULLs.
170         strncpy(_bad, *iter, len);
171         _valid = false;
172       } else {
173         assert(cpt < 64, "out of bounds");
174         mask |= CompilerPhaseTypeHelper::to_bitmask(cpt);
175       }
176     }
177   }
178 
179   ~PhaseNameValidator() {
180     if (_bad != NULL) {
181       FREE_C_HEAP_ARRAY(char, _bad);
182     }
183   }
184 
185   bool is_valid() const {
186     return _valid;
187   }
188 
189   const char* what() const {
190     return _bad;
191   }
192 };
193 
194 #endif // SHARE_OPTO_PHASETYPE_HPP