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(MACHANALYSIS,                 "After mach analysis") \
 61   flags(INCREMENTAL_INLINE,           "Incremental Inline") \
 62   flags(INCREMENTAL_INLINE_STEP,      "Incremental Inline Step") \
 63   flags(INCREMENTAL_INLINE_CLEANUP,   "Incremental Inline Cleanup") \
 64   flags(INCREMENTAL_BOXING_INLINE,    "Incremental Boxing Inline") \
 65   flags(CALL_CATCH_CLEANUP,           "Call catch cleanup") \
 66   flags(MACRO_EXPANSION,              "Macro expand") \
 67   flags(BARRIER_EXPANSION,            "Barrier expand") \
 68   flags(END,                          "End") \
 69   flags(FAILURE,                      "Failure") \
 70   flags(SPLIT_INLINES_ARRAY,          "Split inlines array") \
 71   flags(SPLIT_INLINES_ARRAY_IGVN,     "IGVN after split inlines array") \
 72   flags(ALL,                          "All") \
 73   flags(DEBUG,                        "Debug")
 74 
 75 #define table_entry(name, description) PHASE_##name,
 76 enum CompilerPhaseType {
 77   COMPILER_PHASES(table_entry)
 78   PHASE_NUM_TYPES,
 79   PHASE_NONE
 80 };
 81 #undef table_entry
 82 
 83 static const char* phase_descriptions[] = {
 84 #define array_of_labels(name, description) description,
 85        COMPILER_PHASES(array_of_labels)
 86 #undef array_of_labels
 87 };
 88 
 89 static const char* phase_names[] = {
 90 #define array_of_labels(name, description) #name,
 91        COMPILER_PHASES(array_of_labels)
 92 #undef array_of_labels
 93 };
 94 
 95 class CompilerPhaseTypeHelper {
 96   public:
 97   static const char* to_name(CompilerPhaseType cpt) {
 98     return phase_names[cpt];
 99   }
100   static const char* to_description(CompilerPhaseType cpt) {
101     return phase_descriptions[cpt];
102   }
103   static uint64_t to_bitmask(CompilerPhaseType cpt) {
104     return (UINT64_C(1) << cpt);
105   }
106 };
107 
108 static CompilerPhaseType find_phase(const char* str) {
109   for (int i = 0; i < PHASE_NUM_TYPES; i++) {
110     if (strcmp(phase_names[i], str) == 0) {
111       return (CompilerPhaseType)i;
112     }
113   }
114   return PHASE_NONE;
115 }
116 
117 class PhaseNameIter {
118  private:
119   char* _token;
120   char* _saved_ptr;
121   char* _list;
122 
123  public:
124   PhaseNameIter(ccstrlist option) {
125     _list = (char*) canonicalize(option);
126     _saved_ptr = _list;
127     _token = strtok_r(_saved_ptr, ",", &_saved_ptr);
128   }
129 
130   ~PhaseNameIter() {
131     FREE_C_HEAP_ARRAY(char, _list);
132   }
133 
134   const char* operator*() const { return _token; }
135 
136   PhaseNameIter& operator++() {
137     _token = strtok_r(NULL, ",", &_saved_ptr);
138     return *this;
139   }
140 
141   ccstrlist canonicalize(ccstrlist option_value) {
142     char* canonicalized_list = NEW_C_HEAP_ARRAY(char, strlen(option_value) + 1, mtCompiler);
143     int i = 0;
144     char current;
145     while ((current = option_value[i]) != '\0') {
146       if (current == '\n' || current == ' ') {
147         canonicalized_list[i] = ',';
148       } else {
149         canonicalized_list[i] = current;
150       }
151       i++;
152     }
153     canonicalized_list[i] = '\0';
154     return canonicalized_list;
155   }
156 };
157 
158 class PhaseNameValidator {
159  private:
160   bool _valid;
161   char* _bad;
162 
163  public:
164   PhaseNameValidator(ccstrlist option, uint64_t& mask) : _valid(true), _bad(nullptr) {
165     for (PhaseNameIter iter(option); *iter != NULL && _valid; ++iter) {
166 
167       CompilerPhaseType cpt = find_phase(*iter);
168       if (PHASE_NONE == cpt) {
169         const size_t len = MIN2<size_t>(strlen(*iter), 63) + 1;  // cap len to a value we know is enough for all phase descriptions
170         _bad = NEW_C_HEAP_ARRAY(char, len, mtCompiler);
171         // strncpy always writes len characters. If the source string is shorter, the function fills the remaining bytes with NULLs.
172         strncpy(_bad, *iter, len);
173         _valid = false;
174       } else if (PHASE_ALL == cpt) {
175         mask = ~(UINT64_C(0));
176       } else {
177         assert(cpt < 64, "out of bounds");
178         mask |= CompilerPhaseTypeHelper::to_bitmask(cpt);
179       }
180     }
181   }
182 
183   ~PhaseNameValidator() {
184     if (_bad != NULL) {
185       FREE_C_HEAP_ARRAY(char, _bad);
186     }
187   }
188 
189   bool is_valid() const {
190     return _valid;
191   }
192 
193   const char* what() const {
194     return _bad;
195   }
196 };
197 
198 #endif // SHARE_OPTO_PHASETYPE_HPP