; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R2Shapes/R2Grid.ll (ZN6R2Grid5ErodeEd)

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

%class.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x double]] }

declare i32 @__gxx_personality_v0(...)

; 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 uwtable
declare dso_local void @_ZN6R2Grid24SquaredDistanceTransformEv(ptr noundef nonnull align 8 dereferenceable(208)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208), double noundef, double noundef, double noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R2Grid5ErodeEd(ptr noundef nonnull align 8 dereferenceable(208) %0, double noundef %1) #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca %class.R2Grid, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store double %1, ptr %4, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 208, ptr %5) #3
  call void @_ZN6R2GridC1ERKS_(ptr noundef nonnull align 8 dereferenceable(208) %5, ptr noundef nonnull align 8 dereferenceable(208) %9)
  invoke void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208) %9, double noundef 0x3BC79CA10C924223, double noundef 1.000000e+00, double noundef 0.000000e+00)
          to label %10 unwind label %22

10:                                               ; preds = %2
  invoke void @_ZN6R2Grid24SquaredDistanceTransformEv(ptr noundef nonnull align 8 dereferenceable(208) %9)
          to label %11 unwind label %22

11:                                               ; preds = %10
  %12 = load double, ptr %4, align 8, !tbaa !9
  %13 = load double, ptr %4, align 8, !tbaa !9
  %14 = fmul double %12, %13
  invoke void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208) %9, double noundef %14, double noundef 0.000000e+00, double noundef 1.000000e+00)
          to label %15 unwind label %22

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %16

16:                                               ; preds = %41, %15
  %17 = load i32, ptr %8, align 4, !tbaa !11
  %18 = getelementptr inbounds %class.R2Grid, ptr %9, i32 0, i32 6
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %26, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %44

22:                                               ; preds = %11, %10, %2
  %23 = landingpad { ptr, i32 }
          cleanup
  %24 = extractvalue { ptr, i32 } %23, 0
  store ptr %24, ptr %6, align 8
  %25 = extractvalue { ptr, i32 } %23, 1
  store i32 %25, ptr %7, align 4
  call void @_ZN6R2GridD1Ev(ptr noundef nonnull align 8 dereferenceable(208) %5) #3
  call void @llvm.lifetime.end.p0(i64 208, ptr %5) #3
  br label %45

26:                                               ; preds = %16
  %27 = getelementptr inbounds %class.R2Grid, ptr %5, i32 0, i32 3
  %28 = load ptr, ptr %27, align 8, !tbaa !18
  %29 = load i32, ptr %8, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds double, ptr %28, i64 %30
  %32 = load double, ptr %31, align 8, !tbaa !9
  %33 = fcmp oeq double %32, -3.210000e+02
  br i1 %33, label %34, label %40

34:                                               ; preds = %26
  %35 = getelementptr inbounds %class.R2Grid, ptr %9, i32 0, i32 3
  %36 = load ptr, ptr %35, align 8, !tbaa !18
  %37 = load i32, ptr %8, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds double, ptr %36, i64 %38
  store double -3.210000e+02, ptr %39, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %34, %26
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %8, align 4, !tbaa !11
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !11
  br label %16, !llvm.loop !19

44:                                               ; preds = %21
  call void @_ZN6R2GridD1Ev(ptr noundef nonnull align 8 dereferenceable(208) %5) #3
  call void @llvm.lifetime.end.p0(i64 208, ptr %5) #3
  ret void

45:                                               ; preds = %22
  %46 = load ptr, ptr %6, align 8
  %47 = load i32, ptr %7, align 4
  %48 = insertvalue { ptr, i32 } poison, ptr %46, 0
  %49 = insertvalue { ptr, i32 } %48, i32 %47, 1
  resume { ptr, i32 } %49
}

declare void @_ZN6R2GridC1ERKS_(ptr, ptr)

declare void @_ZN6R2GridD1Ev(ptr)

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 204}
!14 = !{!"_ZTS6R2Grid", !15, i64 0, !15, i64 88, !10, i64 176, !6, i64 184, !7, i64 192, !12, i64 200, !12, i64 204}
!15 = !{!"_ZTS8R2Affine", !16, i64 0, !17, i64 8, !12, i64 80}
!16 = !{!"_ZTS16R2Transformation"}
!17 = !{!"_ZTS8R3Matrix", !7, i64 0}
!18 = !{!14, !6, i64 184}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%class.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x double]] }

