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/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(NeverActAsServerClassMachine)) {
199     FLAG_SET_ERGO(NeverActAsServerClassMachine, true);
200   }
201   if (FLAG_IS_DEFAULT(InitialCodeCacheSize)) {
202     FLAG_SET_ERGO(InitialCodeCacheSize, 160*K);
203   }
204   if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
205     FLAG_SET_ERGO(ReservedCodeCacheSize, 32*M);
206   }
207   if (FLAG_IS_DEFAULT(NonProfiledCodeHeapSize)) {
208     FLAG_SET_ERGO(NonProfiledCodeHeapSize, 27*M);
209   }
210   if (FLAG_IS_DEFAULT(ProfiledCodeHeapSize)) {
211     FLAG_SET_ERGO(ProfiledCodeHeapSize, 0);
212   }
213   if (FLAG_IS_DEFAULT(NonNMethodCodeHeapSize)) {
214     FLAG_SET_ERGO(NonNMethodCodeHeapSize, 5*M);
215   }
216   if (FLAG_IS_DEFAULT(CodeCacheExpansionSize)) {
217     FLAG_SET_ERGO(CodeCacheExpansionSize, 32*K);
218   }
219   if (FLAG_IS_DEFAULT(CICompilerCount)) {
220     FLAG_SET_ERGO(CICompilerCount, 1);
221   }
222 }
223 
224 bool CompilerConfig::is_compilation_mode_selected() {
225   return !FLAG_IS_DEFAULT(TieredCompilation) ||
226          !FLAG_IS_DEFAULT(TieredStopAtLevel) ||
227          !FLAG_IS_DEFAULT(CompilationMode)
228          JVMCI_ONLY(|| !FLAG_IS_DEFAULT(EnableJVMCI)
229                     || !FLAG_IS_DEFAULT(UseJVMCICompiler));
230 }
231 
232 static bool check_legacy_flags() {
233   JVMFlag* compile_threshold_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(CompileThreshold));
234   if (JVMFlagAccess::check_constraint(compile_threshold_flag, JVMFlagLimit::get_constraint(compile_threshold_flag)->constraint_func(), false) != JVMFlag::SUCCESS) {
235     return false;
236   }
237   JVMFlag* on_stack_replace_percentage_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(OnStackReplacePercentage));
238   if (JVMFlagAccess::check_constraint(on_stack_replace_percentage_flag, JVMFlagLimit::get_constraint(on_stack_replace_percentage_flag)->constraint_func(), false) != JVMFlag::SUCCESS) {
239     return false;
240   }
241   JVMFlag* interpreter_profile_percentage_flag = JVMFlag::flag_from_enum(FLAG_MEMBER_ENUM(InterpreterProfilePercentage));
242   if (JVMFlagAccess::check_range(interpreter_profile_percentage_flag, false) != JVMFlag::SUCCESS) {
243     return false;
244   }
245   return true;
246 }
247 
248 void CompilerConfig::set_legacy_emulation_flags() {
249   // Any legacy flags set?
250   if (!FLAG_IS_DEFAULT(CompileThreshold)         ||
251       !FLAG_IS_DEFAULT(OnStackReplacePercentage) ||
252       !FLAG_IS_DEFAULT(InterpreterProfilePercentage)) {
253     if (CompilerConfig::is_c1_only() || CompilerConfig::is_c2_or_jvmci_compiler_only()) {
254       // This function is called before these flags are validated. In order to not confuse the user with extraneous
255       // error messages, we check the validity of these flags here and bail out if any of them are invalid.
256       if (!check_legacy_flags()) {
257         return;
258       }
259       // Note, we do not scale CompileThreshold before this because the tiered flags are
260       // all going to be scaled further in set_compilation_policy_flags().
261       const intx threshold = CompileThreshold;
262       const intx profile_threshold = threshold * InterpreterProfilePercentage / 100;
263       const intx osr_threshold = threshold * OnStackReplacePercentage / 100;
264       const intx osr_profile_threshold = osr_threshold * InterpreterProfilePercentage / 100;
265 
266       const intx threshold_log = log2i_graceful(CompilerConfig::is_c1_only() ? threshold : profile_threshold);
267       const intx osr_threshold_log = log2i_graceful(CompilerConfig::is_c1_only() ? osr_threshold : osr_profile_threshold);
268 
269       if (Tier0InvokeNotifyFreqLog > threshold_log) {
270         FLAG_SET_ERGO(Tier0InvokeNotifyFreqLog, MAX2<intx>(0, threshold_log));
271       }
272 
273       // Note: Emulation oddity. The legacy policy limited the amount of callbacks from the
274       // interpreter for backedge events to once every 1024 counter increments.
275       // We simulate this behavior by limiting the backedge notification frequency to be
276       // at least 2^10.
277       if (Tier0BackedgeNotifyFreqLog > osr_threshold_log) {
278         FLAG_SET_ERGO(Tier0BackedgeNotifyFreqLog, MAX2<intx>(10, osr_threshold_log));
279       }
280       // Adjust the tiered policy flags to approximate the legacy behavior.
281       FLAG_SET_ERGO(Tier3InvocationThreshold, threshold);
282       FLAG_SET_ERGO(Tier3MinInvocationThreshold, threshold);
283       FLAG_SET_ERGO(Tier3CompileThreshold, threshold);
284       FLAG_SET_ERGO(Tier3BackEdgeThreshold, osr_threshold);
285       if (CompilerConfig::is_c2_or_jvmci_compiler_only()) {
286         FLAG_SET_ERGO(Tier4InvocationThreshold, threshold);
287         FLAG_SET_ERGO(Tier4MinInvocationThreshold, threshold);
288         FLAG_SET_ERGO(Tier4CompileThreshold, threshold);
289         FLAG_SET_ERGO(Tier4BackEdgeThreshold, osr_threshold);
290         FLAG_SET_ERGO(Tier0ProfilingStartPercentage, InterpreterProfilePercentage);
291       }
292     } else {
293       // Normal tiered mode, ignore legacy flags
294     }
295   }
296   // Scale CompileThreshold
297   // CompileThresholdScaling == 0.0 is equivalent to -Xint and leaves CompileThreshold unchanged.
298   if (!FLAG_IS_DEFAULT(CompileThresholdScaling) && CompileThresholdScaling > 0.0 && CompileThreshold > 0) {
299     intx scaled_value = scaled_compile_threshold(CompileThreshold);
300     if (CompileThresholdConstraintFunc(scaled_value, true) != JVMFlag::VIOLATES_CONSTRAINT) {
301       FLAG_SET_ERGO(CompileThreshold, scaled_value);
302     }
303   }
304 }
305 
306 
307 void CompilerConfig::set_compilation_policy_flags() {
308   if (is_tiered()) {
309     // Increase the code cache size - tiered compiles a lot more.
310     if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
311       FLAG_SET_ERGO(ReservedCodeCacheSize,
312                     MIN2(CODE_CACHE_DEFAULT_LIMIT, ReservedCodeCacheSize * 5));
313     }
314     // Enable SegmentedCodeCache if tiered compilation is enabled, ReservedCodeCacheSize >= 240M
315     // and the code cache contains at least 8 pages (segmentation disables advantage of huge pages).
316     if (FLAG_IS_DEFAULT(SegmentedCodeCache) && ReservedCodeCacheSize >= 240*M &&
317         8 * CodeCache::page_size() <= ReservedCodeCacheSize) {
318       FLAG_SET_ERGO(SegmentedCodeCache, true);
319     }
320     if (Arguments::is_compiler_only()) { // -Xcomp
321       // Be much more aggressive in tiered mode with -Xcomp and exercise C2 more.
322       // We will first compile a level 3 version (C1 with full profiling), then do one invocation of it and
323       // compile a level 4 (C2) and then continue executing it.
324       if (FLAG_IS_DEFAULT(Tier3InvokeNotifyFreqLog)) {
325         FLAG_SET_CMDLINE(Tier3InvokeNotifyFreqLog, 0);
326       }
327       if (FLAG_IS_DEFAULT(Tier4InvocationThreshold)) {
328         FLAG_SET_CMDLINE(Tier4InvocationThreshold, 0);
329       }
330     }
331   }
332 
333   // Current Leyden implementation requires SegmentedCodeCache: the archive-backed AOT code
334   // cache would be initialized only then. Force SegmentedCodeCache if we are loading/storing
335   // AOT code. TODO: Resolve this in code cache initialization code.
336   if (!SegmentedCodeCache && AOTCodeCache::is_caching_enabled()) {
337     FLAG_SET_ERGO(SegmentedCodeCache, true);
338     if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
339       FLAG_SET_ERGO(ReservedCodeCacheSize,
340                     MIN2(CODE_CACHE_DEFAULT_LIMIT, (size_t)ReservedCodeCacheSize * 5));
341     }
342   }
343 
344   if (CompileThresholdScaling < 0) {
345     vm_exit_during_initialization("Negative value specified for CompileThresholdScaling", nullptr);
346   }
347 
348   if (CompilationModeFlag::disable_intermediate()) {
349     if (FLAG_IS_DEFAULT(Tier0ProfilingStartPercentage)) {
350       FLAG_SET_DEFAULT(Tier0ProfilingStartPercentage, 33);
351     }
352 
353     if (FLAG_IS_DEFAULT(Tier4InvocationThreshold)) {
354       FLAG_SET_DEFAULT(Tier4InvocationThreshold, 5000);
355     }
356     if (FLAG_IS_DEFAULT(Tier4MinInvocationThreshold)) {
357       FLAG_SET_DEFAULT(Tier4MinInvocationThreshold, 600);
358     }
359     if (FLAG_IS_DEFAULT(Tier4CompileThreshold)) {
360       FLAG_SET_DEFAULT(Tier4CompileThreshold, 10000);
361     }
362     if (FLAG_IS_DEFAULT(Tier4BackEdgeThreshold)) {
363       FLAG_SET_DEFAULT(Tier4BackEdgeThreshold, 15000);
364     }
365 
366     if (FLAG_IS_DEFAULT(Tier3InvocationThreshold)) {
367       FLAG_SET_DEFAULT(Tier3InvocationThreshold, Tier4InvocationThreshold);
368     }
369     if (FLAG_IS_DEFAULT(Tier3MinInvocationThreshold)) {
370       FLAG_SET_DEFAULT(Tier3MinInvocationThreshold, Tier4MinInvocationThreshold);
371     }
372     if (FLAG_IS_DEFAULT(Tier3CompileThreshold)) {
373       FLAG_SET_DEFAULT(Tier3CompileThreshold, Tier4CompileThreshold);
374     }
375     if (FLAG_IS_DEFAULT(Tier3BackEdgeThreshold)) {
376       FLAG_SET_DEFAULT(Tier3BackEdgeThreshold, Tier4BackEdgeThreshold);
377     }
378 
379   }
380 
381   // Scale tiered compilation thresholds.
382   // CompileThresholdScaling == 0.0 is equivalent to -Xint and leaves compilation thresholds unchanged.
383   if (!FLAG_IS_DEFAULT(CompileThresholdScaling) && CompileThresholdScaling > 0.0) {
384     FLAG_SET_ERGO(Tier0InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier0InvokeNotifyFreqLog));
385     FLAG_SET_ERGO(Tier0BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier0BackedgeNotifyFreqLog));
386 
387     FLAG_SET_ERGO(Tier3InvocationThreshold, jvmflag_scaled_compile_threshold(Tier3InvocationThreshold));
388     FLAG_SET_ERGO(Tier3MinInvocationThreshold, jvmflag_scaled_compile_threshold(Tier3MinInvocationThreshold));
389     FLAG_SET_ERGO(Tier3CompileThreshold, jvmflag_scaled_compile_threshold(Tier3CompileThreshold));
390     FLAG_SET_ERGO(Tier3BackEdgeThreshold, jvmflag_scaled_compile_threshold(Tier3BackEdgeThreshold));
391 
392     // Tier2{Invocation,MinInvocation,Compile,Backedge}Threshold should be scaled here
393     // once these thresholds become supported.
394 
395     FLAG_SET_ERGO(Tier2InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier2InvokeNotifyFreqLog));
396     FLAG_SET_ERGO(Tier2BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier2BackedgeNotifyFreqLog));
397 
398     FLAG_SET_ERGO(Tier3InvokeNotifyFreqLog, jvmflag_scaled_freq_log(Tier3InvokeNotifyFreqLog));
399     FLAG_SET_ERGO(Tier3BackedgeNotifyFreqLog, jvmflag_scaled_freq_log(Tier3BackedgeNotifyFreqLog));
400 
401     FLAG_SET_ERGO(Tier23InlineeNotifyFreqLog, jvmflag_scaled_freq_log(Tier23InlineeNotifyFreqLog));
402 
403     FLAG_SET_ERGO(Tier4InvocationThreshold, jvmflag_scaled_compile_threshold(Tier4InvocationThreshold));
404     FLAG_SET_ERGO(Tier4MinInvocationThreshold, jvmflag_scaled_compile_threshold(Tier4MinInvocationThreshold));
405     FLAG_SET_ERGO(Tier4CompileThreshold, jvmflag_scaled_compile_threshold(Tier4CompileThreshold));
406     FLAG_SET_ERGO(Tier4BackEdgeThreshold, jvmflag_scaled_compile_threshold(Tier4BackEdgeThreshold));
407   }
408 
409 #ifdef COMPILER1
410   // Reduce stack usage due to inlining of methods which require much stack.
411   // (High tier compiler can inline better based on profiling information.)
412   if (FLAG_IS_DEFAULT(C1InlineStackLimit) &&
413       TieredStopAtLevel == CompLevel_full_optimization && !CompilerConfig::is_c1_only()) {
414     FLAG_SET_DEFAULT(C1InlineStackLimit, 5);
415   }
416 #endif
417 
418   if (CompilerConfig::is_tiered() && CompilerConfig::is_c2_enabled()) {
419 #ifdef COMPILER2
420     // Some inlining tuning
421 #if defined(X86) || defined(AARCH64) || defined(RISCV64)
422     if (FLAG_IS_DEFAULT(InlineSmallCode)) {
423       FLAG_SET_DEFAULT(InlineSmallCode, 2500);
424     }
425 #endif
426 #endif // COMPILER2
427   }
428 
429 }
430 
431 #if INCLUDE_JVMCI
432 void CompilerConfig::set_jvmci_specific_flags() {
433   if (UseJVMCICompiler) {
434     if (FLAG_IS_DEFAULT(TypeProfileWidth)) {
435       FLAG_SET_DEFAULT(TypeProfileWidth, 8);
436     }
437     if (FLAG_IS_DEFAULT(TypeProfileLevel)) {
438       FLAG_SET_DEFAULT(TypeProfileLevel, 0);
439     }
440 
441     if (UseJVMCINativeLibrary) {
442       // SVM compiled code requires more stack space
443       if (FLAG_IS_DEFAULT(CompilerThreadStackSize)) {
444         // Duplicate logic in the implementations of os::create_thread
445         // so that we can then double the computed stack size. Once
446         // the stack size requirements of SVM are better understood,
447         // this logic can be pushed down into os::create_thread.
448         int stack_size = CompilerThreadStackSize;
449         if (stack_size == 0) {
450           stack_size = VMThreadStackSize;
451         }
452         if (stack_size != 0) {
453           FLAG_SET_DEFAULT(CompilerThreadStackSize, stack_size * 2);
454         }
455       }
456     } else {
457       // JVMCI needs values not less than defaults
458       if (FLAG_IS_DEFAULT(ReservedCodeCacheSize)) {
459         FLAG_SET_DEFAULT(ReservedCodeCacheSize, MAX2(64*M, ReservedCodeCacheSize));
460       }
461       if (FLAG_IS_DEFAULT(InitialCodeCacheSize)) {
462         FLAG_SET_DEFAULT(InitialCodeCacheSize, MAX2(16*M, InitialCodeCacheSize));
463       }
464       if (FLAG_IS_DEFAULT(NewSizeThreadIncrease)) {
465         FLAG_SET_DEFAULT(NewSizeThreadIncrease, MAX2(4*K, NewSizeThreadIncrease));
466       }
467       if (FLAG_IS_DEFAULT(Tier3DelayOn)) {
468         // This effectively prevents the compile broker scheduling tier 2
469         // (i.e., limited C1 profiling) compilations instead of tier 3
470         // (i.e., full C1 profiling) compilations when the tier 4 queue
471         // backs up (which is quite likely when using a non-AOT compiled JVMCI
472         // compiler). The observation based on jargraal is that the downside
473         // of skipping full profiling is much worse for performance than the
474         // queue backing up.
475         FLAG_SET_DEFAULT(Tier3DelayOn, 100000);
476       }
477     } // !UseJVMCINativeLibrary
478   } // UseJVMCICompiler
479 }
480 #endif // INCLUDE_JVMCI
481 
482 bool CompilerConfig::check_args_consistency(bool status) {
483   // Check lower bounds of the code cache
484   // Template Interpreter code is approximately 3X larger in debug builds.
485   size_t min_code_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3);
486   if (ReservedCodeCacheSize < InitialCodeCacheSize) {
487     jio_fprintf(defaultStream::error_stream(),
488                 "Invalid ReservedCodeCacheSize: %zuK. Must be at least InitialCodeCacheSize=%zuK.\n",
489                 ReservedCodeCacheSize/K, InitialCodeCacheSize/K);
490     status = false;
491   } else if (ReservedCodeCacheSize < min_code_cache_size) {
492     jio_fprintf(defaultStream::error_stream(),
493                 "Invalid ReservedCodeCacheSize=%zuK. Must be at least %zuK.\n", ReservedCodeCacheSize/K,
494                 min_code_cache_size/K);
495     status = false;
496   } else if (ReservedCodeCacheSize > CODE_CACHE_SIZE_LIMIT) {
497     // Code cache size larger than CODE_CACHE_SIZE_LIMIT is not supported.
498     jio_fprintf(defaultStream::error_stream(),
499                 "Invalid ReservedCodeCacheSize=%zuM. Must be at most %zuM.\n", ReservedCodeCacheSize/M,
500                 CODE_CACHE_SIZE_LIMIT/M);
501     status = false;
502   } else if (NonNMethodCodeHeapSize < min_code_cache_size) {
503     jio_fprintf(defaultStream::error_stream(),
504                 "Invalid NonNMethodCodeHeapSize=%zuK. Must be at least %zuK.\n", NonNMethodCodeHeapSize/K,
505                 min_code_cache_size/K);
506     status = false;
507   }
508 
509 #ifdef _LP64
510   if (!FLAG_IS_DEFAULT(CICompilerCount) && !FLAG_IS_DEFAULT(CICompilerCountPerCPU) && CICompilerCountPerCPU) {
511     warning("The VM option CICompilerCountPerCPU overrides CICompilerCount.");
512   }
513 #endif
514 
515   if (BackgroundCompilation && ReplayCompiles) {
516     if (!FLAG_IS_DEFAULT(BackgroundCompilation)) {
517       warning("BackgroundCompilation disabled due to ReplayCompiles option.");
518     }
519     FLAG_SET_CMDLINE(BackgroundCompilation, false);
520   }
521 
522   if (CompilerConfig::is_interpreter_only()) {
523     if (UseCompiler) {
524       if (!FLAG_IS_DEFAULT(UseCompiler)) {
525         warning("UseCompiler disabled due to -Xint.");
526       }
527       FLAG_SET_CMDLINE(UseCompiler, false);
528     }
529     if (ProfileInterpreter) {
530       if (!FLAG_IS_DEFAULT(ProfileInterpreter)) {
531         warning("ProfileInterpreter disabled due to -Xint.");
532       }
533       FLAG_SET_CMDLINE(ProfileInterpreter, false);
534     }
535     if (TieredCompilation) {
536       if (!FLAG_IS_DEFAULT(TieredCompilation)) {
537         warning("TieredCompilation disabled due to -Xint.");
538       }
539       FLAG_SET_CMDLINE(TieredCompilation, false);
540     }
541     if (SegmentedCodeCache) {
542       warning("SegmentedCodeCache has no meaningful effect with -Xint");
543       FLAG_SET_DEFAULT(SegmentedCodeCache, false);
544     }
545 #if INCLUDE_JVMCI
546     if (EnableJVMCI || UseJVMCICompiler) {
547       if (!FLAG_IS_DEFAULT(EnableJVMCI) || !FLAG_IS_DEFAULT(UseJVMCICompiler)) {
548         warning("JVMCI Compiler disabled due to -Xint.");
549       }
550       FLAG_SET_CMDLINE(EnableJVMCI, false);
551       FLAG_SET_CMDLINE(UseJVMCICompiler, false);
552     }
553 #endif
554   } else {
555 #if INCLUDE_JVMCI
556     status = status && JVMCIGlobals::check_jvmci_flags_are_consistent();
557 #endif
558   }
559 
560   return status;
561 }
562 
563 bool CompilerConfig::should_set_client_emulation_mode_flags() {
564 #if !COMPILER1_OR_COMPILER2
565   return false;
566 #endif
567 
568   if (has_c1()) {
569     if (!is_compilation_mode_selected()) {
570       if (NeverActAsServerClassMachine) {
571         return true;
572       }
573     } else if (!has_c2() && !is_jvmci_compiler()) {
574       return true;
575     }
576   }
577 
578   return false;
579 }
580 
581 void CompilerConfig::ergo_initialize() {
582 #if !COMPILER1_OR_COMPILER2
583   return;
584 #endif
585 
586   // This property is also checked when selecting the heap size. Since client
587   // emulation mode influences Java heap memory usage, part of the logic must
588   // occur before choosing the heap size.
589   if (should_set_client_emulation_mode_flags()) {
590     set_client_emulation_mode_flags();
591   }
592 
593   set_legacy_emulation_flags();
594   set_compilation_policy_flags();
595 
596 #if INCLUDE_JVMCI
597   // Check that JVMCI supports selected GC.
598   // Should be done after GCConfig::initialize() was called.
599   JVMCIGlobals::check_jvmci_supported_gc();
600 
601   // Do JVMCI specific settings
602   set_jvmci_specific_flags();
603 #endif
604 
605   if (PreloadOnly) {
606     // Disable profiling/counter updates in interpreter and C1.
607     // This effectively disables most of the normal JIT (re-)compilations.
608     FLAG_SET_DEFAULT(ProfileInterpreter, false);
609     FLAG_SET_DEFAULT(UseOnStackReplacement, false);
610     FLAG_SET_DEFAULT(UseLoopCounter, false);
611 
612     // Disable compilations through training data replay.
613     FLAG_SET_DEFAULT(AOTReplayTraining, false);
614   }
615 
616   if (UseOnStackReplacement && !UseLoopCounter) {
617     warning("On-stack-replacement requires loop counters; enabling loop counters");
618     FLAG_SET_DEFAULT(UseLoopCounter, true);
619   }
620 
621   if (ProfileInterpreter && CompilerConfig::is_c1_simple_only()) {
622     if (!FLAG_IS_DEFAULT(ProfileInterpreter)) {
623         warning("ProfileInterpreter disabled due to client emulation mode");
624     }
625     FLAG_SET_CMDLINE(ProfileInterpreter, false);
626   }
627 
628 #ifdef COMPILER2
629   if (!EliminateLocks) {
630     EliminateNestedLocks = false;
631   }
632   if (!Inline || !IncrementalInline) {
633     IncrementalInline = false;
634     IncrementalInlineMH = false;
635     IncrementalInlineVirtual = false;
636     StressIncrementalInlining = false;
637   }
638 #ifndef PRODUCT
639   if (!IncrementalInline) {
640     AlwaysIncrementalInline = false;
641   }
642   if (FLAG_IS_CMDLINE(PrintIdealGraph) && !PrintIdealGraph) {
643     FLAG_SET_ERGO(PrintIdealGraphLevel, -1);
644   }
645 #endif
646   if (!UseTypeSpeculation && FLAG_IS_DEFAULT(TypeProfileLevel)) {
647     // nothing to use the profiling, turn if off
648     FLAG_SET_DEFAULT(TypeProfileLevel, 0);
649   }
650   if (!FLAG_IS_DEFAULT(OptoLoopAlignment) && FLAG_IS_DEFAULT(MaxLoopPad)) {
651     FLAG_SET_DEFAULT(MaxLoopPad, OptoLoopAlignment-1);
652   }
653   if (FLAG_IS_DEFAULT(LoopStripMiningIterShortLoop)) {
654     // blind guess
655     LoopStripMiningIterShortLoop = LoopStripMiningIter / 10;
656   }
657   if (UseAutoVectorizationSpeculativeAliasingChecks && !LoopMultiversioning && !UseAutoVectorizationPredicate) {
658     warning("Disabling UseAutoVectorizationSpeculativeAliasingChecks, because neither of the following is enabled:"
659             "  LoopMultiversioning UseAutoVectorizationPredicate");
660     UseAutoVectorizationSpeculativeAliasingChecks = false;
661   }
662 #endif // COMPILER2
663 }