[llvm] r363527 - [SimplifyCFG] Fix prof branch_weights MD while removing unreachable switch cases

Yevgeny Rouban via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 16 22:55:12 PDT 2019


Author: yrouban
Date: Sun Jun 16 22:55:12 2019
New Revision: 363527

URL: http://llvm.org/viewvc/llvm-project?rev=363527&view=rev
Log:
[SimplifyCFG] Fix prof branch_weights MD while removing unreachable switch cases

SimplifyCFG has a bug that results in inconsistent prof branch_weights metadata
if unreachable switch cases are removed. This patch fixes this bug by making use
of the newly introduced SwitchInstProfUpdateWrapper class (see patch D62122).
A new test is created.

Differential Revision: https://reviews.llvm.org/D62186

Added:
    llvm/trunk/test/Transforms/SimplifyCFG/switch-profmd.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=363527&r1=363526&r2=363527&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sun Jun 16 22:55:12 2019
@@ -4218,14 +4218,15 @@ bool SimplifyCFGOpt::SimplifyUnreachable
         }
       }
     } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
-      for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
+      SwitchInstProfUpdateWrapper SU(*SI);
+      for (auto i = SU->case_begin(), e = SU->case_end(); i != e;) {
         if (i->getCaseSuccessor() != BB) {
           ++i;
           continue;
         }
-        BB->removePredecessor(SI->getParent());
-        i = SI->removeCase(i);
-        e = SI->case_end();
+        BB->removePredecessor(SU->getParent());
+        i = SU.removeCase(i);
+        e = SU->case_end();
         Changed = true;
       }
     } else if (auto *II = dyn_cast<InvokeInst>(TI)) {

Added: llvm/trunk/test/Transforms/SimplifyCFG/switch-profmd.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/switch-profmd.ll?rev=363527&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/switch-profmd.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyCFG/switch-profmd.ll Sun Jun 16 22:55:12 2019
@@ -0,0 +1,35 @@
+; RUN: opt -S -simplifycfg < %s | FileCheck %s
+
+declare i32 @f(i32 %val)
+
+; Check that eliminating cases with unreachable branches keeps
+; prof branch_weights metadata consistent with switch instruction.
+define i32 @test_switch_to_unreachable(i32 %val) {
+; CHECK-LABEL: test_switch_to_unreachable
+; CHECK: switch
+; CHECK-NOT: i32 0, label %on0
+  switch i32 %val, label %otherwise [
+    i32 0, label %on0
+    i32 1, label %on1
+    i32 2, label %on2
+  ], !prof !{!"branch_weights", i32 99, i32 0, i32 1, i32 2}
+; CHECK: !prof ![[MD0:[0-9]+]]
+
+otherwise:
+  %result = call i32 @f(i32 -1)
+  ret i32 %result
+
+on0:
+  unreachable
+  ret i32 125
+
+on1:
+  %result1 = call i32 @f(i32 -2)
+  ret i32 %result1
+
+on2:
+  %result2 = call i32 @f(i32 -3)
+  ret i32 %result2
+}
+
+; CHECK: ![[MD0]] = !{!"branch_weights", i32 99, i32 2, i32 1}




More information about the llvm-commits mailing list