@@ -71,10 +71,15 @@ namespace {
71
71
MachineBasicBlock *NewDest);
72
72
MachineBasicBlock *SplitMBBAt (MachineBasicBlock &CurMBB,
73
73
MachineBasicBlock::iterator BBI1);
74
+ unsigned ComputeSameTails (unsigned CurHash, unsigned minCommonTailLength);
75
+ void RemoveBlocksWithHash (unsigned CurHash, MachineBasicBlock* SuccBB,
76
+ MachineBasicBlock* PredBB);
74
77
75
78
typedef std::pair<unsigned ,MachineBasicBlock*> MergePotentialsElt;
76
- std::vector<MergePotentialsElt> MergePotentials;
77
79
typedef std::vector<MergePotentialsElt>::iterator MPIterator;
80
+ std::vector<MergePotentialsElt> MergePotentials;
81
+ typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt;
82
+ std::vector<SameTailElt> SameTails;
78
83
79
84
const TargetRegisterInfo *RegInfo;
80
85
RegScavenger *RS;
@@ -221,8 +226,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
221
226
// If a jump table was merge with another one, walk the function rewriting
222
227
// references to jump tables to reference the new JT ID's. Keep track of
223
228
// whether we see a jump table idx, if not, we can delete the JT.
224
- std::vector<bool > JTIsLive;
225
- JTIsLive.resize (JTs.size ());
229
+ BitVector JTIsLive (JTs.size ());
226
230
for (MachineFunction::iterator BB = MF.begin (), E = MF.end ();
227
231
BB != E; ++BB) {
228
232
for (MachineBasicBlock::iterator I = BB->begin (), E = BB->end ();
@@ -234,15 +238,15 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
234
238
Op.setIndex (NewIdx);
235
239
236
240
// Remember that this JT is live.
237
- JTIsLive[ NewIdx] = true ;
241
+ JTIsLive. set ( NewIdx) ;
238
242
}
239
243
}
240
244
241
245
// Finally, remove dead jump tables. This happens either because the
242
246
// indirect jump was unreachable (and thus deleted) or because the jump
243
247
// table was merged with some other one.
244
248
for (unsigned i = 0 , e = JTIsLive.size (); i != e; ++i)
245
- if (!JTIsLive[i] ) {
249
+ if (!JTIsLive. test (i) ) {
246
250
JTI->RemoveJumpTable (i);
247
251
EverMadeChange = true ;
248
252
}
@@ -468,7 +472,7 @@ static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB,
468
472
if (I != MF->end () &&
469
473
!TII->AnalyzeBranch (*CurMBB, TBB, FBB, Cond)) {
470
474
MachineBasicBlock *NextBB = I;
471
- if (TBB == NextBB && Cond.size () && !FBB) {
475
+ if (TBB == NextBB && ! Cond.empty () && !FBB) {
472
476
if (!TII->ReverseBranchCondition (Cond)) {
473
477
TII->RemoveBranch (*CurMBB);
474
478
TII->InsertBranch (*CurMBB, SuccBB, NULL , Cond);
@@ -499,6 +503,68 @@ static bool MergeCompare(const std::pair<unsigned,MachineBasicBlock*> &p,
499
503
}
500
504
}
501
505
506
+ // / ComputeSameTails - Look through all the blocks in MergePotentials that have
507
+ // / hash CurHash (guaranteed to match the last element). Build the vector
508
+ // / SameTails of all those that have the (same) largest number of instructions
509
+ // / in common of any pair of these blocks. SameTails entries contain an
510
+ // / iterator into MergePotentials (from which the MachineBasicBlock can be
511
+ // / found) and a MachineBasicBlock::iterator into that MBB indicating the
512
+ // / instruction where the matching code sequence begins.
513
+ // / Order of elements in SameTails is the reverse of the order in which
514
+ // / those blocks appear in MergePotentials (where they are not necessarily
515
+ // / consecutive).
516
+ unsigned BranchFolder::ComputeSameTails (unsigned CurHash,
517
+ unsigned minCommonTailLength) {
518
+ unsigned maxCommonTailLength = 0U ;
519
+ SameTails.clear ();
520
+ MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
521
+ MPIterator HighestMPIter = prior (MergePotentials.end ());
522
+ for (MPIterator CurMPIter = prior (MergePotentials.end ()),
523
+ B = MergePotentials.begin ();
524
+ CurMPIter!=B && CurMPIter->first ==CurHash;
525
+ --CurMPIter) {
526
+ for (MPIterator I = prior (CurMPIter); I->first ==CurHash ; --I) {
527
+ unsigned CommonTailLen = ComputeCommonTailLength (
528
+ CurMPIter->second ,
529
+ I->second ,
530
+ TrialBBI1, TrialBBI2);
531
+ if (CommonTailLen >= minCommonTailLength) {
532
+ if (CommonTailLen > maxCommonTailLength) {
533
+ SameTails.clear ();
534
+ maxCommonTailLength = CommonTailLen;
535
+ HighestMPIter = CurMPIter;
536
+ SameTails.push_back (std::make_pair (CurMPIter, TrialBBI1));
537
+ }
538
+ if (HighestMPIter == CurMPIter &&
539
+ CommonTailLen == maxCommonTailLength)
540
+ SameTails.push_back (std::make_pair (I, TrialBBI2));
541
+ }
542
+ if (I==B)
543
+ break ;
544
+ }
545
+ }
546
+ return maxCommonTailLength;
547
+ }
548
+
549
+ // / RemoveBlocksWithHash - Remove all blocks with hash CurHash from
550
+ // / MergePotentials, restoring branches at ends of blocks as appropriate.
551
+ void BranchFolder::RemoveBlocksWithHash (unsigned CurHash,
552
+ MachineBasicBlock* SuccBB,
553
+ MachineBasicBlock* PredBB) {
554
+ for (MPIterator CurMPIter = prior (MergePotentials.end ()),
555
+ B = MergePotentials.begin ();
556
+ CurMPIter->first ==CurHash;
557
+ --CurMPIter) {
558
+ // Put the unconditional branch back, if we need one.
559
+ MachineBasicBlock *CurMBB = CurMPIter->second ;
560
+ if (SuccBB && CurMBB != PredBB)
561
+ FixTail (CurMBB, SuccBB, TII);
562
+ MergePotentials.erase (CurMPIter);
563
+ if (CurMPIter==B)
564
+ break ;
565
+ }
566
+ }
567
+
502
568
// See if any of the blocks in MergePotentials (which all have a common single
503
569
// successor, or all have no successor) can be tail-merged. If there is a
504
570
// successor, any blocks in MergePotentials that are not tail-merged and
@@ -520,67 +586,24 @@ bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
520
586
MadeChange = false ;
521
587
522
588
DOUT << " \n TryMergeBlocks " << MergePotentials.size ();
589
+
523
590
// Sort by hash value so that blocks with identical end sequences sort
524
591
// together.
525
- std::stable_sort (MergePotentials.begin (), MergePotentials.end (), MergeCompare);
592
+ std::stable_sort (MergePotentials.begin (), MergePotentials.end (),MergeCompare);
526
593
527
594
// Walk through equivalence sets looking for actual exact matches.
528
595
while (MergePotentials.size () > 1 ) {
529
596
unsigned CurHash = prior (MergePotentials.end ())->first ;
530
597
531
- // Look through all the other blocks that have the same hash as this
532
- // one, and build a vector of all those that have the (same) largest number
533
- // of instructions in common.
534
- // Order of elements in SameTails is the reverse of the order in which
535
- // those blocks appear in MergePotentials (where they are not necessarily
536
- // consecutive).
537
- typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt;
538
- std::vector<SameTailElt> SameTails;
539
-
540
- unsigned maxCommonTailLength = 0U ;
541
- SameTails.clear ();
542
- MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
543
- MPIterator HighestMPIter = prior (MergePotentials.end ());
544
- for (MPIterator CurMPIter = prior (MergePotentials.end ()),
545
- B = MergePotentials.begin ();
546
- CurMPIter!=B && CurMPIter->first ==CurHash;
547
- --CurMPIter) {
548
- for (MPIterator I = prior (CurMPIter); I->first ==CurHash ; --I) {
549
- unsigned CommonTailLen = ComputeCommonTailLength (
550
- CurMPIter->second ,
551
- I->second ,
552
- TrialBBI1, TrialBBI2);
553
- if (CommonTailLen >= minCommonTailLength) {
554
- if (CommonTailLen > maxCommonTailLength) {
555
- SameTails.clear ();
556
- maxCommonTailLength = CommonTailLen;
557
- HighestMPIter = CurMPIter;
558
- SameTails.push_back (std::make_pair (CurMPIter, TrialBBI1));
559
- }
560
- if (HighestMPIter == CurMPIter &&
561
- CommonTailLen == maxCommonTailLength)
562
- SameTails.push_back (std::make_pair (I, TrialBBI2));
563
- }
564
- if (I==B)
565
- break ;
566
- }
567
- }
598
+ // Build SameTails, identifying the set of blocks with this hash code
599
+ // and with the maximum number of instructions in common.
600
+ unsigned maxCommonTailLength = ComputeSameTails (CurHash,
601
+ minCommonTailLength);
568
602
569
603
// If we didn't find any pair that has at least minCommonTailLength
570
604
// instructions in common, remove all blocks with this hash code and retry.
571
605
if (SameTails.empty ()) {
572
- for (MPIterator CurMPIter = prior (MergePotentials.end ()),
573
- B = MergePotentials.begin ();
574
- CurMPIter->first ==CurHash;
575
- --CurMPIter) {
576
- // Put the unconditional branch back, if we need one.
577
- MachineBasicBlock *CurMBB = CurMPIter->second ;
578
- if (SuccBB && CurMBB != PredBB)
579
- FixTail (CurMBB, SuccBB, TII);
580
- MergePotentials.erase (CurMPIter);
581
- if (CurMPIter==B)
582
- break ;
583
- }
606
+ RemoveBlocksWithHash (CurHash, SuccBB, PredBB);
584
607
continue ;
585
608
}
586
609
@@ -629,9 +652,9 @@ bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
629
652
// entry block, so if one block is the entry block, split the other one.
630
653
631
654
// The second half of the split block will remain in SameTails, and will
632
- // consist entirely of common code. Thus in the case where there are multiple
633
- // blocks that would all need to be split, the next iteration of the
634
- // outer loop will handle all the rest of them.
655
+ // consist entirely of common code. Thus in the case where there are
656
+ // multiple blocks that would all need to be split, the next iteration of
657
+ // the outer loop will handle all the rest of them.
635
658
636
659
// Decide whether we want to split MBB1 or MBB2.
637
660
if (ShouldSplitFirstBlock (MBB1, BBI1, MBB2, BBI2, PredBB)) {
@@ -717,7 +740,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
717
740
// Failing case: IBB is the target of a cbr, and
718
741
// we cannot reverse the branch.
719
742
std::vector<MachineOperand> NewCond (Cond);
720
- if (Cond.size () && TBB==IBB) {
743
+ if (! Cond.empty () && TBB==IBB) {
721
744
if (TII->ReverseBranchCondition (NewCond))
722
745
continue ;
723
746
// This is the QBB case described above
@@ -747,9 +770,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
747
770
}
748
771
}
749
772
// Remove the unconditional branch at the end, if any.
750
- if (TBB && (Cond.size ()== 0 || FBB)) {
773
+ if (TBB && (Cond.empty () || FBB)) {
751
774
TII->RemoveBranch (*PBB);
752
- if (Cond.size ())
775
+ if (! Cond.empty ())
753
776
// reinsert conditional branch only, for now
754
777
TII->InsertBranch (*PBB, (TBB==IBB) ? FBB : TBB, 0 , NewCond);
755
778
}
0 commit comments