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