@@ -72,8 +72,8 @@ class BranchRelaxation : public MachineFunctionPass {
72
72
if (Alignment <= ParentAlign)
73
73
return alignTo (PO, Alignment);
74
74
75
- // The alignment of this MBB is larger than the function's alignment, so we
76
- // can't tell whether or not it will insert nops. Assume that it will.
75
+ // The alignment of this MBB is larger than the function's alignment, so
76
+ // we can't tell whether or not it will insert nops. Assume that it will.
77
77
return alignTo (PO, Alignment) + Alignment.value () - ParentAlign.value ();
78
78
}
79
79
};
@@ -103,7 +103,10 @@ class BranchRelaxation : public MachineFunctionPass {
103
103
MachineBasicBlock *splitBlockBeforeInstr (MachineInstr &MI,
104
104
MachineBasicBlock *DestBB);
105
105
void adjustBlockOffsets (MachineBasicBlock &Start);
106
- bool isBlockInRange (const MachineInstr &MI, const MachineBasicBlock &BB) const ;
106
+ void adjustBlockOffsets (MachineBasicBlock &Start,
107
+ MachineFunction::iterator End);
108
+ bool isBlockInRange (const MachineInstr &MI,
109
+ const MachineBasicBlock &BB) const ;
107
110
108
111
bool fixupConditionalBranch (MachineInstr &MI);
109
112
bool fixupUnconditionalBranch (MachineInstr &MI);
@@ -199,7 +202,8 @@ void BranchRelaxation::scanFunction() {
199
202
}
200
203
201
204
// / computeBlockSize - Compute the size for MBB.
202
- uint64_t BranchRelaxation::computeBlockSize (const MachineBasicBlock &MBB) const {
205
+ uint64_t
206
+ BranchRelaxation::computeBlockSize (const MachineBasicBlock &MBB) const {
203
207
uint64_t Size = 0 ;
204
208
for (const MachineInstr &MI : MBB)
205
209
Size += TII->getInstSizeInBytes (MI);
@@ -227,9 +231,14 @@ unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
227
231
}
228
232
229
233
void BranchRelaxation::adjustBlockOffsets (MachineBasicBlock &Start) {
234
+ adjustBlockOffsets (Start, MF->end ());
235
+ }
236
+
237
+ void BranchRelaxation::adjustBlockOffsets (MachineBasicBlock &Start,
238
+ MachineFunction::iterator End) {
230
239
unsigned PrevNum = Start.getNumber ();
231
240
for (auto &MBB :
232
- make_range (std::next (MachineFunction::iterator (Start)), MF-> end () )) {
241
+ make_range (std::next (MachineFunction::iterator (Start)), End )) {
233
242
unsigned Num = MBB.getNumber ();
234
243
// Get the offset and known bits at the end of the layout predecessor.
235
244
// Include the alignment of the current block.
@@ -314,8 +323,8 @@ BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI,
314
323
// block, it may contain a tablejump.
315
324
BlockInfo[NewBB->getNumber ()].Size = computeBlockSize (*NewBB);
316
325
317
- // All BBOffsets following these blocks must be modified .
318
- adjustBlockOffsets (*OrigBB);
326
+ // Update the offset of the new block .
327
+ adjustBlockOffsets (*OrigBB, std::next (NewBB-> getIterator ()) );
319
328
320
329
// Need to fix live-in lists if we track liveness.
321
330
if (TRI->trackLivenessAfterRegAlloc (*MF))
@@ -328,8 +337,8 @@ BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI,
328
337
329
338
// / isBlockInRange - Returns true if the distance between specific MI and
330
339
// / specific BB can fit in MI's displacement field.
331
- bool BranchRelaxation::isBlockInRange (
332
- const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
340
+ bool BranchRelaxation::isBlockInRange (const MachineInstr &MI,
341
+ const MachineBasicBlock &DestBB) const {
333
342
int64_t BrOffset = getInstrOffset (MI);
334
343
int64_t DestOffset = BlockInfo[DestBB.getNumber ()].Offset ;
335
344
@@ -369,7 +378,7 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
369
378
};
370
379
auto insertBranch = [&](MachineBasicBlock *MBB, MachineBasicBlock *TBB,
371
380
MachineBasicBlock *FBB,
372
- SmallVectorImpl<MachineOperand>& Cond) {
381
+ SmallVectorImpl<MachineOperand> & Cond) {
373
382
unsigned &BBSize = BlockInfo[MBB->getNumber ()].Size ;
374
383
int NewBrSize = 0 ;
375
384
TII->insertBranch (*MBB, TBB, FBB, Cond, DL, &NewBrSize);
@@ -382,13 +391,18 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
382
391
BBSize -= RemovedSize;
383
392
};
384
393
385
- auto finalizeBlockChanges = [&](MachineBasicBlock *MBB,
386
- MachineBasicBlock *NewBB) {
387
- // Keep the block offsets up to date.
388
- adjustBlockOffsets (*MBB);
394
+ // Populate the block offset and live-ins for a new basic block.
395
+ auto updateOffsetAndLiveness = [&](MachineBasicBlock *NewBB) {
396
+ assert (NewBB != nullptr && " can't populate offset for nullptr" );
397
+
398
+ // Keep the block offsets approximately up to date. While they will be
399
+ // slight underestimates, we will update them appropriately in the next
400
+ // scan through the function.
401
+ adjustBlockOffsets (*std::prev (NewBB->getIterator ()),
402
+ std::next (NewBB->getIterator ()));
389
403
390
404
// Need to fix live-in lists if we track liveness.
391
- if (NewBB && TRI->trackLivenessAfterRegAlloc (*MF))
405
+ if (TRI->trackLivenessAfterRegAlloc (*MF))
392
406
computeAndAddLiveIns (LiveRegs, *NewBB);
393
407
};
394
408
@@ -428,7 +442,7 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
428
442
insertBranch (MBB, NewBB, FBB, Cond);
429
443
430
444
TrampolineInsertionPoint = NewBB;
431
- finalizeBlockChanges (MBB, NewBB);
445
+ updateOffsetAndLiveness ( NewBB);
432
446
return true ;
433
447
}
434
448
@@ -438,6 +452,7 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
438
452
<< " .\n " );
439
453
TrampolineInsertionPoint->setIsEndSection (NewBB->isEndSection ());
440
454
MF->erase (NewBB);
455
+ NewBB = nullptr ;
441
456
}
442
457
443
458
// Add an unconditional branch to the destination and invert the branch
@@ -464,7 +479,6 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
464
479
465
480
removeBranch (MBB);
466
481
insertBranch (MBB, FBB, TBB, Cond);
467
- finalizeBlockChanges (MBB, nullptr );
468
482
return true ;
469
483
}
470
484
if (FBB) {
@@ -477,10 +491,11 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
477
491
// Do it here since if there's no split, no update is needed.
478
492
MBB->replaceSuccessor (FBB, NewBB);
479
493
NewBB->addSuccessor (FBB);
494
+ updateOffsetAndLiveness (NewBB);
480
495
}
481
496
482
- // We now have an appropriate fall-through block in place (either naturally or
483
- // just created), so we can use the inverted the condition.
497
+ // We now have an appropriate fall-through block in place (either naturally
498
+ // or just created), so we can use the inverted the condition.
484
499
MachineBasicBlock &NextBB = *std::next (MachineFunction::iterator (MBB));
485
500
486
501
LLVM_DEBUG (dbgs () << " Insert B to " << printMBBReference (*TBB)
@@ -490,8 +505,6 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
490
505
removeBranch (MBB);
491
506
// Insert a new conditional branch and a new unconditional branch.
492
507
insertBranch (MBB, &NextBB, TBB, Cond);
493
-
494
- finalizeBlockChanges (MBB, NewBB);
495
508
return true ;
496
509
}
497
510
// Branch cond can't be inverted.
@@ -531,7 +544,7 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
531
544
removeBranch (MBB);
532
545
insertBranch (MBB, NewBB, FBB, Cond);
533
546
534
- finalizeBlockChanges (MBB, NewBB);
547
+ updateOffsetAndLiveness ( NewBB);
535
548
return true ;
536
549
}
537
550
@@ -577,8 +590,8 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
577
590
// Create the optional restore block and, initially, place it at the end of
578
591
// function. That block will be placed later if it's used; otherwise, it will
579
592
// be erased.
580
- MachineBasicBlock *RestoreBB = createNewBlockAfter (MF-> back (),
581
- DestBB->getBasicBlock ());
593
+ MachineBasicBlock *RestoreBB =
594
+ createNewBlockAfter (MF-> back (), DestBB->getBasicBlock ());
582
595
std::prev (RestoreBB->getIterator ())
583
596
->setIsEndSection (RestoreBB->isEndSection ());
584
597
RestoreBB->setIsEndSection (false );
@@ -589,8 +602,10 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
589
602
: DestOffset - SrcOffset,
590
603
RS.get ());
591
604
605
+ // Update the block size and offset for the BranchBB (which may be newly
606
+ // created).
592
607
BlockInfo[BranchBB->getNumber ()].Size = computeBlockSize (*BranchBB);
593
- adjustBlockOffsets (*MBB);
608
+ adjustBlockOffsets (*MBB, std::next (BranchBB-> getIterator ()) );
594
609
595
610
// If RestoreBB is required, place it appropriately.
596
611
if (!RestoreBB->empty ()) {
@@ -601,6 +616,8 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
601
616
MachineBasicBlock *NewBB = createNewBlockAfter (*TrampolineInsertionPoint);
602
617
TII->insertUnconditionalBranch (*NewBB, DestBB, DebugLoc ());
603
618
BlockInfo[NewBB->getNumber ()].Size = computeBlockSize (*NewBB);
619
+ adjustBlockOffsets (*TrampolineInsertionPoint,
620
+ std::next (NewBB->getIterator ()));
604
621
605
622
// New trampolines should be inserted after NewBB.
606
623
TrampolineInsertionPoint = NewBB;
@@ -636,8 +653,8 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
636
653
computeAndAddLiveIns (LiveRegs, *RestoreBB);
637
654
// Compute the restore block size.
638
655
BlockInfo[RestoreBB->getNumber ()].Size = computeBlockSize (*RestoreBB);
639
- // Update the offset starting from the previous block.
640
- adjustBlockOffsets (*PrevBB);
656
+ // Update the estimated offset for the restore block.
657
+ adjustBlockOffsets (*PrevBB, DestBB-> getIterator () );
641
658
642
659
// Fix up section information for RestoreBB and DestBB
643
660
RestoreBB->setSectionID (DestBB->getSectionID ());
@@ -718,6 +735,12 @@ bool BranchRelaxation::relaxBranchInstructions() {
718
735
}
719
736
}
720
737
738
+ // If we relaxed a branch, we must recompute offsets for *all* basic blocks.
739
+ // Otherwise, we may underestimate branch distances and fail to relax a branch
740
+ // that has been pushed out of range.
741
+ if (Changed)
742
+ adjustBlockOffsets (MF->front ());
743
+
721
744
return Changed;
722
745
}
723
746
0 commit comments