[PATCH] D19948: Propagate branch metadata when some branch probability is missing.

Dehao Chen via llvm-commits llvm-commits at lists.llvm.org
Tue May 10 11:50:31 PDT 2016


danielcdh updated this revision to Diff 56772.
danielcdh added a comment.

Rebase the patch to resolve conflicts. Now this patch does not depend on http://reviews.llvm.org/D19674


http://reviews.llvm.org/D19948

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/preserve-branchweights.ll

Index: test/Transforms/SimplifyCFG/preserve-branchweights.ll
===================================================================
--- test/Transforms/SimplifyCFG/preserve-branchweights.ll
+++ test/Transforms/SimplifyCFG/preserve-branchweights.ll
@@ -464,6 +464,30 @@
   ret i32 %outval
 }
 
+define i32 @SimplifyCondBranchToCondBranchSwapMissingWeight(i1 %cmpa, i1 %cmpb) {
+; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwapMissingWeight(
+; CHECK-NEXT:  block1:
+; CHECK-NEXT:    [[CMPA_NOT:%.*]] = xor i1 %cmpa, true 
+; CHECK-NEXT:    [[CMPB_NOT:%.*]] = xor i1 %cmpb, true
+; CHECK-NEXT:    [[BRMERGE:%.*]] = or i1 [[CMPA_NOT]], [[CMPB_NOT]]
+; CHECK-NEXT:    [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2
+; CHECK-NEXT:    [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !16
+; CHECK-NEXT:    ret i32 [[OUTVAL]]
+;
+block1:
+  br i1 %cmpa, label %block2, label %block3, !prof !13
+
+block2:
+  br i1 %cmpb, label %exit, label %block3
+
+block3:
+  %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
+  br label %exit
+
+exit:
+  %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
+  ret i32 %outval
+}
 
 !0 = !{!"branch_weights", i32 3, i32 5}
 !1 = !{!"branch_weights", i32 1, i32 1}
@@ -499,4 +523,4 @@
 ; CHECK: !13 = !{!"branch_weights", i32 34, i32 21}
 ; CHECK: !14 = !{!"branch_weights", i32 33, i32 14}
 ; CHECK: !15 = !{!"branch_weights", i32 47, i32 8}
-
+; CHECK: !16 = !{!"branch_weights", i32 8, i32 2}
Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2821,15 +2821,23 @@
 
   // Update branch weight for PBI.
   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
+  uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
   bool PredHasWeights =
       PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
   bool SuccHasWeights =
       BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
-  if (PredHasWeights && SuccHasWeights) {
-    uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
-    uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
-    uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
-    uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
+  bool HasWeights = PredHasWeights || SuccHasWeights;
+  if (HasWeights) {
+    if (!PredHasWeights) {
+      PredFalseWeight = PredTrueWeight = 1;
+    }
+    if (!SuccHasWeights) {
+      SuccFalseWeight = SuccTrueWeight = 1;
+    }
+    PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
+    PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
+    SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
+    SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
     // The weight to CommonDest should be PredCommon * SuccTotal +
     //                                    PredOther * SuccCommon.
     // The weight to OtherDest should be PredOther * SuccOther.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19948.56772.patch
Type: text/x-patch
Size: 3011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160510/d1f2eb5d/attachment.bin>


More information about the llvm-commits mailing list