[llvm] [SandboxIR] Preserve the order of switch cases after revert. (PR #115577)
Jorge Gorbe Moya via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 11 14:19:25 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);
----------------
slackito wrote:
If I understand the implementation correctly, if you remove Case0 from (Case0, Case1, Case2), it swaps the element to be deleted with the last one, and then shrinks the list. So you actually get (Case2, Case1), which you can undo with a single swap after adding the case back at the end of the list.
https://github.com/llvm/llvm-project/pull/115577
More information about the llvm-commits
mailing list