[PATCH] D62126: [CorrelatedValuePropagation] Fix prof branch_weights metadata handling for SwitchInst

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 01:38:59 PDT 2019


yrouban created this revision.
yrouban added reviewers: nikic, reames, davide.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
yrouban added a parent revision: D62122: [NFC] Introduce SwitchInst wrapper for prof branch_weights handling.
yrouban added a child revision: D61179: [WIP] Verifier: check prof branch_weights.

This patch fixes the CorrelatedValuePropagation pass to keep prof branch_weights metadata of SwitchInst consistent.
It makes use of //SwitchInstProfBranchWeightsWrapper// introduced in D62122 <https://reviews.llvm.org/D62122>.
A new test is added.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D62126

Files:
  llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
  llvm/test/Transforms/CorrelatedValuePropagation/profmd.ll


Index: llvm/test/Transforms/CorrelatedValuePropagation/profmd.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/CorrelatedValuePropagation/profmd.ll
@@ -0,0 +1,63 @@
+; RUN: opt < %s -correlated-propagation -S | FileCheck %s
+
+define i32 @switch1(i32 %s) {
+; CHECK-LABEL: @switch1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[S:%.*]], 0
+; CHECK-NEXT:    br i1 [[CMP]], label [[NEGATIVE:%.*]], label [[OUT:%.*]]
+;
+entry:
+  %cmp = icmp slt i32 %s, 0
+  br i1 %cmp, label %negative, label %out
+
+negative:
+; CHECK:       negative:
+; CHECK-NEXT:    switch i32 [[S]], label [[OUT]] [
+; CHECK-NEXT:    i32 -2, label [[NEXT:%.*]]
+; CHECK-NEXT:    i32 -1, label [[NEXT]]
+  switch i32 %s, label %out [
+  i32 0, label %out
+  i32 1, label %out
+  i32 -1, label %next
+  i32 -2, label %next
+  i32 2, label %out
+  i32 3, label %out
+; CHECK-NEXT: !prof ![[MD0:[0-9]+]]
+  ], !prof !{!"branch_weights", i32 99, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6}
+
+out:
+  %p = phi i32 [ 1, %entry ], [ -1, %negative ], [ -1, %negative ], [ -1, %negative ], [ -1, %negative ], [ -1, %negative ]
+  ret i32 %p
+
+next:
+  %q = phi i32 [ 0, %negative ], [ 0, %negative ]
+  ret i32 %q
+}
+
+define i32 @switch2(i32 %s) {
+; CHECK-LABEL: @switch2(
+;
+entry:
+  %cmp = icmp sgt i32 %s, 0
+  br i1 %cmp, label %positive, label %out
+
+positive:
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[S:%.*]], 0
+; CHECK-NEXT:    br i1 [[CMP]], label [[POSITIVE:%.*]], label [[OUT:%.*]]
+  switch i32 %s, label %out [
+  i32 0, label %out
+  i32 -1, label %next
+  i32 -2, label %next
+  ], !prof !{!"branch_weights", i32 99, i32 1, i32 2, i32 3}
+
+out:
+  %p = phi i32 [ -1, %entry ], [ 1, %positive ], [ 1, %positive ]
+  ret i32 %p
+
+next:
+  %q = phi i32 [ 0, %positive ], [ 0, %positive ]
+  ret i32 %q
+}
+
+; CHECK: ![[MD0]] = !{!"branch_weights", i32 99, i32 4, i32 3}
Index: llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -306,8 +306,9 @@
 /// that cannot fire no matter what the incoming edge can safely be removed. If
 /// a case fires on every incoming edge then the entire switch can be removed
 /// and replaced with a branch to the case destination.
-static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI,
+static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
                           DominatorTree *DT) {
+  SwitchInstProfBranchWeightsWrapper SI(*I);
   DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
   Value *Cond = SI->getCondition();
   BasicBlock *BB = SI->getParent();
@@ -363,7 +364,7 @@
       // This case never fires - remove it.
       BasicBlock *Succ = CI->getCaseSuccessor();
       Succ->removePredecessor(BB);
-      CI = SI->removeCase(CI);
+      CI = SI.removeCase(CI);
       CE = SI->case_end();
 
       // The condition can be modified by removePredecessor's PHI simplification


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62126.200218.patch
Type: text/x-patch
Size: 3147 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190520/2a5c7cb4/attachment.bin>


More information about the llvm-commits mailing list