xref: /aosp_15_r20/external/llvm/test/Transforms/InstCombine/ctpop.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -S -instcombine | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker
3*9880d681SAndroid Build Coastguard Workerdeclare i32 @llvm.ctpop.i32(i32)
4*9880d681SAndroid Build Coastguard Workerdeclare i8 @llvm.ctpop.i8(i8)
5*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.assume(i1)
6*9880d681SAndroid Build Coastguard Worker
7*9880d681SAndroid Build Coastguard Workerdefine i1 @test1(i32 %arg) {
8*9880d681SAndroid Build Coastguard Worker; CHECK: @test1
9*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 false
10*9880d681SAndroid Build Coastguard Worker  %and = and i32 %arg, 15
11*9880d681SAndroid Build Coastguard Worker  %cnt = call i32 @llvm.ctpop.i32(i32 %and)
12*9880d681SAndroid Build Coastguard Worker  %res = icmp eq i32 %cnt, 9
13*9880d681SAndroid Build Coastguard Worker  ret i1 %res
14*9880d681SAndroid Build Coastguard Worker}
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Workerdefine i1 @test2(i32 %arg) {
17*9880d681SAndroid Build Coastguard Worker; CHECK: @test2
18*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 false
19*9880d681SAndroid Build Coastguard Worker  %and = and i32 %arg, 1
20*9880d681SAndroid Build Coastguard Worker  %cnt = call i32 @llvm.ctpop.i32(i32 %and)
21*9880d681SAndroid Build Coastguard Worker  %res = icmp eq i32 %cnt, 2
22*9880d681SAndroid Build Coastguard Worker  ret i1 %res
23*9880d681SAndroid Build Coastguard Worker}
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Workerdefine i1 @test3(i32 %arg) {
26*9880d681SAndroid Build Coastguard Worker; CHECK: @test3
27*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 false
28*9880d681SAndroid Build Coastguard Worker  ;; Use an assume to make all the bits known without triggering constant
29*9880d681SAndroid Build Coastguard Worker  ;; folding.  This is trying to hit a corner case where we have to avoid
30*9880d681SAndroid Build Coastguard Worker  ;; taking the log of 0.
31*9880d681SAndroid Build Coastguard Worker  %assume = icmp eq i32 %arg, 0
32*9880d681SAndroid Build Coastguard Worker  call void @llvm.assume(i1 %assume)
33*9880d681SAndroid Build Coastguard Worker  %cnt = call i32 @llvm.ctpop.i32(i32 %arg)
34*9880d681SAndroid Build Coastguard Worker  %res = icmp eq i32 %cnt, 2
35*9880d681SAndroid Build Coastguard Worker  ret i1 %res
36*9880d681SAndroid Build Coastguard Worker}
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker; Negative test for when we know nothing
39*9880d681SAndroid Build Coastguard Workerdefine i1 @test4(i8 %arg) {
40*9880d681SAndroid Build Coastguard Worker; CHECK: @test4
41*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 %res
42*9880d681SAndroid Build Coastguard Worker  %cnt = call i8 @llvm.ctpop.i8(i8 %arg)
43*9880d681SAndroid Build Coastguard Worker  %res = icmp eq i8 %cnt, 2
44*9880d681SAndroid Build Coastguard Worker  ret i1 %res
45*9880d681SAndroid Build Coastguard Worker}
46