; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Tagl/Lib/gport.ll (ZN11GraphicPort6ZClearEt)

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

@_ZN16GraphicComponent7_heightE = external global i32, align 4
@_ZN16GraphicComponent6_z_memE = external global ptr, align 8
@_ZN16GraphicComponent6_widthE = external global i32, align 4

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN11GraphicPort6ZClearEt(ptr noundef nonnull align 8 dereferenceable(14128) %0, i16 noundef zeroext %1) unnamed_addr #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load i32, ptr @_ZN16GraphicComponent6_widthE, align 4, !tbaa !11
  %9 = load i32, ptr @_ZN16GraphicComponent7_heightE, align 4, !tbaa !11
  %10 = mul nsw i32 %8, %9
  store i32 %10, ptr %6, align 4, !tbaa !11
  %11 = load ptr, ptr @_ZN16GraphicComponent6_z_memE, align 8, !tbaa !5
  store ptr %11, ptr %5, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %19, %2
  %13 = load i32, ptr %6, align 4, !tbaa !11
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %15, label %22

15:                                               ; preds = %12
  %16 = load i16, ptr %4, align 2, !tbaa !9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds i16, ptr %17, i32 1
  store ptr %18, ptr %5, align 8, !tbaa !5
  store i16 %16, ptr %17, align 2, !tbaa !9
  br label %19

19:                                               ; preds = %15
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = add nsw i32 %20, -1
  store i32 %21, ptr %6, align 4, !tbaa !11
  br label %12, !llvm.loop !13

22:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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"

@_ZN16GraphicComponent7_heightE = external local_unnamed_addr global i32, align 4
@_ZN16GraphicComponent6_z_memE = external local_unnamed_addr global ptr, align 8
@_ZN16GraphicComponent6_widthE = external local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local void @_ZN11GraphicPort6ZClearEt(ptr nocapture noundef nonnull readnone align 8 dereferenceable(14128) %0, i16 noundef zeroext %1) unnamed_addr #0 align 2 {
  %3 = load i32, ptr @_ZN16GraphicComponent6_widthE, align 4, !tbaa !5
  %4 = load i32, ptr @_ZN16GraphicComponent7_heightE, align 4, !tbaa !5
  %5 = mul nsw i32 %4, %3
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %7 = load ptr, ptr @_ZN16GraphicComponent6_z_memE, align 8, !tbaa !9
  %8 = zext nneg i32 %5 to i64
  %min.iters.check = icmp ult i32 %5, 16
  br i1 %min.iters.check, label %.lr.ph.preheader10, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %8, 2147483632
  %.cast = trunc nuw nsw i64 %n.vec to i32
  %ind.end = sub nsw i32 %5, %.cast
  %9 = shl nuw nsw i64 %n.vec, 1
  %ind.end7 = getelementptr i8, ptr %7, i64 %9
  %broadcast.splatinsert = insertelement <8 x i16> poison, i16 %1, i64 0
  %broadcast.splat = shufflevector <8 x i16> %broadcast.splatinsert, <8 x i16> 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 ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %7, i64 %offset.idx
  %10 = getelementptr i8, ptr %next.gep, i64 16
  store <8 x i16> %broadcast.splat, ptr %next.gep, align 2, !tbaa !11
  store <8 x i16> %broadcast.splat, ptr %10, align 2, !tbaa !11
  %index.next = add nuw i64 %index, 16
  %11 = icmp eq i64 %index.next, %n.vec
  br i1 %11, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %8
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader10

.lr.ph.preheader10:                               ; preds = %middle.block, %.lr.ph.preheader
  %.06.ph = phi i32 [ %5, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.045.ph = phi ptr [ %7, %.lr.ph.preheader ], [ %ind.end7, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader10, %.lr.ph
  %.06 = phi i32 [ %13, %.lr.ph ], [ %.06.ph, %.lr.ph.preheader10 ]
  %.045 = phi ptr [ %12, %.lr.ph ], [ %.045.ph, %.lr.ph.preheader10 ]
  %12 = getelementptr inbounds i8, ptr %.045, i64 2
  store i16 %1, ptr %.045, align 2, !tbaa !11
  %13 = add nsw i32 %.06, -1
  %14 = icmp ugt i32 %.06, 1
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !16, !15}


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