declare i32 @__gxx_personality_v0(...)

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6R2Grid24SquaredDistanceTransformEv(ptr noundef nonnull align 8 dereferenceable(208)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208), double noundef, double noundef, double noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R2Grid5ErodeEd(ptr noundef nonnull align 8 dereferenceable(208) %0, double noundef %1) local_unnamed_addr #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca %class.R2Grid, align 8
  call void @llvm.lifetime.start.p0(i64 208, ptr nonnull %3) #3
  call void @_ZN6R2GridC1ERKS_(ptr noundef nonnull align 8 dereferenceable(208) %3, ptr noundef nonnull align 8 dereferenceable(208) %0)
  call void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208) %0, double noundef 0x3BC79CA10C924223, double noundef 1.000000e+00, double noundef 0.000000e+00)
  invoke void @_ZN6R2Grid24SquaredDistanceTransformEv(ptr noundef nonnull align 8 dereferenceable(208) %0)
          to label %4 unwind label %39

4:                                                ; preds = %2
  %5 = fmul double %1, %1
  call void @_ZN6R2Grid9ThresholdEddd(ptr noundef nonnull align 8 dereferenceable(208) %0, double noundef %5, double noundef 0.000000e+00, double noundef 1.000000e+00)
  %6 = getelementptr inbounds i8, ptr %0, i64 204
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %9 = getelementptr inbounds i8, ptr %3, i64 184
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %0, i64 184
  %12 = load ptr, ptr %11, align 8
  %wide.trip.count = zext nneg i32 %7 to i64
  %min.iters.check = icmp ult i32 %7, 4
  %13 = ptrtoint ptr %12 to i64
  %14 = ptrtoint ptr %10 to i64
  %15 = sub i64 %13, %14
  %diff.check = icmp ult i64 %15, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 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
  %16 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.ph
  %17 = load double, ptr %16, align 8, !tbaa !16
  %18 = fcmp oeq double %17, -3.210000e+02
  br i1 %18, label %19, label %scalar.ph.prol.loopexit.unr-lcssa

19:                                               ; preds = %scalar.ph.prol
  %20 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.ph
  store double -3.210000e+02, ptr %20, align 8, !tbaa !16
  br label %scalar.ph.prol.loopexit.unr-lcssa

scalar.ph.prol.loopexit.unr-lcssa:                ; preds = %19, %scalar.ph.prol
  %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.loopexit.unr-lcssa, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol.loopexit.unr-lcssa ]
  %21 = add nsw i64 %wide.trip.count, -1
  %22 = icmp eq i64 %indvars.iv.ph, %21
  br i1 %22, label %._crit_edge, label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %wide.trip.count, 2147483644
  br label %vector.body

vector.body:                                      ; preds = %pred.store.continue16, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %pred.store.continue16 ]
  %23 = or disjoint i64 %index, 2
  %24 = getelementptr inbounds double, ptr %10, i64 %index
  %25 = getelementptr inbounds i8, ptr %24, i64 16
  %wide.load = load <2 x double>, ptr %24, align 8, !tbaa !16
  %wide.load10 = load <2 x double>, ptr %25, align 8, !tbaa !16
  %26 = fcmp oeq <2 x double> %wide.load, <double -3.210000e+02, double -3.210000e+02>
  %27 = fcmp oeq <2 x double> %wide.load10, <double -3.210000e+02, double -3.210000e+02>
  %28 = extractelement <2 x i1> %26, i64 0
  br i1 %28, label %pred.store.if, label %pred.store.continue

pred.store.if:                                    ; preds = %vector.body
  %29 = getelementptr inbounds double, ptr %12, i64 %index
  store double -3.210000e+02, ptr %29, align 8, !tbaa !16
  br label %pred.store.continue

pred.store.continue:                              ; preds = %pred.store.if, %vector.body
  %30 = extractelement <2 x i1> %26, i64 1
  br i1 %30, label %pred.store.if11, label %pred.store.continue12

pred.store.if11:                                  ; preds = %pred.store.continue
  %31 = or disjoint i64 %index, 1
  %32 = getelementptr inbounds double, ptr %12, i64 %31
  store double -3.210000e+02, ptr %32, align 8, !tbaa !16
  br label %pred.store.continue12

pred.store.continue12:                            ; preds = %pred.store.if11, %pred.store.continue
  %33 = extractelement <2 x i1> %27, i64 0
  br i1 %33, label %pred.store.if13, label %pred.store.continue14

pred.store.if13:                                  ; preds = %pred.store.continue12
  %34 = getelementptr inbounds double, ptr %12, i64 %23
  store double -3.210000e+02, ptr %34, align 8, !tbaa !16
  br label %pred.store.continue14

pred.store.continue14:                            ; preds = %pred.store.if13, %pred.store.continue12
  %35 = extractelement <2 x i1> %27, i64 1
  br i1 %35, label %pred.store.if15, label %pred.store.continue16

pred.store.if15:                                  ; preds = %pred.store.continue14
  %36 = or disjoint i64 %index, 3
  %37 = getelementptr inbounds double, ptr %12, i64 %36
  store double -3.210000e+02, ptr %37, align 8, !tbaa !16
  br label %pred.store.continue16

pred.store.continue16:                            ; preds = %pred.store.if15, %pred.store.continue14
  %index.next = add nuw i64 %index, 4
  %38 = icmp eq i64 %index.next, %n.vec
  br i1 %38, label %middle.block, label %vector.body, !llvm.loop !17

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

._crit_edge:                                      ; preds = %scalar.ph.prol.loopexit, %51, %middle.block, %4
  call void @_ZN6R2GridD1Ev(ptr noundef nonnull align 8 dereferenceable(208) %3) #3
  call void @llvm.lifetime.end.p0(i64 208, ptr nonnull %3) #3
  ret void

39:                                               ; preds = %2
  %40 = landingpad { ptr, i32 }
          cleanup
  call void @_ZN6R2GridD1Ev(ptr noundef nonnull align 8 dereferenceable(208) %3) #3
  call void @llvm.lifetime.end.p0(i64 208, ptr nonnull %3) #3
  resume { ptr, i32 } %40

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %51
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %51 ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %41 = getelementptr inbounds double, ptr %10, i64 %indvars.iv
  %42 = load double, ptr %41, align 8, !tbaa !16
  %43 = fcmp oeq double %42, -3.210000e+02
  br i1 %43, label %44, label %scalar.ph.1

44:                                               ; preds = %scalar.ph
  %45 = getelementptr inbounds double, ptr %12, i64 %indvars.iv
  store double -3.210000e+02, ptr %45, align 8, !tbaa !16
  br label %scalar.ph.1

scalar.ph.1:                                      ; preds = %scalar.ph, %44
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %46 = getelementptr inbounds double, ptr %10, i64 %indvars.iv.next
  %47 = load double, ptr %46, align 8, !tbaa !16
  %48 = fcmp oeq double %47, -3.210000e+02
  br i1 %48, label %49, label %51

49:                                               ; preds = %scalar.ph.1
  %50 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.next
  store double -3.210000e+02, ptr %50, align 8, !tbaa !16
  br label %51

51:                                               ; preds = %49, %scalar.ph.1
  %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 !21
}

declare void @_ZN6R2GridC1ERKS_(ptr, ptr) local_unnamed_addr

declare void @_ZN6R2GridD1Ev(ptr) local_unnamed_addr

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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 #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, !12, i64 204}
!6 = !{!"_ZTS6R2Grid", !7, i64 0, !7, i64 88, !13, i64 176, !14, i64 184, !10, i64 192, !12, i64 200, !12, i64 204}
!7 = !{!"_ZTS8R2Affine", !8, i64 0, !9, i64 8, !12, i64 80}
!8 = !{!"_ZTS16R2Transformation"}
!9 = !{!"_ZTS8R3Matrix", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"double", !10, i64 0}
!14 = !{!"any pointer", !10, i64 0}
!15 = !{!6, !14, i64 184}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !19}


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