; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (r_plane)

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

%struct.visplane_t = type { i32, i32, i32, i32, i32, i16, [320 x i16], i16, i16, [320 x i16], i16 }

@viewwidth = external global i32, align 4
@viewheight = external global i32, align 4
@cachedheight = external dso_local global [200 x i32], align 16
@basexscale = external dso_local global i32, align 4
@baseyscale = external dso_local global i32, align 4
@viewangle = external global i32, align 4
@finecosine = external global ptr, align 8
@finesine = external global [10240 x i32], align 16
@floorclip = external dso_local global [320 x i16], align 16
@ceilingclip = external dso_local global [320 x i16], align 16
@visplanes = external dso_local global [128 x %struct.visplane_t], align 16
@lastvisplane = external dso_local global ptr, align 8
@openings = external dso_local global [20480 x i16], align 16
@lastopening = external dso_local global ptr, align 8
@centerxfrac = 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: nounwind uwtable
define dso_local void @R_ClearPlanes() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %16, %0
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = load i32, ptr @viewwidth, align 4, !tbaa !5
  %6 = icmp slt i32 %4, %5
  br i1 %6, label %7, label %19

7:                                                ; preds = %3
  %8 = load i32, ptr @viewheight, align 4, !tbaa !5
  %9 = trunc i32 %8 to i16
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [320 x i16], ptr @floorclip, i64 0, i64 %11
  store i16 %9, ptr %12, align 2, !tbaa !9
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [320 x i16], ptr @ceilingclip, i64 0, i64 %14
  store i16 -1, ptr %15, align 2, !tbaa !9
  br label %16

16:                                               ; preds = %7
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %1, align 4, !tbaa !5
  br label %3, !llvm.loop !11

19:                                               ; preds = %3
  store ptr @visplanes, ptr @lastvisplane, align 8, !tbaa !13
  store ptr @openings, ptr @lastopening, align 8, !tbaa !13
  call void @llvm.memset.p0.i64(ptr align 16 @cachedheight, i8 0, i64 800, i1 false)
  %20 = load i32, ptr @viewangle, align 4, !tbaa !5
  %21 = sub i32 %20, 1073741824
  %22 = lshr i32 %21, 19
  store i32 %22, ptr %2, align 4, !tbaa !5
  %23 = load ptr, ptr @finecosine, align 8, !tbaa !13
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = load i32, ptr @centerxfrac, align 4, !tbaa !5
  %29 = call i32 @FixedDiv(i32 noundef %27, i32 noundef %28)
  store i32 %29, ptr @basexscale, align 4, !tbaa !5
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = load i32, ptr @centerxfrac, align 4, !tbaa !5
  %35 = call i32 @FixedDiv(i32 noundef %33, i32 noundef %34)
  %36 = sub nsw i32 0, %35
  store i32 %36, ptr @baseyscale, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare i32 @FixedDiv(i32 noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}


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

%struct.visplane_t = type { i32, i32, i32, i32, i32, i16, [320 x i16], i16, i16, [320 x i16], i16 }

@viewwidth = external local_unnamed_addr global i32, align 4
@viewheight = external local_unnamed_addr global i32, align 4
@cachedheight = external dso_local local_unnamed_addr global [200 x i32], align 16
@basexscale = external dso_local local_unnamed_addr global i32, align 4
@baseyscale = external dso_local local_unnamed_addr global i32, align 4
@viewangle = external local_unnamed_addr global i32, align 4
@finecosine = external local_unnamed_addr global ptr, align 8
@finesine = external local_unnamed_addr global [10240 x i32], align 16
@floorclip = external dso_local local_unnamed_addr global [320 x i16], align 16
@ceilingclip = external dso_local local_unnamed_addr global [320 x i16], align 16
@visplanes = external dso_local global [128 x %struct.visplane_t], align 16
@lastvisplane = external dso_local local_unnamed_addr global ptr, align 8
@openings = external dso_local global [20480 x i16], align 16
@lastopening = external dso_local local_unnamed_addr global ptr, align 8
@centerxfrac = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @R_ClearPlanes() local_unnamed_addr #0 {
  %1 = load i32, ptr @viewwidth, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %3 = load i32, ptr @viewheight, align 4, !tbaa !5
  %4 = trunc i32 %3 to i16
  %5 = zext nneg i32 %1 to i64
  %6 = shl nuw nsw i64 %5, 1
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @ceilingclip, i8 -1, i64 %6, i1 false), !tbaa !9
  %min.iters.check = icmp ult i32 %1, 16
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %5, 2147483632
  %broadcast.splatinsert = insertelement <8 x i16> poison, i16 %4, 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 ]
  %7 = getelementptr inbounds [320 x i16], ptr @floorclip, i64 0, i64 %index
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store <8 x i16> %broadcast.splat, ptr %7, align 16, !tbaa !9
  store <8 x i16> %broadcast.splat, ptr %8, align 16, !tbaa !9
  %index.next = add nuw i64 %index, 16
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !11

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

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %10 = getelementptr inbounds [320 x i16], ptr @floorclip, i64 0, i64 %indvars.iv
  store i16 %4, ptr %10, align 2, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %5
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %0
  store ptr @visplanes, ptr @lastvisplane, align 8, !tbaa !16
  store ptr @openings, ptr @lastopening, align 8, !tbaa !16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(800) @cachedheight, i8 0, i64 800, i1 false)
  %11 = load i32, ptr @viewangle, align 4, !tbaa !5
  %12 = add i32 %11, -1073741824
  %13 = lshr i32 %12, 19
  %14 = load ptr, ptr @finecosine, align 8, !tbaa !16
  %15 = zext nneg i32 %13 to i64
  %16 = getelementptr inbounds i32, ptr %14, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = load i32, ptr @centerxfrac, align 4, !tbaa !5
  %19 = tail call i32 @FixedDiv(i32 noundef %17, i32 noundef %18) #3
  store i32 %19, ptr @basexscale, align 4, !tbaa !5
  %20 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %15
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = load i32, ptr @centerxfrac, align 4, !tbaa !5
  %23 = tail call i32 @FixedDiv(i32 noundef %21, i32 noundef %22) #3
  %24 = sub nsw i32 0, %23
  store i32 %24, ptr @baseyscale, align 4, !tbaa !5
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

declare i32 @FixedDiv(i32 noundef, i32 noundef) local_unnamed_addr #2

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { "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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}
!16 = !{!17, !17, i64 0}
!17 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass