[PATCH] D18133: allow branch weight metadata on select instructions (PR26636)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 13 10:01:51 PDT 2016


spatel created this revision.
spatel added reviewers: hfinkel, bkramer, davidxl.
spatel added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

As noted in:
https://llvm.org/bugs/show_bug.cgi?id=26636

This doesn't accomplish anything on its own. It's the first step towards preserving and using branch weights with selects.

If this looks ok, the next step would be to make sure we're propagating the info in all of the other places where we create selects (SimplifyCFG, InstCombine, etc). I don't think there's an easy fix to make this happen; we have to look at each transform individually to determine how to correctly propagate the weights. 

Along with that step, we need to then use the weights when making subsequent transform decisions such as discussed in http://reviews.llvm.org/D16836.

http://reviews.llvm.org/D18133

Files:
  include/llvm/IR/IRBuilder.h
  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
@@ -364,6 +364,35 @@
   ret void
 }
 
+; PR26636: The branch metadata should get propagated to the select.
+
+define i32 @FoldTwoEntryPHINode(i32 %x) #0 {
+entry:
+  %cmp = icmp sgt i32 %x, 4
+  br i1 %cmp, label %if.then, label %if.end, !prof !12
+
+if.then:
+  %add = add nsw i32 %x, 7
+  br label %return
+
+if.end:
+  %mul = mul nsw i32 %x, 17
+  br label %return
+
+return:
+  %retval.0 = phi i32 [ %add, %if.then ], [ %mul, %if.end ]
+  ret i32 %retval.0
+
+; CHECK-LABEL: @FoldTwoEntryPHINode(
+; CHECK-NEXT: entry:
+; CHECK-NEXT:  %cmp = icmp sgt i32 %x, 4
+; CHECK-NEXT:  %add = add nsw i32 %x, 7
+; CHECK-NEXT:  %mul = mul nsw i32 %x, 17
+; CHECK-NEXT:  %retval.0 = select i1 %cmp, i32 %add, i32 %mul, !prof !11
+; CHECK-NEXT:  ret i32 %retval.0
+}
+
+
 !0 = !{!"branch_weights", i32 3, i32 5}
 !1 = !{!"branch_weights", i32 1, i32 1}
 !2 = !{!"branch_weights", i32 1, i32 2}
@@ -376,6 +405,7 @@
 !9 = !{!"branch_weights", i32 7, i32 6}
 !10 = !{!"branch_weights", i32 672646, i32 21604207}
 !11 = !{!"branch_weights", i32 6960, i32 21597248}
+!12 = !{!"branch_weights", i32 6, i32 2000}
 
 ; CHECK: !0 = !{!"branch_weights", i32 5, i32 11}
 ; CHECK: !1 = !{!"branch_weights", i32 1, i32 5}
@@ -390,3 +420,4 @@
 ;; The false weight prints out as a negative integer here, but inside llvm, we
 ;; treat the weight as an unsigned integer.
 ; CHECK: !10 = !{!"branch_weights", i32 112017436, i32 -735157296}
+; CHECK: !11 = !{!"branch_weights", i32 6, i32 2000}
Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1935,7 +1935,8 @@
     Value *TrueVal  = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
     Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
 
-    Value *Select = Builder.CreateSelect(IfCond, TrueVal, FalseVal);
+    MDNode *MDN = InsertPt->getMetadata(LLVMContext::MD_prof);
+    Value *Select = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", MDN);
     PN->replaceAllUsesWith(Select);
     Select->takeName(PN);
     PN->eraseFromParent();
Index: include/llvm/IR/IRBuilder.h
===================================================================
--- include/llvm/IR/IRBuilder.h
+++ include/llvm/IR/IRBuilder.h
@@ -1577,12 +1577,16 @@
   }
 
   Value *CreateSelect(Value *C, Value *True, Value *False,
-                      const Twine &Name = "") {
+                      const Twine &Name = "", MDNode *ProfWeights = nullptr) {
     if (Constant *CC = dyn_cast<Constant>(C))
       if (Constant *TC = dyn_cast<Constant>(True))
         if (Constant *FC = dyn_cast<Constant>(False))
           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
-    return Insert(SelectInst::Create(C, True, False), Name);
+
+    SelectInst *Sel = SelectInst::Create(C, True, False);
+    // TODO: "unpredictable" metadata can apply to a select too.
+    Sel->setMetadata(LLVMContext::MD_prof, ProfWeights);
+    return Insert(Sel, Name);
   }
 
   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18133.50553.patch
Type: text/x-patch
Size: 3363 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160313/8bcaf17a/attachment.bin>


More information about the llvm-commits mailing list