[llvm] [SandboxIR] Preserve the order of switch cases after revert. (PR #115577)

Jorge Gorbe Moya via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 12 12:55:41 PST 2024


================
@@ -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
----------------
slackito wrote:

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.

https://github.com/llvm/llvm-project/pull/115577


More information about the llvm-commits mailing list