-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SandboxIR] Preserve the order of switch cases after revert. #115577
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 1 commit
b266ea7
6bccc01
120eb87
17ea6d8
8a980b2
79bdeb8
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 |
|---|---|---|
|
|
@@ -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 | ||
| // 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. | ||
|
||
| unsigned NumCases = Switch->getNumCases(); | ||
| for (unsigned I = 0; I < NumCases; ++I) { | ||
|
||
| Switch->removeCase(Switch->case_begin()); | ||
| } | ||
| for (auto &Case : Cases) { | ||
| Switch->addCase(Case.Val, Case.Dest); | ||
| } | ||
| } | ||
|
|
||
| #ifndef NDEBUG | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.