; LLVM IR from aspuru-guzik-group/qtorch :: nlopt-2.4.2/stogo/linalg.ll (Z3gerdRK7RVectorS1_RK7RMatrix)

; 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.RVector = type { i32, ptr }
%class.RMatrix = type <{ ptr, i32, [4 x i8] }>

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3gerdRK7RVectorS1_RK7RMatrix(double noundef %0, ptr noundef nonnull align 8 dereferenceable(16) %1, ptr noundef nonnull align 8 dereferenceable(16) %2, ptr noundef nonnull align 8 dereferenceable(12) %3) #2 {
  %5 = alloca double, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store double %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds %class.RVector, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  store i32 %16, ptr %9, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !9
  %18 = getelementptr inbounds %class.RMatrix, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  store ptr %19, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !14
  br label %20

20:                                               ; preds = %58, %4
  %21 = load i32, ptr %11, align 4, !tbaa !14
  %22 = load i32, ptr %9, align 4, !tbaa !14
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 2, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %61

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !14
  br label %26

26:                                               ; preds = %54, %25
  %27 = load i32, ptr %13, align 4, !tbaa !14
  %28 = load i32, ptr %9, align 4, !tbaa !14
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 5, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %57

31:                                               ; preds = %26
  %32 = load double, ptr %5, align 8, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds %class.RVector, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !17
  %36 = load i32, ptr %11, align 4, !tbaa !14
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds double, ptr %35, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !5
  %40 = fmul double %32, %39
  %41 = load ptr, ptr %7, align 8, !tbaa !9
  %42 = getelementptr inbounds %class.RVector, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = load i32, ptr %13, align 4, !tbaa !14
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds double, ptr %43, i64 %45
  %47 = load double, ptr %46, align 8, !tbaa !5
  %48 = load ptr, ptr %10, align 8, !tbaa !9
  %49 = load double, ptr %48, align 8, !tbaa !5
  %50 = call double @llvm.fmuladd.f64(double %40, double %47, double %49)
  %51 = load ptr, ptr %10, align 8, !tbaa !9
  store double %50, ptr %51, align 8, !tbaa !5
  %52 = load ptr, ptr %10, align 8, !tbaa !9
  %53 = getelementptr inbounds double, ptr %52, i32 1
  store ptr %53, ptr %10, align 8, !tbaa !9
  br label %54

54:                                               ; preds = %31
  %55 = load i32, ptr %13, align 4, !tbaa !14
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %13, align 4, !tbaa !14
  br label %26, !llvm.loop !18

57:                                               ; preds = %30
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %11, align 4, !tbaa !14
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %11, align 4, !tbaa !14
  br label %20, !llvm.loop !20

61:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"double", !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, !13, i64 0}
!12 = !{!"_ZTS7RVector", !13, i64 0, !10, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !10, i64 0}
!16 = !{!"_ZTS7RMatrix", !10, i64 0, !13, i64 8}
!17 = !{!12, !10, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z3gerdRK7RVectorS1_RK7RMatrix(double noundef %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %1, ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %2, ptr nocapture noundef nonnull readonly align 8 dereferenceable(12) %3) local_unnamed_addr #1 {
  %5 = load i32, ptr %1, align 8, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.preheader.lr.ph, label %._crit_edge22

.preheader.lr.ph:                                 ; preds = %4
  %7 = load ptr, ptr %3, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = load ptr, ptr %8, align 8
  %10 = getelementptr inbounds i8, ptr %2, i64 8
  %11 = load ptr, ptr %10, align 8
  %wide.trip.count27 = zext nneg i32 %5 to i64
  %12 = shl nuw nsw i64 %wide.trip.count27, 3
  %scevgep29 = getelementptr i8, ptr %9, i64 %12
  %scevgep30 = getelementptr i8, ptr %11, i64 %12
  %min.iters.check = icmp ult i32 %5, 4
  %n.vec = and i64 %wide.trip.count27, 2147483644
  %13 = shl nuw nsw i64 %n.vec, 3
  %broadcast.splatinsert36 = insertelement <2 x double> poison, double %0, i64 0
  %broadcast.splat37 = shufflevector <2 x double> %broadcast.splatinsert36, <2 x double> poison, <2 x i32> zeroinitializer
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count27
  %xtraiter = and i64 %wide.trip.count27, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %14 = add nsw i64 %wide.trip.count27, -1
  %invariant.gep = getelementptr i8, ptr %11, i64 8
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph
  %indvars.iv24 = phi i64 [ %indvars.iv.next25, %._crit_edge.us ], [ 0, %.preheader.lr.ph ]
  %.01620.us = phi ptr [ %.lcssa, %._crit_edge.us ], [ %7, %.preheader.lr.ph ]
  %15 = getelementptr inbounds double, ptr %9, i64 %indvars.iv24
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.preheader.us
  %scevgep = getelementptr i8, ptr %.01620.us, i64 %12
  %bound0 = icmp ult ptr %.01620.us, %scevgep29
  %bound1 = icmp ult ptr %9, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  %bound031 = icmp ult ptr %.01620.us, %scevgep30
  %bound132 = icmp ult ptr %11, %scevgep
  %found.conflict33 = and i1 %bound031, %bound132
  %conflict.rdx = or i1 %found.conflict, %found.conflict33
  br i1 %conflict.rdx, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %ind.end = getelementptr i8, ptr %.01620.us, i64 %13
  %16 = load double, ptr %15, align 8, !tbaa !13, !alias.scope !15
  %broadcast.splatinsert38 = insertelement <2 x double> poison, double %16, i64 0
  %broadcast.splat39 = shufflevector <2 x double> %broadcast.splatinsert38, <2 x double> poison, <2 x i32> zeroinitializer
  %17 = fmul <2 x double> %broadcast.splat39, %broadcast.splat37
  %18 = fmul <2 x double> %broadcast.splat39, %broadcast.splat37
  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, 3
  %next.gep = getelementptr i8, ptr %.01620.us, i64 %offset.idx
  %19 = getelementptr inbounds double, ptr %11, i64 %index
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  %wide.load = load <2 x double>, ptr %19, align 8, !tbaa !13, !alias.scope !18
  %wide.load40 = load <2 x double>, ptr %20, align 8, !tbaa !13, !alias.scope !18
  %21 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load41 = load <2 x double>, ptr %next.gep, align 8, !tbaa !13, !alias.scope !20, !noalias !22
  %wide.load42 = load <2 x double>, ptr %21, align 8, !tbaa !13, !alias.scope !20, !noalias !22
  %22 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %17, <2 x double> %wide.load, <2 x double> %wide.load41)
  %23 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %18, <2 x double> %wide.load40, <2 x double> %wide.load42)
  store <2 x double> %22, ptr %next.gep, align 8, !tbaa !13, !alias.scope !20, !noalias !22
  store <2 x double> %23, ptr %21, align 8, !tbaa !13, !alias.scope !20, !noalias !22
  %index.next = add nuw i64 %index, 4
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !23

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.preheader.us
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  %.118.us.ph = phi ptr [ %.01620.us, %vector.memcheck ], [ %.01620.us, %.preheader.us ], [ %ind.end, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader
  %25 = load double, ptr %15, align 8, !tbaa !13
  %26 = fmul double %25, %0
  %27 = getelementptr inbounds double, ptr %11, i64 %indvars.iv.ph
  %28 = load double, ptr %27, align 8, !tbaa !13
  %29 = load double, ptr %.118.us.ph, align 8, !tbaa !13
  %30 = tail call double @llvm.fmuladd.f64(double %26, double %28, double %29)
  store double %30, ptr %.118.us.ph, align 8, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %.118.us.ph, i64 8
  %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
  %.lcssa43.unr = phi ptr [ poison, %scalar.ph.preheader ], [ %31, %scalar.ph.prol ]
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %.118.us.unr = phi ptr [ %.118.us.ph, %scalar.ph.preheader ], [ %31, %scalar.ph.prol ]
  %32 = icmp eq i64 %indvars.iv.ph, %14
  br i1 %32, label %._crit_edge.us, 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 ]
  %.118.us = phi ptr [ %45, %scalar.ph ], [ %.118.us.unr, %scalar.ph.prol.loopexit ]
  %33 = load double, ptr %15, align 8, !tbaa !13
  %34 = fmul double %33, %0
  %35 = getelementptr inbounds double, ptr %11, i64 %indvars.iv
  %36 = load double, ptr %35, align 8, !tbaa !13
  %37 = load double, ptr %.118.us, align 8, !tbaa !13
  %38 = tail call double @llvm.fmuladd.f64(double %34, double %36, double %37)
  store double %38, ptr %.118.us, align 8, !tbaa !13
  %39 = getelementptr inbounds i8, ptr %.118.us, i64 8
  %40 = load double, ptr %15, align 8, !tbaa !13
  %41 = fmul double %40, %0
  %gep = getelementptr double, ptr %invariant.gep, i64 %indvars.iv
  %42 = load double, ptr %gep, align 8, !tbaa !13
  %43 = load double, ptr %39, align 8, !tbaa !13
  %44 = tail call double @llvm.fmuladd.f64(double %41, double %42, double %43)
  store double %44, ptr %39, align 8, !tbaa !13
  %45 = getelementptr inbounds i8, ptr %.118.us, i64 16
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count27
  br i1 %exitcond.not.1, label %._crit_edge.us, label %scalar.ph, !llvm.loop !27

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %.lcssa = phi ptr [ %ind.end, %middle.block ], [ %.lcssa43.unr, %scalar.ph.prol.loopexit ], [ %45, %scalar.ph ]
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %exitcond28.not = icmp eq i64 %indvars.iv.next25, %wide.trip.count27
  br i1 %exitcond28.not, label %._crit_edge22, label %.preheader.us, !llvm.loop !28

._crit_edge22:                                    ; preds = %._crit_edge.us, %4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x double> @llvm.fmuladd.v2f64(<2 x double>, <2 x double>, <2 x double>) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress 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" }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !7, i64 0}
!6 = !{!"_ZTS7RVector", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS7RMatrix", !10, i64 0, !7, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !8, i64 0}
!15 = !{!16}
!16 = distinct !{!16, !17}
!17 = distinct !{!17, !"LVerDomain"}
!18 = !{!19}
!19 = distinct !{!19, !17}
!20 = !{!21}
!21 = distinct !{!21, !17}
!22 = !{!16, !19}
!23 = distinct !{!23, !24, !25, !26}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!"llvm.loop.isvectorized", i32 1}
!26 = !{!"llvm.loop.unroll.runtime.disable"}
!27 = distinct !{!27, !24, !25}
!28 = distinct !{!28, !24}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass