-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MachineBasicBlock] Fix use after free in SplitCriticalEdge #66188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,7 +681,7 @@ static int findJumpTableIndex(const MachineBasicBlock &MBB) { | |
} | ||
|
||
void MachineBasicBlock::updateTerminator( | ||
MachineBasicBlock *PreviousLayoutSuccessor) { | ||
MachineBasicBlock *PreviousLayoutSuccessor, SlotIndexes *Indexes) { | ||
LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this) | ||
<< "\n"); | ||
|
||
|
@@ -693,15 +693,15 @@ void MachineBasicBlock::updateTerminator( | |
MachineBasicBlock *TBB = nullptr, *FBB = nullptr; | ||
SmallVector<MachineOperand, 4> Cond; | ||
DebugLoc DL = findBranchDebugLoc(); | ||
bool B = TII->analyzeBranch(*this, TBB, FBB, Cond); | ||
bool B = TII->analyzeBranch(*this, TBB, FBB, Cond, /*AllowModify=*/false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is NFC, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is NFC, but I felt it was appropriate to add an explicit configuration of AllowModify=false here as SlotIndexes will be invalidated if analyzeBranch modifies the instructions. |
||
(void) B; | ||
assert(!B && "UpdateTerminators requires analyzable predecessors!"); | ||
if (Cond.empty()) { | ||
if (TBB) { | ||
// The block has an unconditional branch. If its successor is now its | ||
// layout successor, delete the branch. | ||
if (isLayoutSuccessor(TBB)) | ||
TII->removeBranch(*this); | ||
TII->removeBranch(*this, Indexes); | ||
} else { | ||
// The block has an unconditional fallthrough, or the end of the block is | ||
// unreachable. | ||
|
@@ -717,7 +717,8 @@ void MachineBasicBlock::updateTerminator( | |
// If the unconditional successor block is not the current layout | ||
// successor, insert a branch to jump to it. | ||
if (!isLayoutSuccessor(PreviousLayoutSuccessor)) | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL, | ||
Indexes); | ||
} | ||
return; | ||
} | ||
|
@@ -729,11 +730,11 @@ void MachineBasicBlock::updateTerminator( | |
if (isLayoutSuccessor(TBB)) { | ||
if (TII->reverseBranchCondition(Cond)) | ||
return; | ||
TII->removeBranch(*this); | ||
TII->insertBranch(*this, FBB, nullptr, Cond, DL); | ||
TII->removeBranch(*this, Indexes); | ||
TII->insertBranch(*this, FBB, nullptr, Cond, DL, Indexes); | ||
} else if (isLayoutSuccessor(FBB)) { | ||
TII->removeBranch(*this); | ||
TII->insertBranch(*this, TBB, nullptr, Cond, DL); | ||
TII->removeBranch(*this, Indexes); | ||
TII->insertBranch(*this, TBB, nullptr, Cond, DL, Indexes); | ||
} | ||
return; | ||
} | ||
|
@@ -747,10 +748,10 @@ void MachineBasicBlock::updateTerminator( | |
// We had a fallthrough to the same basic block as the conditional jump | ||
// targets. Remove the conditional jump, leaving an unconditional | ||
// fallthrough or an unconditional jump. | ||
TII->removeBranch(*this); | ||
TII->removeBranch(*this, Indexes); | ||
if (!isLayoutSuccessor(TBB)) { | ||
Cond.clear(); | ||
TII->insertBranch(*this, TBB, nullptr, Cond, DL); | ||
TII->insertBranch(*this, TBB, nullptr, Cond, DL, Indexes); | ||
} | ||
return; | ||
} | ||
|
@@ -760,14 +761,16 @@ void MachineBasicBlock::updateTerminator( | |
if (TII->reverseBranchCondition(Cond)) { | ||
// We can't reverse the condition, add an unconditional branch. | ||
Cond.clear(); | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL, | ||
Indexes); | ||
return; | ||
} | ||
TII->removeBranch(*this); | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); | ||
TII->removeBranch(*this, Indexes); | ||
TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL, | ||
Indexes); | ||
} else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) { | ||
TII->removeBranch(*this); | ||
TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL); | ||
TII->removeBranch(*this, Indexes); | ||
TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL, Indexes); | ||
} | ||
} | ||
|
||
|
@@ -1166,51 +1169,20 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge( | |
|
||
ReplaceUsesOfBlockWith(Succ, NMBB); | ||
|
||
// If updateTerminator() removes instructions, we need to remove them from | ||
// SlotIndexes. | ||
SmallVector<MachineInstr*, 4> Terminators; | ||
if (Indexes) { | ||
for (MachineInstr &MI : | ||
llvm::make_range(getFirstInstrTerminator(), instr_end())) | ||
Terminators.push_back(&MI); | ||
} | ||
|
||
// Since we replaced all uses of Succ with NMBB, that should also be treated | ||
// as the fallthrough successor | ||
if (Succ == PrevFallthrough) | ||
PrevFallthrough = NMBB; | ||
|
||
if (!ChangedIndirectJump) | ||
updateTerminator(PrevFallthrough); | ||
|
||
if (Indexes) { | ||
SmallVector<MachineInstr*, 4> NewTerminators; | ||
for (MachineInstr &MI : | ||
llvm::make_range(getFirstInstrTerminator(), instr_end())) | ||
NewTerminators.push_back(&MI); | ||
|
||
for (MachineInstr *Terminator : Terminators) { | ||
if (!is_contained(NewTerminators, Terminator)) | ||
Indexes->removeMachineInstrFromMaps(*Terminator); | ||
} | ||
} | ||
updateTerminator(PrevFallthrough, Indexes); | ||
|
||
// Insert unconditional "jump Succ" instruction in NMBB if necessary. | ||
NMBB->addSuccessor(Succ); | ||
if (!NMBB->isLayoutSuccessor(Succ)) { | ||
SmallVector<MachineOperand, 4> Cond; | ||
const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); | ||
TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL); | ||
|
||
if (Indexes) { | ||
for (MachineInstr &MI : NMBB->instrs()) { | ||
// Some instructions may have been moved to NMBB by updateTerminator(), | ||
// so we first remove any instruction that already has an index. | ||
if (Indexes->hasIndex(MI)) | ||
Indexes->removeMachineInstrFromMaps(MI); | ||
Indexes->insertMachineInstrInMaps(MI); | ||
} | ||
} | ||
TII->insertBranch(*NMBB, Succ, nullptr, Cond, DL, Indexes); | ||
} | ||
|
||
// Fix PHI nodes in Succ so they refer to NMBB instead of this. | ||
|
Uh oh!
There was an error while loading. Please reload this page.