Skip to content

Commit 66da8b5

Browse files
author
Dale Johannesen
committed
Remove an evil vector bool. Cosmetic refactoring,
no functional change. llvm-svn: 50921
1 parent 664ef65 commit 66da8b5

File tree

1 file changed

+85
-62
lines changed

1 file changed

+85
-62
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ namespace {
7171
MachineBasicBlock *NewDest);
7272
MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
7373
MachineBasicBlock::iterator BBI1);
74+
unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength);
75+
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
76+
MachineBasicBlock* PredBB);
7477

7578
typedef std::pair<unsigned,MachineBasicBlock*> MergePotentialsElt;
76-
std::vector<MergePotentialsElt> MergePotentials;
7779
typedef std::vector<MergePotentialsElt>::iterator MPIterator;
80+
std::vector<MergePotentialsElt> MergePotentials;
81+
typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt;
82+
std::vector<SameTailElt> SameTails;
7883

7984
const TargetRegisterInfo *RegInfo;
8085
RegScavenger *RS;
@@ -221,8 +226,7 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
221226
// If a jump table was merge with another one, walk the function rewriting
222227
// references to jump tables to reference the new JT ID's. Keep track of
223228
// 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());
226230
for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
227231
BB != E; ++BB) {
228232
for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
@@ -234,15 +238,15 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
234238
Op.setIndex(NewIdx);
235239

236240
// Remember that this JT is live.
237-
JTIsLive[NewIdx] = true;
241+
JTIsLive.set(NewIdx);
238242
}
239243
}
240244

241245
// Finally, remove dead jump tables. This happens either because the
242246
// indirect jump was unreachable (and thus deleted) or because the jump
243247
// table was merged with some other one.
244248
for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
245-
if (!JTIsLive[i]) {
249+
if (!JTIsLive.test(i)) {
246250
JTI->RemoveJumpTable(i);
247251
EverMadeChange = true;
248252
}
@@ -468,7 +472,7 @@ static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB,
468472
if (I != MF->end() &&
469473
!TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond)) {
470474
MachineBasicBlock *NextBB = I;
471-
if (TBB == NextBB && Cond.size() && !FBB) {
475+
if (TBB == NextBB && !Cond.empty() && !FBB) {
472476
if (!TII->ReverseBranchCondition(Cond)) {
473477
TII->RemoveBranch(*CurMBB);
474478
TII->InsertBranch(*CurMBB, SuccBB, NULL, Cond);
@@ -499,6 +503,68 @@ static bool MergeCompare(const std::pair<unsigned,MachineBasicBlock*> &p,
499503
}
500504
}
501505

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+
502568
// See if any of the blocks in MergePotentials (which all have a common single
503569
// successor, or all have no successor) can be tail-merged. If there is a
504570
// successor, any blocks in MergePotentials that are not tail-merged and
@@ -520,67 +586,24 @@ bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
520586
MadeChange = false;
521587

522588
DOUT << "\nTryMergeBlocks " << MergePotentials.size();
589+
523590
// Sort by hash value so that blocks with identical end sequences sort
524591
// together.
525-
std::stable_sort(MergePotentials.begin(), MergePotentials.end(), MergeCompare);
592+
std::stable_sort(MergePotentials.begin(), MergePotentials.end(),MergeCompare);
526593

527594
// Walk through equivalence sets looking for actual exact matches.
528595
while (MergePotentials.size() > 1) {
529596
unsigned CurHash = prior(MergePotentials.end())->first;
530597

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);
568602

569603
// If we didn't find any pair that has at least minCommonTailLength
570604
// instructions in common, remove all blocks with this hash code and retry.
571605
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);
584607
continue;
585608
}
586609

@@ -629,9 +652,9 @@ bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
629652
// entry block, so if one block is the entry block, split the other one.
630653

631654
// 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.
635658

636659
// Decide whether we want to split MBB1 or MBB2.
637660
if (ShouldSplitFirstBlock(MBB1, BBI1, MBB2, BBI2, PredBB)) {
@@ -717,7 +740,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
717740
// Failing case: IBB is the target of a cbr, and
718741
// we cannot reverse the branch.
719742
std::vector<MachineOperand> NewCond(Cond);
720-
if (Cond.size() && TBB==IBB) {
743+
if (!Cond.empty() && TBB==IBB) {
721744
if (TII->ReverseBranchCondition(NewCond))
722745
continue;
723746
// This is the QBB case described above
@@ -747,9 +770,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
747770
}
748771
}
749772
// Remove the unconditional branch at the end, if any.
750-
if (TBB && (Cond.size()==0 || FBB)) {
773+
if (TBB && (Cond.empty() || FBB)) {
751774
TII->RemoveBranch(*PBB);
752-
if (Cond.size())
775+
if (!Cond.empty())
753776
// reinsert conditional branch only, for now
754777
TII->InsertBranch(*PBB, (TBB==IBB) ? FBB : TBB, 0, NewCond);
755778
}

0 commit comments

Comments
 (0)