[PATCH] D24166: [Profile] Lower expect intrinsic properly for select instruction

David Li via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 21:43:20 PDT 2016


davidxl updated this revision to Diff 70133.
davidxl added a comment.

Make the implementation more efficient -- avoid traversing IR twice.


https://reviews.llvm.org/D24166

Files:
  lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
  test/Transforms/LowerExpectIntrinsic/basic.ll

Index: test/Transforms/LowerExpectIntrinsic/basic.ll
===================================================================
--- test/Transforms/LowerExpectIntrinsic/basic.ll
+++ test/Transforms/LowerExpectIntrinsic/basic.ll
@@ -273,6 +273,19 @@
   ret i32 %0
 }
 
+; CHECK-LABEL: @test10(
+define i32 @test10(i32, i32, i32) #0 {
+  %4 = and i32 %0, 3
+  %5 = icmp eq i32 %4, 1
+  %6 = zext i1 %5 to i64
+  %7 = call i64 @llvm.expect.i64(i64 %6, i64 0)
+  %8 = icmp ne i64 %7, 0
+  %9 = select i1 %8, i32 %1, i32 %2
+; CHECK: select{{.*}}, !prof !1
+  ret i32 %9
+}
+
+
 declare i1 @llvm.expect.i1(i1, i1) nounwind readnone
 
 ; CHECK: !0 = !{!"branch_weights", i32 2000, i32 1}
Index: lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
===================================================================
--- lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
+++ lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
@@ -83,9 +83,7 @@
   return true;
 }
 
-static bool handleBranchExpect(BranchInst &BI) {
-  if (BI.isUnconditional())
-    return false;
+template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BI) {
 
   // Handle non-optimized IR code like:
   //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
@@ -138,6 +136,13 @@
   return true;
 }
 
+static bool handleBranchExpect(BranchInst &BI) {
+  if (BI.isUnconditional())
+    return false;
+
+  return handleBrSelExpect<BranchInst>(BI);
+}
+
 static bool lowerExpectIntrinsic(Function &F) {
   bool Changed = false;
 
@@ -152,10 +157,16 @@
     }
 
     // Remove llvm.expect intrinsics.
-    for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
-      CallInst *CI = dyn_cast<CallInst>(BI++);
-      if (!CI)
+    for (BasicBlock::reverse_iterator BI = BB.rbegin(), BE = BB.rend(); BI != BE;) {
+      Instruction *Inst = &*BI++;
+      CallInst *CI = dyn_cast<CallInst>(Inst);
+      if (!CI) {
+        if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) {
+          if (handleBrSelExpect(*SI))
+            ExpectIntrinsicsHandled++;
+        }
         continue;
+      }
 
       Function *Fn = CI->getCalledFunction();
       if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24166.70133.patch
Type: text/x-patch
Size: 2171 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160902/2a7bdf69/attachment.bin>


More information about the llvm-commits mailing list