Make SwitchInst::removeCase() more efficient.
authorJay Foad <jay.foad@gmail.com>
Tue, 1 Feb 2011 09:22:34 +0000 (09:22 +0000)
committerJay Foad <jay.foad@gmail.com>
Tue, 1 Feb 2011 09:22:34 +0000 (09:22 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124659 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Instructions.h
lib/VMCore/Instructions.cpp

index c79fda0deb4ad62785d8e191172f013400333696..17ff763c52bf4df68926214966237691494ea3bc 100644 (file)
@@ -2247,7 +2247,8 @@ public:
 
   /// removeCase - This method removes the specified successor from the switch
   /// instruction.  Note that this cannot be used to remove the default
-  /// destination (successor #0).
+  /// destination (successor #0). Also note that this operation may reorder the
+  /// remaining cases at index idx and above.
   ///
   void removeCase(unsigned idx);
 
index db38a1568fc8d3f2aba02c6f0317713f1febe108..6b561f34af2b5e61aea3b3ebd1deb93b5342f56a 100644 (file)
@@ -3009,14 +3009,10 @@ void SwitchInst::removeCase(unsigned idx) {
   unsigned NumOps = getNumOperands();
   Use *OL = OperandList;
 
-  // Move everything after this operand down.
-  //
-  // FIXME: we could just swap with the end of the list, then erase.  However,
-  // client might not expect this to happen.  The code as it is thrashes the
-  // use/def lists, which is kinda lame.
-  for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
-    OL[i-2] = OL[i];
-    OL[i-2+1] = OL[i+1];
+  // Overwrite this case with the end of the list.
+  if ((idx + 1) * 2 != NumOps) {
+    OL[idx * 2] = OL[NumOps - 2];
+    OL[idx * 2 + 1] = OL[NumOps - 1];
   }
 
   // Nuke the last value.