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