[PATCH] Constant fold llvm.expect(c, ...) -> c

Pete Cooper peter_cooper at apple.com
Sun Jan 25 21:37:17 PST 2015


Hi hfinkel,

This teaches the constant folder that an llvm.expect intrinsic with a constant as the first operand will return that constant.

This reduces the size of an LTO'd llc by over 1% due to branches which can be removed once the constant is propagated.

http://reviews.llvm.org/D7168

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstSimplify/call.ll

Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1238,6 +1238,7 @@
 /// Return true if it's even possible to fold a call to the specified function.
 bool llvm::canConstantFoldCallTo(const Function *F) {
   switch (F->getIntrinsicID()) {
+  case Intrinsic::expect:
   case Intrinsic::fabs:
   case Intrinsic::minnum:
   case Intrinsic::maxnum:
@@ -1722,6 +1723,9 @@
           if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
             return UndefValue::get(Ty);
           return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
+        case Intrinsic::expect:
+          // llvm.expect(constant, ...) -> constant
+          return Op1;
         }
       }
 
Index: test/Transforms/InstSimplify/call.ll
===================================================================
--- test/Transforms/InstSimplify/call.ll
+++ test/Transforms/InstSimplify/call.ll
@@ -164,3 +164,14 @@
 }
 
 declare noalias i8* @malloc(i64)
+
+; Constant fold llvm.expect(c, ...) -> c
+define i64 @constant_fold_expect() {
+  %c = call i64 @llvm.expect.i64(i64 1, i64 0)
+  ret i64 %c
+
+; CHECK-LABEL: @constant_fold_expect
+; CHECK: ret i64 1
+}
+
+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: D7168.18739.patch
Type: text/x-patch
Size: 1324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150126/69606f4f/attachment.bin>


More information about the llvm-commits mailing list