forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowGraph.cpp
More file actions
3386 lines (3006 loc) · 105 KB
/
FlowGraph.cpp
File metadata and controls
3386 lines (3006 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
FlowGraph *
FlowGraph::New(Func * func, JitArenaAllocator * alloc)
{
FlowGraph * graph;
graph = JitAnew(alloc, FlowGraph, func, alloc);
return graph;
}
///----------------------------------------------------------------------------
///
/// FlowGraph::Build
///
/// Construct flow graph and loop structures for the current state of the function.
///
///----------------------------------------------------------------------------
void
FlowGraph::Build(void)
{
Func * func = this->func;
BEGIN_CODEGEN_PHASE(func, Js::FGPeepsPhase);
this->RunPeeps();
END_CODEGEN_PHASE(func, Js::FGPeepsPhase);
// We don't optimize fully with SimpleJit. But, when JIT loop body is enabled, we do support
// bailing out from a simple jitted function to do a full jit of a loop body in the function
// (BailOnSimpleJitToFullJitLoopBody). For that purpose, we need the flow from try to catch.
if (this->func->HasTry() &&
(this->func->DoOptimizeTryCatch() ||
(this->func->IsSimpleJit() && this->func->GetJITFunctionBody()->DoJITLoopBody())
)
)
{
this->catchLabelStack = JitAnew(this->alloc, SList<IR::LabelInstr*>, this->alloc);
}
IR::Instr * currLastInstr = nullptr;
BasicBlock * currBlock = nullptr;
BasicBlock * nextBlock = nullptr;
bool hasCall = false;
FOREACH_INSTR_IN_FUNC_BACKWARD_EDITING(instr, instrPrev, func)
{
if (currLastInstr == nullptr || instr->EndsBasicBlock())
{
// Start working on a new block.
// If we're currently processing a block, then wrap it up before beginning a new one.
if (currLastInstr != nullptr)
{
nextBlock = currBlock;
currBlock = this->AddBlock(instr->m_next, currLastInstr, nextBlock);
currBlock->hasCall = hasCall;
hasCall = false;
}
currLastInstr = instr;
}
if (instr->StartsBasicBlock())
{
// Insert a BrOnException after the loop top if we are in a try-catch. This is required to
// model flow from the loop to the catch block for loops that don't have a break condition.
if (instr->IsLabelInstr() && instr->AsLabelInstr()->m_isLoopTop &&
this->catchLabelStack && !this->catchLabelStack->Empty() &&
instr->m_next->m_opcode != Js::OpCode::BrOnException)
{
IR::BranchInstr * brOnException = IR::BranchInstr::New(Js::OpCode::BrOnException, this->catchLabelStack->Top(), instr->m_func);
instr->InsertAfter(brOnException);
instrPrev = brOnException; // process BrOnException before adding a new block for loop top label.
continue;
}
// Wrap up the current block and get ready to process a new one.
nextBlock = currBlock;
currBlock = this->AddBlock(instr, currLastInstr, nextBlock);
currBlock->hasCall = hasCall;
hasCall = false;
currLastInstr = nullptr;
}
switch (instr->m_opcode)
{
case Js::OpCode::Catch:
Assert(instr->m_prev->IsLabelInstr());
if (this->catchLabelStack)
{
this->catchLabelStack->Push(instr->m_prev->AsLabelInstr());
}
break;
case Js::OpCode::TryCatch:
if (this->catchLabelStack)
{
this->catchLabelStack->Pop();
}
break;
case Js::OpCode::CloneBlockScope:
case Js::OpCode::CloneInnerScopeSlots:
// It would be nice to do this in IRBuilder, but doing so gives us
// trouble when doing the DoSlotArrayCheck since it assume single def
// of the sym to do its check properly. So instead we assign the dst
// here in FlowGraph.
instr->SetDst(instr->GetSrc1());
break;
}
if (OpCodeAttr::UseAllFields(instr->m_opcode))
{
// UseAllFields opcode are call instruction or opcode that would call.
hasCall = true;
if (OpCodeAttr::CallInstr(instr->m_opcode))
{
if (!instr->isCallInstrProtectedByNoProfileBailout)
{
instr->m_func->SetHasCallsOnSelfAndParents();
}
// For ARM & X64 because of their register calling convention
// the ArgOuts need to be moved next to the call.
#if defined(_M_ARM) || defined(_M_X64)
IR::Instr* argInsertInstr = instr;
instr->IterateArgInstrs([&](IR::Instr* argInstr)
{
if (argInstr->m_opcode != Js::OpCode::LdSpreadIndices &&
argInstr->m_opcode != Js::OpCode::ArgOut_A_Dynamic &&
argInstr->m_opcode != Js::OpCode::ArgOut_A_FromStackArgs &&
argInstr->m_opcode != Js::OpCode::ArgOut_A_SpreadArg)
{
// don't have bailout in asm.js so we don't need BytecodeArgOutCapture
if (!argInstr->m_func->GetJITFunctionBody()->IsAsmJsMode())
{
// Need to always generate byte code arg out capture,
// because bailout can't restore from the arg out as it is
// replaced by new sym for register calling convention in lower
argInstr->GenerateBytecodeArgOutCapture();
}
// Check if the instruction is already next
if (argInstr != argInsertInstr->m_prev)
{
// It is not, move it.
argInstr->Move(argInsertInstr);
}
argInsertInstr = argInstr;
}
return false;
});
#endif
}
}
}
NEXT_INSTR_IN_FUNC_BACKWARD_EDITING;
this->func->isFlowGraphValid = true;
Assert(!this->catchLabelStack || this->catchLabelStack->Empty());
// We've been walking backward so that edge lists would be in the right order. Now walk the blocks
// forward to number the blocks in lexical order.
unsigned int blockNum = 0;
FOREACH_BLOCK(block, this)
{
block->SetBlockNum(blockNum++);
}NEXT_BLOCK;
AssertMsg(blockNum == this->blockCount, "Block count is out of whack");
this->RemoveUnreachableBlocks();
this->FindLoops();
bool breakBlocksRelocated = this->CanonicalizeLoops();
#if DBG
this->VerifyLoopGraph();
#endif
// Renumber the blocks. Break block remove code has likely inserted new basic blocks.
blockNum = 0;
// Regions need to be assigned before Globopt because:
// 1. FullJit: The Backward Pass will set the write-through symbols on the regions and the forward pass will
// use this information to insert ToVars for those symbols. Also, for a symbol determined as write-through
// in the try region to be restored correctly by the bailout, it should not be removed from the
// byteCodeUpwardExposedUsed upon a def in the try region (the def might be preempted by an exception).
//
// 2. SimpleJit: Same case of correct restoration as above applies in SimpleJit too. However, the only bailout
// we have in Simple Jitted code right now is BailOnSimpleJitToFullJitLoopBody, installed in IRBuilder. So,
// for now, we can just check if the func has a bailout to assign regions pre globopt while running SimpleJit.
bool assignRegionsBeforeGlobopt = this->func->HasTry() &&
(this->func->DoOptimizeTryCatch() || (this->func->IsSimpleJit() && this->func->hasBailout));
Region ** blockToRegion = nullptr;
if (assignRegionsBeforeGlobopt)
{
blockToRegion = JitAnewArrayZ(this->alloc, Region*, this->blockCount);
}
FOREACH_BLOCK_ALL(block, this)
{
block->SetBlockNum(blockNum++);
if (assignRegionsBeforeGlobopt)
{
if (block->isDeleted && !block->isDead)
{
continue;
}
this->UpdateRegionForBlock(block, blockToRegion);
}
} NEXT_BLOCK_ALL;
AssertMsg (blockNum == this->blockCount, "Block count is out of whack");
if (breakBlocksRelocated)
{
// Sort loop lists only if there is break block removal.
SortLoopLists();
}
#if DBG_DUMP
this->Dump(false, nullptr);
#endif
}
void
FlowGraph::SortLoopLists()
{
// Sort the blocks in loopList
for (Loop *loop = this->loopList; loop; loop = loop->next)
{
unsigned int lastBlockNumber = loop->GetHeadBlock()->GetBlockNum();
// Insertion sort as the blockList is almost sorted in the loop.
FOREACH_BLOCK_IN_LOOP_EDITING(block, loop, iter)
{
if (lastBlockNumber <= block->GetBlockNum())
{
lastBlockNumber = block->GetBlockNum();
}
else
{
iter.UnlinkCurrent();
FOREACH_BLOCK_IN_LOOP_EDITING(insertBlock,loop,newIter)
{
if (insertBlock->GetBlockNum() > block->GetBlockNum())
{
break;
}
}NEXT_BLOCK_IN_LOOP_EDITING;
newIter.InsertBefore(block);
}
}NEXT_BLOCK_IN_LOOP_EDITING;
}
}
void
FlowGraph::RunPeeps()
{
if (this->func->HasTry())
{
return;
}
if (PHASE_OFF(Js::FGPeepsPhase, this->func))
{
return;
}
IR::Instr * instrCm = nullptr;
bool tryUnsignedCmpPeep = false;
FOREACH_INSTR_IN_FUNC_EDITING(instr, instrNext, this->func)
{
switch(instr->m_opcode)
{
case Js::OpCode::Br:
case Js::OpCode::BrEq_I4:
case Js::OpCode::BrGe_I4:
case Js::OpCode::BrGt_I4:
case Js::OpCode::BrLt_I4:
case Js::OpCode::BrLe_I4:
case Js::OpCode::BrUnGe_I4:
case Js::OpCode::BrUnGt_I4:
case Js::OpCode::BrUnLt_I4:
case Js::OpCode::BrUnLe_I4:
case Js::OpCode::BrNeq_I4:
case Js::OpCode::BrEq_A:
case Js::OpCode::BrGe_A:
case Js::OpCode::BrGt_A:
case Js::OpCode::BrLt_A:
case Js::OpCode::BrLe_A:
case Js::OpCode::BrUnGe_A:
case Js::OpCode::BrUnGt_A:
case Js::OpCode::BrUnLt_A:
case Js::OpCode::BrUnLe_A:
case Js::OpCode::BrNotEq_A:
case Js::OpCode::BrNotNeq_A:
case Js::OpCode::BrSrNotEq_A:
case Js::OpCode::BrSrNotNeq_A:
case Js::OpCode::BrNotGe_A:
case Js::OpCode::BrNotGt_A:
case Js::OpCode::BrNotLt_A:
case Js::OpCode::BrNotLe_A:
case Js::OpCode::BrNeq_A:
case Js::OpCode::BrNotNull_A:
case Js::OpCode::BrNotAddr_A:
case Js::OpCode::BrAddr_A:
case Js::OpCode::BrSrEq_A:
case Js::OpCode::BrSrNeq_A:
case Js::OpCode::BrOnHasProperty:
case Js::OpCode::BrOnNoProperty:
case Js::OpCode::BrHasSideEffects:
case Js::OpCode::BrNotHasSideEffects:
case Js::OpCode::BrFncEqApply:
case Js::OpCode::BrFncNeqApply:
case Js::OpCode::BrOnEmpty:
case Js::OpCode::BrOnNotEmpty:
case Js::OpCode::BrFncCachedScopeEq:
case Js::OpCode::BrFncCachedScopeNeq:
case Js::OpCode::BrOnObject_A:
case Js::OpCode::BrOnClassConstructor:
case Js::OpCode::BrOnBaseConstructorKind:
if (tryUnsignedCmpPeep)
{
this->UnsignedCmpPeep(instr);
}
instrNext = Peeps::PeepBranch(instr->AsBranchInstr());
break;
case Js::OpCode::MultiBr:
// TODO: Run peeps on these as well...
break;
case Js::OpCode::BrTrue_I4:
case Js::OpCode::BrFalse_I4:
case Js::OpCode::BrTrue_A:
case Js::OpCode::BrFalse_A:
if (instrCm)
{
if (instrCm->GetDst()->IsInt32())
{
Assert(instr->m_opcode == Js::OpCode::BrTrue_I4 || instr->m_opcode == Js::OpCode::BrFalse_I4);
instrNext = this->PeepTypedCm(instrCm);
}
else
{
instrNext = this->PeepCm(instrCm);
}
instrCm = nullptr;
if (instrNext == nullptr)
{
// Set instrNext back to the current instr.
instrNext = instr;
}
}
else
{
instrNext = Peeps::PeepBranch(instr->AsBranchInstr());
}
break;
case Js::OpCode::CmEq_I4:
case Js::OpCode::CmGe_I4:
case Js::OpCode::CmGt_I4:
case Js::OpCode::CmLt_I4:
case Js::OpCode::CmLe_I4:
case Js::OpCode::CmNeq_I4:
case Js::OpCode::CmEq_A:
case Js::OpCode::CmGe_A:
case Js::OpCode::CmGt_A:
case Js::OpCode::CmLt_A:
case Js::OpCode::CmLe_A:
case Js::OpCode::CmNeq_A:
case Js::OpCode::CmSrEq_A:
case Js::OpCode::CmSrNeq_A:
if (tryUnsignedCmpPeep)
{
this->UnsignedCmpPeep(instr);
}
case Js::OpCode::CmUnGe_I4:
case Js::OpCode::CmUnGt_I4:
case Js::OpCode::CmUnLt_I4:
case Js::OpCode::CmUnLe_I4:
case Js::OpCode::CmUnGe_A:
case Js::OpCode::CmUnGt_A:
case Js::OpCode::CmUnLt_A:
case Js::OpCode::CmUnLe_A:
// There may be useless branches between the Cm instr and the branch that uses the result.
// So save the last Cm instr seen, and trigger the peep on the next BrTrue/BrFalse.
instrCm = instr;
break;
case Js::OpCode::Label:
if (instr->AsLabelInstr()->IsUnreferenced())
{
instrNext = Peeps::PeepUnreachableLabel(instr->AsLabelInstr(), false);
}
break;
case Js::OpCode::StatementBoundary:
instr->ClearByteCodeOffset();
instr->SetByteCodeOffset(instr->GetNextRealInstrOrLabel());
break;
case Js::OpCode::ShrU_I4:
case Js::OpCode::ShrU_A:
if (tryUnsignedCmpPeep)
{
break;
}
if (instr->GetDst()->AsRegOpnd()->m_sym->IsSingleDef()
&& instr->GetSrc2()->IsRegOpnd() && instr->GetSrc2()->AsRegOpnd()->m_sym->IsTaggableIntConst()
&& instr->GetSrc2()->AsRegOpnd()->m_sym->GetIntConstValue() == 0)
{
tryUnsignedCmpPeep = true;
}
break;
default:
Assert(!instr->IsBranchInstr());
}
} NEXT_INSTR_IN_FUNC_EDITING;
}
void
Loop::InsertLandingPad(FlowGraph *fg)
{
BasicBlock *headBlock = this->GetHeadBlock();
// Always create a landing pad. This allows globopt to easily hoist instructions
// and re-optimize the block if needed.
BasicBlock *landingPad = BasicBlock::New(fg);
this->landingPad = landingPad;
IR::Instr * headInstr = headBlock->GetFirstInstr();
IR::LabelInstr *landingPadLabel = IR::LabelInstr::New(Js::OpCode::Label, headInstr->m_func);
landingPadLabel->SetByteCodeOffset(headInstr);
headInstr->InsertBefore(landingPadLabel);
landingPadLabel->SetBasicBlock(landingPad);
landingPad->SetBlockNum(fg->blockCount++);
landingPad->SetFirstInstr(landingPadLabel);
landingPad->SetLastInstr(landingPadLabel);
landingPad->prev = headBlock->prev;
landingPad->prev->next = landingPad;
landingPad->next = headBlock;
headBlock->prev = landingPad;
Loop *parentLoop = this->parent;
landingPad->loop = parentLoop;
// We need to add this block to the block list of the parent loops
while (parentLoop)
{
// Find the head block in the block list of the parent loop
FOREACH_BLOCK_IN_LOOP_EDITING(block, parentLoop, iter)
{
if (block == headBlock)
{
// Add the landing pad to the block list
iter.InsertBefore(landingPad);
break;
}
} NEXT_BLOCK_IN_LOOP_EDITING;
parentLoop = parentLoop->parent;
}
// Fix predecessor flow edges
FOREACH_PREDECESSOR_EDGE_EDITING(edge, headBlock, iter)
{
// Make sure it isn't a back-edge
if (edge->GetPred()->loop != this && !this->IsDescendentOrSelf(edge->GetPred()->loop))
{
if (edge->GetPred()->GetLastInstr()->IsBranchInstr() && headBlock->GetFirstInstr()->IsLabelInstr())
{
IR::BranchInstr *branch = edge->GetPred()->GetLastInstr()->AsBranchInstr();
branch->ReplaceTarget(headBlock->GetFirstInstr()->AsLabelInstr(), landingPadLabel);
}
headBlock->UnlinkPred(edge->GetPred(), false);
landingPad->AddPred(edge, fg);
edge->SetSucc(landingPad);
}
} NEXT_PREDECESSOR_EDGE_EDITING;
fg->AddEdge(landingPad, headBlock);
}
bool
Loop::RemoveBreakBlocks(FlowGraph *fg)
{
bool breakBlockRelocated = false;
if (PHASE_OFF(Js::RemoveBreakBlockPhase, fg->GetFunc()))
{
return false;
}
BasicBlock *loopTailBlock = nullptr;
FOREACH_BLOCK_IN_LOOP(block, this)
{
loopTailBlock = block;
}NEXT_BLOCK_IN_LOOP;
AnalysisAssert(loopTailBlock);
FOREACH_BLOCK_BACKWARD_IN_RANGE_EDITING(breakBlockEnd, loopTailBlock, this->GetHeadBlock(), blockPrev)
{
while (!this->IsDescendentOrSelf(breakBlockEnd->loop))
{
// Found at least one break block;
breakBlockRelocated = true;
#if DBG
breakBlockEnd->isBreakBlock = true;
#endif
// Find the first block in this break block sequence.
BasicBlock *breakBlockStart = breakBlockEnd;
BasicBlock *breakBlockStartPrev = breakBlockEnd->GetPrev();
// Walk back the blocks until we find a block which belongs to that block.
// Note: We don't really care if there are break blocks corresponding to different loops. We move the blocks conservatively to the end of the loop.
// Algorithm works on one loop at a time.
while((breakBlockStartPrev->loop == breakBlockEnd->loop) || !this->IsDescendentOrSelf(breakBlockStartPrev->loop))
{
breakBlockStart = breakBlockStartPrev;
breakBlockStartPrev = breakBlockStartPrev->GetPrev();
}
#if DBG
breakBlockStart->isBreakBlock = true; // Mark the first block as well.
#endif
BasicBlock *exitLoopTail = loopTailBlock;
// Move these break blocks to the tail of the loop.
fg->MoveBlocksBefore(breakBlockStart, breakBlockEnd, exitLoopTail->next);
#if DBG_DUMP
fg->Dump(true /*needs verbose flag*/, _u("\n After Each iteration of canonicalization \n"));
#endif
// Again be conservative, there are edits to the loop graph. Start fresh for this loop.
breakBlockEnd = loopTailBlock;
blockPrev = breakBlockEnd->prev;
}
} NEXT_BLOCK_BACKWARD_IN_RANGE_EDITING;
return breakBlockRelocated;
}
void
FlowGraph::MoveBlocksBefore(BasicBlock *blockStart, BasicBlock *blockEnd, BasicBlock *insertBlock)
{
BasicBlock *srcPredBlock = blockStart->prev;
BasicBlock *srcNextBlock = blockEnd->next;
BasicBlock *dstPredBlock = insertBlock->prev;
IR::Instr* dstPredBlockLastInstr = dstPredBlock->GetLastInstr();
IR::Instr* blockEndLastInstr = blockEnd->GetLastInstr();
// Fix block linkage
srcPredBlock->next = srcNextBlock;
srcNextBlock->prev = srcPredBlock;
dstPredBlock->next = blockStart;
insertBlock->prev = blockEnd;
blockStart->prev = dstPredBlock;
blockEnd->next = insertBlock;
// Fix instruction linkage
IR::Instr::MoveRangeAfter(blockStart->GetFirstInstr(), blockEndLastInstr, dstPredBlockLastInstr);
// Fix instruction flow
IR::Instr *srcLastInstr = srcPredBlock->GetLastInstr();
if (srcLastInstr->IsBranchInstr() && srcLastInstr->AsBranchInstr()->HasFallThrough())
{
// There was a fallthrough in the break blocks original position.
IR::BranchInstr *srcBranch = srcLastInstr->AsBranchInstr();
IR::Instr *srcBranchNextInstr = srcBranch->GetNextRealInstrOrLabel();
// Save the target and invert the branch.
IR::LabelInstr *srcBranchTarget = srcBranch->GetTarget();
srcPredBlock->InvertBranch(srcBranch);
IR::LabelInstr *srcLabel = blockStart->GetFirstInstr()->AsLabelInstr();
// Point the inverted branch to break block.
srcBranch->SetTarget(srcLabel);
if (srcBranchNextInstr != srcBranchTarget)
{
FlowEdge *srcEdge = this->FindEdge(srcPredBlock, srcBranchTarget->GetBasicBlock());
Assert(srcEdge);
BasicBlock *compensationBlock = this->InsertCompensationCodeForBlockMove(srcEdge, true /*insert compensation block to loop list*/, false /*At source*/);
Assert(compensationBlock);
}
}
IR::Instr *dstLastInstr = dstPredBlockLastInstr;
if (dstLastInstr->IsBranchInstr() && dstLastInstr->AsBranchInstr()->HasFallThrough())
{
//There is a fallthrough in the block after which break block is inserted.
FlowEdge *dstEdge = this->FindEdge(dstPredBlock, blockEnd->GetNext());
Assert(dstEdge);
BasicBlock *compensationBlock = this->InsertCompensationCodeForBlockMove(dstEdge, true /*insert compensation block to loop list*/, true /*At sink*/);
Assert(compensationBlock);
}
}
FlowEdge *
FlowGraph::FindEdge(BasicBlock *predBlock, BasicBlock *succBlock)
{
FlowEdge *srcEdge = nullptr;
FOREACH_SUCCESSOR_EDGE(edge, predBlock)
{
if (edge->GetSucc() == succBlock)
{
srcEdge = edge;
break;
}
} NEXT_SUCCESSOR_EDGE;
return srcEdge;
}
void
BasicBlock::InvertBranch(IR::BranchInstr *branch)
{
Assert(this->GetLastInstr() == branch);
Assert(this->GetSuccList()->HasTwo());
branch->Invert();
this->GetSuccList()->Reverse();
}
bool
FlowGraph::CanonicalizeLoops()
{
if (this->func->HasProfileInfo())
{
this->implicitCallFlags = this->func->GetReadOnlyProfileInfo()->GetImplicitCallFlags();
for (Loop *loop = this->loopList; loop; loop = loop->next)
{
this->implicitCallFlags = (Js::ImplicitCallFlags)(this->implicitCallFlags | loop->GetImplicitCallFlags());
}
}
#if DBG_DUMP
this->Dump(true, _u("\n Before canonicalizeLoops \n"));
#endif
bool breakBlockRelocated = false;
for (Loop *loop = this->loopList; loop; loop = loop->next)
{
loop->InsertLandingPad(this);
if (!this->func->HasTry() || this->func->DoOptimizeTryCatch())
{
bool relocated = loop->RemoveBreakBlocks(this);
if (!breakBlockRelocated && relocated)
{
breakBlockRelocated = true;
}
}
}
#if DBG_DUMP
this->Dump(true, _u("\n After canonicalizeLoops \n"));
#endif
return breakBlockRelocated;
}
// Find the loops in this function, build the loop structure, and build a linked
// list of the basic blocks in this loop (including blocks of inner loops). The
// list preserves the reverse post-order of the blocks in the flowgraph block list.
void
FlowGraph::FindLoops()
{
if (!this->hasLoop)
{
return;
}
Func * func = this->func;
FOREACH_BLOCK_BACKWARD_IN_FUNC(block, func)
{
if (block->loop != nullptr)
{
// Block already visited
continue;
}
FOREACH_SUCCESSOR_BLOCK(succ, block)
{
if (succ->isLoopHeader && succ->loop == nullptr)
{
// Found a loop back-edge
BuildLoop(succ, block);
}
} NEXT_SUCCESSOR_BLOCK;
if (block->isLoopHeader && block->loop == nullptr)
{
// We would have built a loop for it if it was a loop...
block->isLoopHeader = false;
block->GetFirstInstr()->AsLabelInstr()->m_isLoopTop = false;
}
} NEXT_BLOCK_BACKWARD_IN_FUNC;
}
void
FlowGraph::BuildLoop(BasicBlock *headBlock, BasicBlock *tailBlock, Loop *parentLoop)
{
// This function is recursive, so when jitting in the foreground, probe the stack
if(!func->IsBackgroundJIT())
{
PROBE_STACK(func->GetScriptContext(), Js::Constants::MinStackDefault);
}
if (tailBlock->number < headBlock->number)
{
// Not a loop. We didn't see any back-edge.
headBlock->isLoopHeader = false;
headBlock->GetFirstInstr()->AsLabelInstr()->m_isLoopTop = false;
return;
}
Assert(headBlock->isLoopHeader);
Loop *loop = JitAnewZ(this->GetFunc()->m_alloc, Loop, this->GetFunc()->m_alloc, this->GetFunc());
loop->next = this->loopList;
this->loopList = loop;
headBlock->loop = loop;
loop->headBlock = headBlock;
loop->int32SymsOnEntry = nullptr;
loop->lossyInt32SymsOnEntry = nullptr;
// If parentLoop is a parent of loop, it's headBlock better appear first.
if (parentLoop && loop->headBlock->number > parentLoop->headBlock->number)
{
loop->parent = parentLoop;
parentLoop->isLeaf = false;
}
loop->hasDeadStoreCollectionPass = false;
loop->hasDeadStorePrepass = false;
loop->memOpInfo = nullptr;
loop->doMemOp = true;
NoRecoverMemoryJitArenaAllocator tempAlloc(_u("BE-LoopBuilder"), this->func->m_alloc->GetPageAllocator(), Js::Throw::OutOfMemory);
WalkLoopBlocks(tailBlock, loop, &tempAlloc);
Assert(loop->GetHeadBlock() == headBlock);
IR::LabelInstr * firstInstr = loop->GetLoopTopInstr();
firstInstr->SetLoop(loop);
if (firstInstr->IsProfiledLabelInstr())
{
loop->SetImplicitCallFlags(firstInstr->AsProfiledLabelInstr()->loopImplicitCallFlags);
if (this->func->HasProfileInfo() && this->func->GetReadOnlyProfileInfo()->IsLoopImplicitCallInfoDisabled())
{
loop->SetImplicitCallFlags(this->func->GetReadOnlyProfileInfo()->GetImplicitCallFlags());
}
loop->SetLoopFlags(firstInstr->AsProfiledLabelInstr()->loopFlags);
}
else
{
// Didn't collect profile information, don't do optimizations
loop->SetImplicitCallFlags(Js::ImplicitCall_All);
}
}
Loop::MemCopyCandidate* Loop::MemOpCandidate::AsMemCopy()
{
Assert(this->IsMemCopy());
return (Loop::MemCopyCandidate*)this;
}
Loop::MemSetCandidate* Loop::MemOpCandidate::AsMemSet()
{
Assert(this->IsMemSet());
return (Loop::MemSetCandidate*)this;
}
void
Loop::EnsureMemOpVariablesInitialized()
{
Assert(this->doMemOp);
if (this->memOpInfo == nullptr)
{
JitArenaAllocator *allocator = this->GetFunc()->GetTopFunc()->m_fg->alloc;
this->memOpInfo = JitAnewStruct(allocator, Loop::MemOpInfo);
this->memOpInfo->inductionVariablesUsedAfterLoop = nullptr;
this->memOpInfo->startIndexOpndCache[0] = nullptr;
this->memOpInfo->startIndexOpndCache[1] = nullptr;
this->memOpInfo->startIndexOpndCache[2] = nullptr;
this->memOpInfo->startIndexOpndCache[3] = nullptr;
this->memOpInfo->inductionVariableChangeInfoMap = JitAnew(allocator, Loop::InductionVariableChangeInfoMap, allocator);
this->memOpInfo->inductionVariableOpndPerUnrollMap = JitAnew(allocator, Loop::InductionVariableOpndPerUnrollMap, allocator);
this->memOpInfo->candidates = JitAnew(allocator, Loop::MemOpList, allocator);
}
}
// Walk the basic blocks backwards until we find the loop header.
// Mark basic blocks in the loop by looking at the predecessors
// of blocks known to be in the loop.
// Recurse on inner loops.
void
FlowGraph::WalkLoopBlocks(BasicBlock *block, Loop *loop, JitArenaAllocator *tempAlloc)
{
AnalysisAssert(loop);
BVSparse<JitArenaAllocator> *loopBlocksBv = JitAnew(tempAlloc, BVSparse<JitArenaAllocator>, tempAlloc);
BasicBlock *tailBlock = block;
BasicBlock *lastBlock;
loopBlocksBv->Set(block->GetBlockNum());
this->AddBlockToLoop(block, loop);
if (block == loop->headBlock)
{
// Single block loop, we're done
return;
}
do
{
BOOL isInLoop = loopBlocksBv->Test(block->GetBlockNum());
FOREACH_SUCCESSOR_BLOCK(succ, block)
{
if (succ->isLoopHeader)
{
// Found a loop back-edge
if (loop->headBlock == succ)
{
isInLoop = true;
}
else if (succ->loop == nullptr || succ->loop->headBlock != succ)
{
// Recurse on inner loop
BuildLoop(succ, block, isInLoop ? loop : nullptr);
}
}
} NEXT_SUCCESSOR_BLOCK;
if (isInLoop)
{
// This block is in the loop. All of it's predecessors should be contained in the loop as well.
FOREACH_PREDECESSOR_BLOCK(pred, block)
{
// Fix up loop parent if it isn't set already.
// If pred->loop != loop, we're looking at an inner loop, which was already visited.
// If pred->loop->parent == nullptr, this is the first time we see this loop from an outer
// loop, so this must be an immediate child.
if (pred->loop && pred->loop != loop && loop->headBlock->number < pred->loop->headBlock->number
&& (pred->loop->parent == nullptr || pred->loop->parent->headBlock->number < loop->headBlock->number))
{
pred->loop->parent = loop;
loop->isLeaf = false;
if (pred->loop->hasCall)
{
loop->SetHasCall();
}
loop->SetImplicitCallFlags(pred->loop->GetImplicitCallFlags());
}
// Add pred to loop bit vector
loopBlocksBv->Set(pred->GetBlockNum());
} NEXT_PREDECESSOR_BLOCK;
if (block->loop == nullptr || block->loop->IsDescendentOrSelf(loop))
{
block->loop = loop;
}
if (block != tailBlock)
{
this->AddBlockToLoop(block, loop);
}
}
lastBlock = block;
block = block->GetPrev();
} while (lastBlock != loop->headBlock);
}
// Add block to this loop, and it's parent loops.
void
FlowGraph::AddBlockToLoop(BasicBlock *block, Loop *loop)
{
loop->blockList.Prepend(block);
if (block->hasCall)
{
loop->SetHasCall();
}
}
///----------------------------------------------------------------------------
///
/// FlowGraph::AddBlock
///
/// Finish processing of a new block: hook up successor arcs, note loops, etc.
///
///----------------------------------------------------------------------------
BasicBlock *
FlowGraph::AddBlock(
IR::Instr * firstInstr,
IR::Instr * lastInstr,
BasicBlock * nextBlock)
{
BasicBlock * block;
IR::LabelInstr * labelInstr;
if (firstInstr->IsLabelInstr())
{
labelInstr = firstInstr->AsLabelInstr();
}
else
{
labelInstr = IR::LabelInstr::New(Js::OpCode::Label, firstInstr->m_func);
labelInstr->SetByteCodeOffset(firstInstr);
if (firstInstr->IsEntryInstr())
{
firstInstr->InsertAfter(labelInstr);
}
else
{
firstInstr->InsertBefore(labelInstr);
}
firstInstr = labelInstr;
}
block = labelInstr->GetBasicBlock();
if (block == nullptr)
{
block = BasicBlock::New(this);
labelInstr->SetBasicBlock(block);
// Remember last block in function to target successor of RETs.
if (!this->tailBlock)
{
this->tailBlock = block;
}
}
// Hook up the successor edges
if (lastInstr->EndsBasicBlock())
{
BasicBlock * blockTarget = nullptr;
if (lastInstr->IsBranchInstr())
{
// Hook up a successor edge to the branch target.
IR::BranchInstr * branchInstr = lastInstr->AsBranchInstr();
if(branchInstr->IsMultiBranch())
{
BasicBlock * blockMultiBrTarget;
IR::MultiBranchInstr * multiBranchInstr = branchInstr->AsMultiBrInstr();
multiBranchInstr->MapUniqueMultiBrLabels([&](IR::LabelInstr * labelInstr) -> void
{
blockMultiBrTarget = SetBlockTargetAndLoopFlag(labelInstr);
this->AddEdge(block, blockMultiBrTarget);
});
}
else
{
IR::LabelInstr * targetLabelInstr = branchInstr->GetTarget();
blockTarget = SetBlockTargetAndLoopFlag(targetLabelInstr);
if (branchInstr->IsConditional())
{
IR::Instr *instrNext = branchInstr->GetNextRealInstrOrLabel();
if (instrNext->IsLabelInstr())
{
SetBlockTargetAndLoopFlag(instrNext->AsLabelInstr());
}
}
}
}
else if (lastInstr->m_opcode == Js::OpCode::Ret && block != this->tailBlock)
{
blockTarget = this->tailBlock;
}
if (blockTarget)
{
this->AddEdge(block, blockTarget);
}
}
if (lastInstr->HasFallThrough())
{
// Add a branch to next instruction so that we don't have to update the flow graph