@@ -181,8 +181,8 @@ class SSAIfConv {
181
181
bool canConvertIf (MachineBasicBlock *MBB, bool Predicate = false );
182
182
183
183
// / convertIf - If-convert the last block passed to canConvertIf(), assuming
184
- // / it is possible. Add any erased blocks to RemovedBlocks .
185
- void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks ,
184
+ // / it is possible. Add any blocks that are to be erased to RemoveBlocks .
185
+ void convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks ,
186
186
bool Predicate = false );
187
187
};
188
188
} // end anonymous namespace
@@ -678,9 +678,9 @@ void SSAIfConv::rewritePHIOperands() {
678
678
// / convertIf - Execute the if conversion after canConvertIf has determined the
679
679
// / feasibility.
680
680
// /
681
- // / Any basic blocks erased will be added to RemovedBlocks .
681
+ // / Any basic blocks that need to be erased will be added to RemoveBlocks .
682
682
// /
683
- void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks ,
683
+ void SSAIfConv::convertIf (SmallVectorImpl<MachineBasicBlock *> &RemoveBlocks ,
684
684
bool Predicate) {
685
685
assert (Head && Tail && TBB && FBB && " Call canConvertIf first." );
686
686
@@ -721,15 +721,18 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
721
721
DebugLoc HeadDL = Head->getFirstTerminator ()->getDebugLoc ();
722
722
TII->removeBranch (*Head);
723
723
724
- // Erase the now empty conditional blocks. It is likely that Head can fall
724
+ // Mark the now empty conditional blocks for removal and move them to the end.
725
+ // It is likely that Head can fall
725
726
// through to Tail, and we can join the two blocks.
726
727
if (TBB != Tail) {
727
- RemovedBlocks.push_back (TBB);
728
- TBB->eraseFromParent ();
728
+ RemoveBlocks.push_back (TBB);
729
+ if (TBB != &TBB->getParent ()->back ())
730
+ TBB->moveAfter (&TBB->getParent ()->back ());
729
731
}
730
732
if (FBB != Tail) {
731
- RemovedBlocks.push_back (FBB);
732
- FBB->eraseFromParent ();
733
+ RemoveBlocks.push_back (FBB);
734
+ if (FBB != &FBB->getParent ()->back ())
735
+ FBB->moveAfter (&FBB->getParent ()->back ());
733
736
}
734
737
735
738
assert (Head->succ_empty () && " Additional head successors?" );
@@ -740,8 +743,9 @@ void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
740
743
Head->splice (Head->end (), Tail,
741
744
Tail->begin (), Tail->end ());
742
745
Head->transferSuccessorsAndUpdatePHIs (Tail);
743
- RemovedBlocks.push_back (Tail);
744
- Tail->eraseFromParent ();
746
+ RemoveBlocks.push_back (Tail);
747
+ if (Tail != &Tail->getParent ()->back ())
748
+ Tail->moveAfter (&Tail->getParent ()->back ());
745
749
} else {
746
750
// We need a branch to Tail, let code placement work it out later.
747
751
LLVM_DEBUG (dbgs () << " Converting to unconditional branch.\n " );
@@ -1062,11 +1066,13 @@ bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
1062
1066
while (IfConv.canConvertIf (MBB) && shouldConvertIf ()) {
1063
1067
// If-convert MBB and update analyses.
1064
1068
invalidateTraces ();
1065
- SmallVector<MachineBasicBlock*, 4 > RemovedBlocks ;
1066
- IfConv.convertIf (RemovedBlocks );
1069
+ SmallVector<MachineBasicBlock *, 4 > RemoveBlocks ;
1070
+ IfConv.convertIf (RemoveBlocks );
1067
1071
Changed = true ;
1068
- updateDomTree (DomTree, IfConv, RemovedBlocks);
1069
- updateLoops (Loops, RemovedBlocks);
1072
+ updateDomTree (DomTree, IfConv, RemoveBlocks);
1073
+ for (MachineBasicBlock *MBB : RemoveBlocks)
1074
+ MBB->eraseFromParent ();
1075
+ updateLoops (Loops, RemoveBlocks);
1070
1076
}
1071
1077
return Changed;
1072
1078
}
@@ -1200,11 +1206,13 @@ bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
1200
1206
bool Changed = false ;
1201
1207
while (IfConv.canConvertIf (MBB, /* Predicate*/ true ) && shouldConvertIf ()) {
1202
1208
// If-convert MBB and update analyses.
1203
- SmallVector<MachineBasicBlock *, 4 > RemovedBlocks ;
1204
- IfConv.convertIf (RemovedBlocks , /* Predicate*/ true );
1209
+ SmallVector<MachineBasicBlock *, 4 > RemoveBlocks ;
1210
+ IfConv.convertIf (RemoveBlocks , /* Predicate*/ true );
1205
1211
Changed = true ;
1206
- updateDomTree (DomTree, IfConv, RemovedBlocks);
1207
- updateLoops (Loops, RemovedBlocks);
1212
+ updateDomTree (DomTree, IfConv, RemoveBlocks);
1213
+ for (MachineBasicBlock *MBB : RemoveBlocks)
1214
+ MBB->eraseFromParent ();
1215
+ updateLoops (Loops, RemoveBlocks);
1208
1216
}
1209
1217
return Changed;
1210
1218
}
0 commit comments