2012-11-26 08:25:33 +08:00
|
|
|
; Test that the ffs* library call simplifier works correctly.
|
|
|
|
;
|
2012-11-26 04:45:27 +08:00
|
|
|
; RUN: opt < %s -instcombine -S | FileCheck %s
|
2015-11-03 02:00:00 +08:00
|
|
|
; RUN: opt < %s -mtriple i386-pc-linux -instcombine -S | FileCheck %s -check-prefix=CHECK-FFS
|
|
|
|
; RUN: opt -instcombine -mtriple=arm64-apple-ios9.0 -S %s | FileCheck --check-prefix=CHECK-FFS %s
|
|
|
|
; RUN: opt -instcombine -mtriple=arm64-apple-tvos9.0 -S %s | FileCheck --check-prefix=CHECK-FFS %s
|
|
|
|
; RUN: opt -instcombine -mtriple=thumbv7k-apple-watchos2.0 -S %s | FileCheck --check-prefix=CHECK-FFS %s
|
|
|
|
; RUN: opt -instcombine -mtriple=x86_64-apple-macosx10.11 -S %s | FileCheck --check-prefix=CHECK-FFS %s
|
|
|
|
; RUN: opt -instcombine -mtriple=x86_64-freebsd-gnu -S %s | FileCheck --check-prefix=CHECK-FFS %s
|
2012-11-26 04:45:27 +08:00
|
|
|
|
|
|
|
declare i32 @ffs(i32)
|
|
|
|
declare i32 @ffsl(i32)
|
|
|
|
declare i32 @ffsll(i64)
|
|
|
|
|
|
|
|
; Check ffs(0) -> 0.
|
|
|
|
|
|
|
|
define i32 @test_simplify1() {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test_simplify1(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffs(i32 0)
|
|
|
|
ret i32 %ret
|
|
|
|
; CHECK-NEXT: ret i32 0
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify2() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify2(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsl(i32 0)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 0
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify3() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify3(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 0)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 0
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
; Check ffs(c) -> cttz(c) + 1, where 'c' is a constant.
|
|
|
|
|
|
|
|
define i32 @test_simplify4() {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test_simplify4(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffs(i32 1)
|
|
|
|
ret i32 %ret
|
|
|
|
; CHECK-NEXT: ret i32 1
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify5() {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test_simplify5(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffs(i32 2048)
|
|
|
|
ret i32 %ret
|
|
|
|
; CHECK-NEXT: ret i32 12
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify6() {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test_simplify6(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffs(i32 65536)
|
|
|
|
ret i32 %ret
|
|
|
|
; CHECK-NEXT: ret i32 17
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify7() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify7(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsl(i32 65536)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 17
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify8() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify8(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 1024)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 11
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify9() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify9(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 65536)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 17
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify10() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify10(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 17179869184)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 35
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify11() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify11(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 281474976710656)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 49
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify12() {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify12(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 1152921504606846976)
|
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 61
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
; Check ffs(x) -> x != 0 ? (i32)llvm.cttz(x) + 1 : 0.
|
|
|
|
|
|
|
|
define i32 @test_simplify13(i32 %x) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @test_simplify13(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffs(i32 %x)
|
[SimplifyLibCalls] Correctly set the is_zero_undef flag for llvm.cttz
If <src> is non-zero we can safely set the flag to true, and this
results in less code generated for, e.g. ffs(x) + 1 on FreeBSD.
Thanks to majnemer for suggesting the fix and reviewing.
Code generated before the patch was applied:
0: 0f bc c7 bsf %edi,%eax
3: b9 20 00 00 00 mov $0x20,%ecx
8: 0f 45 c8 cmovne %eax,%ecx
b: 83 c1 02 add $0x2,%ecx
e: b8 01 00 00 00 mov $0x1,%eax
13: 85 ff test %edi,%edi
15: 0f 45 c1 cmovne %ecx,%eax
18: c3 retq
Code generated after the patch was applied:
0: 0f bc cf bsf %edi,%ecx
3: 83 c1 02 add $0x2,%ecx
6: 85 ff test %edi,%edi
8: b8 01 00 00 00 mov $0x1,%eax
d: 0f 45 c1 cmovne %ecx,%eax
10: c3 retq
It seems we can still use cmove and save another 'test' instruction, but
that can be tackled separately.
Differential Revision: http://reviews.llvm.org/D11989
llvm-svn: 244947
2015-08-14 04:34:26 +08:00
|
|
|
; CHECK-NEXT: [[CTTZ:%[a-z0-9]+]] = call i32 @llvm.cttz.i32(i32 %x, i1 true)
|
2014-06-17 08:42:07 +08:00
|
|
|
; CHECK-NEXT: [[INC:%[a-z0-9]+]] = add nuw nsw i32 [[CTTZ]], 1
|
2012-11-26 04:45:27 +08:00
|
|
|
; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 %x, 0
|
|
|
|
; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[CMP]], i32 [[INC]], i32 0
|
|
|
|
ret i32 %ret
|
|
|
|
; CHECK-NEXT: ret i32 [[RET]]
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify14(i32 %x) {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify14(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsl(i32 %x)
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: [[CTTZ:%[a-z0-9]+]] = call i32 @llvm.cttz.i32(i32 %x, i1 true)
|
|
|
|
; CHECK-FFS-NEXT: [[INC:%[a-z0-9]+]] = add nuw nsw i32 [[CTTZ]], 1
|
|
|
|
; CHECK-FFS-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 %x, 0
|
|
|
|
; CHECK-FFS-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[CMP]], i32 [[INC]], i32 0
|
2012-11-26 04:45:27 +08:00
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 [[RET]]
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test_simplify15(i64 %x) {
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-LABEL: @test_simplify15(
|
2012-11-26 04:45:27 +08:00
|
|
|
%ret = call i32 @ffsll(i64 %x)
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: [[CTTZ:%[a-z0-9]+]] = call i64 @llvm.cttz.i64(i64 %x, i1 true)
|
|
|
|
; CHECK-FFS-NEXT: [[INC:%[a-z0-9]+]] = add nuw nsw i64 [[CTTZ]], 1
|
|
|
|
; CHECK-FFS-NEXT: [[TRUNC:%[a-z0-9]+]] = trunc i64 [[INC]] to i32
|
|
|
|
; CHECK-FFS-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i64 %x, 0
|
|
|
|
; CHECK-FFS-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[CMP]], i32 [[TRUNC]], i32 0
|
2012-11-26 04:45:27 +08:00
|
|
|
ret i32 %ret
|
2015-11-03 02:00:00 +08:00
|
|
|
; CHECK-FFS-NEXT: ret i32 [[RET]]
|
2012-11-26 04:45:27 +08:00
|
|
|
}
|