/
graph-builder-interface.cc
2158 lines (1906 loc) Β· 84.6 KB
/
graph-builder-interface.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 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/graph-builder-interface.h"
#include "src/compiler/wasm-compiler-definitions.h"
#include "src/compiler/wasm-compiler.h"
#include "src/flags/flags.h"
#include "src/wasm/branch-hint-map.h"
#include "src/wasm/decoder.h"
#include "src/wasm/function-body-decoder-impl.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/value-type.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-linkage.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes-inl.h"
namespace v8 {
namespace internal {
namespace wasm {
namespace {
// Expose {compiler::Node} opaquely as {wasm::TFNode}.
using TFNode = compiler::Node;
// An SsaEnv environment carries the current local variable renaming
// as well as the current effect and control dependency in the TF graph.
// It maintains a control state that tracks whether the environment
// is reachable, has reached a control end, or has been merged.
struct SsaEnv : public ZoneObject {
enum State { kUnreachable, kReached, kMerged };
State state;
TFNode* control;
TFNode* effect;
compiler::WasmInstanceCacheNodes instance_cache;
ZoneVector<TFNode*> locals;
SsaEnv(Zone* zone, State state, TFNode* control, TFNode* effect,
uint32_t locals_size)
: state(state),
control(control),
effect(effect),
locals(locals_size, zone) {}
SsaEnv(const SsaEnv& other) V8_NOEXCEPT = default;
SsaEnv(SsaEnv&& other) V8_NOEXCEPT : state(other.state),
control(other.control),
effect(other.effect),
instance_cache(other.instance_cache),
locals(std::move(other.locals)) {
other.Kill();
}
void Kill() {
state = kUnreachable;
for (TFNode*& local : locals) {
local = nullptr;
}
control = nullptr;
effect = nullptr;
instance_cache = {};
}
void SetNotMerged() {
if (state == kMerged) state = kReached;
}
};
class WasmGraphBuildingInterface {
public:
static constexpr Decoder::ValidateFlag validate = Decoder::kFullValidation;
using FullDecoder = WasmFullDecoder<validate, WasmGraphBuildingInterface>;
using CheckForNull = compiler::WasmGraphBuilder::CheckForNull;
struct Value : public ValueBase<validate> {
TFNode* node = nullptr;
template <typename... Args>
explicit Value(Args&&... args) V8_NOEXCEPT
: ValueBase(std::forward<Args>(args)...) {}
};
using ValueVector = base::SmallVector<Value, 8>;
using NodeVector = base::SmallVector<TFNode*, 8>;
struct TryInfo : public ZoneObject {
SsaEnv* catch_env;
TFNode* exception = nullptr;
bool first_catch = true;
bool might_throw() const { return exception != nullptr; }
MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(TryInfo);
explicit TryInfo(SsaEnv* c) : catch_env(c) {}
};
struct Control : public ControlBase<Value, validate> {
SsaEnv* merge_env = nullptr; // merge environment for the construct.
SsaEnv* false_env = nullptr; // false environment (only for if).
TryInfo* try_info = nullptr; // information about try statements.
int32_t previous_catch = -1; // previous Control with a catch.
BitVector* loop_assignments = nullptr; // locals assigned in this loop.
TFNode* loop_node = nullptr; // loop header of this loop.
MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(Control);
template <typename... Args>
explicit Control(Args&&... args) V8_NOEXCEPT
: ControlBase(std::forward<Args>(args)...) {}
};
WasmGraphBuildingInterface(compiler::WasmGraphBuilder* builder,
int func_index, InlinedStatus inlined_status)
: builder_(builder),
func_index_(func_index),
inlined_status_(inlined_status) {}
void StartFunction(FullDecoder* decoder) {
// Get the branch hints map and type feedback for this function (if
// available).
if (decoder->module_) {
auto branch_hints_it = decoder->module_->branch_hints.find(func_index_);
if (branch_hints_it != decoder->module_->branch_hints.end()) {
branch_hints_ = &branch_hints_it->second;
}
TypeFeedbackStorage& feedbacks = decoder->module_->type_feedback;
base::MutexGuard mutex_guard(&feedbacks.mutex);
auto feedback = feedbacks.feedback_for_function.find(func_index_);
if (feedback != feedbacks.feedback_for_function.end()) {
// This creates a copy of the vector, which is cheaper than holding on
// to the mutex throughout graph building.
type_feedback_ = feedback->second.feedback_vector;
// Preallocate space for storing call counts to save Zone memory.
int total_calls = 0;
for (size_t i = 0; i < type_feedback_.size(); i++) {
total_calls += type_feedback_[i].num_cases();
}
builder_->ReserveCallCounts(static_cast<size_t>(total_calls));
// We need to keep the feedback in the module to inline later. However,
// this means we are stuck with it forever.
// TODO(jkummerow): Reconsider our options here.
}
}
// The first '+ 1' is needed by TF Start node, the second '+ 1' is for the
// instance parameter.
builder_->Start(static_cast<int>(decoder->sig_->parameter_count() + 1 + 1));
uint32_t num_locals = decoder->num_locals();
SsaEnv* ssa_env = decoder->zone()->New<SsaEnv>(
decoder->zone(), SsaEnv::kReached, effect(), control(), num_locals);
SetEnv(ssa_env);
// Initialize local variables. Parameters are shifted by 1 because of the
// the instance parameter.
uint32_t index = 0;
for (; index < decoder->sig_->parameter_count(); ++index) {
ssa_env->locals[index] = builder_->SetType(
builder_->Param(index + 1), decoder->sig_->GetParam(index));
}
while (index < num_locals) {
ValueType type = decoder->local_type(index);
TFNode* node;
if (!type.is_defaultable()) {
DCHECK(type.is_reference());
// TODO(jkummerow): Consider using "the hole" instead, to make any
// illegal uses more obvious.
node = builder_->SetType(builder_->RefNull(), type);
} else {
node = builder_->SetType(builder_->DefaultValue(type), type);
}
while (index < num_locals && decoder->local_type(index) == type) {
// Do a whole run of like-typed locals at a time.
ssa_env->locals[index++] = node;
}
}
LoadContextIntoSsa(ssa_env, decoder);
if (v8_flags.trace_wasm && inlined_status_ == kRegularFunction) {
builder_->TraceFunctionEntry(decoder->position());
}
}
// Reload the instance cache entries into the Ssa Environment.
void LoadContextIntoSsa(SsaEnv* ssa_env, FullDecoder* decoder) {
if (ssa_env != nullptr) {
builder_->InitInstanceCache(&ssa_env->instance_cache);
TFNode* mem_size = ssa_env->instance_cache.mem_size;
if (mem_size != nullptr) {
bool is_memory64 =
decoder->module_ != nullptr && decoder->module_->is_memory64;
builder_->SetType(mem_size, is_memory64 ? kWasmI64 : kWasmI32);
}
}
}
void StartFunctionBody(FullDecoder* decoder, Control* block) {}
void FinishFunction(FullDecoder*) {
if (v8_flags.wasm_speculative_inlining) {
DCHECK_EQ(feedback_instruction_index_, type_feedback_.size());
}
if (inlined_status_ == kRegularFunction) {
builder_->PatchInStackCheckIfNeeded();
}
}
void OnFirstError(FullDecoder*) {}
void NextInstruction(FullDecoder*, WasmOpcode) {}
void Block(FullDecoder* decoder, Control* block) {
// The branch environment is the outer environment.
block->merge_env = ssa_env_;
SetEnv(Steal(decoder->zone(), ssa_env_));
}
void Loop(FullDecoder* decoder, Control* block) {
// This is the merge environment at the beginning of the loop.
SsaEnv* merge_env = Steal(decoder->zone(), ssa_env_);
block->merge_env = merge_env;
SetEnv(merge_env);
ssa_env_->state = SsaEnv::kMerged;
TFNode* loop_node = builder_->Loop(control());
if (emit_loop_exits()) {
uint32_t nesting_depth = 0;
for (uint32_t depth = 1; depth < decoder->control_depth(); depth++) {
if (decoder->control_at(depth)->is_loop()) {
nesting_depth++;
}
}
// If this loop is nested, the parent loop's can_be_innermost field needs
// to be false. If the last loop in loop_infos_ has less depth, it has to
// be the parent loop. If it does not, it means another loop has been
// found within the parent loop, and that loop will have set the parent's
// can_be_innermost to false, so we do not need to do anything.
if (nesting_depth > 0 &&
loop_infos_.back().nesting_depth < nesting_depth) {
loop_infos_.back().can_be_innermost = false;
}
loop_infos_.emplace_back(loop_node, nesting_depth, true);
}
builder_->SetControl(loop_node);
decoder->control_at(0)->loop_node = loop_node;
TFNode* effect_inputs[] = {effect(), control()};
builder_->SetEffect(builder_->EffectPhi(1, effect_inputs));
builder_->TerminateLoop(effect(), control());
// Doing a preprocessing pass to analyze loop assignments seems to pay off
// compared to reallocating Nodes when rearranging Phis in Goto.
BitVector* assigned = WasmDecoder<validate>::AnalyzeLoopAssignment(
decoder, decoder->pc(), decoder->num_locals(), decoder->zone());
if (decoder->failed()) return;
int instance_cache_index = decoder->num_locals();
// If the module has shared memory, the stack guard might reallocate the
// shared memory. We have to assume the instance cache will be updated.
if (decoder->module_->has_shared_memory) {
assigned->Add(instance_cache_index);
}
DCHECK_NOT_NULL(assigned);
decoder->control_at(0)->loop_assignments = assigned;
// Only introduce phis for variables assigned in this loop.
for (int i = decoder->num_locals() - 1; i >= 0; i--) {
if (!assigned->Contains(i)) continue;
TFNode* inputs[] = {ssa_env_->locals[i], control()};
ssa_env_->locals[i] =
builder_->SetType(builder_->Phi(decoder->local_type(i), 1, inputs),
decoder->local_type(i));
}
// Introduce phis for instance cache pointers if necessary.
if (assigned->Contains(instance_cache_index)) {
builder_->PrepareInstanceCacheForLoop(&ssa_env_->instance_cache,
control());
}
// Now we setup a new environment for the inside of the loop.
SetEnv(Split(decoder->zone(), ssa_env_));
builder_->StackCheck(decoder->module_->has_shared_memory
? &ssa_env_->instance_cache
: nullptr,
decoder->position());
ssa_env_->SetNotMerged();
// Wrap input merge into phis.
for (uint32_t i = 0; i < block->start_merge.arity; ++i) {
Value& val = block->start_merge[i];
TFNode* inputs[] = {val.node, block->merge_env->control};
SetAndTypeNode(&val, builder_->Phi(val.type, 1, inputs));
}
}
void Try(FullDecoder* decoder, Control* block) {
SsaEnv* outer_env = ssa_env_;
SsaEnv* catch_env = Split(decoder->zone(), outer_env);
// Mark catch environment as unreachable, since only accessable
// through catch unwinding (i.e. landing pads).
catch_env->state = SsaEnv::kUnreachable;
SsaEnv* try_env = Steal(decoder->zone(), outer_env);
SetEnv(try_env);
TryInfo* try_info = decoder->zone()->New<TryInfo>(catch_env);
block->merge_env = outer_env;
block->try_info = try_info;
}
void If(FullDecoder* decoder, const Value& cond, Control* if_block) {
TFNode* if_true = nullptr;
TFNode* if_false = nullptr;
WasmBranchHint hint = WasmBranchHint::kNoHint;
if (branch_hints_) {
hint = branch_hints_->GetHintFor(decoder->pc_relative_offset());
}
switch (hint) {
case WasmBranchHint::kNoHint:
builder_->BranchNoHint(cond.node, &if_true, &if_false);
break;
case WasmBranchHint::kUnlikely:
builder_->BranchExpectFalse(cond.node, &if_true, &if_false);
break;
case WasmBranchHint::kLikely:
builder_->BranchExpectTrue(cond.node, &if_true, &if_false);
break;
}
SsaEnv* merge_env = ssa_env_;
SsaEnv* false_env = Split(decoder->zone(), ssa_env_);
false_env->control = if_false;
SsaEnv* true_env = Steal(decoder->zone(), ssa_env_);
true_env->control = if_true;
if_block->merge_env = merge_env;
if_block->false_env = false_env;
SetEnv(true_env);
}
void FallThruTo(FullDecoder* decoder, Control* c) {
DCHECK(!c->is_loop());
MergeValuesInto(decoder, c, &c->end_merge);
}
void PopControl(FullDecoder* decoder, Control* block) {
// A loop just continues with the end environment. There is no merge.
// However, if loop unrolling is enabled, we must create a loop exit and
// wrap the fallthru values on the stack.
if (block->is_loop()) {
if (emit_loop_exits() && block->reachable()) {
BuildLoopExits(decoder, block);
WrapLocalsAtLoopExit(decoder, block);
uint32_t arity = block->end_merge.arity;
if (arity > 0) {
Value* stack_base = decoder->stack_value(arity);
for (uint32_t i = 0; i < arity; i++) {
Value* val = stack_base + i;
SetAndTypeNode(val,
builder_->LoopExitValue(
val->node, val->type.machine_representation()));
}
}
}
return;
}
// Any other block falls through to the parent block.
if (block->reachable()) FallThruTo(decoder, block);
if (block->is_onearmed_if()) {
// Merge the else branch into the end merge.
SetEnv(block->false_env);
DCHECK_EQ(block->start_merge.arity, block->end_merge.arity);
Value* values =
block->start_merge.arity > 0 ? &block->start_merge[0] : nullptr;
MergeValuesInto(decoder, block, &block->end_merge, values);
}
// Now continue with the merged environment.
SetEnv(block->merge_env);
}
void UnOp(FullDecoder* decoder, WasmOpcode opcode, const Value& value,
Value* result) {
SetAndTypeNode(result,
builder_->Unop(opcode, value.node, decoder->position()));
}
void BinOp(FullDecoder* decoder, WasmOpcode opcode, const Value& lhs,
const Value& rhs, Value* result) {
TFNode* node =
builder_->Binop(opcode, lhs.node, rhs.node, decoder->position());
if (result) SetAndTypeNode(result, node);
}
void TraceInstruction(FullDecoder* decoder, uint32_t markid) {
builder_->TraceInstruction(markid);
}
void I32Const(FullDecoder* decoder, Value* result, int32_t value) {
SetAndTypeNode(result, builder_->Int32Constant(value));
}
void I64Const(FullDecoder* decoder, Value* result, int64_t value) {
SetAndTypeNode(result, builder_->Int64Constant(value));
}
void F32Const(FullDecoder* decoder, Value* result, float value) {
SetAndTypeNode(result, builder_->Float32Constant(value));
}
void F64Const(FullDecoder* decoder, Value* result, double value) {
SetAndTypeNode(result, builder_->Float64Constant(value));
}
void S128Const(FullDecoder* decoder, const Simd128Immediate<validate>& imm,
Value* result) {
SetAndTypeNode(result, builder_->Simd128Constant(imm.value));
}
void RefNull(FullDecoder* decoder, ValueType type, Value* result) {
SetAndTypeNode(result, builder_->RefNull());
}
void RefFunc(FullDecoder* decoder, uint32_t function_index, Value* result) {
SetAndTypeNode(result, builder_->RefFunc(function_index));
}
void RefAsNonNull(FullDecoder* decoder, const Value& arg, Value* result) {
TFNode* cast_node =
v8_flags.experimental_wasm_skip_null_checks
? builder_->TypeGuard(arg.node, result->type)
: builder_->RefAsNonNull(arg.node, decoder->position());
SetAndTypeNode(result, cast_node);
}
void Drop(FullDecoder* decoder) {}
void LocalGet(FullDecoder* decoder, Value* result,
const IndexImmediate<validate>& imm) {
result->node = ssa_env_->locals[imm.index];
}
void LocalSet(FullDecoder* decoder, const Value& value,
const IndexImmediate<validate>& imm) {
ssa_env_->locals[imm.index] = value.node;
}
void LocalTee(FullDecoder* decoder, const Value& value, Value* result,
const IndexImmediate<validate>& imm) {
result->node = value.node;
ssa_env_->locals[imm.index] = value.node;
}
void GlobalGet(FullDecoder* decoder, Value* result,
const GlobalIndexImmediate<validate>& imm) {
SetAndTypeNode(result, builder_->GlobalGet(imm.index));
}
void GlobalSet(FullDecoder* decoder, const Value& value,
const GlobalIndexImmediate<validate>& imm) {
builder_->GlobalSet(imm.index, value.node);
}
void TableGet(FullDecoder* decoder, const Value& index, Value* result,
const IndexImmediate<validate>& imm) {
SetAndTypeNode(
result, builder_->TableGet(imm.index, index.node, decoder->position()));
}
void TableSet(FullDecoder* decoder, const Value& index, const Value& value,
const IndexImmediate<validate>& imm) {
builder_->TableSet(imm.index, index.node, value.node, decoder->position());
}
void Trap(FullDecoder* decoder, TrapReason reason) {
builder_->Trap(reason, decoder->position());
}
void AssertNull(FullDecoder* decoder, const Value& obj, Value* result) {
builder_->TrapIfFalse(wasm::TrapReason::kTrapIllegalCast,
builder_->IsNull(obj.node), decoder->position());
Forward(decoder, obj, result);
}
void NopForTestingUnsupportedInLiftoff(FullDecoder* decoder) {}
void Select(FullDecoder* decoder, const Value& cond, const Value& fval,
const Value& tval, Value* result) {
SetAndTypeNode(result, builder_->Select(cond.node, tval.node, fval.node,
result->type));
}
ValueVector CopyStackValues(FullDecoder* decoder, uint32_t count,
uint32_t drop_values) {
Value* stack_base =
count > 0 ? decoder->stack_value(count + drop_values) : nullptr;
ValueVector stack_values(count);
for (uint32_t i = 0; i < count; i++) {
stack_values[i] = stack_base[i];
}
return stack_values;
}
void DoReturn(FullDecoder* decoder, uint32_t drop_values) {
uint32_t ret_count = static_cast<uint32_t>(decoder->sig_->return_count());
NodeVector values(ret_count);
SsaEnv* internal_env = ssa_env_;
if (emit_loop_exits()) {
SsaEnv* exit_env = Split(decoder->zone(), ssa_env_);
SetEnv(exit_env);
auto stack_values = CopyStackValues(decoder, ret_count, drop_values);
BuildNestedLoopExits(decoder, decoder->control_depth() - 1, false,
stack_values);
GetNodes(values.begin(), base::VectorOf(stack_values));
} else {
Value* stack_base = ret_count == 0
? nullptr
: decoder->stack_value(ret_count + drop_values);
GetNodes(values.begin(), stack_base, ret_count);
}
if (v8_flags.trace_wasm && inlined_status_ == kRegularFunction) {
builder_->TraceFunctionExit(base::VectorOf(values), decoder->position());
}
builder_->Return(base::VectorOf(values));
SetEnv(internal_env);
}
void BrOrRet(FullDecoder* decoder, uint32_t depth, uint32_t drop_values) {
if (depth == decoder->control_depth() - 1) {
DoReturn(decoder, drop_values);
} else {
Control* target = decoder->control_at(depth);
if (emit_loop_exits()) {
SsaEnv* internal_env = ssa_env_;
SsaEnv* exit_env = Split(decoder->zone(), ssa_env_);
SetEnv(exit_env);
uint32_t value_count = target->br_merge()->arity;
auto stack_values = CopyStackValues(decoder, value_count, drop_values);
BuildNestedLoopExits(decoder, depth, true, stack_values);
MergeValuesInto(decoder, target, target->br_merge(),
stack_values.data());
SetEnv(internal_env);
} else {
MergeValuesInto(decoder, target, target->br_merge(), drop_values);
}
}
}
void BrIf(FullDecoder* decoder, const Value& cond, uint32_t depth) {
SsaEnv* fenv = ssa_env_;
SsaEnv* tenv = Split(decoder->zone(), fenv);
fenv->SetNotMerged();
WasmBranchHint hint = WasmBranchHint::kNoHint;
if (branch_hints_) {
hint = branch_hints_->GetHintFor(decoder->pc_relative_offset());
}
switch (hint) {
case WasmBranchHint::kNoHint:
builder_->BranchNoHint(cond.node, &tenv->control, &fenv->control);
break;
case WasmBranchHint::kUnlikely:
builder_->BranchExpectFalse(cond.node, &tenv->control, &fenv->control);
break;
case WasmBranchHint::kLikely:
builder_->BranchExpectTrue(cond.node, &tenv->control, &fenv->control);
break;
}
builder_->SetControl(fenv->control);
SetEnv(tenv);
BrOrRet(decoder, depth, 1);
SetEnv(fenv);
}
void BrTable(FullDecoder* decoder, const BranchTableImmediate<validate>& imm,
const Value& key) {
if (imm.table_count == 0) {
// Only a default target. Do the equivalent of br.
uint32_t target = BranchTableIterator<validate>(decoder, imm).next();
BrOrRet(decoder, target, 1);
return;
}
SsaEnv* branch_env = ssa_env_;
// Build branches to the various blocks based on the table.
TFNode* sw = builder_->Switch(imm.table_count + 1, key.node);
SsaEnv* copy = Steal(decoder->zone(), branch_env);
SetEnv(copy);
BranchTableIterator<validate> iterator(decoder, imm);
while (iterator.has_next()) {
uint32_t i = iterator.cur_index();
uint32_t target = iterator.next();
SetEnv(Split(decoder->zone(), copy));
builder_->SetControl(i == imm.table_count ? builder_->IfDefault(sw)
: builder_->IfValue(i, sw));
BrOrRet(decoder, target, 1);
}
DCHECK(decoder->ok());
SetEnv(branch_env);
}
void Else(FullDecoder* decoder, Control* if_block) {
if (if_block->reachable()) {
// Merge the if branch into the end merge.
MergeValuesInto(decoder, if_block, &if_block->end_merge);
}
SetEnv(if_block->false_env);
}
void LoadMem(FullDecoder* decoder, LoadType type,
const MemoryAccessImmediate<validate>& imm, const Value& index,
Value* result) {
SetAndTypeNode(result, builder_->LoadMem(
type.value_type(), type.mem_type(), index.node,
imm.offset, imm.alignment, decoder->position()));
}
void LoadTransform(FullDecoder* decoder, LoadType type,
LoadTransformationKind transform,
const MemoryAccessImmediate<validate>& imm,
const Value& index, Value* result) {
SetAndTypeNode(result,
builder_->LoadTransform(type.value_type(), type.mem_type(),
transform, index.node, imm.offset,
imm.alignment, decoder->position()));
}
void LoadLane(FullDecoder* decoder, LoadType type, const Value& value,
const Value& index, const MemoryAccessImmediate<validate>& imm,
const uint8_t laneidx, Value* result) {
SetAndTypeNode(
result, builder_->LoadLane(
type.value_type(), type.mem_type(), value.node, index.node,
imm.offset, imm.alignment, laneidx, decoder->position()));
}
void StoreMem(FullDecoder* decoder, StoreType type,
const MemoryAccessImmediate<validate>& imm, const Value& index,
const Value& value) {
builder_->StoreMem(type.mem_rep(), index.node, imm.offset, imm.alignment,
value.node, decoder->position(), type.value_type());
}
void StoreLane(FullDecoder* decoder, StoreType type,
const MemoryAccessImmediate<validate>& imm, const Value& index,
const Value& value, const uint8_t laneidx) {
builder_->StoreLane(type.mem_rep(), index.node, imm.offset, imm.alignment,
value.node, laneidx, decoder->position(),
type.value_type());
}
void CurrentMemoryPages(FullDecoder* decoder, Value* result) {
SetAndTypeNode(result, builder_->CurrentMemoryPages());
}
void MemoryGrow(FullDecoder* decoder, const Value& value, Value* result) {
SetAndTypeNode(result, builder_->MemoryGrow(value.node));
// Always reload the instance cache after growing memory.
LoadContextIntoSsa(ssa_env_, decoder);
}
void CallDirect(FullDecoder* decoder,
const CallFunctionImmediate<validate>& imm,
const Value args[], Value returns[]) {
int maybe_call_count = -1;
if (v8_flags.wasm_speculative_inlining && type_feedback_.size() > 0) {
const CallSiteFeedback& feedback = next_call_feedback();
DCHECK_EQ(feedback.num_cases(), 1);
maybe_call_count = feedback.call_count(0);
}
DoCall(decoder, CallInfo::CallDirect(imm.index, maybe_call_count), imm.sig,
args, returns);
}
void ReturnCall(FullDecoder* decoder,
const CallFunctionImmediate<validate>& imm,
const Value args[]) {
int maybe_call_count = -1;
if (v8_flags.wasm_speculative_inlining && type_feedback_.size() > 0) {
const CallSiteFeedback& feedback = next_call_feedback();
DCHECK_EQ(feedback.num_cases(), 1);
maybe_call_count = feedback.call_count(0);
}
DoReturnCall(decoder, CallInfo::CallDirect(imm.index, maybe_call_count),
imm.sig, args);
}
void CallIndirect(FullDecoder* decoder, const Value& index,
const CallIndirectImmediate<validate>& imm,
const Value args[], Value returns[]) {
DoCall(
decoder,
CallInfo::CallIndirect(index, imm.table_imm.index, imm.sig_imm.index),
imm.sig, args, returns);
}
void ReturnCallIndirect(FullDecoder* decoder, const Value& index,
const CallIndirectImmediate<validate>& imm,
const Value args[]) {
DoReturnCall(
decoder,
CallInfo::CallIndirect(index, imm.table_imm.index, imm.sig_imm.index),
imm.sig, args);
}
void CallRef(FullDecoder* decoder, const Value& func_ref,
const FunctionSig* sig, uint32_t sig_index, const Value args[],
Value returns[]) {
const CallSiteFeedback* feedback = nullptr;
if (v8_flags.wasm_speculative_inlining && type_feedback_.size() > 0) {
feedback = &next_call_feedback();
}
if (feedback == nullptr || feedback->num_cases() == 0) {
DoCall(decoder, CallInfo::CallRef(func_ref, NullCheckFor(func_ref.type)),
sig, args, returns);
return;
}
// Check for equality against a function at a specific index, and if
// successful, just emit a direct call.
int num_cases = feedback->num_cases();
std::vector<TFNode*> control_args;
std::vector<TFNode*> effect_args;
std::vector<Value*> returns_values;
control_args.reserve(num_cases + 1);
effect_args.reserve(num_cases + 2);
returns_values.reserve(num_cases);
for (int i = 0; i < num_cases; i++) {
const uint32_t expected_function_index = feedback->function_index(i);
if (v8_flags.trace_wasm_speculative_inlining) {
PrintF("[Function #%d call #%d: graph support for inlining #%d]\n",
func_index_, feedback_instruction_index_ - 1,
expected_function_index);
}
TFNode* success_control;
TFNode* failure_control;
builder_->CompareToInternalFunctionAtIndex(
func_ref.node, expected_function_index, &success_control,
&failure_control, i == num_cases - 1);
TFNode* initial_effect = effect();
builder_->SetControl(success_control);
ssa_env_->control = success_control;
Value* returns_direct =
decoder->zone()->NewArray<Value>(sig->return_count());
for (size_t i = 0; i < sig->return_count(); i++) {
returns_direct[i].type = returns[i].type;
}
DoCall(decoder,
CallInfo::CallDirect(expected_function_index,
feedback->call_count(i)),
decoder->module_->signature(sig_index), args, returns_direct);
control_args.push_back(control());
effect_args.push_back(effect());
returns_values.push_back(returns_direct);
builder_->SetEffectControl(initial_effect, failure_control);
ssa_env_->effect = initial_effect;
ssa_env_->control = failure_control;
}
Value* returns_ref = decoder->zone()->NewArray<Value>(sig->return_count());
for (size_t i = 0; i < sig->return_count(); i++) {
returns_ref[i].type = returns[i].type;
}
DoCall(decoder, CallInfo::CallRef(func_ref, NullCheckFor(func_ref.type)),
sig, args, returns_ref);
control_args.push_back(control());
TFNode* control = builder_->Merge(num_cases + 1, control_args.data());
effect_args.push_back(effect());
effect_args.push_back(control);
TFNode* effect = builder_->EffectPhi(num_cases + 1, effect_args.data());
ssa_env_->control = control;
ssa_env_->effect = effect;
builder_->SetEffectControl(effect, control);
// Each of the {DoCall} helpers above has created a reload of the instance
// cache nodes. Rather than merging all of them into a Phi here, just
// let them get DCE'ed and perform a single reload after the merge.
if (decoder->module_->initial_pages != decoder->module_->maximum_pages) {
// The invoked function could have used grow_memory, so we need to
// reload mem_size and mem_start.
LoadContextIntoSsa(ssa_env_, decoder);
}
for (uint32_t i = 0; i < sig->return_count(); i++) {
std::vector<TFNode*> phi_args;
for (int j = 0; j < num_cases; j++) {
phi_args.push_back(returns_values[j][i].node);
}
phi_args.push_back(returns_ref[i].node);
phi_args.push_back(control);
SetAndTypeNode(
&returns[i],
builder_->Phi(sig->GetReturn(i), num_cases + 1, phi_args.data()));
}
}
void ReturnCallRef(FullDecoder* decoder, const Value& func_ref,
const FunctionSig* sig, uint32_t sig_index,
const Value args[]) {
const CallSiteFeedback* feedback = nullptr;
if (v8_flags.wasm_speculative_inlining && type_feedback_.size() > 0) {
feedback = &next_call_feedback();
}
if (feedback == nullptr || feedback->num_cases() == 0) {
DoReturnCall(decoder,
CallInfo::CallRef(func_ref, NullCheckFor(func_ref.type)),
sig, args);
return;
}
// Check for equality against a function at a specific index, and if
// successful, just emit a direct call.
int num_cases = feedback->num_cases();
for (int i = 0; i < num_cases; i++) {
const uint32_t expected_function_index = feedback->function_index(i);
if (v8_flags.trace_wasm_speculative_inlining) {
PrintF("[Function #%d call #%d: graph support for inlining #%d]\n",
func_index_, feedback_instruction_index_ - 1,
expected_function_index);
}
TFNode* success_control;
TFNode* failure_control;
builder_->CompareToInternalFunctionAtIndex(
func_ref.node, expected_function_index, &success_control,
&failure_control, i == num_cases - 1);
TFNode* initial_effect = effect();
builder_->SetControl(success_control);
ssa_env_->control = success_control;
DoReturnCall(decoder,
CallInfo::CallDirect(expected_function_index,
feedback->call_count(i)),
sig, args);
builder_->SetEffectControl(initial_effect, failure_control);
ssa_env_->effect = initial_effect;
ssa_env_->control = failure_control;
}
DoReturnCall(decoder,
CallInfo::CallRef(func_ref, NullCheckFor(func_ref.type)), sig,
args);
}
void BrOnNull(FullDecoder* decoder, const Value& ref_object, uint32_t depth,
bool pass_null_along_branch, Value* result_on_fallthrough) {
SsaEnv* false_env = ssa_env_;
SsaEnv* true_env = Split(decoder->zone(), false_env);
false_env->SetNotMerged();
builder_->BrOnNull(ref_object.node, &true_env->control,
&false_env->control);
builder_->SetControl(false_env->control);
SetEnv(true_env);
BrOrRet(decoder, depth, pass_null_along_branch ? 0 : 1);
SetEnv(false_env);
SetAndTypeNode(
result_on_fallthrough,
builder_->TypeGuard(ref_object.node, result_on_fallthrough->type));
}
void BrOnNonNull(FullDecoder* decoder, const Value& ref_object, Value* result,
uint32_t depth, bool /* drop_null_on_fallthrough */) {
result->node =
builder_->TypeGuard(ref_object.node, ref_object.type.AsNonNull());
SsaEnv* false_env = ssa_env_;
SsaEnv* true_env = Split(decoder->zone(), false_env);
false_env->SetNotMerged();
builder_->BrOnNull(ref_object.node, &false_env->control,
&true_env->control);
builder_->SetControl(false_env->control);
SetEnv(true_env);
BrOrRet(decoder, depth, 0);
SetEnv(false_env);
}
void SimdOp(FullDecoder* decoder, WasmOpcode opcode, base::Vector<Value> args,
Value* result) {
NodeVector inputs(args.size());
GetNodes(inputs.begin(), args);
TFNode* node = builder_->SimdOp(opcode, inputs.begin());
if (result) SetAndTypeNode(result, node);
}
void SimdLaneOp(FullDecoder* decoder, WasmOpcode opcode,
const SimdLaneImmediate<validate>& imm,
base::Vector<Value> inputs, Value* result) {
NodeVector nodes(inputs.size());
GetNodes(nodes.begin(), inputs);
SetAndTypeNode(result,
builder_->SimdLaneOp(opcode, imm.lane, nodes.begin()));
}
void Simd8x16ShuffleOp(FullDecoder* decoder,
const Simd128Immediate<validate>& imm,
const Value& input0, const Value& input1,
Value* result) {
TFNode* input_nodes[] = {input0.node, input1.node};
SetAndTypeNode(result, builder_->Simd8x16ShuffleOp(imm.value, input_nodes));
}
void Throw(FullDecoder* decoder, const TagIndexImmediate<validate>& imm,
const base::Vector<Value>& value_args) {
int count = value_args.length();
ZoneVector<TFNode*> args(count, decoder->zone());
for (int i = 0; i < count; ++i) {
args[i] = value_args[i].node;
}
CheckForException(decoder,
builder_->Throw(imm.index, imm.tag, base::VectorOf(args),
decoder->position()));
builder_->TerminateThrow(effect(), control());
}
void Rethrow(FullDecoder* decoder, Control* block) {
DCHECK(block->is_try_catchall() || block->is_try_catch());
TFNode* exception = block->try_info->exception;
DCHECK_NOT_NULL(exception);
CheckForException(decoder, builder_->Rethrow(exception));
builder_->TerminateThrow(effect(), control());
}
void CatchException(FullDecoder* decoder,
const TagIndexImmediate<validate>& imm, Control* block,
base::Vector<Value> values) {
DCHECK(block->is_try_catch());
// The catch block is unreachable if no possible throws in the try block
// exist. We only build a landing pad if some node in the try block can
// (possibly) throw. Otherwise the catch environments remain empty.
if (!block->try_info->might_throw()) {
block->reachability = kSpecOnlyReachable;
return;
}
TFNode* exception = block->try_info->exception;
SetEnv(block->try_info->catch_env);
if (block->try_info->first_catch) {
LoadContextIntoSsa(ssa_env_, decoder);
block->try_info->first_catch = false;
}
TFNode* if_catch = nullptr;
TFNode* if_no_catch = nullptr;
// Get the exception tag and see if it matches the expected one.
TFNode* caught_tag = builder_->GetExceptionTag(exception);
TFNode* exception_tag = builder_->LoadTagFromTable(imm.index);
TFNode* compare = builder_->ExceptionTagEqual(caught_tag, exception_tag);
builder_->BranchNoHint(compare, &if_catch, &if_no_catch);
// If the tags don't match we continue with the next tag by setting the
// false environment as the new {TryInfo::catch_env} here.
SsaEnv* if_no_catch_env = Split(decoder->zone(), ssa_env_);
if_no_catch_env->control = if_no_catch;
SsaEnv* if_catch_env = Steal(decoder->zone(), ssa_env_);
if_catch_env->control = if_catch;
block->try_info->catch_env = if_no_catch_env;
// If the tags match we extract the values from the exception object and
// push them onto the operand stack using the passed {values} vector.
SetEnv(if_catch_env);
NodeVector caught_values(values.size());
base::Vector<TFNode*> caught_vector = base::VectorOf(caught_values);
builder_->GetExceptionValues(exception, imm.tag, caught_vector);
for (size_t i = 0, e = values.size(); i < e; ++i) {
SetAndTypeNode(&values[i], caught_values[i]);
}
}
void Delegate(FullDecoder* decoder, uint32_t depth, Control* block) {
DCHECK_EQ(decoder->control_at(0), block);
DCHECK(block->is_incomplete_try());
if (block->try_info->might_throw()) {
// Merge the current env into the target handler's env.
SetEnv(block->try_info->catch_env);
if (depth == decoder->control_depth() - 1) {
// We just throw to the caller here, so no need to generate IfSuccess
// and IfFailure nodes.
builder_->Rethrow(block->try_info->exception);
builder_->TerminateThrow(effect(), control());
return;
}
DCHECK(decoder->control_at(depth)->is_try());
TryInfo* target_try = decoder->control_at(depth)->try_info;
if (emit_loop_exits()) {
ValueVector stack_values;
BuildNestedLoopExits(decoder, depth, true, stack_values,
&block->try_info->exception);
}
Goto(decoder, target_try->catch_env);
// Create or merge the exception.
if (target_try->catch_env->state == SsaEnv::kReached) {
target_try->exception = block->try_info->exception;
} else {
DCHECK_EQ(target_try->catch_env->state, SsaEnv::kMerged);