[PATCH] D14590: [SimplifyLibCalls] Constant folding for fls, flsl, flsll
Davide Italiano via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 11 14:39:34 PST 2015
davide created this revision.
davide added reviewers: majnemer, echristo.
davide added a subscriber: llvm-commits.
This enables constant folding for fls() [FreeBSD specific]. This should be quite straighforward (unless I got the math wrong, which I don't exclude).
Thanks!
http://reviews.llvm.org/D14590
Files:
include/llvm/Transforms/Utils/SimplifyLibCalls.h
lib/Transforms/Utils/SimplifyLibCalls.cpp
test/Transforms/InstCombine/fls.ll
Index: test/Transforms/InstCombine/fls.ll
===================================================================
--- test/Transforms/InstCombine/fls.ll
+++ test/Transforms/InstCombine/fls.ll
@@ -0,0 +1,15 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+target triple = "x86_64-unknown-freebsd11.0"
+
+define i32 @myfls() {
+entry:
+ %call = call i32 @fls(i32 42) #2
+ ret i32 %call
+}
+
+declare i32 @fls(i32)
+
+; CHECK-LABEL: define i32 @myfls(
+; CHECK: ret i32 6
+; CHECK: }
Index: lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1572,6 +1572,22 @@
return B.CreateSelect(Cond, V, B.getInt32(0));
}
+Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilder<> &B) {
+ Function *Callee = CI->getCalledFunction();
+ if (!checkIntUnaryReturnAndParam(Callee))
+ return nullptr;
+ Value *Op = CI->getArgOperand(0);
+
+ // Constant fold.
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
+ if (CI->isZero()) // fls(0) -> 0.
+ return B.getInt32(0);
+ // fls(c) -> 32 - ctlz(c)
+ return B.getInt32(32 - CI->getValue().countLeadingZeros());
+ }
+ return nullptr;
+}
+
Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
Function *Callee = CI->getCalledFunction();
FunctionType *FT = Callee->getFunctionType();
@@ -2157,6 +2173,10 @@
case LibFunc::ffsl:
case LibFunc::ffsll:
return optimizeFFS(CI, Builder);
+ case LibFunc::fls:
+ case LibFunc::flsl:
+ case LibFunc::flsll:
+ return optimizeFls(CI, Builder);
case LibFunc::abs:
case LibFunc::labs:
case LibFunc::llabs:
Index: include/llvm/Transforms/Utils/SimplifyLibCalls.h
===================================================================
--- include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -138,6 +138,7 @@
// Integer Library Call Optimizations
Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);
+ Value *optimizeFls(CallInst *CI, IRBuilder<> &B);
Value *optimizeAbs(CallInst *CI, IRBuilder<> &B);
Value *optimizeIsDigit(CallInst *CI, IRBuilder<> &B);
Value *optimizeIsAscii(CallInst *CI, IRBuilder<> &B);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14590.39977.patch
Type: text/x-patch
Size: 2299 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151111/6ae48129/attachment.bin>
More information about the llvm-commits
mailing list