1 /*
  2  * Copyright (c) 2012, 2025, 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 #include "utilities/bitMap.inline.hpp"
 29 #include "utilities/stringUtils.hpp"
 30 
 31 #define COMPILER_PHASES(flags) \
 32   flags(BEFORE_STRINGOPTS,              "Before StringOpts") \
 33   flags(AFTER_STRINGOPTS,               "After StringOpts") \
 34   flags(BEFORE_REMOVEUSELESS,           "Before RemoveUseless") \
 35   flags(AFTER_PARSING,                  "After Parsing") \
 36   flags(BEFORE_ITER_GVN,                "Before Iter GVN") \
 37   flags(ITER_GVN1,                      "Iter GVN 1") \
 38   flags(AFTER_ITER_GVN_STEP,            "After Iter GVN Step") \
 39   flags(AFTER_ITER_GVN,                 "After Iter GVN") \
 40   flags(INCREMENTAL_INLINE_STEP,        "Incremental Inline Step") \
 41   flags(INCREMENTAL_INLINE_CLEANUP,     "Incremental Inline Cleanup") \
 42   flags(INCREMENTAL_INLINE,             "Incremental Inline") \
 43   flags(INCREMENTAL_BOXING_INLINE,      "Incremental Boxing Inline") \
 44   flags(EXPAND_VUNBOX,                  "Expand VectorUnbox") \
 45   flags(SCALARIZE_VBOX,                 "Scalarize VectorBox") \
 46   flags(INLINE_VECTOR_REBOX,            "Inline Vector Rebox Calls") \
 47   flags(EXPAND_VBOX,                    "Expand VectorBox") \
 48   flags(ELIMINATE_VBOX_ALLOC,           "Eliminate VectorBoxAllocate") \
 49   flags(ITER_GVN_BEFORE_EA,             "Iter GVN before EA") \
 50   flags(ITER_GVN_AFTER_VECTOR,          "Iter GVN after Vector Box Elimination") \
 51   flags(BEFORE_LOOP_OPTS,               "Before Loop Optimizations") \
 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(BEFORE_BEAUTIFY_LOOPS,          "Before Beautify Loops") \
 56   flags(AFTER_BEAUTIFY_LOOPS,           "After Beautify Loops") \
 57   flags(BEFORE_CLOOPS,                  "Before CountedLoop") \
 58   flags(AFTER_CLOOPS,                   "After CountedLoop") \
 59   flags(BEFORE_SPLIT_IF,                "Before Split-If") \
 60   flags(AFTER_SPLIT_IF,                 "After Split-If") \
 61   flags(BEFORE_LOOP_PREDICATION_IC,     "Before Loop Predication IC") \
 62   flags(AFTER_LOOP_PREDICATION_IC,      "After Loop Predication IC") \
 63   flags(BEFORE_LOOP_PREDICATION_RC,     "Before Loop Predication RC") \
 64   flags(AFTER_LOOP_PREDICATION_RC,      "After Loop Predication RC") \
 65   flags(BEFORE_PARTIAL_PEELING,         "Before Partial Peeling") \
 66   flags(AFTER_PARTIAL_PEELING,          "After Partial Peeling") \
 67   flags(BEFORE_LOOP_PEELING,            "Before Loop Peeling") \
 68   flags(AFTER_LOOP_PEELING,             "After Loop Peeling") \
 69   flags(BEFORE_LOOP_UNSWITCHING,        "Before Loop Unswitching") \
 70   flags(AFTER_LOOP_UNSWITCHING,         "After Loop Unswitching") \
 71   flags(BEFORE_LOOP_MULTIVERSIONING,    "Before Loop Multiversioning") \
 72   flags(AFTER_LOOP_MULTIVERSIONING,     "After Loop Multiversioning") \
 73   flags(BEFORE_RANGE_CHECK_ELIMINATION, "Before Range Check Elimination") \
 74   flags(AFTER_RANGE_CHECK_ELIMINATION,  "After Range Check Elimination") \
 75   flags(ITER_GVN_AFTER_ELIMINATION,     "Iter GVN after Eliminating Allocations and Locks") \
 76   flags(BEFORE_PRE_MAIN_POST,           "Before Pre/Main/Post Loops") \
 77   flags(AFTER_PRE_MAIN_POST,            "After Pre/Main/Post Loops") \
 78   flags(BEFORE_LOOP_UNROLLING,          "Before Loop Unrolling") \
 79   flags(AFTER_LOOP_UNROLLING,           "After Loop Unrolling") \
 80   flags(PHASEIDEALLOOP1,                "PhaseIdealLoop 1") \
 81   flags(PHASEIDEALLOOP2,                "PhaseIdealLoop 2") \
 82   flags(PHASEIDEALLOOP3,                "PhaseIdealLoop 3") \
 83   flags(AUTO_VECTORIZATION1_BEFORE_APPLY,                     "AutoVectorization 1, before Apply") \
 84   flags(AUTO_VECTORIZATION2_AFTER_REORDER,                    "AutoVectorization 2, after Apply Memop Reordering") \
 85   flags(AUTO_VECTORIZATION3_AFTER_ADJUST_LIMIT,               "AutoVectorization 3, after Adjusting Pre-loop Limit") \
 86   flags(AUTO_VECTORIZATION4_AFTER_SPECULATIVE_RUNTIME_CHECKS, "AutoVectorization 4, after Adding Speculative Runtime Checks") \
 87   flags(AUTO_VECTORIZATION5_AFTER_APPLY,                      "AutoVectorization 5, after Apply") \
 88   flags(BEFORE_CCP1,                    "Before PhaseCCP 1") \
 89   flags(CCP1,                           "PhaseCCP 1") \
 90   flags(ITER_GVN2,                      "Iter GVN 2") \
 91   flags(PHASEIDEALLOOP_ITERATIONS,      "PhaseIdealLoop iterations") \
 92   flags(AFTER_LOOP_OPTS,                "After Loop Optimizations") \
 93   flags(AFTER_MERGE_STORES,             "After Merge Stores") \
 94   flags(AFTER_MACRO_ELIMINATION_STEP,   "After Macro Elimination Step") \
 95   flags(AFTER_MACRO_ELIMINATION,        "After Macro Elimination") \
 96   flags(BEFORE_MACRO_EXPANSION ,        "Before Macro Expansion") \
 97   flags(AFTER_MACRO_EXPANSION_STEP,     "After Macro Expansion Step") \
 98   flags(AFTER_MACRO_EXPANSION,          "After Macro Expansion") \
 99   flags(BARRIER_EXPANSION,              "Barrier Expand") \
100   flags(OPTIMIZE_FINISHED,              "Optimize Finished") \
101   flags(BEFORE_MATCHING,                "Before Matching") \
102   flags(MATCHING,                       "After Matching") \
103   flags(GLOBAL_CODE_MOTION,             "Global Code Motion") \
104   flags(INITIAL_LIVENESS,               "Initial Liveness") \
105   flags(LIVE_RANGE_STRETCHING,          "Live Range Stretching") \
106   flags(AGGRESSIVE_COALESCING,          "Aggressive Coalescing") \
107   flags(INITIAL_SPILLING,               "Initial Spilling") \
108   flags(CONSERVATIVE_COALESCING,        "Conservative Coalescing") \
109   flags(ITERATIVE_SPILLING,             "Iterative Spilling") \
110   flags(AFTER_ITERATIVE_SPILLING,       "After Iterative Spilling") \
111   flags(POST_ALLOCATION_COPY_REMOVAL,   "Post-allocation Copy Removal") \
112   flags(MERGE_MULTI_DEFS,               "Merge Multiple Definitions") \
113   flags(FIX_UP_SPILLS,                  "Fix up Spills") \
114   flags(REGISTER_ALLOCATION,            "Register Allocation") \
115   flags(BLOCK_ORDERING,                 "Block Ordering") \
116   flags(PEEPHOLE,                       "Peephole") \
117   flags(POSTALLOC_EXPAND,               "Post-allocation Expand") \
118   flags(MACH_ANALYSIS,                  "After Mach Analysis") \
119   flags(FINAL_CODE,                     "Final Code") \
120   flags(END,                            "End") \
121   flags(FAILURE,                        "Failure") \
122   flags(ALL,                            "All") \
123   flags(DEBUG,                          "Debug")
124 
125 #define table_entry(name, description) PHASE_##name,
126 enum CompilerPhaseType {
127   COMPILER_PHASES(table_entry)
128   PHASE_NUM_TYPES,
129   PHASE_NONE
130 };
131 #undef table_entry
132 
133 static const char* phase_descriptions[] = {
134 #define array_of_labels(name, description) description,
135        COMPILER_PHASES(array_of_labels)
136 #undef array_of_labels
137 };
138 
139 static const char* phase_names[] = {
140 #define array_of_labels(name, description) #name,
141        COMPILER_PHASES(array_of_labels)
142 #undef array_of_labels
143 };
144 
145 class CompilerPhaseTypeHelper {
146   public:
147   static const char* to_name(CompilerPhaseType cpt) {
148     return phase_names[cpt];
149   }
150   static const char* to_description(CompilerPhaseType cpt) {
151     return phase_descriptions[cpt];
152   }
153 };
154 
155 static CompilerPhaseType find_phase(const char* str) {
156   for (int i = 0; i < PHASE_NUM_TYPES; i++) {
157     if (strcmp(phase_names[i], str) == 0) {
158       return (CompilerPhaseType)i;
159     }
160   }
161   return PHASE_NONE;
162 }
163 
164 class PhaseNameValidator {
165  private:
166   CHeapBitMap _phase_name_set;
167   bool _valid;
168   char* _bad;
169 
170  public:
171   PhaseNameValidator(ccstrlist option) :
172     _phase_name_set(PHASE_NUM_TYPES, mtCompiler),
173     _valid(true),
174     _bad(nullptr)
175   {
176     for (StringUtils::CommaSeparatedStringIterator iter(option); *iter != nullptr && _valid; ++iter) {
177 
178       CompilerPhaseType cpt = find_phase(*iter);
179       if (PHASE_NONE == cpt) {
180         const size_t len = MIN2<size_t>(strlen(*iter), 63) + 1;  // cap len to a value we know is enough for all phase descriptions
181         _bad = NEW_C_HEAP_ARRAY(char, len, mtCompiler);
182         // strncpy always writes len characters. If the source string is shorter, the function fills the remaining bytes with nulls.
183         strncpy(_bad, *iter, len);
184         _valid = false;
185       } else if (PHASE_ALL == cpt) {
186         _phase_name_set.set_range(0, PHASE_NUM_TYPES);
187       } else {
188         assert(cpt < PHASE_NUM_TYPES, "out of bounds");
189         _phase_name_set.set_bit(cpt);
190       }
191     }
192   }
193 
194   ~PhaseNameValidator() {
195     if (_bad != nullptr) {
196       FREE_C_HEAP_ARRAY(char, _bad);
197     }
198   }
199 
200   const BitMap& phase_name_set() const {
201     assert(is_valid(), "Use of invalid phase name set");
202     return _phase_name_set;
203   }
204 
205   bool is_valid() const {
206     return _valid;
207   }
208 
209   const char* what() const {
210     return _bad;
211   }
212 };
213 
214 #endif // SHARE_OPTO_PHASETYPE_HPP