-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdeopt_instructions.cc
1390 lines (1193 loc) · 46.9 KB
/
deopt_instructions.cc
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) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#if !defined(DART_PRECOMPILED_RUNTIME)
#include "vm/deopt_instructions.h"
#include "vm/code_patcher.h"
#include "vm/compiler/assembler/assembler.h"
#include "vm/compiler/assembler/disassembler.h"
#include "vm/compiler/backend/il.h"
#include "vm/compiler/backend/locations.h"
#include "vm/compiler/jit/compiler.h"
#include "vm/parser.h"
#include "vm/stack_frame.h"
#include "vm/thread.h"
#include "vm/timeline.h"
namespace dart {
DEFINE_FLAG(bool,
compress_deopt_info,
true,
"Compress the size of the deoptimization info for optimized code.");
DECLARE_FLAG(bool, trace_deoptimization);
DECLARE_FLAG(bool, trace_deoptimization_verbose);
DeoptContext::DeoptContext(const StackFrame* frame,
const Code& code,
DestFrameOptions dest_options,
fpu_register_t* fpu_registers,
intptr_t* cpu_registers,
bool is_lazy_deopt,
bool deoptimizing_code)
: code_(code.ptr()),
object_pool_(code.GetObjectPool()),
deopt_info_(TypedData::null()),
dest_frame_is_allocated_(false),
dest_frame_(nullptr),
dest_frame_size_(0),
source_frame_is_allocated_(false),
source_frame_(nullptr),
source_frame_size_(0),
cpu_registers_(cpu_registers),
fpu_registers_(fpu_registers),
num_args_(0),
deopt_reason_(ICData::kDeoptUnknown),
deopt_flags_(0),
thread_(Thread::Current()),
deopt_start_micros_(0),
deferred_slots_(nullptr),
deferred_objects_count_(0),
deferred_objects_(nullptr),
is_lazy_deopt_(is_lazy_deopt),
deoptimizing_code_(deoptimizing_code) {
const TypedData& deopt_info = TypedData::Handle(
code.GetDeoptInfoAtPc(frame->pc(), &deopt_reason_, &deopt_flags_));
#if defined(DEBUG)
if (deopt_info.IsNull()) {
OS::PrintErr("Missing deopt info for pc %" Px "\n", frame->pc());
DisassembleToStdout formatter;
code.Disassemble(&formatter);
}
#endif
ASSERT(!deopt_info.IsNull());
deopt_info_ = deopt_info.ptr();
const Function& function = Function::Handle(code.function());
// Do not include incoming arguments if there are optional arguments
// (they are copied into local space at method entry).
num_args_ =
function.MakesCopyOfParameters() ? 0 : function.num_fixed_parameters();
// The fixed size section of the (fake) Dart frame called via a stub by the
// optimized function contains FP, PP (ARM only), PC-marker and
// return-address. This section is copied as well, so that its contained
// values can be updated before returning to the deoptimized function.
ASSERT(frame->fp() >= frame->sp());
const intptr_t frame_size = (frame->fp() - frame->sp()) / kWordSize;
source_frame_size_ = +kDartFrameFixedSize // For saved values below sp.
+ frame_size // For frame size incl. sp.
+ 1 // For fp.
+ kParamEndSlotFromFp // For saved values above fp.
+ num_args_; // For arguments.
source_frame_ = FrameBase(frame);
if (dest_options == kDestIsOriginalFrame) {
// Work from a copy of the source frame.
intptr_t* original_frame = source_frame_;
source_frame_ = new intptr_t[source_frame_size_];
ASSERT(source_frame_ != nullptr);
for (intptr_t i = 0; i < source_frame_size_; i++) {
source_frame_[i] = original_frame[i];
}
source_frame_is_allocated_ = true;
}
caller_fp_ = GetSourceFp();
dest_frame_size_ = DeoptInfo::FrameSize(deopt_info);
if (dest_options == kDestIsAllocated) {
dest_frame_ = new intptr_t[dest_frame_size_];
ASSERT(source_frame_ != nullptr);
for (intptr_t i = 0; i < dest_frame_size_; i++) {
dest_frame_[i] = 0;
}
dest_frame_is_allocated_ = true;
}
if (dest_options != kDestIsAllocated) {
// kDestIsAllocated is used by the debugger to generate a stack trace
// and does not signal a real deopt.
deopt_start_micros_ = OS::GetCurrentMonotonicMicros();
}
if (FLAG_trace_deoptimization || FLAG_trace_deoptimization_verbose) {
THR_Print(
"Deoptimizing (reason %d '%s') at "
"pc=%" Pp " fp=%" Pp " '%s' (count %d)\n",
deopt_reason(), DeoptReasonToCString(deopt_reason()), frame->pc(),
frame->fp(), function.ToFullyQualifiedCString(),
function.deoptimization_counter());
}
}
DeoptContext::~DeoptContext() {
// Delete memory for source frame and registers.
if (source_frame_is_allocated_) {
delete[] source_frame_;
}
source_frame_ = nullptr;
delete[] fpu_registers_;
delete[] cpu_registers_;
fpu_registers_ = nullptr;
cpu_registers_ = nullptr;
if (dest_frame_is_allocated_) {
delete[] dest_frame_;
}
dest_frame_ = nullptr;
// Delete all deferred objects.
for (intptr_t i = 0; i < deferred_objects_count_; i++) {
delete deferred_objects_[i];
}
delete[] deferred_objects_;
deferred_objects_ = nullptr;
deferred_objects_count_ = 0;
#if defined(SUPPORT_TIMELINE)
if (deopt_start_micros_ != 0) {
TimelineStream* compiler_stream = Timeline::GetCompilerStream();
ASSERT(compiler_stream != nullptr);
if (compiler_stream->enabled()) {
// Allocate all Dart objects needed before calling StartEvent,
// which blocks safe points until Complete is called.
const Code& code = Code::Handle(zone(), code_);
const Function& function = Function::Handle(zone(), code.function());
const String& function_name =
String::Handle(zone(), function.QualifiedScrubbedName());
const char* reason = DeoptReasonToCString(deopt_reason());
const int counter = function.deoptimization_counter();
TimelineEvent* timeline_event = compiler_stream->StartEvent();
if (timeline_event != nullptr) {
timeline_event->Duration("Deoptimize", deopt_start_micros_,
OS::GetCurrentMonotonicMicros());
timeline_event->SetNumArguments(3);
timeline_event->CopyArgument(0, "function", function_name.ToCString());
timeline_event->CopyArgument(1, "reason", reason);
timeline_event->FormatArgument(2, "deoptimizationCount", "%d", counter);
timeline_event->Complete();
}
}
}
#endif // !PRODUCT
}
void DeoptContext::VisitObjectPointers(ObjectPointerVisitor* visitor) {
visitor->VisitPointer(reinterpret_cast<ObjectPtr*>(&code_));
visitor->VisitPointer(reinterpret_cast<ObjectPtr*>(&object_pool_));
visitor->VisitPointer(reinterpret_cast<ObjectPtr*>(&deopt_info_));
// Visit any object pointers on the destination stack.
if (dest_frame_is_allocated_) {
for (intptr_t i = 0; i < dest_frame_size_; i++) {
if (dest_frame_[i] != 0) {
visitor->VisitPointer(reinterpret_cast<ObjectPtr*>(&dest_frame_[i]));
}
}
}
}
intptr_t DeoptContext::DestStackAdjustment() const {
return dest_frame_size_ - kDartFrameFixedSize - num_args_ - 1 // For fp.
- kParamEndSlotFromFp;
}
intptr_t DeoptContext::GetSourceFp() const {
return source_frame_[source_frame_size_ - 1 - num_args_ -
kParamEndSlotFromFp + kSavedCallerFpSlotFromFp];
}
intptr_t DeoptContext::GetSourcePp() const {
return source_frame_[source_frame_size_ - 1 - num_args_ -
kParamEndSlotFromFp +
StackFrame::SavedCallerPpSlotFromFp()];
}
intptr_t DeoptContext::GetSourcePc() const {
return source_frame_[source_frame_size_ - num_args_ + kSavedPcSlotFromSp];
}
intptr_t DeoptContext::GetCallerFp() const {
return caller_fp_;
}
void DeoptContext::SetCallerFp(intptr_t caller_fp) {
caller_fp_ = caller_fp;
}
static bool IsObjectInstruction(DeoptInstr::Kind kind) {
switch (kind) {
case DeoptInstr::kConstant:
case DeoptInstr::kPp:
case DeoptInstr::kCallerPp:
case DeoptInstr::kMaterializedObjectRef:
case DeoptInstr::kFloat32x4:
case DeoptInstr::kInt32x4:
case DeoptInstr::kFloat64x2:
case DeoptInstr::kWord:
case DeoptInstr::kFloat:
case DeoptInstr::kDouble:
case DeoptInstr::kMint:
case DeoptInstr::kMintPair:
case DeoptInstr::kInt32:
case DeoptInstr::kUint32:
return true;
case DeoptInstr::kRetAddress:
case DeoptInstr::kPcMarker:
case DeoptInstr::kCallerFp:
case DeoptInstr::kCallerPc:
return false;
case DeoptInstr::kMaterializeObject:
default:
// We should not encounter these instructions when filling stack slots.
UNREACHABLE();
return false;
}
UNREACHABLE();
return false;
}
void DeoptContext::FillDestFrame() {
const Code& code = Code::Handle(code_);
const TypedData& deopt_info = TypedData::Handle(deopt_info_);
GrowableArray<DeoptInstr*> deopt_instructions;
const Array& deopt_table = Array::Handle(code.deopt_info_array());
ASSERT(!deopt_table.IsNull());
DeoptInfo::Unpack(deopt_table, deopt_info, &deopt_instructions);
const intptr_t len = deopt_instructions.length();
const intptr_t frame_size = dest_frame_size_;
// For now, we never place non-objects in the deoptimized frame if
// the destination frame is a copy. This allows us to copy the
// deoptimized frame into an Array.
const bool objects_only = dest_frame_is_allocated_;
// All kMaterializeObject instructions are emitted before the instructions
// that describe stack frames. Skip them and defer materialization of
// objects until the frame is fully reconstructed and it is safe to perform
// GC.
// Arguments (class of the instance to allocate and field-value pairs) are
// described as part of the expression stack for the bottom-most deoptimized
// frame. They will be used during materialization and removed from the stack
// right before control switches to the unoptimized code.
const intptr_t num_materializations =
DeoptInfo::NumMaterializations(deopt_instructions);
PrepareForDeferredMaterialization(num_materializations);
for (intptr_t from_index = 0, to_index = kDartFrameFixedSize;
from_index < num_materializations; from_index++) {
const intptr_t field_count =
DeoptInstr::GetFieldCount(deopt_instructions[from_index]);
intptr_t* args = GetDestFrameAddressAt(to_index);
DeferredObject* obj = new DeferredObject(field_count, args);
SetDeferredObjectAt(from_index, obj);
to_index += obj->ArgumentCount();
}
// Populate stack frames.
for (intptr_t to_index = frame_size - 1, from_index = len - 1; to_index >= 0;
to_index--, from_index--) {
intptr_t* to_addr = GetDestFrameAddressAt(to_index);
DeoptInstr* instr = deopt_instructions[from_index];
if (!objects_only || IsObjectInstruction(instr->kind())) {
instr->Execute(this, to_addr);
} else {
*reinterpret_cast<ObjectPtr*>(to_addr) = Object::null();
}
}
if (FLAG_trace_deoptimization_verbose) {
for (intptr_t i = 0; i < frame_size; i++) {
intptr_t* to_addr = GetDestFrameAddressAt(i);
THR_Print("*%" Pd ". [%p] 0x%" Px " [%s]\n", i, to_addr, *to_addr,
deopt_instructions[i + (len - frame_size)]->ToCString());
}
}
}
static void FillDeferredSlots(DeoptContext* deopt_context,
DeferredSlot** slot_list) {
DeferredSlot* slot = *slot_list;
*slot_list = nullptr;
while (slot != nullptr) {
DeferredSlot* current = slot;
slot = slot->next();
current->Materialize(deopt_context);
delete current;
}
}
// Materializes all deferred objects. Returns the total number of
// artificial arguments used during deoptimization.
intptr_t DeoptContext::MaterializeDeferredObjects() {
// Populate slots with references to all unboxed "primitive" values (doubles,
// mints, simd) and deferred objects. Deferred objects are only allocated
// but not filled with data. This is done later because deferred objects
// can references each other.
FillDeferredSlots(this, &deferred_slots_);
// Compute total number of artificial arguments used during deoptimization.
intptr_t deopt_arg_count = 0;
for (intptr_t i = 0; i < DeferredObjectsCount(); i++) {
GetDeferredObject(i)->Fill();
deopt_arg_count += GetDeferredObject(i)->ArgumentCount();
}
// Since this is the only step where GC can occur during deoptimization,
// use it to report the source line where deoptimization occurred.
if (FLAG_trace_deoptimization || FLAG_trace_deoptimization_verbose) {
DartFrameIterator iterator(Thread::Current(),
StackFrameIterator::kNoCrossThreadIteration);
StackFrame* top_frame = iterator.NextFrame();
ASSERT(top_frame != nullptr);
const Code& code = Code::Handle(top_frame->LookupDartCode());
const Function& top_function = Function::Handle(code.function());
const Script& script = Script::Handle(top_function.script());
const TokenPosition token_pos = code.GetTokenIndexOfPC(top_frame->pc());
THR_Print(" Function: %s\n", top_function.ToFullyQualifiedCString());
intptr_t line;
if (script.GetTokenLocation(token_pos, &line)) {
String& line_string = String::Handle(script.GetLine(line));
char line_buffer[80];
Utils::SNPrint(line_buffer, sizeof(line_buffer), " Line %" Pd ": '%s'",
line, line_string.ToCString());
THR_Print("%s\n", line_buffer);
}
THR_Print(" Deopt args: %" Pd "\n", deopt_arg_count);
}
return deopt_arg_count;
}
ArrayPtr DeoptContext::DestFrameAsArray() {
ASSERT(dest_frame_ != nullptr && dest_frame_is_allocated_);
const Array& dest_array = Array::Handle(zone(), Array::New(dest_frame_size_));
PassiveObject& obj = PassiveObject::Handle(zone());
for (intptr_t i = 0; i < dest_frame_size_; i++) {
obj = static_cast<ObjectPtr>(dest_frame_[i]);
dest_array.SetAt(i, obj);
}
return dest_array.ptr();
}
// Deoptimization instruction creating return address using function and
// deopt-id stored at 'object_table_index'.
class DeoptRetAddressInstr : public DeoptInstr {
public:
DeoptRetAddressInstr(intptr_t object_table_index, intptr_t deopt_id)
: object_table_index_(object_table_index), deopt_id_(deopt_id) {
ASSERT(object_table_index >= 0);
ASSERT(deopt_id >= 0);
}
explicit DeoptRetAddressInstr(intptr_t source_index)
: object_table_index_(ObjectTableIndex::decode(source_index)),
deopt_id_(DeoptId::decode(source_index)) {}
virtual intptr_t source_index() const {
return ObjectTableIndex::encode(object_table_index_) |
DeoptId::encode(deopt_id_);
}
virtual DeoptInstr::Kind kind() const { return kRetAddress; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString(
"%" Pd ", %" Pd "", object_table_index_, deopt_id_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = Smi::RawValue(0);
deopt_context->DeferRetAddrMaterialization(object_table_index_, deopt_id_,
dest_addr);
}
intptr_t object_table_index() const { return object_table_index_; }
intptr_t deopt_id() const { return deopt_id_; }
private:
static constexpr intptr_t kFieldWidth = kBitsPerWord / 2;
using ObjectTableIndex = BitField<intptr_t, intptr_t, 0, kFieldWidth>;
using DeoptId =
BitField<intptr_t, intptr_t, ObjectTableIndex::kNextBit, kFieldWidth>;
const intptr_t object_table_index_;
const intptr_t deopt_id_;
DISALLOW_COPY_AND_ASSIGN(DeoptRetAddressInstr);
};
// Deoptimization instruction moving a constant stored at 'object_table_index'.
class DeoptConstantInstr : public DeoptInstr {
public:
explicit DeoptConstantInstr(intptr_t object_table_index)
: object_table_index_(object_table_index) {
ASSERT(object_table_index >= 0);
}
virtual intptr_t source_index() const { return object_table_index_; }
virtual DeoptInstr::Kind kind() const { return kConstant; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("%" Pd "",
object_table_index_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
const PassiveObject& obj = PassiveObject::Handle(
deopt_context->zone(), deopt_context->ObjectAt(object_table_index_));
*reinterpret_cast<ObjectPtr*>(dest_addr) = obj.ptr();
}
private:
const intptr_t object_table_index_;
DISALLOW_COPY_AND_ASSIGN(DeoptConstantInstr);
};
// Deoptimization instruction moving value from optimized frame at
// 'source_index' to specified slots in the unoptimized frame.
// 'source_index' represents the slot index of the frame (0 being
// first argument) and accounts for saved return address, frame
// pointer, pool pointer and pc marker.
// Deoptimization instruction moving a CPU register.
class DeoptWordInstr : public DeoptInstr {
public:
explicit DeoptWordInstr(intptr_t source_index) : source_(source_index) {}
explicit DeoptWordInstr(const CpuRegisterSource& source) : source_(source) {}
virtual intptr_t source_index() const { return source_.source_index(); }
virtual DeoptInstr::Kind kind() const { return kWord; }
virtual const char* ArgumentsToCString() const { return source_.ToCString(); }
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = source_.Value<intptr_t>(deopt_context);
}
private:
const CpuRegisterSource source_;
DISALLOW_COPY_AND_ASSIGN(DeoptWordInstr);
};
class DeoptIntegerInstrBase : public DeoptInstr {
public:
DeoptIntegerInstrBase() {}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
const int64_t value = GetValue(deopt_context);
if (Smi::IsValid(value)) {
*dest_addr = Smi::RawValue(static_cast<intptr_t>(value));
} else {
*dest_addr = Smi::RawValue(0);
deopt_context->DeferMintMaterialization(
value, reinterpret_cast<MintPtr*>(dest_addr));
}
}
virtual int64_t GetValue(DeoptContext* deopt_context) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(DeoptIntegerInstrBase);
};
class DeoptMintPairInstr : public DeoptIntegerInstrBase {
public:
explicit DeoptMintPairInstr(intptr_t source_index)
: DeoptIntegerInstrBase(),
lo_(LoRegister::decode(source_index)),
hi_(HiRegister::decode(source_index)) {}
DeoptMintPairInstr(const CpuRegisterSource& lo, const CpuRegisterSource& hi)
: DeoptIntegerInstrBase(), lo_(lo), hi_(hi) {}
virtual intptr_t source_index() const {
return LoRegister::encode(lo_.source_index()) |
HiRegister::encode(hi_.source_index());
}
virtual DeoptInstr::Kind kind() const { return kMintPair; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("%s,%s", lo_.ToCString(),
hi_.ToCString());
}
virtual int64_t GetValue(DeoptContext* deopt_context) {
return Utils::LowHighTo64Bits(lo_.Value<uint32_t>(deopt_context),
hi_.Value<int32_t>(deopt_context));
}
private:
static constexpr intptr_t kFieldWidth = kBitsPerWord / 2;
using LoRegister = BitField<intptr_t, intptr_t, 0, kFieldWidth>;
using HiRegister =
BitField<intptr_t, intptr_t, LoRegister::kNextBit, kFieldWidth>;
const CpuRegisterSource lo_;
const CpuRegisterSource hi_;
DISALLOW_COPY_AND_ASSIGN(DeoptMintPairInstr);
};
template <DeoptInstr::Kind K, CatchEntryMove::SourceKind slot_kind, typename T>
class DeoptIntInstr : public DeoptIntegerInstrBase {
public:
explicit DeoptIntInstr(intptr_t source_index)
: DeoptIntegerInstrBase(), source_(source_index) {}
explicit DeoptIntInstr(const CpuRegisterSource& source)
: DeoptIntegerInstrBase(), source_(source) {}
virtual intptr_t source_index() const { return source_.source_index(); }
virtual DeoptInstr::Kind kind() const { return K; }
virtual const char* ArgumentsToCString() const { return source_.ToCString(); }
virtual int64_t GetValue(DeoptContext* deopt_context) {
return static_cast<int64_t>(source_.Value<T>(deopt_context));
}
private:
const CpuRegisterSource source_;
DISALLOW_COPY_AND_ASSIGN(DeoptIntInstr);
};
typedef DeoptIntInstr<DeoptInstr::kUint32,
CatchEntryMove::SourceKind::kUint32Slot,
uint32_t>
DeoptUint32Instr;
typedef DeoptIntInstr<DeoptInstr::kInt32,
CatchEntryMove::SourceKind::kInt32Slot,
int32_t>
DeoptInt32Instr;
typedef DeoptIntInstr<DeoptInstr::kMint,
CatchEntryMove::SourceKind::kInt64Slot,
int64_t>
DeoptMintInstr;
template <DeoptInstr::Kind K,
CatchEntryMove::SourceKind slot_kind,
typename Type,
typename PtrType>
class DeoptFpuInstr : public DeoptInstr {
public:
explicit DeoptFpuInstr(intptr_t source_index) : source_(source_index) {}
explicit DeoptFpuInstr(const FpuRegisterSource& source) : source_(source) {}
virtual intptr_t source_index() const { return source_.source_index(); }
virtual DeoptInstr::Kind kind() const { return K; }
virtual const char* ArgumentsToCString() const { return source_.ToCString(); }
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = Smi::RawValue(0);
deopt_context->DeferMaterialization(source_.Value<Type>(deopt_context),
reinterpret_cast<PtrType*>(dest_addr));
}
private:
const FpuRegisterSource source_;
DISALLOW_COPY_AND_ASSIGN(DeoptFpuInstr);
};
typedef DeoptFpuInstr<DeoptInstr::kFloat,
CatchEntryMove::SourceKind::kFloatSlot,
float,
DoublePtr>
DeoptFloatInstr;
typedef DeoptFpuInstr<DeoptInstr::kDouble,
CatchEntryMove::SourceKind::kDoubleSlot,
double,
DoublePtr>
DeoptDoubleInstr;
// Simd128 types.
typedef DeoptFpuInstr<DeoptInstr::kFloat32x4,
CatchEntryMove::SourceKind::kFloat32x4Slot,
simd128_value_t,
Float32x4Ptr>
DeoptFloat32x4Instr;
typedef DeoptFpuInstr<DeoptInstr::kFloat64x2,
CatchEntryMove::SourceKind::kFloat64x2Slot,
simd128_value_t,
Float64x2Ptr>
DeoptFloat64x2Instr;
typedef DeoptFpuInstr<DeoptInstr::kInt32x4,
CatchEntryMove::SourceKind::kInt32x4Slot,
simd128_value_t,
Int32x4Ptr>
DeoptInt32x4Instr;
// Deoptimization instruction creating a PC marker for the code of
// function at 'object_table_index'.
class DeoptPcMarkerInstr : public DeoptInstr {
public:
explicit DeoptPcMarkerInstr(intptr_t object_table_index)
: object_table_index_(object_table_index) {
ASSERT(object_table_index >= 0);
}
virtual intptr_t source_index() const { return object_table_index_; }
virtual DeoptInstr::Kind kind() const { return kPcMarker; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("%" Pd "",
object_table_index_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
Function& function = Function::Handle(deopt_context->zone());
function ^= deopt_context->ObjectAt(object_table_index_);
if (function.IsNull()) {
*reinterpret_cast<ObjectPtr*>(dest_addr) =
deopt_context->is_lazy_deopt()
? StubCode::DeoptimizeLazyFromReturn().ptr()
: StubCode::Deoptimize().ptr();
return;
}
// We don't always have the Code object for the frame's corresponding
// unoptimized code as it may have been collected. Use a stub as the pc
// marker until we can recreate that Code object during deferred
// materialization to maintain the invariant that Dart frames always have
// a pc marker.
*reinterpret_cast<ObjectPtr*>(dest_addr) =
StubCode::FrameAwaitingMaterialization().ptr();
deopt_context->DeferPcMarkerMaterialization(object_table_index_, dest_addr);
}
private:
intptr_t object_table_index_;
DISALLOW_COPY_AND_ASSIGN(DeoptPcMarkerInstr);
};
// Deoptimization instruction creating a pool pointer for the code of
// function at 'object_table_index'.
class DeoptPpInstr : public DeoptInstr {
public:
explicit DeoptPpInstr(intptr_t object_table_index)
: object_table_index_(object_table_index) {
ASSERT(object_table_index >= 0);
}
virtual intptr_t source_index() const { return object_table_index_; }
virtual DeoptInstr::Kind kind() const { return kPp; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("%" Pd "",
object_table_index_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = Smi::RawValue(0);
deopt_context->DeferPpMaterialization(
object_table_index_, reinterpret_cast<ObjectPtr*>(dest_addr));
}
private:
intptr_t object_table_index_;
DISALLOW_COPY_AND_ASSIGN(DeoptPpInstr);
};
// Deoptimization instruction copying the caller saved FP from optimized frame.
class DeoptCallerFpInstr : public DeoptInstr {
public:
DeoptCallerFpInstr() {}
virtual intptr_t source_index() const { return 0; }
virtual DeoptInstr::Kind kind() const { return kCallerFp; }
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = deopt_context->GetCallerFp();
deopt_context->SetCallerFp(
reinterpret_cast<intptr_t>(dest_addr - kSavedCallerFpSlotFromFp));
}
private:
DISALLOW_COPY_AND_ASSIGN(DeoptCallerFpInstr);
};
// Deoptimization instruction copying the caller saved PP from optimized frame.
class DeoptCallerPpInstr : public DeoptInstr {
public:
DeoptCallerPpInstr() {}
virtual intptr_t source_index() const { return 0; }
virtual DeoptInstr::Kind kind() const { return kCallerPp; }
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = deopt_context->GetSourcePp();
}
private:
DISALLOW_COPY_AND_ASSIGN(DeoptCallerPpInstr);
};
// Deoptimization instruction copying the caller return address from optimized
// frame.
class DeoptCallerPcInstr : public DeoptInstr {
public:
DeoptCallerPcInstr() {}
virtual intptr_t source_index() const { return 0; }
virtual DeoptInstr::Kind kind() const { return kCallerPc; }
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*dest_addr = deopt_context->GetSourcePc();
}
private:
DISALLOW_COPY_AND_ASSIGN(DeoptCallerPcInstr);
};
// Write reference to a materialized object with the given index into the
// stack slot.
class DeoptMaterializedObjectRefInstr : public DeoptInstr {
public:
explicit DeoptMaterializedObjectRefInstr(intptr_t index) : index_(index) {
ASSERT(index >= 0);
}
virtual intptr_t source_index() const { return index_; }
virtual DeoptInstr::Kind kind() const { return kMaterializedObjectRef; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("#%" Pd "", index_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
*reinterpret_cast<SmiPtr*>(dest_addr) = Smi::New(0);
deopt_context->DeferMaterializedObjectRef(index_, dest_addr);
}
private:
intptr_t index_;
DISALLOW_COPY_AND_ASSIGN(DeoptMaterializedObjectRefInstr);
};
// Materialize object with the given number of fields.
// Arguments for materialization (class and field-value pairs) are pushed
// to the expression stack of the bottom-most frame.
class DeoptMaterializeObjectInstr : public DeoptInstr {
public:
explicit DeoptMaterializeObjectInstr(intptr_t field_count)
: field_count_(field_count) {
ASSERT(field_count >= 0);
}
virtual intptr_t source_index() const { return field_count_; }
virtual DeoptInstr::Kind kind() const { return kMaterializeObject; }
virtual const char* ArgumentsToCString() const {
return Thread::Current()->zone()->PrintToString("%" Pd "", field_count_);
}
void Execute(DeoptContext* deopt_context, intptr_t* dest_addr) {
// This instructions are executed manually by the DeoptimizeWithDeoptInfo.
UNREACHABLE();
}
private:
intptr_t field_count_;
DISALLOW_COPY_AND_ASSIGN(DeoptMaterializeObjectInstr);
};
uword DeoptInstr::GetRetAddress(DeoptInstr* instr,
const ObjectPool& object_table,
Code* code) {
ASSERT(instr->kind() == kRetAddress);
DeoptRetAddressInstr* ret_address_instr =
static_cast<DeoptRetAddressInstr*>(instr);
ASSERT(!object_table.IsNull());
Thread* thread = Thread::Current();
Zone* zone = thread->zone();
Function& function = Function::Handle(zone);
function ^= object_table.ObjectAt(ret_address_instr->object_table_index());
ASSERT(!function.ForceOptimize());
ASSERT(code != nullptr);
const Error& error =
Error::Handle(zone, Compiler::EnsureUnoptimizedCode(thread, function));
if (!error.IsNull()) {
Exceptions::PropagateError(error);
}
*code = function.unoptimized_code();
ASSERT(!code->IsNull());
uword res = code->GetPcForDeoptId(ret_address_instr->deopt_id(),
UntaggedPcDescriptors::kDeopt);
ASSERT(res != 0);
return res;
}
DeoptInstr* DeoptInstr::Create(intptr_t kind_as_int, intptr_t source_index) {
Kind kind = static_cast<Kind>(kind_as_int);
switch (kind) {
case kWord:
return new DeoptWordInstr(source_index);
case kFloat:
return new DeoptFloatInstr(source_index);
case kDouble:
return new DeoptDoubleInstr(source_index);
case kMint:
return new DeoptMintInstr(source_index);
case kMintPair:
return new DeoptMintPairInstr(source_index);
case kInt32:
return new DeoptInt32Instr(source_index);
case kUint32:
return new DeoptUint32Instr(source_index);
case kFloat32x4:
return new DeoptFloat32x4Instr(source_index);
case kFloat64x2:
return new DeoptFloat64x2Instr(source_index);
case kInt32x4:
return new DeoptInt32x4Instr(source_index);
case kRetAddress:
return new DeoptRetAddressInstr(source_index);
case kConstant:
return new DeoptConstantInstr(source_index);
case kPcMarker:
return new DeoptPcMarkerInstr(source_index);
case kPp:
return new DeoptPpInstr(source_index);
case kCallerFp:
return new DeoptCallerFpInstr();
case kCallerPp:
return new DeoptCallerPpInstr();
case kCallerPc:
return new DeoptCallerPcInstr();
case kMaterializedObjectRef:
return new DeoptMaterializedObjectRefInstr(source_index);
case kMaterializeObject:
return new DeoptMaterializeObjectInstr(source_index);
}
UNREACHABLE();
return nullptr;
}
const char* DeoptInstr::KindToCString(Kind kind) {
switch (kind) {
case kWord:
return "word";
case kFloat:
return "float";
case kDouble:
return "double";
case kMint:
case kMintPair:
return "mint";
case kInt32:
return "int32";
case kUint32:
return "uint32";
case kFloat32x4:
return "float32x4";
case kFloat64x2:
return "float64x2";
case kInt32x4:
return "int32x4";
case kRetAddress:
return "retaddr";
case kConstant:
return "const";
case kPcMarker:
return "pc";
case kPp:
return "pp";
case kCallerFp:
return "callerfp";
case kCallerPp:
return "callerpp";
case kCallerPc:
return "callerpc";
case kMaterializedObjectRef:
return "ref";
case kMaterializeObject:
return "mat";
}
UNREACHABLE();
return nullptr;
}
class DeoptInfoBuilder::TrieNode : public ZoneAllocated {
public:
// Construct the root node representing the implicit "shared" terminator
// at the end of each deopt info.
TrieNode() : instruction_(nullptr), info_number_(-1), children_(16) {}
// Construct a node representing a written instruction.
TrieNode(DeoptInstr* instruction, intptr_t info_number)
: instruction_(instruction), info_number_(info_number), children_(4) {}
intptr_t info_number() const { return info_number_; }
void AddChild(TrieNode* child) {
if (child != nullptr) children_.Add(child);
}
TrieNode* FindChild(const DeoptInstr& instruction) {
for (intptr_t i = 0; i < children_.length(); ++i) {
TrieNode* child = children_[i];
if (child->instruction_->Equals(instruction)) return child;
}
return nullptr;
}
private:
const DeoptInstr* instruction_; // Instruction that was written.
const intptr_t info_number_; // Index of the deopt info it was written to.
GrowableArray<TrieNode*> children_;
};
DeoptInfoBuilder::DeoptInfoBuilder(Zone* zone,
const intptr_t num_args,
compiler::Assembler* assembler)
: zone_(zone),
instructions_(),
num_args_(num_args),
assembler_(assembler),
trie_root_(new(zone) TrieNode()),
current_info_number_(0),
frame_start_(-1),
materializations_() {}
intptr_t DeoptInfoBuilder::FindOrAddObjectInTable(const Object& obj) const {
return assembler_->object_pool_builder().FindObject(obj);
}
intptr_t DeoptInfoBuilder::CalculateStackIndex(
const Location& source_loc) const {
intptr_t index = -compiler::target::frame_layout.VariableIndexForFrameSlot(
source_loc.stack_index());
return index < 0 ? index + num_args_
: index + num_args_ + kDartFrameFixedSize;
}
CpuRegisterSource DeoptInfoBuilder::ToCpuRegisterSource(const Location& loc) {
if (loc.IsRegister()) {
return CpuRegisterSource(CpuRegisterSource::kRegister, loc.reg());
} else {
ASSERT(loc.IsStackSlot());
return CpuRegisterSource(CpuRegisterSource::kStackSlot,
CalculateStackIndex(loc));
}
}
FpuRegisterSource DeoptInfoBuilder::ToFpuRegisterSource(
const Location& loc,
Location::Kind stack_slot_kind) {