Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions llvm/include/llvm/SandboxIR/Tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,15 @@ class SwitchAddCase : public IRChangeBase {

class SwitchRemoveCase : public IRChangeBase {
SwitchInst *Switch;
unsigned Index;
ConstantInt *Val;
BasicBlock *Dest;
struct Case {
ConstantInt *Val;
BasicBlock *Dest;
};
SmallVector<Case> Cases;

public:
SwitchRemoveCase(SwitchInst *Switch, unsigned Index, ConstantInt *Val,
BasicBlock *Dest)
: Switch(Switch), Index(Index), Val(Val), Dest(Dest) {}
SwitchRemoveCase(SwitchInst *Switch);

void revert(Tracker &Tracker) final;
void accept() final {}
#ifndef NDEBUG
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/SandboxIR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,9 +1131,7 @@ void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
}

SwitchInst::CaseIt SwitchInst::removeCase(CaseIt It) {
auto &Case = *It;
Ctx.getTracker().emplaceIfTracking<SwitchRemoveCase>(
this, Case.getCaseIndex(), Case.getCaseValue(), Case.getCaseSuccessor());
Ctx.getTracker().emplaceIfTracking<SwitchRemoveCase>(this);

auto *LLVMSwitch = cast<llvm::SwitchInst>(Val);
unsigned CaseNum = It - case_begin();
Expand Down
29 changes: 18 additions & 11 deletions llvm/lib/SandboxIR/Tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,25 @@ void CatchSwitchAddHandler::revert(Tracker &Tracker) {
LLVMCSI->removeHandler(LLVMCSI->handler_begin() + HandlerIdx);
}

SwitchRemoveCase::SwitchRemoveCase(SwitchInst *Switch): Switch(Switch) {
for (const auto& C : Switch->cases()) {
Cases.push_back({C.getCaseValue(), C.getCaseSuccessor()});
}
}

void SwitchRemoveCase::revert(Tracker &Tracker) {
// removeCase swaps the last case with the deleted one. To revert it, we use
// addCase (which adds the new case to the end), and swap the newly-added
// value and successor operands to the positions for the original case index.
Switch->addCase(Val, Dest);
auto ValUseA = Switch->getOperandUse(2 + Index * 2);
auto SucUseA = Switch->getOperandUse(2 + Index * 2 + 1);
unsigned NumOps = Switch->getNumOperands();
auto ValUseB = Switch->getOperandUse(NumOps - 2);
auto SucUseB = Switch->getOperandUse(NumOps - 2 + 1);
ValUseA.swap(ValUseB);
SucUseA.swap(SucUseB);
// llvm::SwitchInst doesn't give us any API to insert cases at a specific
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also mention that removeCase() provides no guarantees about the order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, even if we had APIs to insert cases at specific indices we'd still need to save everything to account for the lack of ordering guarantees in removeCase, so I've changed it to say just that.

// index. In order to preserve the original ordering, we save all of them and,
// when reverting, clear them all then insert them in the desired order. This
// still relies on the fact that `addCase` will insert them at the end, but it
// is documented to invalidate `case_end()` so it's probably okay.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm so this too is implementation dependent, but I guess less so?

unsigned NumCases = Switch->getNumCases();
for (unsigned I = 0; I < NumCases; ++I) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop the curly braces?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

Switch->removeCase(Switch->case_begin());
}
for (auto &Case : Cases) {
Switch->addCase(Case.Val, Case.Dest);
}
}

#ifndef NDEBUG
Expand Down
Loading