-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathIRBuilder.cpp
7981 lines (6834 loc) · 291 KB
/
IRBuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#include "Library/ForInObjectEnumerator.h"
#pragma prefast(push)
#pragma prefast(disable:28652, "Prefast complains that the OR are causing the compiler to emit dynamic initializers and the variable to be allocated in read/write mem...")
static const IR::BailOutKind c_debuggerBailOutKindForCall =
IR::BailOutForceByFlag | IR::BailOutStackFrameBase | IR::BailOutBreakPointInFunction | IR::BailOutLocalValueChanged | IR::BailOutIgnoreException | IR::BailOutStep;
static const IR::BailOutKind c_debuggerBaseBailOutKindForHelper = IR::BailOutIgnoreException | IR::BailOutForceByFlag;
#pragma prefast(pop)
uint
IRBuilder::AddStatementBoundary(uint statementIndex, uint offset)
{
// Under debugger we use full stmt map, so that we know exactly start and end of each user stmt.
// We insert additional instrs in between statements, such as ProfiledLoopStart, for these bytecode reader acts as
// there is "unknown" stmt boundary with statementIndex == -1. Don't add stmt boundary for that as later
// it may cause issues, e.g. see WinBlue 218307.
if (!(statementIndex == Js::Constants::NoStatementIndex && this->m_func->IsJitInDebugMode()))
{
IR::PragmaInstr* pragmaInstr = IR::PragmaInstr::New(Js::OpCode::StatementBoundary, statementIndex, m_func);
this->AddInstr(pragmaInstr, offset);
}
#ifdef BAILOUT_INJECTION
if (!this->m_func->IsOOPJIT())
{
// Don't inject bailout if the function have trys
if (!this->m_func->GetTopFunc()->HasTry() && (statementIndex != Js::Constants::NoStatementIndex))
{
if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutFlag) && !this->m_func->GetJITFunctionBody()->IsLibraryCode())
{
ULONG line;
LONG col;
// Since we're on a separate thread, don't allow the line cache to be allocated in the Recycler.
if (((Js::FunctionBody*)m_func->GetJITFunctionBody()->GetAddr())->GetLineCharOffset(this->m_jnReader.GetCurrentOffset(), &line, &col, false /*canAllocateLineCache*/))
{
line++;
if (Js::Configuration::Global.flags.BailOut.Contains(line, (uint32)col) || Js::Configuration::Global.flags.BailOut.Contains(line, (uint32)-1))
{
this->InjectBailOut(offset);
}
}
}
else if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryLineFlag))
{
this->InjectBailOut(offset);
}
}
}
#endif
return m_statementReader.MoveNextStatementBoundary();
}
// Add conditional bailout for breaking into interpreter debug thunk - for fast F12.
void
IRBuilder::InsertBailOutForDebugger(uint byteCodeOffset, IR::BailOutKind kind, IR::Instr* insertBeforeInstr /* default nullptr */)
{
Assert(m_func->IsJitInDebugMode());
Assert(byteCodeOffset != Js::Constants::NoByteCodeOffset);
BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, byteCodeOffset, m_func);
IR::BailOutInstr * instr = IR::BailOutInstr::New(Js::OpCode::BailForDebugger, kind, bailOutInfo, bailOutInfo->bailOutFunc);
if (insertBeforeInstr)
{
InsertInstr(instr, insertBeforeInstr);
}
else
{
this->AddInstr(instr, m_lastInstr->GetByteCodeOffset());
}
}
bool
IRBuilder::DoBailOnNoProfile()
{
if (PHASE_OFF(Js::BailOnNoProfilePhase, this->m_func->GetTopFunc()))
{
return false;
}
Func *const topFunc = m_func->GetTopFunc();
if(topFunc->GetWorkItem()->GetProfiledIterations() == 0)
{
// The top function has not been profiled yet. Some switch must have been used to force jitting. This is not a
// real-world case, but for the purpose of testing the JIT, it's beneficial to generate code in unprofiled paths.
return false;
}
if (m_func->HasProfileInfo() && m_func->GetReadOnlyProfileInfo()->IsNoProfileBailoutsDisabled())
{
return false;
}
if (!m_func->DoGlobOpt())
{
return false;
}
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (this->m_func->GetTopFunc() != this->m_func && Js::Configuration::Global.flags.IsEnabled(Js::ForceJITLoopBodyFlag))
{
// No profile data for loop bodies with -force...
return false;
}
#endif
if (!this->m_func->HasProfileInfo())
{
return false;
}
if (m_func->GetTopFunc()->GetJITFunctionBody()->IsCoroutine() && !m_func->IsLoopBody())
{
return false;
}
return true;
}
void
IRBuilder::InsertBailOnNoProfile(uint offset)
{
Assert(DoBailOnNoProfile());
if (this->callTreeHasSomeProfileInfo)
{
return;
}
IR::Instr *startCall = nullptr;
int count = 0;
FOREACH_SLIST_ENTRY(IR::Instr *, argInstr, this->m_argStack)
{
if (argInstr->m_opcode == Js::OpCode::StartCall)
{
startCall = argInstr;
count++;
if (count > 1)
{
return;
}
}
} NEXT_SLIST_ENTRY;
AnalysisAssert(startCall);
if (startCall->m_prev->m_opcode != Js::OpCode::BailOnNoProfile)
{
InsertBailOnNoProfile(startCall);
}
}
void IRBuilder::InsertBailOnNoProfile(IR::Instr *const insertBeforeInstr)
{
Assert(DoBailOnNoProfile());
IR::Instr *const bailOnNoProfileInstr = IR::Instr::New(Js::OpCode::BailOnNoProfile, m_func);
InsertInstr(bailOnNoProfileInstr, insertBeforeInstr);
}
#ifdef BAILOUT_INJECTION
void
IRBuilder::InjectBailOut(uint offset)
{
if(m_func->IsSimpleJit())
{
return; // bailout injection is only applicable to full JIT
}
IR::IntConstOpnd * opnd = IR::IntConstOpnd::New(0, TyInt32, m_func);
uint bailOutOffset = offset;
if (bailOutOffset == Js::Constants::NoByteCodeOffset)
{
bailOutOffset = m_lastInstr->GetByteCodeOffset();
}
BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, bailOutOffset, m_func);
IR::BailOutInstr * instr = IR::BailOutInstr::New(Js::OpCode::BailOnEqual, IR::BailOutInjected, bailOutInfo, bailOutInfo->bailOutFunc);
instr->SetSrc1(opnd);
instr->SetSrc2(opnd);
this->AddInstr(instr, offset);
}
void
IRBuilder::CheckBailOutInjection(Js::OpCode opcode)
{
// Detect these sequence and disable Bailout injection between them:
// LdStackArgPtr
// LdArgCnt
// ApplyArgs
// This assumes that LdStackArgPtr, LdArgCnt and ApplyArgs can
// only be emitted in these sequence. This will need to be modified if it changes.
//
// Also insert a single bailout before the beginning of a switch case block for all the case labels.
switch(opcode)
{
case Js::OpCode::LdStackArgPtr:
Assert(!seenLdStackArgPtr);
Assert(!expectApplyArg);
seenLdStackArgPtr = true;
break;
case Js::OpCode::LdArgCnt:
Assert(seenLdStackArgPtr);
Assert(!expectApplyArg);
expectApplyArg = true;
break;
case Js::OpCode::ApplyArgs:
Assert(seenLdStackArgPtr);
Assert(expectApplyArg);
seenLdStackArgPtr = false;
expectApplyArg = false;
break;
case Js::OpCode::BeginSwitch:
case Js::OpCode::ProfiledBeginSwitch:
Assert(!seenProfiledBeginSwitch);
seenProfiledBeginSwitch = true;
break;
case Js::OpCode::EndSwitch:
Assert(seenProfiledBeginSwitch);
seenProfiledBeginSwitch = false;
break;
default:
Assert(!seenLdStackArgPtr);
Assert(!expectApplyArg);
break;
}
}
#endif
bool
IRBuilder::IsLoopBody() const
{
return m_func->IsLoopBody();
}
bool
IRBuilder::IsLoopBodyInTry() const
{
return m_func->IsLoopBodyInTry();
}
bool
IRBuilder::IsLoopBodyReturnIPInstr(IR::Instr * instr) const
{
IR::Opnd * dst = instr->GetDst();
return (dst && dst->IsRegOpnd() && dst->AsRegOpnd()->m_sym == m_loopBodyRetIPSym);
}
bool
IRBuilder::IsLoopBodyOuterOffset(uint offset) const
{
if (!IsLoopBody())
{
return false;
}
return (offset >= m_func->m_workItem->GetLoopHeader()->endOffset || offset < m_func->m_workItem->GetLoopHeader()->startOffset);
}
uint
IRBuilder::GetLoopBodyExitInstrOffset() const
{
// End of loop body, start of StSlot and Ret instruction at endOffset + 1
return m_func->m_workItem->GetLoopHeader()->endOffset + 1;
}
Js::RegSlot
IRBuilder::GetEnvReg() const
{
return m_func->GetJITFunctionBody()->GetEnvReg();
}
Js::RegSlot
IRBuilder::GetEnvRegForInnerFrameDisplay() const
{
Js::RegSlot envReg = m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
if (envReg == Js::Constants::NoRegister)
{
envReg = this->GetEnvReg();
}
return envReg;
}
void
IRBuilder::AddEnvOpndForInnerFrameDisplay(IR::Instr *instr, uint offset)
{
Js::RegSlot envReg = this->GetEnvRegForInnerFrameDisplay();
if (envReg != Js::Constants::NoRegister)
{
IR::RegOpnd *src2Opnd;
if (envReg == m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg() &&
m_func->DoStackFrameDisplay() &&
m_func->IsTopFunc())
{
src2Opnd = IR::RegOpnd::New(TyVar, m_func);
this->AddInstr(
IR::Instr::New(
Js::OpCode::LdSlot, src2Opnd,
this->BuildFieldOpnd(Js::OpCode::LdSlot, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
m_func),
offset);
}
else
{
src2Opnd = this->BuildSrcOpnd(envReg);
}
instr->SetSrc2(src2Opnd);
}
}
///----------------------------------------------------------------------------
///
/// IRBuilder::Build
///
/// IRBuilder main entry point. Read the bytecode for this function and
/// generate IR.
///
///----------------------------------------------------------------------------
void
IRBuilder::Build()
{
m_funcAlloc = m_func->m_alloc;
NoRecoverMemoryJitArenaAllocator localAlloc(_u("BE-IRBuilder"), m_funcAlloc->GetPageAllocator(), Js::Throw::OutOfMemory);
m_tempAlloc = &localAlloc;
uint32 offset;
uint32 statementIndex = m_statementReader.GetStatementIndex();
m_argStack = JitAnew(m_tempAlloc, SList<IR::Instr *>, m_tempAlloc);
this->branchRelocList = JitAnew(m_tempAlloc, SList<BranchReloc *>, m_tempAlloc);
Func * topFunc = this->m_func->GetTopFunc();
if (topFunc->HasTry() &&
((!topFunc->IsLoopBody() && !PHASE_OFF(Js::OptimizeTryCatchPhase, topFunc)) ||
(topFunc->HasFinally() && !topFunc->IsLoopBody() && !PHASE_OFF(Js::OptimizeTryFinallyPhase, topFunc)) ||
(topFunc->IsSimpleJit() && topFunc->GetJITFunctionBody()->DoJITLoopBody()) || // should be relaxed as more bailouts are added in Simple Jit
topFunc->IsLoopBodyInTryFinally())) // We need accurate flow when we are full jitting loop bodies which have try finally
{
this->handlerOffsetStack = JitAnew(m_tempAlloc, SList<handlerStackElementType>, m_tempAlloc);
}
this->firstTemp = m_func->GetJITFunctionBody()->GetFirstTmpReg();
Js::RegSlot tempCount = m_func->GetJITFunctionBody()->GetTempCount();
if (tempCount > 0)
{
this->tempMap = AnewArrayZ(m_tempAlloc, SymID, tempCount);
}
else
{
this->tempMap = nullptr;
}
m_func->m_headInstr = IR::EntryInstr::New(Js::OpCode::FunctionEntry, m_func);
m_func->m_exitInstr = IR::ExitInstr::New(Js::OpCode::FunctionExit, m_func);
m_func->m_tailInstr = m_func->m_exitInstr;
m_func->m_headInstr->InsertAfter(m_func->m_tailInstr);
if (m_func->GetJITFunctionBody()->IsParamAndBodyScopeMerged() || this->IsLoopBody())
{
this->SetParamScopeDone();
}
if (m_func->GetJITFunctionBody()->GetLocalClosureReg() != Js::Constants::NoRegister)
{
m_func->InitLocalClosureSyms();
}
m_functionStartOffset = m_jnReader.GetCurrentOffset();
m_lastInstr = m_func->m_headInstr;
AssertMsg(sizeof(SymID) >= sizeof(Js::RegSlot), "sizeof(SymID) != sizeof(Js::RegSlot)!!");
// Skip the last EndOfBlock opcode
Assert(!OpCodeAttr::HasMultiSizeLayout(Js::OpCode::EndOfBlock));
uint32 lastOffset = m_func->GetJITFunctionBody()->GetByteCodeLength() - Js::OpCodeUtil::EncodedSize(Js::OpCode::EndOfBlock, Js::SmallLayout);
uint32 offsetToInstructionCount = lastOffset;
if (this->IsLoopBody())
{
// LdSlot needs to cover all the register, including the temps, because we might treat
// those as if they are local for the value of the with statement
this->m_ldSlots = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetLocalsCount(), m_tempAlloc);
this->m_stSlots = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetFirstTmpReg(), m_tempAlloc);
this->m_loopBodyRetIPSym = StackSym::New(TyMachReg, this->m_func);
#if DBG
if (m_func->GetJITFunctionBody()->GetTempCount() != 0)
{
this->m_usedAsTemp = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetTempCount(), m_tempAlloc);
}
#endif
lastOffset = m_func->m_workItem->GetLoopHeader()->endOffset;
AssertOrFailFast(lastOffset < m_func->GetJITFunctionBody()->GetByteCodeLength());
// Ret is created at lastOffset + 1, so we need lastOffset + 2 entries
offsetToInstructionCount = lastOffset + 2;
// Compute the offset of the start of the locals array as a Var index.
size_t localsOffset = Js::InterpreterStackFrame::GetOffsetOfLocals();
Assert(localsOffset % sizeof(Js::Var) == 0);
this->m_loopBodyLocalsStartSlot = (Js::PropertyId)(localsOffset / sizeof(Js::Var));
}
m_offsetToInstructionCount = offsetToInstructionCount;
m_offsetToInstruction = JitAnewArrayZ(m_tempAlloc, IR::Instr *, offsetToInstructionCount);
#ifdef BYTECODE_BRANCH_ISLAND
longBranchMap = JitAnew(m_tempAlloc, LongBranchMap, m_tempAlloc);
#endif
m_switchBuilder.Init(m_func, m_tempAlloc, false);
this->LoadNativeCodeData();
this->BuildConstantLoads();
if (!this->IsLoopBody() && m_func->GetJITFunctionBody()->HasImplicitArgIns())
{
this->BuildImplicitArgIns();
}
if (!this->IsLoopBody() && m_func->GetJITFunctionBody()->HasRestParameter())
{
this->BuildArgInRest();
}
// This is first bailout in the function, the locals at stack have not initialized to undefined, so do not restore them.
// Note that for generators, we insert the bailout after the jump table to allow
// the generator's execution to proceed before bailing out. Otherwise, we would always
// bail to the beginning of the function in the interpreter, creating an infinite loop.
if (m_func->IsJitInDebugMode() && (!this->m_func->GetJITFunctionBody()->IsCoroutine() || this->IsLoopBody()))
{
this->InsertBailOutForDebugger(m_functionStartOffset, IR::BailOutForceByFlag | IR::BailOutBreakPointInFunction | IR::BailOutStep, nullptr);
}
#ifdef BAILOUT_INJECTION
// Start bailout inject after the constant and arg load. We don't bailout before that
IR::Instr * lastInstr = m_lastInstr;
#endif
offset = Js::Constants::NoByteCodeOffset;
if (!this->IsLoopBody())
{
IR::Instr *instr;
// Do the implicit operations LdEnv, NewScopeSlots, LdFrameDisplay, as indicated by function body attributes.
Js::RegSlot envReg = m_func->GetJITFunctionBody()->GetEnvReg();
if (envReg != Js::Constants::NoRegister && !this->RegIsConstant(envReg))
{
Js::OpCode newOpcode;
Js::RegSlot thisReg = m_func->GetJITFunctionBody()->GetThisRegForEventHandler();
IR::RegOpnd *srcOpnd = nullptr;
IR::RegOpnd *dstOpnd = nullptr;
if (thisReg != Js::Constants::NoRegister)
{
this->BuildArgIn0(offset, thisReg);
srcOpnd = BuildSrcOpnd(thisReg);
newOpcode = Js::OpCode::LdHandlerScope;
}
else
{
newOpcode = Js::OpCode::LdEnv;
}
dstOpnd = BuildDstOpnd(envReg);
instr = IR::Instr::New(newOpcode, dstOpnd, m_func);
if (srcOpnd)
{
instr->SetSrc1(srcOpnd);
}
if (dstOpnd->m_sym->m_isSingleDef)
{
dstOpnd->m_sym->m_isNotNumber = true;
}
this->AddInstr(instr, offset);
}
// The point at which we insert the generator resume jump table is important.
// We want to insert it right *after* the environment and constants have
// been loaded and *before* we create any other important objects
// (e.g: FrameDisplay, LocalClosure) which will be passed on to the interpreter
// frame when we bail out. Those values, if used when we resume, will be restored
// by the bail-in code, therefore we don't want to unnecessarily create those new
// objects every time we "resume" a generator
//
// Note: We need to make sure that all the values below are allocated on the heap.
// so that they don't go away once this jit'd frame is popped off.
#ifdef BAILOUT_INJECTION
lastInstr = this->m_generatorJumpTable.BuildJumpTable();
#else
this->m_generatorJumpTable.BuildJumpTable();
#endif
// When debugging generators, insert bail-out after the jump table so that we can
// get to the right point before going back to the interpreter.
// This bailout is equivalent to the one inserted above for non-generator functions.
// Additionally, we also need to insert bailouts on each resume point and right
// after the bail-in code since this bailout is only for the very first time
// we are in the generator.
if (m_func->IsJitInDebugMode() && this->m_func->GetJITFunctionBody()->IsCoroutine())
{
this->InsertBailOutForDebugger(m_functionStartOffset, IR::BailOutForceByFlag | IR::BailOutBreakPointInFunction | IR::BailOutStep, nullptr);
}
Js::RegSlot funcExprScopeReg = m_func->GetJITFunctionBody()->GetFuncExprScopeReg();
IR::RegOpnd *frameDisplayOpnd = nullptr;
if (funcExprScopeReg != Js::Constants::NoRegister)
{
IR::RegOpnd *funcExprScopeOpnd = BuildDstOpnd(funcExprScopeReg);
instr = IR::Instr::New(Js::OpCode::NewPseudoScope, funcExprScopeOpnd, m_func);
this->AddInstr(instr, offset);
}
Js::RegSlot closureReg = m_func->GetJITFunctionBody()->GetLocalClosureReg();
IR::RegOpnd *closureOpnd = nullptr;
if (closureReg != Js::Constants::NoRegister)
{
Assert(!this->RegIsConstant(closureReg));
if (m_func->DoStackScopeSlots())
{
closureOpnd = IR::RegOpnd::New(TyVar, m_func);
}
else
{
closureOpnd = this->BuildDstOpnd(closureReg);
}
if (m_func->GetJITFunctionBody()->HasScopeObject())
{
if (m_func->GetJITFunctionBody()->HasCachedScopePropIds())
{
this->BuildInitCachedScope(0, offset);
}
else
{
instr = IR::Instr::New(Js::OpCode::NewScopeObject, closureOpnd, m_func);
this->AddInstr(instr, offset);
}
}
else
{
Js::OpCode op =
m_func->DoStackScopeSlots() ? Js::OpCode::NewStackScopeSlots : Js::OpCode::NewScopeSlots;
uint size = m_func->GetJITFunctionBody()->IsParamAndBodyScopeMerged() ? m_func->GetJITFunctionBody()->GetScopeSlotArraySize() : m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
IR::Opnd * srcOpnd = IR::IntConstOpnd::New(size + Js::ScopeSlots::FirstSlotIndex, TyUint32, m_func);
instr = IR::Instr::New(op, closureOpnd, srcOpnd, m_func);
this->AddInstr(instr, offset);
}
if (closureOpnd->m_sym->m_isSingleDef)
{
closureOpnd->m_sym->m_isNotNumber = true;
}
if (m_func->DoStackScopeSlots())
{
// Init the stack closure sym and use it to save the scope slot pointer.
this->AddInstr(
IR::Instr::New(
Js::OpCode::InitLocalClosure, this->BuildDstOpnd(m_func->GetLocalClosureSym()->m_id), m_func),
offset);
this->AddInstr(
IR::Instr::New(
Js::OpCode::StSlot,
this->BuildFieldOpnd(
Js::OpCode::StSlot, m_func->GetLocalClosureSym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
closureOpnd, m_func),
offset);
}
}
Js::RegSlot frameDisplayReg = m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
if (frameDisplayReg != Js::Constants::NoRegister)
{
Assert(!this->RegIsConstant(frameDisplayReg));
Js::OpCode op = m_func->DoStackScopeSlots() ? Js::OpCode::NewStackFrameDisplay : Js::OpCode::LdFrameDisplay;
if (funcExprScopeReg != Js::Constants::NoRegister)
{
// Insert the function expression scope ahead of any enclosing scopes.
IR::RegOpnd * funcExprScopeOpnd = BuildSrcOpnd(funcExprScopeReg);
frameDisplayOpnd = closureReg != Js::Constants::NoRegister ? IR::RegOpnd::New(TyVar, m_func) : BuildDstOpnd(frameDisplayReg);
instr = IR::Instr::New(Js::OpCode::LdFrameDisplay, frameDisplayOpnd, funcExprScopeOpnd, m_func);
if (envReg != Js::Constants::NoRegister)
{
instr->SetSrc2(this->BuildSrcOpnd(envReg));
}
this->AddInstr(instr, (uint)-1);
}
if (closureReg != Js::Constants::NoRegister)
{
IR::RegOpnd *dstOpnd;
if (m_func->DoStackScopeSlots() && m_func->IsTopFunc())
{
dstOpnd = IR::RegOpnd::New(TyVar, m_func);
}
else
{
dstOpnd = this->BuildDstOpnd(frameDisplayReg);
}
instr = IR::Instr::New(op, dstOpnd, closureOpnd, m_func);
if (frameDisplayOpnd != nullptr)
{
// We're building on an intermediate LdFrameDisplay result.
instr->SetSrc2(frameDisplayOpnd);
}
else if (envReg != Js::Constants::NoRegister)
{
// We're building on the environment created by the enclosing function.
instr->SetSrc2(this->BuildSrcOpnd(envReg));
}
this->AddInstr(instr, offset);
if (dstOpnd->m_sym->m_isSingleDef)
{
dstOpnd->m_sym->m_isNotNumber = true;
}
if (m_func->DoStackFrameDisplay())
{
// Use the stack closure sym to save the frame display pointer.
this->AddInstr(
IR::Instr::New(
Js::OpCode::InitLocalClosure, this->BuildDstOpnd(m_func->GetLocalFrameDisplaySym()->m_id), m_func),
offset);
this->AddInstr(
IR::Instr::New(
Js::OpCode::StSlot,
this->BuildFieldOpnd(Js::OpCode::StSlot, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
dstOpnd, m_func),
offset);
}
}
}
}
offset = m_functionStartOffset;
if (m_statementReader.AtStatementBoundary(&m_jnReader))
{
statementIndex = this->AddStatementBoundary(statementIndex, offset);
}
// For label instr we can add bailout only after all labels were finalized. Put to list/add in the end.
JsUtil::BaseDictionary<IR::Instr*, int, JitArenaAllocator> ignoreExBranchInstrToOffsetMap(m_tempAlloc);
Js::LayoutSize layoutSize;
IR::Instr* lastProcessedInstrForJITLoopBody = m_func->m_headInstr;
for (Js::OpCode newOpcode = m_jnReader.ReadOp(layoutSize); (uint)m_jnReader.GetCurrentOffset() <= lastOffset; newOpcode = m_jnReader.ReadOp(layoutSize))
{
Assert(newOpcode != Js::OpCode::EndOfBlock);
#ifdef BAILOUT_INJECTION
if (!this->m_func->GetTopFunc()->HasTry()
#ifdef BYTECODE_BRANCH_ISLAND
&& newOpcode != Js::OpCode::BrLong // Don't inject bailout on BrLong as they are just redirecting to a different offset anyways
#endif
)
{
if (!this->m_func->IsOOPJIT())
{
if (!seenLdStackArgPtr && !seenProfiledBeginSwitch)
{
if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutByteCodeFlag))
{
ThreadContext * threadContext = this->m_func->GetScriptContext()->GetThreadContext();
if (Js::Configuration::Global.flags.BailOutByteCode.Contains(threadContext->bailOutByteCodeLocationCount))
{
this->InjectBailOut(offset);
}
}
else if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryByteCodeFlag))
{
this->InjectBailOut(offset);
}
}
CheckBailOutInjection(newOpcode);
}
}
#endif
AssertOrFailFastMsg(Js::OpCodeUtil::IsValidByteCodeOpcode(newOpcode), "Error getting opcode from m_jnReader.Op()");
uint layoutAndSize = layoutSize * Js::OpLayoutType::Count + Js::OpCodeUtil::GetOpCodeLayout(newOpcode);
switch(layoutAndSize)
{
#define LAYOUT_TYPE(layout) \
case Js::OpLayoutType::layout: \
Assert(layoutSize == Js::SmallLayout); \
this->Build##layout(newOpcode, offset); \
break;
#define LAYOUT_TYPE_WMS(layout) \
case Js::SmallLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
this->Build##layout<Js::SmallLayoutSizePolicy>(newOpcode, offset); \
break; \
case Js::MediumLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
this->Build##layout<Js::MediumLayoutSizePolicy>(newOpcode, offset); \
break; \
case Js::LargeLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
this->Build##layout<Js::LargeLayoutSizePolicy>(newOpcode, offset); \
break;
#include "ByteCode/LayoutTypes.h"
default:
AssertMsg(0, "Unimplemented layout");
break;
}
#ifdef BAILOUT_INJECTION
if (!this->m_func->IsOOPJIT())
{
if (!this->m_func->GetTopFunc()->HasTry() && Js::Configuration::Global.flags.IsEnabled(Js::BailOutByteCodeFlag))
{
ThreadContext * threadContext = this->m_func->GetScriptContext()->GetThreadContext();
if (lastInstr != m_lastInstr)
{
lastInstr = lastInstr->GetNextRealInstr();
if (lastInstr->HasBailOutInfo())
{
lastInstr = lastInstr->m_next;
}
lastInstr->bailOutByteCodeLocation = threadContext->bailOutByteCodeLocationCount;
lastInstr = m_lastInstr;
}
threadContext->bailOutByteCodeLocationCount++;
}
}
#endif
if (IsLoopBodyInTry() && lastProcessedInstrForJITLoopBody != m_lastInstr)
{
// traverse in backward so we get new/later value of given symId for storing instead of the earlier/stale
// symId value. m_stSlots is used to prevent multiple stores to the same symId.
FOREACH_INSTR_BACKWARD_EDITING_IN_RANGE(
instr,
instrPrev,
m_lastInstr,
lastProcessedInstrForJITLoopBody->m_next)
{
if (instr->GetDst() && instr->GetDst()->IsRegOpnd() && instr->GetDst()->GetStackSym()->HasByteCodeRegSlot())
{
StackSym * dstSym = instr->GetDst()->GetStackSym();
Js::RegSlot dstRegSlot = dstSym->GetByteCodeRegSlot();
if (!this->RegIsTemp(dstRegSlot) && !this->RegIsConstant(dstRegSlot))
{
SymID symId = dstSym->m_id;
AssertOrFailFast(symId < m_stSlots->Length());
if (this->m_stSlots->Test(symId))
{
// For jitted loop bodies that are in a try block, we consider any symbol that has a
// non-temp bytecode reg slot, to be write-through. Hence, generating StSlots at all
// defs for such symbols
IR::Instr * stSlot = this->GenerateLoopBodyStSlot(dstRegSlot);
AddInstr(stSlot, Js::Constants::NoByteCodeOffset);
this->m_stSlots->Clear(symId);
}
else
{
Assert(dstSym->m_isCatchObjectSym);
}
}
}
} NEXT_INSTR_BACKWARD_EDITING_IN_RANGE;
lastProcessedInstrForJITLoopBody = m_lastInstr;
}
offset = m_jnReader.GetCurrentOffset();
if (m_func->IsJitInDebugMode())
{
bool needBailoutForHelper = CONFIG_FLAG(EnableContinueAfterExceptionWrappersForHelpers) &&
(OpCodeAttr::NeedsPostOpDbgBailOut(newOpcode) ||
(m_lastInstr->m_opcode == Js::OpCode::CallHelper && m_lastInstr->GetSrc1() &&
HelperMethodAttributes::CanThrow(m_lastInstr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper)));
if (needBailoutForHelper)
{
// Insert bailout after return from a helper call.
// For now use offset of next instr, when we get & ignore exception, we replace this with next statement offset.
if (m_lastInstr->IsBranchInstr())
{
// Debugger bailout on branches goes to different block which can become dead. Keep bailout with real instr.
// Can't convert to bailout at this time, can do that only after branches are finalized, remember for later.
ignoreExBranchInstrToOffsetMap.Add(m_lastInstr, offset);
}
else if (
m_lastInstr->m_opcode == Js::OpCode::Throw ||
m_lastInstr->m_opcode == Js::OpCode::RuntimeReferenceError ||
m_lastInstr->m_opcode == Js::OpCode::RuntimeTypeError)
{
uint32 lastInstrOffset = m_lastInstr->GetByteCodeOffset();
AssertOrFailFast(lastInstrOffset < m_offsetToInstructionCount);
#if DBG
__analysis_assume(lastInstrOffset < this->m_offsetToInstructionCount);
#endif
bool isLastInstrUpdateNeeded = m_offsetToInstruction[lastInstrOffset] == m_lastInstr;
BailOutInfo * bailOutInfo = JitAnew(this->m_func->m_alloc, BailOutInfo, offset, this->m_func);
m_lastInstr = m_lastInstr->ConvertToBailOutInstr(bailOutInfo, c_debuggerBaseBailOutKindForHelper, true);
if (isLastInstrUpdateNeeded)
{
m_offsetToInstruction[lastInstrOffset] = m_lastInstr;
}
}
else
{
IR::BailOutKind bailOutKind = c_debuggerBaseBailOutKindForHelper;
if (OpCodeAttr::HasImplicitCall(newOpcode) || OpCodeAttr::OpndHasImplicitCall(newOpcode))
{
// When we get out of e.g. valueOf called by a helper (e.g. Add_A) during stepping,
// we need to bail out to continue debugging calling function in interpreter,
// essentially this is similar to bail out on return from a method.
bailOutKind |= c_debuggerBailOutKindForCall;
}
this->InsertBailOutForDebugger(offset, bailOutKind);
}
}
}
while (m_statementReader.AtStatementBoundary(&m_jnReader))
{
statementIndex = this->AddStatementBoundary(statementIndex, offset);
}
}
if (Js::Constants::NoStatementIndex != statementIndex)
{
// If we are inside a user statement then create a trailing line pragma instruction
statementIndex = this->AddStatementBoundary(statementIndex, Js::Constants::NoByteCodeOffset);
}
if (IsLoopBody())
{
// Insert the LdSlot/StSlot and Ret
IR::Opnd * retOpnd = this->InsertLoopBodyReturnIPInstr(offset, offset);
// Restore and Ret are at the last offset + 1
GenerateLoopBodySlotAccesses(lastOffset + 1);
InsertDoneLoopBodyLoopCounter(lastOffset);
IR::Instr * retInstr = IR::Instr::New(Js::OpCode::Ret, m_func);
retInstr->SetSrc1(retOpnd);
this->AddInstr(retInstr, lastOffset + 1);
}
// Now fix up the targets for all the branches we've introduced.
InsertLabels();
Assert(!this->handlerOffsetStack || this->handlerOffsetStack->Empty());
// Insert bailout for ignore exception for labels, after all labels were finalized.
ignoreExBranchInstrToOffsetMap.Map([this](IR::Instr* instr, int byteCodeOffset) {
BailOutInfo * bailOutInfo = JitAnew(this->m_func->m_alloc, BailOutInfo, byteCodeOffset, this->m_func);
instr->ConvertToBailOutInstr(bailOutInfo, c_debuggerBaseBailOutKindForHelper, true);
});
// Now that we know whether the func is a leaf or not, decide whether we'll emit fast paths.
// Do this once and for all, per-func, since the source size on the ThreadContext will be
// changing while we JIT.
if (this->m_func->IsTopFunc())
{
this->m_func->SetDoFastPaths();
this->EmitClosureRangeChecks();
}
}
void
IRBuilder::EmitClosureRangeChecks()
{
if (m_func->frameDisplayCheckTable)
{
// Frame display checks. Again, chain to the instruction (LdEnv/LdSlot).
FOREACH_HASHTABLE_ENTRY(FrameDisplayCheckRecord*, bucket, m_func->frameDisplayCheckTable)
{
StackSym *stackSym = m_func->m_symTable->FindStackSym(bucket.value);
Assert(stackSym && stackSym->m_instrDef);
IR::Instr *instrDef = stackSym->m_instrDef;
IR::Instr *insertInstr = instrDef->m_next;
IR::RegOpnd *dstOpnd = instrDef->UnlinkDst()->AsRegOpnd();
IR::Instr *instr = IR::Instr::New(Js::OpCode::FrameDisplayCheck, dstOpnd, m_func);
dstOpnd = IR::RegOpnd::New(TyVar, m_func);
instrDef->SetDst(dstOpnd);
instr->SetSrc1(dstOpnd);
// Attach the two-dimensional check info.
IR::AddrOpnd *recordOpnd = IR::AddrOpnd::New(bucket.element, IR::AddrOpndKindDynamicMisc, m_func, true);
instr->SetSrc2(recordOpnd);
insertInstr->InsertBefore(instr);
}
NEXT_HASHTABLE_ENTRY;
}
// If not a loop, but there are loops and trys, restore scope slot pointer and FD
if (!m_func->IsLoopBody() && m_func->HasTry() && m_func->GetJITFunctionBody()->GetByteCodeInLoopCount() != 0)
{
BVSparse<JitArenaAllocator> * bv = nullptr;
if (m_func->GetLocalClosureSym() && m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
{
bv = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
bv->Set(m_func->GetLocalClosureSym()->m_id);
}
if (m_func->GetLocalFrameDisplaySym() && m_func->GetLocalFrameDisplaySym()->HasByteCodeRegSlot())
{
if (!bv)
{
bv = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
}
bv->Set(m_func->GetLocalFrameDisplaySym()->m_id);
}
if (bv)
{
FOREACH_INSTR_IN_FUNC_BACKWARD(instr, m_func)
{
if (instr->m_opcode == Js::OpCode::Ret)
{
IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(instr);
byteCodeUse->SetBV(bv);
instr->InsertBefore(byteCodeUse);
break;
}
}
NEXT_INSTR_IN_FUNC_BACKWARD;
}
}
}
///----------------------------------------------------------------------------
///
/// IRBuilder::InsertLabels
///
/// Insert label instructions at the offsets recorded in the branch reloc list.
///
///----------------------------------------------------------------------------
void
IRBuilder::InsertLabels()
{
AssertMsg(this->branchRelocList, "Malformed branch reloc list");
SList<BranchReloc *>::Iterator iter(this->branchRelocList);
while (iter.Next())
{
IR::LabelInstr * labelInstr = nullptr;
BranchReloc * reloc = iter.Data();
IR::BranchInstr * branchInstr = reloc->GetBranchInstr();
uint offset = reloc->GetOffset();
uint const branchOffset = reloc->GetBranchOffset();
Assert(!IsLoopBody() || offset <= GetLoopBodyExitInstrOffset());
if(branchInstr->m_opcode == Js::OpCode::MultiBr)
{
IR::MultiBranchInstr * multiBranchInstr = branchInstr->AsBranchInstr()->AsMultiBrInstr();
multiBranchInstr->UpdateMultiBrTargetOffsets([&](uint32 offset) -> IR::LabelInstr *
{
labelInstr = this->CreateLabel(branchInstr, offset);
multiBranchInstr->ChangeLabelRef(nullptr, labelInstr);
return labelInstr;
});
}
else
{
labelInstr = this->CreateLabel(branchInstr, offset);
branchInstr->SetTarget(labelInstr);
}
if (!reloc->IsNotBackEdge() && branchOffset >= offset)
{
bool wasLoopTop = labelInstr->m_isLoopTop;
labelInstr->m_isLoopTop = true;