[llvm] r277332 - [SimplifyCFG] Fix nasty RAUW bug from r277325

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 1 02:34:48 PDT 2016


Author: jamesm
Date: Mon Aug  1 04:34:48 2016
New Revision: 277332

URL: http://llvm.org/viewvc/llvm-project?rev=277332&view=rev
Log:
[SimplifyCFG] Fix nasty RAUW bug from r277325

Using RAUW was wrong here; if we have a switch transform such as:
  18 -> 6 then
  6 -> 0

If we use RAUW, while performing the second transform the  *transformed* 6
from the first will be also replaced, so we end up with:
  18 -> 0
  6 -> 0

Found by clang stage2 bootstrap; testcase added.

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/test/Transforms/SimplifyCFG/rangereduce.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=277332&r1=277331&r2=277332&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Mon Aug  1 04:34:48 2016
@@ -5132,11 +5132,12 @@ static bool ReduceSwitchRange(SwitchInst
                                Builder.CreateShl(Sub, Ty->getBitWidth() - Shift));
   SI->replaceUsesOfWith(SI->getCondition(), Rot);
 
-  for (auto &C : SI->cases()) {
+  for (SwitchInst::CaseIt C = SI->case_begin(), E = SI->case_end(); C != E;
+       ++C) {
     auto *Orig = C.getCaseValue();
     auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base);
-    SI->replaceUsesOfWith(Orig,
-                          ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue())));
+    C.setValue(
+        cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue()))));
   }
   return true;
 }

Modified: llvm/trunk/test/Transforms/SimplifyCFG/rangereduce.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/rangereduce.ll?rev=277332&r1=277331&r2=277332&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/rangereduce.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/rangereduce.ll Mon Aug  1 04:34:48 2016
@@ -192,4 +192,30 @@ two:
   ret i32 1143
 three:
   ret i32 99783
-}
\ No newline at end of file
+}
+
+; CHECK-LABEL: @test9
+; CHECK:  switch
+; CHECK:  i32 6
+; CHECK:  i32 7
+; CHECK:  i32 0
+; CHECK:  i32 2
+define i32 @test9(i32 %a) {
+  switch i32 %a, label %def [
+    i32 18, label %one
+    i32 20, label %two
+    i32 6, label %three
+    i32 10, label %three
+  ]
+
+def:
+  ret i32 8867
+
+one:
+  ret i32 11984
+two:
+  ret i32 1143
+three:
+  ret i32 99783
+}
+




More information about the llvm-commits mailing list