forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateOopMap.cpp
More file actions
2565 lines (2205 loc) · 82.1 KB
/
Copy pathgenerateOopMap.cpp
File metadata and controls
2565 lines (2205 loc) · 82.1 KB
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) 1997, 2021, 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 "precompiled.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/constantPool.hpp"
#include "oops/generateOopMap.hpp"
#include "oops/oop.inline.hpp"
#include "oops/symbol.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/os.hpp"
#include "runtime/relocator.hpp"
#include "runtime/timerTrace.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/ostream.hpp"
//
//
// Compute stack layouts for each instruction in method.
//
// Problems:
// - What to do about jsr with different types of local vars?
// Need maps that are conditional on jsr path?
// - Jsr and exceptions should be done more efficiently (the retAddr stuff)
//
// Alternative:
// - Could extend verifier to provide this information.
// For: one fewer abstract interpreter to maintain. Against: the verifier
// solves a bigger problem so slower (undesirable to force verification of
// everything?).
//
// Algorithm:
// Partition bytecodes into basic blocks
// For each basic block: store entry state (vars, stack). For instructions
// inside basic blocks we do not store any state (instead we recompute it
// from state produced by previous instruction).
//
// Perform abstract interpretation of bytecodes over this lattice:
//
// _--'#'--_
// / / \ \
// / / \ \
// / | | \
// 'r' 'v' 'p' ' '
// \ | | /
// \ \ / /
// \ \ / /
// -- '@' --
//
// '#' top, result of conflict merge
// 'r' reference type
// 'v' value type
// 'p' pc type for jsr/ret
// ' ' uninitialized; never occurs on operand stack in Java
// '@' bottom/unexecuted; initial state each bytecode.
//
// Basic block headers are the only merge points. We use this iteration to
// compute the information:
//
// find basic blocks;
// initialize them with uninitialized state;
// initialize first BB according to method signature;
// mark first BB changed
// while (some BB is changed) do {
// perform abstract interpration of all bytecodes in BB;
// merge exit state of BB into entry state of all successor BBs,
// noting if any of these change;
// }
//
// One additional complication is necessary. The jsr instruction pushes
// a return PC on the stack (a 'p' type in the abstract interpretation).
// To be able to process "ret" bytecodes, we keep track of these return
// PC's in a 'retAddrs' structure in abstract interpreter context (when
// processing a "ret" bytecodes, it is not sufficient to know that it gets
// an argument of the right type 'p'; we need to know which address it
// returns to).
//
// (Note this comment is borrowed form the original author of the algorithm)
// ComputeCallStack
//
// Specialization of SignatureIterator - compute the effects of a call
//
class ComputeCallStack : public SignatureIterator {
CellTypeState *_effect;
int _idx;
void setup();
void set(CellTypeState state) { _effect[_idx++] = state; }
int length() { return _idx; };
friend class SignatureIterator; // so do_parameters_on can call do_type
void do_type(BasicType type, bool for_return = false) {
if (for_return && type == T_VOID) {
set(CellTypeState::bottom);
} else if (is_reference_type(type)) {
set(CellTypeState::ref);
} else {
assert(is_java_primitive(type), "");
set(CellTypeState::value);
if (is_double_word_type(type)) {
set(CellTypeState::value);
}
}
}
public:
ComputeCallStack(Symbol* signature) : SignatureIterator(signature) {};
// Compute methods
int compute_for_parameters(bool is_static, CellTypeState *effect) {
_idx = 0;
_effect = effect;
if (!is_static)
effect[_idx++] = CellTypeState::ref;
do_parameters_on(this);
return length();
};
int compute_for_returntype(CellTypeState *effect) {
_idx = 0;
_effect = effect;
do_type(return_type(), true);
set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
return length();
}
};
//=========================================================================================
// ComputeEntryStack
//
// Specialization of SignatureIterator - in order to set up first stack frame
//
class ComputeEntryStack : public SignatureIterator {
CellTypeState *_effect;
int _idx;
void setup();
void set(CellTypeState state) { _effect[_idx++] = state; }
int length() { return _idx; };
friend class SignatureIterator; // so do_parameters_on can call do_type
void do_type(BasicType type, bool for_return = false) {
if (for_return && type == T_VOID) {
set(CellTypeState::bottom);
} else if (is_reference_type(type)) {
set(CellTypeState::make_slot_ref(_idx));
} else {
assert(is_java_primitive(type), "");
set(CellTypeState::value);
if (is_double_word_type(type)) {
set(CellTypeState::value);
}
}
}
public:
ComputeEntryStack(Symbol* signature) : SignatureIterator(signature) {};
// Compute methods
int compute_for_parameters(bool is_static, CellTypeState *effect) {
_idx = 0;
_effect = effect;
if (!is_static)
effect[_idx++] = CellTypeState::make_slot_ref(0);
do_parameters_on(this);
return length();
};
int compute_for_returntype(CellTypeState *effect) {
_idx = 0;
_effect = effect;
do_type(return_type(), true);
set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
return length();
}
};
//=====================================================================================
//
// Implementation of RetTable/RetTableEntry
//
// Contains function to itereate through all bytecodes
// and find all return entry points
//
int RetTable::_init_nof_entries = 10;
int RetTableEntry::_init_nof_jsrs = 5;
RetTableEntry::RetTableEntry(int target, RetTableEntry *next) {
_target_bci = target;
_jsrs = new GrowableArray<intptr_t>(_init_nof_jsrs);
_next = next;
}
void RetTableEntry::add_delta(int bci, int delta) {
if (_target_bci > bci) _target_bci += delta;
for (int k = 0; k < _jsrs->length(); k++) {
int jsr = _jsrs->at(k);
if (jsr > bci) _jsrs->at_put(k, jsr+delta);
}
}
void RetTable::compute_ret_table(const methodHandle& method) {
BytecodeStream i(method);
Bytecodes::Code bytecode;
while( (bytecode = i.next()) >= 0) {
switch (bytecode) {
case Bytecodes::_jsr:
add_jsr(i.next_bci(), i.dest());
break;
case Bytecodes::_jsr_w:
add_jsr(i.next_bci(), i.dest_w());
break;
default:
break;
}
}
}
void RetTable::add_jsr(int return_bci, int target_bci) {
RetTableEntry* entry = _first;
// Scan table for entry
for (;entry && entry->target_bci() != target_bci; entry = entry->next());
if (!entry) {
// Allocate new entry and put in list
entry = new RetTableEntry(target_bci, _first);
_first = entry;
}
// Now "entry" is set. Make sure that the entry is initialized
// and has room for the new jsr.
entry->add_jsr(return_bci);
}
RetTableEntry* RetTable::find_jsrs_for_target(int targBci) {
RetTableEntry *cur = _first;
while(cur) {
assert(cur->target_bci() != -1, "sanity check");
if (cur->target_bci() == targBci) return cur;
cur = cur->next();
}
ShouldNotReachHere();
return NULL;
}
// The instruction at bci is changing size by "delta". Update the return map.
void RetTable::update_ret_table(int bci, int delta) {
RetTableEntry *cur = _first;
while(cur) {
cur->add_delta(bci, delta);
cur = cur->next();
}
}
//
// Celltype state
//
CellTypeState CellTypeState::bottom = CellTypeState::make_bottom();
CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value);
CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict);
CellTypeState CellTypeState::value = CellTypeState::make_any(val_value);
CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value);
CellTypeState CellTypeState::top = CellTypeState::make_top();
CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict);
// Commonly used constants
static CellTypeState epsilonCTS[1] = { CellTypeState::bottom };
static CellTypeState refCTS = CellTypeState::ref;
static CellTypeState valCTS = CellTypeState::value;
static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom };
static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom };
static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom };
static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
char CellTypeState::to_char() const {
if (can_be_reference()) {
if (can_be_value() || can_be_address())
return '#'; // Conflict that needs to be rewritten
else
return 'r';
} else if (can_be_value())
return 'v';
else if (can_be_address())
return 'p';
else if (can_be_uninit())
return ' ';
else
return '@';
}
// Print a detailed CellTypeState. Indicate all bits that are set. If
// the CellTypeState represents an address or a reference, print the
// value of the additional information.
void CellTypeState::print(outputStream *os) {
if (can_be_address()) {
os->print("(p");
} else {
os->print("( ");
}
if (can_be_reference()) {
os->print("r");
} else {
os->print(" ");
}
if (can_be_value()) {
os->print("v");
} else {
os->print(" ");
}
if (can_be_uninit()) {
os->print("u|");
} else {
os->print(" |");
}
if (is_info_top()) {
os->print("Top)");
} else if (is_info_bottom()) {
os->print("Bot)");
} else {
if (is_reference()) {
int info = get_info();
int data = info & ~(ref_not_lock_bit | ref_slot_bit);
if (info & ref_not_lock_bit) {
// Not a monitor lock reference.
if (info & ref_slot_bit) {
// slot
os->print("slot%d)", data);
} else {
// line
os->print("line%d)", data);
}
} else {
// lock
os->print("lock%d)", data);
}
} else {
os->print("%d)", get_info());
}
}
}
//
// Basicblock handling methods
//
void GenerateOopMap::initialize_bb() {
_gc_points = 0;
_bb_count = 0;
_bb_hdr_bits.reinitialize(method()->code_size());
}
void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) {
assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
if (c->is_bb_header(bci))
return;
if (TraceNewOopMapGeneration) {
tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci);
}
c->set_bbmark_bit(bci);
c->_bb_count++;
}
void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
initialize_bb();
bool fellThrough = false; // False to get first BB marked.
// First mark all exception handlers as start of a basic-block
ExceptionTable excps(method());
for(int i = 0; i < excps.length(); i ++) {
bb_mark_fct(this, excps.handler_pc(i), NULL);
}
// Then iterate through the code
BytecodeStream bcs(_method);
Bytecodes::Code bytecode;
while( (bytecode = bcs.next()) >= 0) {
int bci = bcs.bci();
if (!fellThrough)
bb_mark_fct(this, bci, NULL);
fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL);
/* We will also mark successors of jsr's as basic block headers. */
switch (bytecode) {
case Bytecodes::_jsr:
assert(!fellThrough, "should not happen");
bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
break;
case Bytecodes::_jsr_w:
assert(!fellThrough, "should not happen");
bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
break;
default:
break;
}
if (possible_gc_point(&bcs))
_gc_points++;
}
}
void GenerateOopMap::set_bbmark_bit(int bci) {
_bb_hdr_bits.at_put(bci, true);
}
void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) {
assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
BasicBlock* bb = c->get_basic_block_at(bci);
if (bb->is_dead()) {
bb->mark_as_alive();
*data = 1; // Mark basicblock as changed
}
}
void GenerateOopMap::mark_reachable_code() {
int change = 1; // int to get function pointers to work
// Mark entry basic block as alive and all exception handlers
_basic_blocks[0].mark_as_alive();
ExceptionTable excps(method());
for(int i = 0; i < excps.length(); i++) {
BasicBlock *bb = get_basic_block_at(excps.handler_pc(i));
// If block is not already alive (due to multiple exception handlers to same bb), then
// make it alive
if (bb->is_dead()) bb->mark_as_alive();
}
BytecodeStream bcs(_method);
// Iterate through all basic blocks until we reach a fixpoint
while (change) {
change = 0;
for (int i = 0; i < _bb_count; i++) {
BasicBlock *bb = &_basic_blocks[i];
if (bb->is_alive()) {
// Position bytecodestream at last bytecode in basicblock
bcs.set_start(bb->_end_bci);
bcs.next();
Bytecodes::Code bytecode = bcs.code();
int bci = bcs.bci();
assert(bci == bb->_end_bci, "wrong bci");
bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change);
// We will also mark successors of jsr's as alive.
switch (bytecode) {
case Bytecodes::_jsr:
case Bytecodes::_jsr_w:
assert(!fell_through, "should not happen");
reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
break;
default:
break;
}
if (fell_through) {
// Mark successor as alive
if (bb[1].is_dead()) {
bb[1].mark_as_alive();
change = 1;
}
}
}
}
}
}
/* If the current instruction in "c" has no effect on control flow,
returns "true". Otherwise, calls "jmpFct" one or more times, with
"c", an appropriate "pcDelta", and "data" as arguments, then
returns "false". There is one exception: if the current
instruction is a "ret", returns "false" without calling "jmpFct".
Arrangements for tracking the control flow of a "ret" must be made
externally. */
bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) {
int bci = bcs->bci();
switch (bcs->code()) {
case Bytecodes::_ifeq:
case Bytecodes::_ifne:
case Bytecodes::_iflt:
case Bytecodes::_ifge:
case Bytecodes::_ifgt:
case Bytecodes::_ifle:
case Bytecodes::_if_icmpeq:
case Bytecodes::_if_icmpne:
case Bytecodes::_if_icmplt:
case Bytecodes::_if_icmpge:
case Bytecodes::_if_icmpgt:
case Bytecodes::_if_icmple:
case Bytecodes::_if_acmpeq:
case Bytecodes::_if_acmpne:
case Bytecodes::_ifnull:
case Bytecodes::_ifnonnull:
(*jmpFct)(this, bcs->dest(), data);
(*jmpFct)(this, bci + 3, data);
break;
case Bytecodes::_goto:
(*jmpFct)(this, bcs->dest(), data);
break;
case Bytecodes::_goto_w:
(*jmpFct)(this, bcs->dest_w(), data);
break;
case Bytecodes::_tableswitch:
{ Bytecode_tableswitch tableswitch(method(), bcs->bcp());
int len = tableswitch.length();
(*jmpFct)(this, bci + tableswitch.default_offset(), data); /* Default. jump address */
while (--len >= 0) {
(*jmpFct)(this, bci + tableswitch.dest_offset_at(len), data);
}
break;
}
case Bytecodes::_lookupswitch:
{ Bytecode_lookupswitch lookupswitch(method(), bcs->bcp());
int npairs = lookupswitch.number_of_pairs();
(*jmpFct)(this, bci + lookupswitch.default_offset(), data); /* Default. */
while(--npairs >= 0) {
LookupswitchPair pair = lookupswitch.pair_at(npairs);
(*jmpFct)(this, bci + pair.offset(), data);
}
break;
}
case Bytecodes::_jsr:
assert(bcs->is_wide()==false, "sanity check");
(*jmpFct)(this, bcs->dest(), data);
break;
case Bytecodes::_jsr_w:
(*jmpFct)(this, bcs->dest_w(), data);
break;
case Bytecodes::_wide:
ShouldNotReachHere();
return true;
break;
case Bytecodes::_athrow:
case Bytecodes::_ireturn:
case Bytecodes::_lreturn:
case Bytecodes::_freturn:
case Bytecodes::_dreturn:
case Bytecodes::_areturn:
case Bytecodes::_return:
case Bytecodes::_ret:
break;
default:
return true;
}
return false;
}
/* Requires "pc" to be the head of a basic block; returns that basic
block. */
BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const {
BasicBlock* bb = get_basic_block_containing(bci);
assert(bb->_bci == bci, "should have found BB");
return bb;
}
// Requires "pc" to be the start of an instruction; returns the basic
// block containing that instruction. */
BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const {
BasicBlock *bbs = _basic_blocks;
int lo = 0, hi = _bb_count - 1;
while (lo <= hi) {
int m = (lo + hi) / 2;
int mbci = bbs[m]._bci;
int nbci;
if ( m == _bb_count-1) {
assert( bci >= mbci && bci < method()->code_size(), "sanity check failed");
return bbs+m;
} else {
nbci = bbs[m+1]._bci;
}
if ( mbci <= bci && bci < nbci) {
return bbs+m;
} else if (mbci < bci) {
lo = m + 1;
} else {
assert(mbci > bci, "sanity check");
hi = m - 1;
}
}
fatal("should have found BB");
return NULL;
}
void GenerateOopMap::restore_state(BasicBlock *bb)
{
memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState));
_stack_top = bb->_stack_top;
_monitor_top = bb->_monitor_top;
}
int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) {
int bbNum = bb - _basic_blocks + 1;
if (bbNum == _bb_count)
return method()->code_size();
return _basic_blocks[bbNum]._bci;
}
//
// CellType handling methods
//
// Allocate memory and throw LinkageError if failure.
#define ALLOC_RESOURCE_ARRAY(var, type, count) \
var = NEW_RESOURCE_ARRAY_RETURN_NULL(type, count); \
if (var == NULL) { \
report_error("Cannot reserve enough memory to analyze this method"); \
return; \
}
void GenerateOopMap::init_state() {
_state_len = _max_locals + _max_stack + _max_monitors;
ALLOC_RESOURCE_ARRAY(_state, CellTypeState, _state_len);
memset(_state, 0, _state_len * sizeof(CellTypeState));
int count = MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */;
ALLOC_RESOURCE_ARRAY(_state_vec_buf, char, count);
}
void GenerateOopMap::make_context_uninitialized() {
CellTypeState* vs = vars();
for (int i = 0; i < _max_locals; i++)
vs[i] = CellTypeState::uninit;
_stack_top = 0;
_monitor_top = 0;
}
int GenerateOopMap::methodsig_to_effect(Symbol* signature, bool is_static, CellTypeState* effect) {
ComputeEntryStack ces(signature);
return ces.compute_for_parameters(is_static, effect);
}
// Return result of merging cts1 and cts2.
CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const {
CellTypeState result;
assert(!is_bottom() && !cts.is_bottom(),
"merge of bottom values is handled elsewhere");
result._state = _state | cts._state;
// If the top bit is set, we don't need to do any more work.
if (!result.is_info_top()) {
assert((result.can_be_address() || result.can_be_reference()),
"only addresses and references have non-top info");
if (!equal(cts)) {
// The two values being merged are different. Raise to top.
if (result.is_reference()) {
result = CellTypeState::make_slot_ref(slot);
} else {
result._state |= info_conflict;
}
}
}
assert(result.is_valid_state(), "checking that CTS merge maintains legal state");
return result;
}
// Merge the variable state for locals and stack from cts into bbts.
bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts,
CellTypeState* bbts) {
int i;
int len = _max_locals + _stack_top;
bool change = false;
for (i = len - 1; i >= 0; i--) {
CellTypeState v = cts[i].merge(bbts[i], i);
change = change || !v.equal(bbts[i]);
bbts[i] = v;
}
return change;
}
// Merge the monitor stack state from cts into bbts.
bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts,
CellTypeState* bbts) {
bool change = false;
if (_max_monitors > 0 && _monitor_top != bad_monitors) {
// If there are no monitors in the program, or there has been
// a monitor matching error before this point in the program,
// then we do not merge in the monitor state.
int base = _max_locals + _max_stack;
int len = base + _monitor_top;
for (int i = len - 1; i >= base; i--) {
CellTypeState v = cts[i].merge(bbts[i], i);
// Can we prove that, when there has been a change, it will already
// have been detected at this point? That would make this equal
// check here unnecessary.
change = change || !v.equal(bbts[i]);
bbts[i] = v;
}
}
return change;
}
void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) {
int len = _max_locals + _stack_top;
for (int i = 0; i < len; i++) {
if (src[i].is_nonlock_reference()) {
dst[i] = CellTypeState::make_slot_ref(i);
} else {
dst[i] = src[i];
}
}
if (_max_monitors > 0 && _monitor_top != bad_monitors) {
int base = _max_locals + _max_stack;
len = base + _monitor_top;
for (int i = base; i < len; i++) {
dst[i] = src[i];
}
}
}
// Merge the states for the current block and the next. As long as a
// block is reachable the locals and stack must be merged. If the
// stack heights don't match then this is a verification error and
// it's impossible to interpret the code. Simultaneously monitor
// states are being check to see if they nest statically. If monitor
// depths match up then their states are merged. Otherwise the
// mismatch is simply recorded and interpretation continues since
// monitor matching is purely informational and doesn't say anything
// about the correctness of the code.
void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) {
guarantee(bb != NULL, "null basicblock");
assert(bb->is_alive(), "merging state into a dead basicblock");
if (_stack_top == bb->_stack_top) {
// always merge local state even if monitors don't match.
if (merge_local_state_vectors(_state, bb->_state)) {
bb->set_changed(true);
}
if (_monitor_top == bb->_monitor_top) {
// monitors still match so continue merging monitor states.
if (merge_monitor_state_vectors(_state, bb->_state)) {
bb->set_changed(true);
}
} else {
if (log_is_enabled(Info, monitormismatch)) {
report_monitor_mismatch("monitor stack height merge conflict");
}
// When the monitor stacks are not matched, we set _monitor_top to
// bad_monitors. This signals that, from here on, the monitor stack cannot
// be trusted. In particular, monitorexit bytecodes may throw
// exceptions. We mark this block as changed so that the change
// propagates properly.
bb->_monitor_top = bad_monitors;
bb->set_changed(true);
_monitor_safe = false;
}
} else if (!bb->is_reachable()) {
// First time we look at this BB
copy_state(bb->_state, _state);
bb->_stack_top = _stack_top;
bb->_monitor_top = _monitor_top;
bb->set_changed(true);
} else {
verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top);
}
}
void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) {
gom->merge_state_into_bb(gom->get_basic_block_at(bci));
}
void GenerateOopMap::set_var(int localNo, CellTypeState cts) {
assert(cts.is_reference() || cts.is_value() || cts.is_address(),
"wrong celltypestate");
if (localNo < 0 || localNo > _max_locals) {
verify_error("variable write error: r%d", localNo);
return;
}
vars()[localNo] = cts;
}
CellTypeState GenerateOopMap::get_var(int localNo) {
assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error");
if (localNo < 0 || localNo > _max_locals) {
verify_error("variable read error: r%d", localNo);
return valCTS; // just to pick something;
}
return vars()[localNo];
}
CellTypeState GenerateOopMap::pop() {
if ( _stack_top <= 0) {
verify_error("stack underflow");
return valCTS; // just to pick something
}
return stack()[--_stack_top];
}
void GenerateOopMap::push(CellTypeState cts) {
if ( _stack_top >= _max_stack) {
verify_error("stack overflow");
return;
}
stack()[_stack_top++] = cts;
}
CellTypeState GenerateOopMap::monitor_pop() {
assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack");
if (_monitor_top == 0) {
// We have detected a pop of an empty monitor stack.
_monitor_safe = false;
_monitor_top = bad_monitors;
if (log_is_enabled(Info, monitormismatch)) {
report_monitor_mismatch("monitor stack underflow");
}
return CellTypeState::ref; // just to keep the analysis going.
}
return monitors()[--_monitor_top];
}
void GenerateOopMap::monitor_push(CellTypeState cts) {
assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack");
if (_monitor_top >= _max_monitors) {
// Some monitorenter is being executed more than once.
// This means that the monitor stack cannot be simulated.
_monitor_safe = false;
_monitor_top = bad_monitors;
if (log_is_enabled(Info, monitormismatch)) {
report_monitor_mismatch("monitor stack overflow");
}
return;
}
monitors()[_monitor_top++] = cts;
}
//
// Interpretation handling methods
//
void GenerateOopMap::do_interpretation()
{
// "i" is just for debugging, so we can detect cases where this loop is
// iterated more than once.
int i = 0;
do {
#ifndef PRODUCT
if (TraceNewOopMapGeneration) {
tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i);
method()->print_name(tty);
tty->print("\n\n");
}
#endif
_conflict = false;
_monitor_safe = true;
// init_state is now called from init_basic_blocks. The length of a
// state vector cannot be determined until we have made a pass through
// the bytecodes counting the possible monitor entries.
if (!_got_error) init_basic_blocks();
if (!_got_error) setup_method_entry_state();
if (!_got_error) interp_all();
if (!_got_error) rewrite_refval_conflicts();
i++;
} while (_conflict && !_got_error);
}
void GenerateOopMap::init_basic_blocks() {
// Note: Could consider reserving only the needed space for each BB's state
// (entry stack may not be of maximal height for every basic block).
// But cumbersome since we don't know the stack heights yet. (Nor the
// monitor stack heights...)
ALLOC_RESOURCE_ARRAY(_basic_blocks, BasicBlock, _bb_count);
// Make a pass through the bytecodes. Count the number of monitorenters.
// This can be used an upper bound on the monitor stack depth in programs
// which obey stack discipline with their monitor usage. Initialize the
// known information about basic blocks.
BytecodeStream j(_method);
Bytecodes::Code bytecode;
int bbNo = 0;
int monitor_count = 0;
int prev_bci = -1;
while( (bytecode = j.next()) >= 0) {
if (j.code() == Bytecodes::_monitorenter) {
monitor_count++;
}
int bci = j.bci();
if (is_bb_header(bci)) {
// Initialize the basicblock structure
BasicBlock *bb = _basic_blocks + bbNo;
bb->_bci = bci;
bb->_max_locals = _max_locals;
bb->_max_stack = _max_stack;
bb->set_changed(false);
bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead.
bb->_monitor_top = bad_monitors;
if (bbNo > 0) {
_basic_blocks[bbNo - 1]._end_bci = prev_bci;
}
bbNo++;
}
// Remember prevous bci.
prev_bci = bci;
}
// Set
_basic_blocks[bbNo-1]._end_bci = prev_bci;
// Check that the correct number of basicblocks was found
if (bbNo !=_bb_count) {
if (bbNo < _bb_count) {
verify_error("jump into the middle of instruction?");
return;
} else {
verify_error("extra basic blocks - should not happen?");
return;
}
}
_max_monitors = monitor_count;
// Now that we have a bound on the depth of the monitor stack, we can
// initialize the CellTypeState-related information.
init_state();
// We allocate space for all state-vectors for all basicblocks in one huge
// chunk. Then in the next part of the code, we set a pointer in each