; LLVM IR from glaretechnologies/substrata :: opus/opus-1.3.1/silk (interpolate)

; 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"

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

12:                                               ; preds = %49, %5
  %13 = load i32, ptr %11, align 4, !tbaa !9
  %14 = load i32, ptr %10, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %52

16:                                               ; preds = %12
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = load i32, ptr %11, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i16, ptr %17, i64 %19
  %21 = load i16, ptr %20, align 2, !tbaa !11
  %22 = sext i16 %21 to i32
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = load i32, ptr %11, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i16, ptr %23, i64 %25
  %27 = load i16, ptr %26, align 2, !tbaa !11
  %28 = sext i16 %27 to i32
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i16, ptr %29, i64 %31
  %33 = load i16, ptr %32, align 2, !tbaa !11
  %34 = sext i16 %33 to i32
  %35 = sub nsw i32 %28, %34
  %36 = trunc i32 %35 to i16
  %37 = sext i16 %36 to i32
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = trunc i32 %38 to i16
  %40 = sext i16 %39 to i32
  %41 = mul nsw i32 %37, %40
  %42 = ashr i32 %41, 2
  %43 = add nsw i32 %22, %42
  %44 = trunc i32 %43 to i16
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i16, ptr %45, i64 %47
  store i16 %44, ptr %48, align 2, !tbaa !11
  br label %49

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

52:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #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 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @silk_interpolate(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = ptrtoint ptr %2 to i64
  %7 = ptrtoint ptr %1 to i64
  %8 = ptrtoint ptr %0 to i64
  %9 = icmp sgt i32 %4, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5
  %sext = shl i32 %3, 16
  %10 = ashr exact i32 %sext, 16
  %wide.trip.count = zext nneg i32 %4 to i64
  %min.iters.check = icmp ult i32 %4, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph
  %11 = sub i64 %8, %7
  %diff.check = icmp ult i64 %11, 16
  %12 = sub i64 %8, %6
  %diff.check13 = icmp ult i64 %12, 16
  %conflict.rdx = or i1 %diff.check, %diff.check13
  br i1 %conflict.rdx, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <8 x i32> poison, i32 %10, i64 0
  %broadcast.splat = shufflevector <8 x i32> %broadcast.splatinsert, <8 x i32> poison, <8 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %13 = getelementptr inbounds i16, ptr %1, i64 %index
  %wide.load = load <8 x i16>, ptr %13, align 2, !tbaa !5
  %14 = getelementptr inbounds i16, ptr %2, i64 %index
  %wide.load14 = load <8 x i16>, ptr %14, align 2, !tbaa !5
  %15 = sub <8 x i16> %wide.load14, %wide.load
  %16 = sext <8 x i16> %15 to <8 x i32>
  %17 = mul nsw <8 x i32> %broadcast.splat, %16
  %18 = lshr <8 x i32> %17, <i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2>
  %19 = trunc <8 x i32> %18 to <8 x i16>
  %20 = add <8 x i16> %wide.load, %19
  %21 = getelementptr inbounds i16, ptr %0, i64 %index
  store <8 x i16> %20, ptr %21, align 2, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !9

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

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader
  %23 = getelementptr inbounds i16, ptr %1, i64 %indvars.iv.ph
  %24 = load i16, ptr %23, align 2, !tbaa !5
  %25 = getelementptr inbounds i16, ptr %2, i64 %indvars.iv.ph
  %26 = load i16, ptr %25, align 2, !tbaa !5
  %27 = sub i16 %26, %24
  %28 = sext i16 %27 to i32
  %29 = mul nsw i32 %10, %28
  %30 = lshr i32 %29, 2
  %31 = trunc i32 %30 to i16
  %32 = add i16 %24, %31
  %33 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.ph
  store i16 %32, ptr %33, align 2, !tbaa !5
  %indvars.iv.next.prol = or disjoint i64 %indvars.iv.ph, 1
  br label %scalar.ph.prol.loopexit

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %34 = add nsw i64 %wide.trip.count, -1
  %35 = icmp eq i64 %indvars.iv.ph, %34
  br i1 %35, label %._crit_edge, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %36 = getelementptr inbounds i16, ptr %1, i64 %indvars.iv
  %37 = load i16, ptr %36, align 2, !tbaa !5
  %38 = getelementptr inbounds i16, ptr %2, i64 %indvars.iv
  %39 = load i16, ptr %38, align 2, !tbaa !5
  %40 = sub i16 %39, %37
  %41 = sext i16 %40 to i32
  %42 = mul nsw i32 %10, %41
  %43 = lshr i32 %42, 2
  %44 = trunc i32 %43 to i16
  %45 = add i16 %37, %44
  %46 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv
  store i16 %45, ptr %46, align 2, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %47 = getelementptr inbounds i16, ptr %1, i64 %indvars.iv.next
  %48 = load i16, ptr %47, align 2, !tbaa !5
  %49 = getelementptr inbounds i16, ptr %2, i64 %indvars.iv.next
  %50 = load i16, ptr %49, align 2, !tbaa !5
  %51 = sub i16 %50, %48
  %52 = sext i16 %51 to i32
  %53 = mul nsw i32 %10, %52
  %54 = lshr i32 %53, 2
  %55 = trunc i32 %54 to i16
  %56 = add i16 %48, %55
  %57 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next
  store i16 %56, ptr %57, align 2, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count
  br i1 %exitcond.not.1, label %._crit_edge, label %scalar.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block, %5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"short", !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