1 /*
  2  * Copyright (c) 2016, 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 #include "code/codeCache.hpp"
 26 #include "compiler/compilerDefinitions.inline.hpp"
 27 #include "interpreter/invocationCounter.hpp"
 28 #include "jvm_io.h"
 29 #include "runtime/arguments.hpp"
 30 #include "runtime/continuation.hpp"
 31 #include "runtime/flags/jvmFlag.hpp"
 32 #include "runtime/flags/jvmFlagAccess.hpp"
 33 #include "runtime/flags/jvmFlagConstraintsCompiler.hpp"
 34 #include "runtime/flags/jvmFlagLimit.hpp"
 35 #include "runtime/globals.hpp"
 36 #include "runtime/globals_extension.hpp"
 37 #include "utilities/defaultStream.hpp"
 38 
 39 const char* compilertype2name_tab[compiler_number_of_types] = {
 40   "",
 41   "c1",
 42   "c2",
 43   "jvmci"
 44 };
 45 
 46 CompilationModeFlag::Mode CompilationModeFlag::_mode = CompilationModeFlag::Mode::NORMAL;
 47 
 48 static void print_mode_unavailable(const char* mode_name, const char* reason) {
 49   warning("%s compilation mode unavailable because %s.", mode_name, reason);
 50 }
 51 
 52 bool CompilationModeFlag::initialize() {
 53   _mode = Mode::NORMAL;
 54   // During parsing we want to be very careful not to use any methods of CompilerConfig that depend on
 55   // CompilationModeFlag.
 56   if (CompilationMode != nullptr) {
 57     if (strcmp(CompilationMode, "default") == 0 || strcmp(CompilationMode, "normal") == 0) {
 58       assert(_mode == Mode::NORMAL, "Precondition");
 59     } else if (strcmp(CompilationMode, "quick-only") == 0) {
 60       if (!CompilerConfig::has_c1()) {
 61         print_mode_unavailable("quick-only", "there is no c1 present");
 62       } else {
 63         _mode = Mode::QUICK_ONLY;
 64       }
 65     } else if (strcmp(CompilationMode, "high-only") == 0) {
 66       if (!CompilerConfig::has_c2() && !CompilerConfig::is_jvmci_compiler()) {
 67         print_mode_unavailable("high-only", "there is no c2 or jvmci compiler present");
 68       } else {
 69         _mode = Mode::HIGH_ONLY;
 70       }
 71     } else if (strcmp(CompilationMode, "high-only-quick-internal") == 0) {
 72       if (!CompilerConfig::has_c1() || !CompilerConfig::is_jvmci_compiler()) {
 73         print_mode_unavailable("high-only-quick-internal", "there is no c1 and jvmci compiler present");
 74       } else {
 75         _mode = Mode::HIGH_ONLY_QUICK_INTERNAL;
 76       }
 77     } else {
 78       print_error();
 79       return false;
 80     }
 81   }
 82 
 83   // Now that the flag is parsed, we can use any methods of CompilerConfig.
 84   if (normal()) {
 85     if (CompilerConfig::is_c1_simple_only()) {
 86       _mode = Mode::QUICK_ONLY;
 87     } else if (CompilerConfig::is_c2_or_jvmci_compiler_only()) {
 88       _mode = Mode::HIGH_ONLY;
 89     } else if (CompilerConfig::is_jvmci_compiler_enabled() && CompilerConfig::is_c1_enabled() && !TieredCompilation) {
 90       warning("Disabling tiered compilation with non-native JVMCI compiler is not recommended, "
 91               "disabling intermediate compilation levels instead. ");
 92       _mode = Mode::HIGH_ONLY_QUICK_INTERNAL;
 93     }
 94   }
 95   return true;
 96 }
 97 
 98 void CompilationModeFlag::print_error() {
 99   jio_fprintf(defaultStream::error_stream(), "Unsupported compilation mode '%s', available modes are:", CompilationMode);
100   bool comma = false;
101   if (CompilerConfig::has_c1()) {
102     jio_fprintf(defaultStream::error_stream(), "%s quick-only", comma ? "," : "");
103     comma = true;
104   }
105   if (CompilerConfig::has_c2() || CompilerConfig::has_jvmci()) {
106     jio_fprintf(defaultStream::error_stream(), "%s high-only", comma ? "," : "");
107     comma = true;
108   }
109   if (CompilerConfig::has_c1() && CompilerConfig::has_jvmci()) {
110     jio_fprintf(defaultStream::error_stream(), "%s high-only-quick-internal", comma ? "," : "");
111     comma = true;
112   }
113   jio_fprintf(defaultStream::error_stream(), "\n");
114 }
115 
116 // Returns threshold scaled with CompileThresholdScaling
117 intx CompilerConfig::scaled_compile_threshold(intx threshold) {
118   return scaled_compile_threshold(threshold, CompileThresholdScaling);
119 }
120 
121 // Returns freq_log scaled with CompileThresholdScaling
122 intx CompilerConfig::scaled_freq_log(intx freq_log) {
123   return scaled_freq_log(freq_log, CompileThresholdScaling);
124 }
125 
126 // For XXXThreshold flags, which all have a valid range of [0 .. max_jint]
127 intx CompilerConfig::jvmflag_scaled_compile_threshold(intx threshold) {
128   return MAX2((intx)0, MIN2(scaled_compile_threshold(threshold), (intx)max_jint));
129 }
130 
131 // For XXXNotifyFreqLog flags, which all have a valid range of [0 .. 30]
132 intx CompilerConfig::jvmflag_scaled_freq_log(intx freq_log) {
133   return MAX2((intx)0, MIN2(scaled_freq_log(freq_log), (intx)30));
134 }
135 
136 // Returns threshold scaled with the value of scale.
137 // If scale < 0.0, threshold is returned without scaling.
138 intx CompilerConfig::scaled_compile_threshold(intx threshold, double scale) {
139   assert(threshold >= 0, "must be");
140   if (scale == 1.0 || scale < 0.0) {
141     return threshold;
142   } else {
143     double v = threshold * scale;
144     assert(v >= 0, "must be");
145     if (g_isnan(v) || !g_isfinite(v)) {
146       return max_intx;
147     }
148     int exp;
149     (void) frexp(v, &exp);
150     int max_exp = sizeof(intx) * BitsPerByte - 1;
151     if (exp > max_exp) {
152       return max_intx;
153     }
154     intx r = (intx)(v);
155     assert(r >= 0, "must be");
156     return r;
157   }
158 }
159 
160 // Returns freq_log scaled with the value of scale.
161 // Returned values are in the range of [0, InvocationCounter::number_of_count_bits + 1].
162 // If scale < 0.0, freq_log is returned without scaling.
163 intx CompilerConfig::scaled_freq_log(intx freq_log, double scale) {
164   // Check if scaling is necessary or if negative value was specified.
165   if (scale == 1.0 || scale < 0.0) {
166     return freq_log;
167   }
168   // Check values to avoid calculating log2 of 0.
169   if (scale == 0.0 || freq_log == 0) {
170     return 0;
171   }
172   // Determine the maximum notification frequency value currently supported.
173   // The largest mask value that the interpreter/C1 can handle is
174   // of length InvocationCounter::number_of_count_bits. Mask values are always
175   // one bit shorter then the value of the notification frequency. Set
176   // max_freq_bits accordingly.
177   int max_freq_bits = InvocationCounter::number_of_count_bits + 1;
178   intx scaled_freq = scaled_compile_threshold((intx)1 << freq_log, scale);
179 
180   if (scaled_freq == 0) {
181     // Return 0 right away to avoid calculating log2 of 0.
182     return 0;
183   } else {
184     return MIN2(log2i(scaled_freq), max_freq_bits);
185   }
186 }
187 
188 void CompilerConfig::set_client_emulation_mode_flags() {
189   assert(has_c1(), "Must have C1 compiler present");
190   CompilationModeFlag::set_quick_only();
191 
192   FLAG_SET_ERGO(ProfileInterpreter, false);
193 #if INCLUDE_JVMCI
194   FLAG_SET_ERGO(EnableJVMCI, false);
195   FLAG_SET_ERGO(UseJVMCICompiler, false);
196 #endif
197   if (FLAG_IS_DEFAULT(NeverActAsServerClassMachine)) {
198     FLAG_SET_ERGO(NeverActAsServerClassMachine, true);
199   }
200   if (FLAG_IS_DEFAULT(InitialCodeCacheSize)) {
201     FLAG_SET_ERGO(InitialCodeCacheSize, 160*K);
202   }
203   if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
204     FLAG_SET_ERGO(ReservedCodeCacheSize, 32*M);
205   }
206   if (FLAG_IS_DEFAULT(NonProfiledCodeHeapSize)) {
207     FLAG_SET_ERGO(NonProfiledCodeHeapSize, 27*M);
208   }
209   if (FLAG_IS_DEFAULT(ProfiledCodeHeapSize)) {
210     FLAG_SET_ERGO(ProfiledCodeHeapSize, 0);
211   }
212   if (FLAG_IS_DEFAULT(NonNMethodCodeHeapSize)) {
213     FLAG_SET_ERGO(NonNMethodCodeHeapSize, 5*M);
214   }
215   if (FLAG_IS_DEFAULT(CodeCacheExpansionSize)) {
216     FLAG_SET_ERGO(CodeCacheExpansionSize, 32*K);
217   }
218   if (FLAG_IS_DEFAULT(CICompilerCount)) {
219     FLAG_SET_ERGO(CICompilerCount, 1);
220   }
221 }
222 
223 bool CompilerConfig::is_compilation_mode_selected() {
224   return !FLAG_IS_DEFAULT(TieredCompilation) ||
225          !FLAG_IS_DEFAULT(TieredStopAtLevel) ||
226          !FLAG_IS_DEFAULT(CompilationMode)
227          JVMCI_ONLY(|| !FLAG_IS_DEFAULT(EnableJVMCI)
228                     || !FLAG_IS_DEFAULT(UseJVMCICompiler));
229 }
230 
231 static bool check_legacy_flags() {
232   JVMFlag* compile_threshold_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(CompileThreshold));
233   if (JVMFlagAccess::check_constraint(compile_threshold_flag, JVMFlagLimit::get_constraint(compile_threshold_flag)->constraint_func(), false) != JVMFlag::SUCCESS) {
234     return false;
235   }
236   JVMFlag* on_stack_replace_percentage_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(OnStackReplacePercentage));
237   if (JVMFlagAccess::check_constraint(on_stack_replace_percentage_flag, JVMFlagLimit::get_constraint(on_stack_replace_percentage_flag)->constraint_func(), false) != JVMFlag::SUCCESS) {
238     return false;
239   }
240   JVMFlag* interpreter_profile_percentage_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(InterpreterProfilePercentage));
241   if (JVMFlagAccess::check_range(interpreter_profile_percentage_flag, false) != JVMFlag::SUCCESS) {
242     return false;
243   }
244   return true;
245 }
246 
247 void CompilerConfig::set_legacy_emulation_flags() {
248   // Any legacy flags set?
249   if (!FLAG_IS_DEFAULT(CompileThreshold)         ||
250       !FLAG_IS_DEFAULT(OnStackReplacePercentage) ||
251       !FLAG_IS_DEFAULT(InterpreterProfilePercentage)) {
252     if (CompilerConfig::is_c1_only() || CompilerConfig::is_c2_or_jvmci_compiler_only()) {
253       // This function is called before these flags are validated. In order to not confuse the user with extraneous
254       // error messages, we check the validity of these flags here and bail out if any of them are invalid.
255       if (!check_legacy_flags()) {
256         return;
257       }
258       // Note, we do not scale CompileThreshold before this because the tiered flags are
259       // all going to be scaled further in set_compilation_policy_flags().
260       const intx threshold = CompileThreshold;
261       const intx profile_threshold = threshold * InterpreterProfilePercentage / 100;
262       const intx osr_threshold = threshold * OnStackReplacePercentage / 100;
263       const intx osr_profile_threshold = osr_threshold * InterpreterProfilePercentage / 100;
264 
265       const intx threshold_log = log2i_graceful(CompilerConfig::is_c1_only() ? threshold : profile_threshold);
266       const intx osr_threshold_log = log2i_graceful(CompilerConfig::is_c1_only() ? osr_threshold : osr_profile_threshold);
267 
268       if (Tier0InvokeNotifyFreqLog > threshold_log) {
269         FLAG_SET_ERGO(Tier0InvokeNotifyFreqLog, MAX2<intx>(0, threshold_log));
270       }
271 
272       // Note: Emulation oddity. The legacy policy limited the amount of callbacks from the
273       // interpreter for backedge events to once every 1024 counter increments.
274       // We simulate this behavior by limiting the backedge notification frequency to be
275       // at least 2^10.
276       if (Tier0BackedgeNotifyFreqLog > osr_threshold_log) {
277         FLAG_SET_ERGO(Tier0BackedgeNotifyFreqLog, MAX2<intx>(10, osr_threshold_log));
278       }
279       // Adjust the tiered policy flags to approximate the legacy behavior.
280       FLAG_SET_ERGO(Tier3InvocationThreshold, threshold);
281       FLAG_SET_ERGO(Tier3MinInvocationThreshold, threshold);
282       FLAG_SET_ERGO(Tier3CompileThreshold, threshold);
283       FLAG_SET_ERGO(Tier3BackEdgeThreshold, osr_threshold);
284       if (CompilerConfig::is_c2_or_jvmci_compiler_only()) {
285         FLAG_SET_ERGO(Tier4InvocationThreshold, threshold);
286         FLAG_SET_ERGO(Tier4MinInvocationThreshold, threshold);
287         FLAG_SET_ERGO(Tier4CompileThreshold, threshold);
288         FLAG_SET_ERGO(Tier4BackEdgeThreshold, osr_threshold);
289         FLAG_SET_ERGO(Tier0ProfilingStartPercentage, InterpreterProfilePercentage);
290       }
291     } else {
292       // Normal tiered mode, ignore legacy flags
293     }
294   }
295   // Scale CompileThreshold
296   // CompileThresholdScaling == 0.0 is equivalent to -Xint and leaves CompileThreshold unchanged.
297   if (!FLAG_IS_DEFAULT(CompileThresholdScaling) && CompileThresholdScaling > 0.0 && CompileThreshold > 0) {
298     intx scaled_value = scaled_compile_threshold(CompileThreshold);
299     if (CompileThresholdConstraintFunc(scaled_value, true) != JVMFlag::VIOLATES_CONSTRAINT) {
300       FLAG_SET_ERGO(CompileThreshold, scaled_value);
301     }
302   }
303 }
304 
305 
306 void CompilerConfig::set_compilation_policy_flags() {
307   if (is_tiered()) {
308     // Increase the code cache size - tiered compiles a lot more.
309     if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
310       FLAG_SET_ERGO(ReservedCodeCacheSize,
311                     MIN2(CODE_CACHE_DEFAULT_LIMIT, ReservedCodeCacheSize * 5));
312     }
313     // Enable SegmentedCodeCache if tiered compilation is enabled, ReservedCodeCacheSize >= 240M
314     // and the code cache contains at least 8 pages (segmentation disables advantage of huge pages).
315     if (FLAG_IS_DEFAULT(SegmentedCodeCache) && ReservedCodeCacheSize >= 240*M &&
316         8 * CodeCache::page_size() <= ReservedCodeCacheSize) {
317       FLAG_SET_ERGO(SegmentedCodeCache, true);
318     }
319     if (Arguments::is_compiler_only()) { // -Xcomp
320       // Be much more aggressive in tiered mode with -Xcomp and exercise C2 more.
321       // We will first compile a level 3 version (C1 with full profiling), then do one invocation of it and
322       // compile a level 4 (C2) and then continue executing it.
323       if (FLAG_IS_DEFAULT(Tier3InvokeNotifyFreqLog)) {
324         FLAG_SET_CMDLINE(Tier3InvokeNotifyFreqLog, 0);
325       }
326       if (FLAG_IS_DEFAULT(Tier4InvocationThreshold)) {
327         FLAG_SET_CMDLINE(Tier4InvocationThreshold, 0);
328       }
329     }
330   }
331 
332   if (CompileThresholdScaling < 0) {
333     vm_exit_during_initialization("Negative value specified for CompileThresholdScaling", nullptr);
334   }
335 
336   if (CompilationModeFlag::disable_intermediate()) {
337     if (FLAG_IS_DEFAULT(Tier0ProfilingStartPercentage)) {
338       FLAG_SET_DEFAULT(Tier0ProfilingStartPercentage, 33);
339     }
340 
341     if (FLAG_IS_DEFAULT(Tier4InvocationThreshold)) {
342       FLAG_SET_DEFAULT(Tier4InvocationThreshold, 5000);
343     }
344     if (FLAG_IS_DEFAULT(Tier4MinInvocationThreshold)) {
345       FLAG_SET_DEFAULT(Tier4MinInvocationThreshold, 600);
346     }
347     if (FLAG_IS_DEFAULT(Tier4CompileThreshold)) {
348       FLAG_SET_DEFAULT(Tier4CompileThreshold, 10000);
349     }
350     if (FLAG_IS_DEFAULT(Tier4BackEdgeThreshold)) {
351       FLAG_SET_DEFAULT(Tier4BackEdgeThreshold, 15000);
352     }
353 
354     if (FLAG_IS_DEFAULT(Tier3InvocationThreshold)) {
355       FLAG_SET_DEFAULT(Tier3InvocationThreshold, Tier4InvocationThreshold);
356     }
357     if (FLAG_IS_DEFAULT(Tier3MinInvocationThreshold)) {
358       FLAG_SET_DEFAULT(Tier3MinInvocationThreshold, Tier4MinInvocationThreshold);
359     }
360     if (FLAG_IS_DEFAULT(Tier3CompileThreshold)) {
361       FLAG_SET_DEFAULT(Tier3CompileThreshold, Tier4CompileThreshold);
362     }
363     if (FLAG_IS_DEFAULT(Tier3BackEdgeThreshold)) {
364       FLAG_SET_DEFAULT(Tier3BackEdgeThreshold, Tier4BackEdgeThreshold);
365     }
366 
367   }
368 
369   // Scale tiered compilation thresholds.
370   // CompileThresholdScaling == 0.0 is equivalent to -Xint and leaves compilation thresholds unchanged.
371   if (!FLAG_IS_DEFAULT(CompileThresholdScaling) && CompileThresholdScaling > 0.0) {
372     FLAG_SET_ERGO(Tier0InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier0InvokeNotifyFreqLog));
373     FLAG_SET_ERGO(Tier0BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier0BackedgeNotifyFreqLog));
374 
375     FLAG_SET_ERGO(Tier3InvocationThreshold, jvmflag_scaled_compile_threshold(Tier3InvocationThreshold));
376     FLAG_SET_ERGO(Tier3MinInvocationThreshold, jvmflag_scaled_compile_threshold(Tier3MinInvocationThreshold));
377     FLAG_SET_ERGO(Tier3CompileThreshold, jvmflag_scaled_compile_threshold(Tier3CompileThreshold));
378     FLAG_SET_ERGO(Tier3BackEdgeThreshold, jvmflag_scaled_compile_threshold(Tier3BackEdgeThreshold));
379 
380     // Tier2{Invocation,MinInvocation,Compile,Backedge}Threshold should be scaled here
381     // once these thresholds become supported.
382 
383     FLAG_SET_ERGO(Tier2InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier2InvokeNotifyFreqLog));
384     FLAG_SET_ERGO(Tier2BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier2BackedgeNotifyFreqLog));
385 
386     FLAG_SET_ERGO(Tier3InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier3InvokeNotifyFreqLog));
387     FLAG_SET_ERGO(Tier3BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier3BackedgeNotifyFreqLog));
388 
389     FLAG_SET_ERGO(Tier23InlineeNotifyFreqLog, jvmflag_scaled_freq_log(Tier23InlineeNotifyFreqLog));
390 
391     FLAG_SET_ERGO(Tier4InvocationThreshold, jvmflag_scaled_compile_threshold(Tier4InvocationThreshold));
392     FLAG_SET_ERGO(Tier4MinInvocationThreshold, jvmflag_scaled_compile_threshold(Tier4MinInvocationThreshold));
393     FLAG_SET_ERGO(Tier4CompileThreshold, jvmflag_scaled_compile_threshold(Tier4CompileThreshold));
394     FLAG_SET_ERGO(Tier4BackEdgeThreshold, jvmflag_scaled_compile_threshold(Tier4BackEdgeThreshold));
395   }
396 
397 #ifdef COMPILER1
398   // Reduce stack usage due to inlining of methods which require much stack.
399   // (High tier compiler can inline better based on profiling information.)
400   if (FLAG_IS_DEFAULT(C1InlineStackLimit) &&
401       TieredStopAtLevel == CompLevel_full_optimization && !CompilerConfig::is_c1_only()) {
402     FLAG_SET_DEFAULT(C1InlineStackLimit, 5);
403   }
404 #endif
405 
406   if (CompilerConfig::is_tiered() && CompilerConfig::is_c2_enabled()) {
407 #ifdef COMPILER2
408     // Some inlining tuning
409 #if defined(X86) || defined(AARCH64) || defined(RISCV64)
410     if (FLAG_IS_DEFAULT(InlineSmallCode)) {
411       FLAG_SET_DEFAULT(InlineSmallCode, 2500);
412     }
413 #endif
414 #endif // COMPILER2
415   }
416 
417 }
418 
419 #if INCLUDE_JVMCI
420 void CompilerConfig::set_jvmci_specific_flags() {
421   if (UseJVMCICompiler) {
422     if (FLAG_IS_DEFAULT(TypeProfileWidth)) {
423       FLAG_SET_DEFAULT(TypeProfileWidth, 8);
424     }
425     if (FLAG_IS_DEFAULT(TypeProfileLevel)) {
426       FLAG_SET_DEFAULT(TypeProfileLevel, 0);
427     }
428 
429     if (UseJVMCINativeLibrary) {
430       // SVM compiled code requires more stack space
431       if (FLAG_IS_DEFAULT(CompilerThreadStackSize)) {
432         // Duplicate logic in the implementations of os::create_thread
433         // so that we can then double the computed stack size. Once
434         // the stack size requirements of SVM are better understood,
435         // this logic can be pushed down into os::create_thread.
436         int stack_size = CompilerThreadStackSize;
437         if (stack_size == 0) {
438           stack_size = VMThreadStackSize;
439         }
440         if (stack_size != 0) {
441           FLAG_SET_DEFAULT(CompilerThreadStackSize, stack_size * 2);
442         }
443       }
444     } else {
445       // JVMCI needs values not less than defaults
446       if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
447         FLAG_SET_DEFAULT(ReservedCodeCacheSize, MAX2(64*M, ReservedCodeCacheSize));
448       }
449       if (FLAG_IS_DEFAULT(InitialCodeCacheSize)) {
450         FLAG_SET_DEFAULT(InitialCodeCacheSize, MAX2(16*M, InitialCodeCacheSize));
451       }
452       if (FLAG_IS_DEFAULT(NewSizeThreadIncrease)) {
453         FLAG_SET_DEFAULT(NewSizeThreadIncrease, MAX2(4*K, NewSizeThreadIncrease));
454       }
455       if (FLAG_IS_DEFAULT(Tier3DelayOn)) {
456         // This effectively prevents the compile broker scheduling tier 2
457         // (i.e., limited C1 profiling) compilations instead of tier 3
458         // (i.e., full C1 profiling) compilations when the tier 4 queue
459         // backs up (which is quite likely when using a non-AOT compiled JVMCI
460         // compiler). The observation based on jargraal is that the downside
461         // of skipping full profiling is much worse for performance than the
462         // queue backing up.
463         FLAG_SET_DEFAULT(Tier3DelayOn, 100000);
464       }
465     } // !UseJVMCINativeLibrary
466   } // UseJVMCICompiler
467 }
468 #endif // INCLUDE_JVMCI
469 
470 bool CompilerConfig::check_args_consistency(bool status) {
471   // Check lower bounds of the code cache
472   // Template Interpreter code is approximately 3X larger in debug builds.
473   size_t min_code_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3);
474   if (ReservedCodeCacheSize < InitialCodeCacheSize) {
475     jio_fprintf(defaultStream::error_stream(),
476                 "Invalid ReservedCodeCacheSize: %zuK. Must be at least InitialCodeCacheSize=%zuK.\n",
477                 ReservedCodeCacheSize/K, InitialCodeCacheSize/K);
478     status = false;
479   } else if (ReservedCodeCacheSize < min_code_cache_size) {
480     jio_fprintf(defaultStream::error_stream(),
481                 "Invalid ReservedCodeCacheSize=%zuK. Must be at least %zuK.\n", ReservedCodeCacheSize/K,
482                 min_code_cache_size/K);
483     status = false;
484   } else if (ReservedCodeCacheSize > CODE_CACHE_SIZE_LIMIT) {
485     // Code cache size larger than CODE_CACHE_SIZE_LIMIT is not supported.
486     jio_fprintf(defaultStream::error_stream(),
487                 "Invalid ReservedCodeCacheSize=%zuM. Must be at most %zuM.\n", ReservedCodeCacheSize/M,
488                 CODE_CACHE_SIZE_LIMIT/M);
489     status = false;
490   } else if (NonNMethodCodeHeapSize < min_code_cache_size) {
491     jio_fprintf(defaultStream::error_stream(),
492                 "Invalid NonNMethodCodeHeapSize=%zuK. Must be at least %zuK.\n", NonNMethodCodeHeapSize/K,
493                 min_code_cache_size/K);
494     status = false;
495   }
496 
497 #ifdef _LP64
498   if (!FLAG_IS_DEFAULT(CICompilerCount) && !FLAG_IS_DEFAULT(CICompilerCountPerCPU) && CICompilerCountPerCPU) {
499     warning("The VM option CICompilerCountPerCPU overrides CICompilerCount.");
500   }
501 #endif
502 
503   if (BackgroundCompilation && ReplayCompiles) {
504     if (!FLAG_IS_DEFAULT(BackgroundCompilation)) {
505       warning("BackgroundCompilation disabled due to ReplayCompiles option.");
506     }
507     FLAG_SET_CMDLINE(BackgroundCompilation, false);
508   }
509 
510   if (CompilerConfig::is_interpreter_only()) {
511     if (UseCompiler) {
512       if (!FLAG_IS_DEFAULT(UseCompiler)) {
513         warning("UseCompiler disabled due to -Xint.");
514       }
515       FLAG_SET_CMDLINE(UseCompiler, false);
516     }
517     if (ProfileInterpreter) {
518       if (!FLAG_IS_DEFAULT(ProfileInterpreter)) {
519         warning("ProfileInterpreter disabled due to -Xint.");
520       }
521       FLAG_SET_CMDLINE(ProfileInterpreter, false);
522     }
523     if (TieredCompilation) {
524       if (!FLAG_IS_DEFAULT(TieredCompilation)) {
525         warning("TieredCompilation disabled due to -Xint.");
526       }
527       FLAG_SET_CMDLINE(TieredCompilation, false);
528     }
529     if (SegmentedCodeCache) {
530       warning("SegmentedCodeCache has no meaningful effect with -Xint");
531       FLAG_SET_DEFAULT(SegmentedCodeCache, false);
532     }
533 #if INCLUDE_JVMCI
534     if (EnableJVMCI || UseJVMCICompiler) {
535       if (!FLAG_IS_DEFAULT(EnableJVMCI) || !FLAG_IS_DEFAULT(UseJVMCICompiler)) {
536         warning("JVMCI Compiler disabled due to -Xint.");
537       }
538       FLAG_SET_CMDLINE(EnableJVMCI, false);
539       FLAG_SET_CMDLINE(UseJVMCICompiler, false);
540     }
541 #endif
542   } else {
543 #if INCLUDE_JVMCI
544     status = status && JVMCIGlobals::check_jvmci_flags_are_consistent();
545 #endif
546   }
547 
548   return status;
549 }
550 
551 bool CompilerConfig::should_set_client_emulation_mode_flags() {
552 #if !COMPILER1_OR_COMPILER2
553   return false;
554 #endif
555 
556   if (has_c1()) {
557     if (!is_compilation_mode_selected()) {
558       if (NeverActAsServerClassMachine) {
559         return true;
560       }
561     } else if (!has_c2() && !is_jvmci_compiler()) {
562       return true;
563     }
564   }
565 
566   return false;
567 }
568 
569 void CompilerConfig::ergo_initialize() {
570 #if !COMPILER1_OR_COMPILER2
571   return;
572 #endif
573 
574   // This property is also checked when selecting the heap size. Since client
575   // emulation mode influences Java heap memory usage, part of the logic must
576   // occur before choosing the heap size.
577   if (should_set_client_emulation_mode_flags()) {
578     set_client_emulation_mode_flags();
579   }
580 
581   set_legacy_emulation_flags();
582   set_compilation_policy_flags();
583 
584 #if INCLUDE_JVMCI
585   // Check that JVMCI supports selected GC.
586   // Should be done after GCConfig::initialize() was called.
587   JVMCIGlobals::check_jvmci_supported_gc();
588 
589   // Do JVMCI specific settings
590   set_jvmci_specific_flags();
591 #endif
592 
593   if (UseOnStackReplacement && !UseLoopCounter) {
594     warning("On-stack-replacement requires loop counters; enabling loop counters");
595     FLAG_SET_DEFAULT(UseLoopCounter, true);
596   }
597 
598   if (ProfileInterpreter && CompilerConfig::is_c1_simple_only()) {
599     if (!FLAG_IS_DEFAULT(ProfileInterpreter)) {
600         warning("ProfileInterpreter disabled due to client emulation mode");
601     }
602     FLAG_SET_CMDLINE(ProfileInterpreter, false);
603   }
604 
605 #ifdef COMPILER2
606   if (!EliminateLocks) {
607     EliminateNestedLocks = false;
608   }
609   if (!Inline || !IncrementalInline) {
610     IncrementalInline = false;
611     IncrementalInlineMH = false;
612     IncrementalInlineVirtual = false;
613     StressIncrementalInlining = false;
614   }
615 #ifndef PRODUCT
616   if (!IncrementalInline) {
617     AlwaysIncrementalInline = false;
618   }
619   if (FLAG_IS_CMDLINE(PrintIdealGraph) && !PrintIdealGraph) {
620     FLAG_SET_ERGO(PrintIdealGraphLevel, -1);
621   }
622 #endif
623   if (!UseTypeSpeculation && FLAG_IS_DEFAULT(TypeProfileLevel)) {
624     // nothing to use the profiling, turn if off
625     FLAG_SET_DEFAULT(TypeProfileLevel, 0);
626   }
627   if (!FLAG_IS_DEFAULT(OptoLoopAlignment) && FLAG_IS_DEFAULT(MaxLoopPad)) {
628     FLAG_SET_DEFAULT(MaxLoopPad, OptoLoopAlignment-1);
629   }
630   if (FLAG_IS_DEFAULT(LoopStripMiningIterShortLoop)) {
631     // blind guess
632     LoopStripMiningIterShortLoop = LoopStripMiningIter / 10;
633   }
634   if (UseAutoVectorizationSpeculativeAliasingChecks && !LoopMultiversioning && !UseAutoVectorizationPredicate) {
635     warning("Disabling UseAutoVectorizationSpeculativeAliasingChecks, because neither of the following is enabled:"
636             "  LoopMultiversioning UseAutoVectorizationPredicate");
637     UseAutoVectorizationSpeculativeAliasingChecks = false;
638   }
639 #endif // COMPILER2
640 }