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