- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathcompilerOracle.cpp
1183 lines (1066 loc) · 40.9 KB
/
compilerOracle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include"classfile/symbolTable.hpp"
#include"compiler/compilerDirectives.hpp"
#include"compiler/compilerOracle.hpp"
#include"compiler/methodMatcher.hpp"
#include"jvm.h"
#include"memory/allocation.inline.hpp"
#include"memory/oopFactory.hpp"
#include"memory/resourceArea.hpp"
#include"oops/klass.hpp"
#include"oops/method.inline.hpp"
#include"oops/symbol.hpp"
#include"opto/phasetype.hpp"
#include"opto/traceAutoVectorizationTag.hpp"
#include"opto/traceMergeStoresTag.hpp"
#include"runtime/globals_extension.hpp"
#include"runtime/handles.inline.hpp"
#include"runtime/jniHandles.hpp"
#include"runtime/os.hpp"
#include"utilities/istream.hpp"
#include"utilities/parseInteger.hpp"
// Default compile commands, if defined, are parsed before any of the
// explicitly defined compile commands. Thus, explicitly defined compile
// commands take precedence over default compile commands. The effect is
// as if the default compile commands had been specified at the start of
// the command line.
staticconstchar* const default_compile_commands[] = {
#ifdef ASSERT
// In debug builds, impose a (generous) per-compilation memory limit
// to catch pathological compilations during testing. The suboption
// "crash" will cause the JVM to assert.
//
// Note: to disable the default limit at the command line,
// set a limit of 0 (e.g. -XX:CompileCommand=MemLimit,*.*,0).
"MemLimit,*.*,1G~crash",
#endif
nullptr };
staticconstchar* optiontype_names[] = {
#defineenum_of_types(type, name) name,
OPTION_TYPES(enum_of_types)
#undef enum_of_types
};
staticconstchar* optiontype2name(enum OptionType type) {
return optiontype_names[static_cast<int>(type)];
}
staticenum OptionType option_types[] = {
#defineenum_of_options(option, name, ctype) OptionType::ctype,
COMPILECOMMAND_OPTIONS(enum_of_options)
#undef enum_of_options
};
staticenum OptionType option2type(CompileCommandEnum option) {
return option_types[static_cast<int>(option)];
}
staticconstchar* option_names[] = {
#defineenum_of_options(option, name, ctype) name,
COMPILECOMMAND_OPTIONS(enum_of_options)
#undef enum_of_options
};
staticconstchar* option2name(CompileCommandEnum option) {
return option_names[static_cast<int>(option)];
}
/* Methods to map real type names to OptionType */
template<typename T>
static OptionType get_type_for() {
return OptionType::Unknown;
};
template<> OptionType get_type_for<intx>() {
return OptionType::Intx;
}
template<> OptionType get_type_for<uintx>() {
return OptionType::Uintx;
}
template<> OptionType get_type_for<bool>() {
return OptionType::Bool;
}
template<> OptionType get_type_for<ccstr>() {
return OptionType::Ccstr;
}
template<> OptionType get_type_for<double>() {
return OptionType::Double;
}
classMethodMatcher;
classTypedMethodOptionMatcher;
static TypedMethodOptionMatcher* option_list = nullptr;
staticbool any_set = false;
// A filter for quick lookup if an option is set
staticbool option_filter[static_cast<int>(CompileCommandEnum::Unknown) + 1] = { 0 };
staticvoidcommand_set_in_filter(CompileCommandEnum option) {
assert(option != CompileCommandEnum::Unknown, "sanity");
assert(option2type(option) != OptionType::Unknown, "sanity");
if ((option != CompileCommandEnum::DontInline) &&
(option != CompileCommandEnum::Inline) &&
(option != CompileCommandEnum::Log)) {
any_set = true;
}
option_filter[static_cast<int>(option)] = true;
}
staticboolhas_command(CompileCommandEnum option) {
return option_filter[static_cast<int>(option)];
}
classTypedMethodOptionMatcher : publicMethodMatcher {
private:
TypedMethodOptionMatcher* _next;
CompileCommandEnum _option;
public:
union {
bool bool_value;
intx intx_value;
uintx uintx_value;
double double_value;
ccstr ccstr_value;
} _u;
TypedMethodOptionMatcher() : MethodMatcher(),
_next(nullptr),
_option(CompileCommandEnum::Unknown) {
memset(&_u, 0, sizeof(_u));
}
~TypedMethodOptionMatcher();
static TypedMethodOptionMatcher* parse_method_pattern(char*& line, char* errorbuf, constint buf_size);
TypedMethodOptionMatcher* match(const methodHandle &method, CompileCommandEnum option);
voidinit(CompileCommandEnum option, TypedMethodOptionMatcher* next) {
_next = next;
_option = option;
}
voidinit_matcher(Symbol* class_name, Mode class_mode,
Symbol* method_name, Mode method_mode,
Symbol* signature) {
MethodMatcher::init(class_name, class_mode, method_name, method_mode, signature);
}
voidset_next(TypedMethodOptionMatcher* next) {_next = next; }
TypedMethodOptionMatcher* next() { return _next; }
CompileCommandEnum option() { return _option; }
template<typename T> T value();
template<typename T> voidset_value(T value);
voidprint();
voidprint_all();
TypedMethodOptionMatcher* clone();
};
// A few templated accessors instead of a full template class.
template<> intx TypedMethodOptionMatcher::value<intx>() {
return _u.intx_value;
}
template<> uintx TypedMethodOptionMatcher::value<uintx>() {
return _u.uintx_value;
}
template<> bool TypedMethodOptionMatcher::value<bool>() {
return _u.bool_value;
}
template<> double TypedMethodOptionMatcher::value<double>() {
return _u.double_value;
}
template<> ccstr TypedMethodOptionMatcher::value<ccstr>() {
return _u.ccstr_value;
}
template<> voidTypedMethodOptionMatcher::set_value(intx value) {
_u.intx_value = value;
}
template<> voidTypedMethodOptionMatcher::set_value(uintx value) {
_u.uintx_value = value;
}
template<> voidTypedMethodOptionMatcher::set_value(double value) {
_u.double_value = value;
}
template<> voidTypedMethodOptionMatcher::set_value(bool value) {
_u.bool_value = value;
}
template<> voidTypedMethodOptionMatcher::set_value(ccstr value) {
_u.ccstr_value = (ccstr)os::strdup_check_oom(value);
}
voidTypedMethodOptionMatcher::print() {
ttyLocker ttyl;
print_base(tty);
constchar* name = option2name(_option);
enum OptionType type = option2type(_option);
switch (type) {
case OptionType::Intx:
tty->print_cr(" intx %s = %zd", name, value<intx>());
break;
case OptionType::Uintx:
tty->print_cr(" uintx %s = %zu", name, value<uintx>());
break;
case OptionType::Bool:
tty->print_cr(" bool %s = %s", name, value<bool>() ? "true" : "false");
break;
case OptionType::Double:
tty->print_cr(" double %s = %f", name, value<double>());
break;
case OptionType::Ccstr:
case OptionType::Ccstrlist:
tty->print_cr(" const char* %s = '%s'", name, value<ccstr>());
break;
default:
ShouldNotReachHere();
}
}
voidTypedMethodOptionMatcher::print_all() {
print();
if (_next != nullptr) {
tty->print("");
_next->print_all();
}
}
TypedMethodOptionMatcher* TypedMethodOptionMatcher::clone() {
TypedMethodOptionMatcher* m = newTypedMethodOptionMatcher();
m->_class_mode = _class_mode;
m->_class_name = _class_name;
m->_method_mode = _method_mode;
m->_method_name = _method_name;
m->_signature = _signature;
// Need to ref count the symbols
if (_class_name != nullptr) {
_class_name->increment_refcount();
}
if (_method_name != nullptr) {
_method_name->increment_refcount();
}
if (_signature != nullptr) {
_signature->increment_refcount();
}
return m;
}
TypedMethodOptionMatcher::~TypedMethodOptionMatcher() {
enum OptionType type = option2type(_option);
if (type == OptionType::Ccstr || type == OptionType::Ccstrlist) {
ccstr v = value<ccstr>();
os::free((void*)v);
}
}
TypedMethodOptionMatcher* TypedMethodOptionMatcher::parse_method_pattern(char*& line, char* errorbuf, constint buf_size) {
assert(*errorbuf == '\0', "Dont call here with error_msg already set");
constchar* error_msg = nullptr;
TypedMethodOptionMatcher* tom = newTypedMethodOptionMatcher();
MethodMatcher::parse_method_pattern(line, error_msg, tom);
if (error_msg != nullptr) {
jio_snprintf(errorbuf, buf_size, error_msg);
delete tom;
returnnullptr;
}
return tom;
}
TypedMethodOptionMatcher* TypedMethodOptionMatcher::match(const methodHandle& method, CompileCommandEnum option) {
TypedMethodOptionMatcher* current = this;
while (current != nullptr) {
if (current->_option == option) {
if (current->matches(method)) {
return current;
}
}
current = current->next();
}
returnnullptr;
}
template<typename T>
staticvoidregister_command(TypedMethodOptionMatcher* matcher,
CompileCommandEnum option,
T value) {
assert(matcher != option_list, "No circular lists please");
if (option == CompileCommandEnum::Log && !LogCompilation) {
tty->print_cr("Warning: +LogCompilation must be enabled in order for individual methods to be logged with ");
tty->print_cr(" CompileCommand=log,<method pattern>");
}
assert(CompilerOracle::option_matches_type(option, value), "Value must match option type");
if (option == CompileCommandEnum::Blackhole && !UnlockExperimentalVMOptions) {
warning("Blackhole compile option is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions");
// Delete matcher as we don't keep it
delete matcher;
return;
}
matcher->init(option, option_list);
matcher->set_value<T>(value);
option_list = matcher;
command_set_in_filter(option);
if (!CompilerOracle::be_quiet()) {
// Print out the successful registration of a compile command
ttyLocker ttyl;
tty->print("CompileCommand: %s ", option2name(option));
matcher->print();
}
return;
}
template<typename T>
boolCompilerOracle::has_option_value(const methodHandle& method, CompileCommandEnum option, T& value) {
assert(option_matches_type(option, value), "Value must match option type");
if (!has_command(option)) {
returnfalse;
}
if (option_list != nullptr) {
TypedMethodOptionMatcher* m = option_list->match(method, option);
if (m != nullptr) {
value = m->value<T>();
returntrue;
}
}
returnfalse;
}
staticboolresolve_inlining_predicate(CompileCommandEnum option, const methodHandle& method) {
assert(option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline, "Sanity");
bool v1 = false;
bool v2 = false;
bool has_inline = CompilerOracle::has_option_value(method, CompileCommandEnum::Inline, v1);
bool has_dnotinline = CompilerOracle::has_option_value(method, CompileCommandEnum::DontInline, v2);
if (has_inline && has_dnotinline) {
if (v1 && v2) {
// Conflict options detected
// Find the last one for that method and return the predicate accordingly
// option_list lists options in reverse order. So the first option we find is the last which was specified.
CompileCommandEnum last_one = CompileCommandEnum::Unknown;
TypedMethodOptionMatcher* current = option_list;
while (current != nullptr) {
last_one = current->option();
if (last_one == CompileCommandEnum::Inline || last_one == CompileCommandEnum::DontInline) {
if (current->matches(method)) {
return last_one == option;
}
}
current = current->next();
}
ShouldNotReachHere();
returnfalse;
} else {
// No conflicts
return option == CompileCommandEnum::Inline ? v1 : v2;
}
} else {
if (option == CompileCommandEnum::Inline) {
return has_inline ? v1 : false;
} else {
return has_dnotinline ? v2 : false;
}
}
}
staticboolcheck_predicate(CompileCommandEnum option, const methodHandle& method) {
// Special handling for Inline and DontInline since conflict options may be specified
if (option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline) {
returnresolve_inlining_predicate(option, method);
}
bool value = false;
if (CompilerOracle::has_option_value(method, option, value)) {
return value;
}
returnfalse;
}
boolCompilerOracle::has_any_command_set() {
return any_set;
}
// Explicit instantiation for all OptionTypes supported.
template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, CompileCommandEnum option, intx& value);
template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, CompileCommandEnum option, uintx& value);
template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, CompileCommandEnum option, bool& value);
template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, CompileCommandEnum option, ccstr& value);
template bool CompilerOracle::has_option_value<double>(const methodHandle& method, CompileCommandEnum option, double& value);
template<typename T>
boolCompilerOracle::option_matches_type(CompileCommandEnum option, T& value) {
enum OptionType option_type = option2type(option);
if (option_type == OptionType::Unknown) {
returnfalse; // Can't query options with type Unknown.
}
if (option_type == OptionType::Ccstrlist) {
option_type = OptionType::Ccstr; // CCstrList type options are stored as Ccstr
}
return (get_type_for<T>() == option_type);
}
template bool CompilerOracle::option_matches_type<intx>(CompileCommandEnum option, intx& value);
template bool CompilerOracle::option_matches_type<uintx>(CompileCommandEnum option, uintx& value);
template bool CompilerOracle::option_matches_type<bool>(CompileCommandEnum option, bool& value);
template bool CompilerOracle::option_matches_type<ccstr>(CompileCommandEnum option, ccstr& value);
template bool CompilerOracle::option_matches_type<double>(CompileCommandEnum option, double& value);
boolCompilerOracle::has_option(const methodHandle& method, CompileCommandEnum option) {
bool value = false;
has_option_value(method, option, value);
return value;
}
boolCompilerOracle::should_exclude(const methodHandle& method) {
if (check_predicate(CompileCommandEnum::Exclude, method)) {
returntrue;
}
if (has_command(CompileCommandEnum::CompileOnly)) {
return !check_predicate(CompileCommandEnum::CompileOnly, method);
}
returnfalse;
}
boolCompilerOracle::should_inline(const methodHandle& method) {
return (check_predicate(CompileCommandEnum::Inline, method));
}
boolCompilerOracle::should_not_inline(const methodHandle& method) {
returncheck_predicate(CompileCommandEnum::DontInline, method) || check_predicate(CompileCommandEnum::Exclude, method);
}
boolCompilerOracle::should_print(const methodHandle& method) {
returncheck_predicate(CompileCommandEnum::Print, method);
}
boolCompilerOracle::should_print_methods() {
returnhas_command(CompileCommandEnum::Print);
}
// Tells whether there are any methods to collect memory statistics for
boolCompilerOracle::should_collect_memstat() {
returnhas_command(CompileCommandEnum::MemStat) || has_command(CompileCommandEnum::MemLimit);
}
boolCompilerOracle::should_log(const methodHandle& method) {
if (!LogCompilation) returnfalse;
if (!has_command(CompileCommandEnum::Log)) {
returntrue; // by default, log all
}
return (check_predicate(CompileCommandEnum::Log, method));
}
boolCompilerOracle::should_break_at(const methodHandle& method) {
returncheck_predicate(CompileCommandEnum::Break, method);
}
voidCompilerOracle::tag_blackhole_if_possible(const methodHandle& method) {
if (!check_predicate(CompileCommandEnum::Blackhole, method)) {
return;
}
guarantee(UnlockExperimentalVMOptions, "Checked during initial parsing");
if (method->result_type() != T_VOID) {
warning("Blackhole compile option only works for methods with void type: %s",
method->name_and_sig_as_C_string());
return;
}
if (!method->is_empty_method()) {
warning("Blackhole compile option only works for empty methods: %s",
method->name_and_sig_as_C_string());
return;
}
if (!method->is_static()) {
warning("Blackhole compile option only works for static methods: %s",
method->name_and_sig_as_C_string());
return;
}
if (method->intrinsic_id() == vmIntrinsics::_blackhole) {
return;
}
if (method->intrinsic_id() != vmIntrinsics::_none) {
warning("Blackhole compile option only works for methods that do not have intrinsic set: %s, %s",
method->name_and_sig_as_C_string(), vmIntrinsics::name_at(method->intrinsic_id()));
return;
}
method->set_intrinsic_id(vmIntrinsics::_blackhole);
}
static CompileCommandEnum match_option_name(constchar* line, int* bytes_read, char* errorbuf, int bufsize) {
assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommandEnum::Count), "option_names size mismatch");
*bytes_read = 0;
char option_buf[256];
int matches = sscanf(line, "%255[a-zA-Z0-9]%n", option_buf, bytes_read);
if (matches > 0 && strcasecmp(option_buf, "unknown") != 0) {
for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
if (strcasecmp(option_buf, option_names[i]) == 0) {
returnstatic_cast<CompileCommandEnum>(i);
}
}
}
jio_snprintf(errorbuf, bufsize, "Unrecognized option '%s'", option_buf);
return CompileCommandEnum::Unknown;
}
// match exactly and don't mess with errorbuf
CompileCommandEnum CompilerOracle::parse_option_name(constchar* line) {
for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
if (strcasecmp(line, option_names[i]) == 0) {
returnstatic_cast<CompileCommandEnum>(i);
}
}
return CompileCommandEnum::Unknown;
}
enum OptionType CompilerOracle::parse_option_type(constchar* type_str) {
for (uint i = 0; i < ARRAY_SIZE(optiontype_names); i++) {
if (strcasecmp(type_str, optiontype_names[i]) == 0) {
returnstatic_cast<enum OptionType>(i);
}
}
return OptionType::Unknown;
}
staticvoidprint_tip() { // CMH Update info
tty->cr();
tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>' - to set boolean option to true");
tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>,<value>'");
tty->print_cr("Use: '-XX:CompileCommand=help' for more information and to list all option.");
tty->cr();
}
staticvoidprint_option(CompileCommandEnum option, constchar* name, enum OptionType type) {
if (type != OptionType::Unknown) {
tty->print_cr(" %s (%s)", name, optiontype2name(type));
}
}
staticvoidprint_commands() {
tty->cr();
tty->print_cr("All available options:");
#defineenum_of_options(option, name, ctype) print_option(CompileCommandEnum::option, name, OptionType::ctype);
COMPILECOMMAND_OPTIONS(enum_of_options)
#undef enum_of_options
tty->cr();
}
staticvoidusage() {
tty->cr();
tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
tty->print_cr("behavior of the dynamic compilers.");
tty->cr();
tty->print_cr("Compile commands has this general form:");
tty->print_cr("-XX:CompileCommand=<option><method pattern><value>");
tty->print_cr(" Sets <option> to the specified value for methods matching <method pattern>");
tty->print_cr(" All options are typed");
tty->cr();
tty->print_cr("-XX:CompileCommand=<option><method pattern>");
tty->print_cr(" Sets <option> to true for methods matching <method pattern>");
tty->print_cr(" Only applies to boolean options.");
tty->cr();
tty->print_cr("-XX:CompileCommand=quiet");
tty->print_cr(" Silence the compile command output");
tty->cr();
tty->print_cr("-XX:CompileCommand=help");
tty->print_cr(" Prints this help text");
tty->cr();
print_commands();
tty->cr();
tty->print_cr("Method patterns has the format:");
tty->print_cr(" package/Class.method()");
tty->cr();
tty->print_cr("For backward compatibility this form is also allowed:");
tty->print_cr(" package.Class::method()");
tty->cr();
tty->print_cr("The signature can be separated by an optional whitespace or comma:");
tty->print_cr(" package/Class.method ()");
tty->cr();
tty->print_cr("The class and method identifier can be used together with leading or");
tty->print_cr("trailing *'s for wildcard matching:");
tty->print_cr(" *ackage/Clas*.*etho*()");
tty->cr();
tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
tty->print_cr(" -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
tty->cr();
tty->print_cr("The CompileCommands can be loaded from a file with the flag");
tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
tty->print_cr("Add one command on each line.");
tty->print_cr(" exclude java/*.*");
tty->print_cr(" option java/*.* ReplayInline");
tty->cr();
tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
tty->print_cr("commands to the same method results in undefined behavior.");
tty->cr();
tty->print_cr("The 'exclude' command excludes methods from top-level compilations as well as");
tty->print_cr("from inlining, whereas the 'compileonly' command only excludes methods from");
tty->print_cr("top-level compilations (i.e. they can still be inlined into other compilation units).");
tty->cr();
};
staticintskip_whitespace(char* &line) {
// Skip any leading spaces
int whitespace_read = 0;
sscanf(line, "%*[ \t]%n", &whitespace_read);
line += whitespace_read;
return whitespace_read;
}
staticvoidskip_comma(char* &line) {
// Skip any leading spaces
if (*line == ',') {
line++;
}
}
staticboolparseMemLimit(constchar* line, intx& value, int& bytes_read, char* errorbuf, constint buf_size) {
// Format:
// "<memory size>['~' <suboption>]"
// <memory size> can have units, e.g. M
// <suboption> one of "crash" "stop", if omitted, "stop" is implied.
//
// Examples:
// -XX:CompileCommand='memlimit,*.*,20m'
// -XX:CompileCommand='memlimit,*.*,20m~stop'
// -XX:CompileCommand='memlimit,Option::toString,1m~crash'
//
// The resulting intx carries the size and whether we are to stop or crash:
// - neg. value means crash
// - pos. value (default) means stop
size_t s = 0;
char* end;
if (!parse_integer<size_t>(line, &end, &s)) {
jio_snprintf(errorbuf, buf_size, "MemLimit: invalid value");
returnfalse;
}
bytes_read = (int)(end - line);
intx v = (intx)s;
if ((*end) != '\0') {
if (strncasecmp(end, "~crash", 6) == 0) {
v = -v;
bytes_read += 6;
} elseif (strncasecmp(end, "~stop", 5) == 0) {
// ok, this is the default
bytes_read += 5;
} else {
jio_snprintf(errorbuf, buf_size, "MemLimit: invalid option");
returnfalse;
}
}
value = v;
returntrue;
}
staticboolparseMemStat(constchar* line, uintx& value, int& bytes_read, char* errorbuf, constint buf_size) {
#defineIF_ENUM_STRING(S, CMD) \
if (strncasecmp(line, S, strlen(S)) == 0) { \
bytes_read += (int)strlen(S); \
CMD \
returntrue; \
}
IF_ENUM_STRING("collect", {
value = (uintx)MemStatAction::collect;
});
IF_ENUM_STRING("print", {
value = (uintx)MemStatAction::print;
});
#undef IF_ENUM_STRING
jio_snprintf(errorbuf, buf_size, "MemStat: invalid option");
returnfalse;
}
staticvoidscan_value(enum OptionType type, char* line, int& total_bytes_read,
TypedMethodOptionMatcher* matcher, CompileCommandEnum option, char* errorbuf, constint buf_size) {
int bytes_read = 0;
constchar* ccname = option2name(option);
constchar* type_str = optiontype2name(type);
int skipped = skip_whitespace(line);
total_bytes_read += skipped;
if (type == OptionType::Intx) {
intx value;
bool success = false;
if (option == CompileCommandEnum::MemLimit) {
// Special parsing for MemLimit
success = parseMemLimit(line, value, bytes_read, errorbuf, buf_size);
} else {
// Is it a raw number?
success = sscanf(line, "%zd%n", &value, &bytes_read) == 1;
}
if (success) {
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, value);
return;
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} elseif (type == OptionType::Uintx) {
uintx value;
bool success = false;
if (option == CompileCommandEnum::MemStat) {
// Special parsing for MemStat
success = parseMemStat(line, value, bytes_read, errorbuf, buf_size);
} else {
// parse as raw number
success = sscanf(line, "%zu%n", &value, &bytes_read) == 1;
}
if (success) {
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, value);
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} elseif (type == OptionType::Ccstr) {
ResourceMark rm;
char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
if (sscanf(line, "%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, (ccstr) value);
return;
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} elseif (type == OptionType::Ccstrlist) {
// Accumulates several strings into one. The internal type is ccstr.
ResourceMark rm;
char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
char* next_value = value;
if (sscanf(line, "%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
total_bytes_read += bytes_read;
line += bytes_read;
next_value += bytes_read + 1;
char* end_value = next_value - 1;
while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
total_bytes_read += bytes_read;
line += bytes_read;
*end_value = ''; // override '\0'
next_value += bytes_read;
end_value = next_value-1;
}
if (option == CompileCommandEnum::ControlIntrinsic || option == CompileCommandEnum::DisableIntrinsic) {
ControlIntrinsicValidator validator(value, (option == CompileCommandEnum::DisableIntrinsic));
if (!validator.is_valid()) {
jio_snprintf(errorbuf, buf_size, "Unrecognized intrinsic detected in %s: %s", option2name(option), validator.what());
}
}
#if !defined(PRODUCT) && defined(COMPILER2)
elseif (option == CompileCommandEnum::TraceAutoVectorization) {
TraceAutoVectorizationTagValidator validator(value, true);
if (!validator.is_valid()) {
jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
}
} elseif (option == CompileCommandEnum::TraceMergeStores) {
TraceMergeStores::TagValidator validator(value, true);
if (!validator.is_valid()) {
jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
}
} elseif (option == CompileCommandEnum::PrintIdealPhase) {
PhaseNameValidator validator(value);
if (!validator.is_valid()) {
jio_snprintf(errorbuf, buf_size, "Unrecognized phase name in %s: %s", option2name(option), validator.what());
}
} elseif (option == CompileCommandEnum::TestOptionList) {
// all values are ok
}
#endif
else {
assert(false, "Ccstrlist type option missing validator");
}
register_command(matcher, option, (ccstr) value);
return;
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} elseif (type == OptionType::Bool) {
char value[256];
if (*line == '\0') {
// Short version of a CompileCommand sets a boolean Option to true
// -XXCompileCommand=<Option>,<method pattern>
register_command(matcher, option, true);
return;
}
if (sscanf(line, "%255[a-zA-Z]%n", value, &bytes_read) == 1) {
if (strcasecmp(value, "true") == 0) {
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, true);
return;
} elseif (strcasecmp(value, "false") == 0) {
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, false);
return;
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} elseif (type == OptionType::Double) {
char buffer[2][256];
// Decimal separator '.' has been replaced with ' ' or '/' earlier,
// so read integer and fraction part of double value separately.
if (sscanf(line, "%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
char value[512] = "";
jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
total_bytes_read += bytes_read;
line += bytes_read;
register_command(matcher, option, atof(value));
return;
} else {
jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
}
} else {
jio_snprintf(errorbuf, buf_size, "Type '%s' not supported ", type_str);
}
}
// Scan next option and value in line, return MethodMatcher object on success, nullptr on failure.
// On failure, error_msg contains description for the first error.
// For future extensions: set error_msg on first error.
staticvoidscan_option_and_value(enum OptionType type, char* line, int& total_bytes_read,
TypedMethodOptionMatcher* matcher,
char* errorbuf, constint buf_size) {
total_bytes_read = 0;
int bytes_read = 0;
char option_buf[256];
// Read option name.
if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option_buf, &bytes_read) == 1) {
line += bytes_read;
total_bytes_read += bytes_read;
int bytes_read2 = 0;
total_bytes_read += skip_whitespace(line);
CompileCommandEnum option = match_option_name(option_buf, &bytes_read2, errorbuf, buf_size);
if (option == CompileCommandEnum::Unknown) {
assert(*errorbuf != '\0', "error must have been set");
return;
}
enum OptionType optiontype = option2type(option);
if (option2type(option) != type) {
constchar* optiontype_name = optiontype2name(optiontype);
constchar* type_name = optiontype2name(type);
jio_snprintf(errorbuf, buf_size, "Option '%s' with type '%s' doesn't match supplied type '%s'", option_buf, optiontype_name, type_name);
return;
}
scan_value(type, line, total_bytes_read, matcher, option, errorbuf, buf_size);
} else {
constchar* type_str = optiontype2name(type);
jio_snprintf(errorbuf, buf_size, "Option name for type '%s' should be alphanumeric ", type_str);
}
return;
}
voidCompilerOracle::print_parse_error(char* error_msg, char* original_line) {
assert(*error_msg != '\0', "Must have error_message");
ttyLocker ttyl;
tty->print_cr("CompileCommand: An error occurred during parsing");
tty->print_cr("Error: %s", error_msg);
tty->print_cr("Line: '%s'", original_line);
print_tip();
}
classLineCopy : StackObj {
constchar* _copy;
public:
LineCopy(char* line) {
_copy = os::strdup(line, mtInternal);
}
~LineCopy() {
os::free((void*)_copy);
}
char* get() {
return (char*)_copy;
}
};
boolCompilerOracle::parse_from_line_quietly(char* line) {
constbool quiet0 = _quiet;
_quiet = true;
constbool result = parse_from_line(line);
_quiet = quiet0;
return result;
}
boolCompilerOracle::parse_from_line(char* line) {
if ((line[0] == '\0') || (line[0] == '#')) {
returntrue;
}
LineCopy original(line);
int bytes_read;
char error_buf[1024] = {0};
CompileCommandEnum option = match_option_name(line, &bytes_read, error_buf, sizeof(error_buf));
line += bytes_read;
ResourceMark rm;
if (option == CompileCommandEnum::Unknown) {
print_parse_error(error_buf, original.get());
returnfalse;
}
if (option == CompileCommandEnum::Quiet) {
_quiet = true;
returntrue;
}
if (option == CompileCommandEnum::Help) {
usage();
returntrue;
}
if (option == CompileCommandEnum::Option) {
// Look for trailing options.
//
// Two types of trailing options are
// supported:
//
// (1) CompileCommand=option,Klass::method,option
// (2) CompileCommand=option,Klass::method,type,option,value
//
// Type (1) is used to enable a boolean option for a method.
//
// Type (2) is used to support options with a value. Values can have the
// the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
char option_type[256]; // stores option for Type (1) and type of Type (2)
skip_comma(line);
TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
if (archetype == nullptr) {
print_parse_error(error_buf, original.get());
returnfalse;
}
skip_whitespace(line);
// This is unnecessarily complex. Should retire multi-option lines and skip while loop
while (sscanf(line, "%255[a-zA-Z0-9]%n", option_type, &bytes_read) == 1) {
line += bytes_read;
// typed_matcher is used as a blueprint for each option, deleted at the end
TypedMethodOptionMatcher* typed_matcher = archetype->clone();
enum OptionType type = parse_option_type(option_type);
if (type != OptionType::Unknown) {
// Type (2) option: parse option name and value.
scan_option_and_value(type, line, bytes_read, typed_matcher, error_buf, sizeof(error_buf));
if (*error_buf != '\0') {