; LLVM IR from parallella/pal :: src/math (p_popcount)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@p_popcount_u32.A = external hidden constant [4 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @p_popcount_u32(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %48, %3
  %14 = load i32, ptr %10, align 4, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %51

17:                                               ; preds = %13
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = lshr i32 %21, 1
  %23 = load i32, ptr @p_popcount_u32.A, align 16, !tbaa !9
  %24 = and i32 %22, %23
  %25 = sub i32 %19, %24
  store i32 %25, ptr %9, align 4, !tbaa !9
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr getelementptr inbounds ([4 x i32], ptr @p_popcount_u32.A, i64 0, i64 1), align 4, !tbaa !9
  %28 = and i32 %26, %27
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = lshr i32 %29, 2
  %31 = load i32, ptr getelementptr inbounds ([4 x i32], ptr @p_popcount_u32.A, i64 0, i64 1), align 4, !tbaa !9
  %32 = and i32 %30, %31
  %33 = add i32 %28, %32
  store i32 %33, ptr %9, align 4, !tbaa !9
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = lshr i32 %35, 4
  %37 = add i32 %34, %36
  %38 = load i32, ptr getelementptr inbounds ([4 x i32], ptr @p_popcount_u32.A, i64 0, i64 2), align 8, !tbaa !9
  %39 = and i32 %37, %38
  %40 = load i32, ptr getelementptr inbounds ([4 x i32], ptr @p_popcount_u32.A, i64 0, i64 3), align 4, !tbaa !9
  %41 = mul i32 %39, %40
  %42 = lshr i32 %41, 24
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %42, ptr %43, align 4, !tbaa !9
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds i32, ptr %44, i32 1
  store ptr %45, ptr %8, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds i32, ptr %46, i32 1
  store ptr %47, ptr %7, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %17
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %10, align 4, !tbaa !9
  br label %13, !llvm.loop !11

51:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@p_popcount_u32.A = external hidden local_unnamed_addr constant [4 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @p_popcount_u32(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %5 = ptrtoint ptr %1 to i64
  %6 = ptrtoint ptr %0 to i64
  %7 = load i32, ptr @p_popcount_u32.A, align 16, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @p_popcount_u32.A, i64 4), align 4, !tbaa !5
  %9 = load i32, ptr getelementptr inbounds (i8, ptr @p_popcount_u32.A, i64 8), align 8, !tbaa !5
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @p_popcount_u32.A, i64 12), align 4, !tbaa !5
  %11 = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 4
  %12 = sub i64 %5, %6
  %diff.check = icmp ult i64 %12, 16
  %or.cond = or i1 %min.iters.check, %diff.check
  br i1 %or.cond, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %11, 2147483644
  %ind.end = trunc nuw nsw i64 %n.vec to i32
  %13 = shl nuw nsw i64 %n.vec, 2
  %ind.end18 = getelementptr i8, ptr %0, i64 %13
  %14 = shl nuw nsw i64 %n.vec, 2
  %ind.end20 = getelementptr i8, ptr %1, i64 %14
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %7, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert24 = insertelement <4 x i32> poison, i32 %8, i64 0
  %broadcast.splat25 = shufflevector <4 x i32> %broadcast.splatinsert24, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert26 = insertelement <4 x i32> poison, i32 %9, i64 0
  %broadcast.splat27 = shufflevector <4 x i32> %broadcast.splatinsert26, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert28 = insertelement <4 x i32> poison, i32 %10, i64 0
  %broadcast.splat29 = shufflevector <4 x i32> %broadcast.splatinsert28, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %offset.idx22 = shl i64 %index, 2
  %next.gep23 = getelementptr i8, ptr %1, i64 %offset.idx22
  %wide.load = load <4 x i32>, ptr %next.gep, align 4, !tbaa !5
  %15 = lshr <4 x i32> %wide.load, <i32 1, i32 1, i32 1, i32 1>
  %16 = and <4 x i32> %15, %broadcast.splat
  %17 = sub <4 x i32> %wide.load, %16
  %18 = and <4 x i32> %17, %broadcast.splat25
  %19 = lshr <4 x i32> %17, <i32 2, i32 2, i32 2, i32 2>
  %20 = and <4 x i32> %19, %broadcast.splat25
  %21 = add <4 x i32> %20, %18
  %22 = lshr <4 x i32> %21, <i32 4, i32 4, i32 4, i32 4>
  %23 = add <4 x i32> %22, %21
  %24 = and <4 x i32> %23, %broadcast.splat27
  %25 = mul <4 x i32> %24, %broadcast.splat29
  %26 = lshr <4 x i32> %25, <i32 24, i32 24, i32 24, i32 24>
  store <4 x i32> %26, ptr %next.gep23, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %11
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %.017.ph = phi i32 [ 0, %.lr.ph ], [ %ind.end, %middle.block ]
  %.01316.ph = phi ptr [ %0, %.lr.ph ], [ %ind.end18, %middle.block ]
  %.01415.ph = phi ptr [ %1, %.lr.ph ], [ %ind.end20, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.017 = phi i32 [ %43, %scalar.ph ], [ %.017.ph, %scalar.ph.preheader ]
  %.01316 = phi ptr [ %42, %scalar.ph ], [ %.01316.ph, %scalar.ph.preheader ]
  %.01415 = phi ptr [ %41, %scalar.ph ], [ %.01415.ph, %scalar.ph.preheader ]
  %28 = load i32, ptr %.01316, align 4, !tbaa !5
  %29 = lshr i32 %28, 1
  %30 = and i32 %29, %7
  %31 = sub i32 %28, %30
  %32 = and i32 %31, %8
  %33 = lshr i32 %31, 2
  %34 = and i32 %33, %8
  %35 = add i32 %34, %32
  %36 = lshr i32 %35, 4
  %37 = add i32 %36, %35
  %38 = and i32 %37, %9
  %39 = mul i32 %38, %10
  %40 = lshr i32 %39, 24
  store i32 %40, ptr %.01415, align 4, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %.01415, i64 4
  %42 = getelementptr inbounds i8, ptr %.01316, i64 4
  %43 = add nuw nsw i32 %.017, 1
  %exitcond.not = icmp eq i32 %43, %2
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %3
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !11}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass