[PATCH] Jump thread llvm.expect

Pete Cooper peter_cooper at apple.com
Sun Jan 25 21:49:45 PST 2015


Hi hfinkel,

When jump threading this code

%B = phi i32 [%v1, %T1], [12, %F1]
%A = icmp ne i32 %B, 42

in asserts builds its common to have an expect in between:

%B = phi i64 [%v1, %T1], [12, %F1]
%expect = call i64 @llvm.expect.i64(i64 %B, i64 42)
%A = icmp ne i64 %expect, 42

This patch teaches JumpThreading about this case so that when we fail to find a PHI, we look through an llvm.expect to see if a PHI is its first source.

Reduces the size of an LTO'd llc by almost 2.5%.

http://reviews.llvm.org/D7169

Files:
  lib/Transforms/Scalar/JumpThreading.cpp
  test/Transforms/JumpThreading/expect.ll

Index: lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- lib/Transforms/Scalar/JumpThreading.cpp
+++ lib/Transforms/Scalar/JumpThreading.cpp
@@ -504,6 +504,13 @@
   if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
     assert(Preference == WantInteger && "Compares only produce integers");
     PHINode *PN = dyn_cast<PHINode>(Cmp->getOperand(0));
+
+    // Look through llvm.expect as its source value could be a PHI.
+    if (!PN) {
+      if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Cmp->getOperand(0)))
+        if (II->getIntrinsicID() == Intrinsic::expect)
+          PN = dyn_cast<PHINode>(II->getArgOperand(0));
+    }
     if (PN && PN->getParent() == BB) {
       // We can do this simplification if any comparisons fold to true or false.
       // See if any do.
Index: test/Transforms/JumpThreading/expect.ll
===================================================================
--- /dev/null
+++ test/Transforms/JumpThreading/expect.ll
@@ -0,0 +1,35 @@
+; There should be no phi nodes left.
+; RUN: opt < %s -jump-threading -S | FileCheck %s
+
+; CHECK-NOT: phi
+
+declare i64 @f1()
+declare i64 @f2()
+declare void @f3()
+
+define i64 @test(i1 %cond) {
+	br i1 %cond, label %T1, label %F1
+
+T1:
+	%v1 = call i64 @f1()
+	br label %Merge
+
+F1:
+	%v2 = call i64 @f2()
+	br label %Merge
+
+Merge:
+	%B = phi i64 [%v1, %T1], [12, %F1]
+	%expect = call i64 @llvm.expect.i64(i64 %B, i64 42)
+	%A = icmp ne i64 %expect, 42
+	br i1 %A, label %T2, label %F2
+
+T2:
+	call void @f3()
+	ret i64 1
+
+F2:
+	ret i64 0
+}
+
+declare i64 @llvm.expect.i64(i64, i64)

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7169.18740.patch
Type: text/x-patch
Size: 1623 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150126/9d8bf94b/attachment.bin>


More information about the llvm-commits mailing list