[llvm] [SandboxIR] Preserve the order of switch cases after revert. (PR #115577)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 9 08:56:10 PST 2024
================
@@ -170,7 +170,19 @@ void CatchSwitchAddHandler::revert(Tracker &Tracker) {
LLVMCSI->removeHandler(LLVMCSI->handler_begin() + HandlerIdx);
}
-void SwitchRemoveCase::revert(Tracker &Tracker) { Switch->addCase(Val, Dest); }
+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);
----------------
vporpo wrote:
I think this would require a loop that swaps the last 'case' one position to the left at a time until we reach Index. Because if the switch contains 3 cases (Case0, Case1, Case2) and we remove case 0, then when we revert we are placing the removed one at the end (Case1, Case2, Case0). Then we would need to bring Case0 back to position 0 without changing the order of 1 and 2.
Also I think we could use `swapOperands()` instead of `swap(Use)`, which may be a bit more compact.
https://github.com/llvm/llvm-project/pull/115577
More information about the llvm-commits
mailing list