; LLVM IR from lzyrapx/Competitive-Programming :: 51Nod/1137（矩阵乘法）.ll (Z9mitriplus3MatS_)

; 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.Mat = type { [100 x [100 x i32]] }

@n = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9mitriplus3MatS_(ptr dead_on_unwind noalias writable sret(%struct.Mat) align 4 %0, ptr noundef byval(%struct.Mat) align 8 %1, ptr noundef byval(%struct.Mat) align 8 %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %67, %3
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %70

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %63, %13
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %66

19:                                               ; preds = %14
  %20 = getelementptr inbounds %struct.Mat, ptr %0, i32 0, i32 0
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100 x [100 x i32]], ptr %20, i64 0, i64 %22
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100 x i32], ptr %23, i64 0, i64 %25
  store i32 0, ptr %26, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %59, %19
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = load i32, ptr @n, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  store i32 8, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %62

32:                                               ; preds = %27
  %33 = getelementptr inbounds %struct.Mat, ptr %1, i32 0, i32 0
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100 x [100 x i32]], ptr %33, i64 0, i64 %35
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100 x i32], ptr %36, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = getelementptr inbounds %struct.Mat, ptr %2, i32 0, i32 0
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100 x [100 x i32]], ptr %41, i64 0, i64 %43
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100 x i32], ptr %44, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = mul nsw i32 %40, %48
  %50 = getelementptr inbounds %struct.Mat, ptr %0, i32 0, i32 0
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [100 x [100 x i32]], ptr %50, i64 0, i64 %52
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [100 x i32], ptr %53, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = add nsw i32 %57, %49
  store i32 %58, ptr %56, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %32
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %27, !llvm.loop !9

62:                                               ; preds = %31
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %6, align 4, !tbaa !5
  br label %14, !llvm.loop !11

66:                                               ; preds = %18
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %4, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !12

70:                                               ; preds = %12
  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 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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.Mat = type { [100 x [100 x i32]] }

@n = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z9mitriplus3MatS_(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.Mat) align 4 %0, ptr nocapture noundef readonly byval(%struct.Mat) align 8 %1, ptr nocapture noundef readonly byval(%struct.Mat) align 8 %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.preheader.us.preheader, label %._crit_edge

.preheader.us.preheader:                          ; preds = %3
  %wide.trip.count30 = zext nneg i32 %4 to i64
  %min.iters.check = icmp ult i32 %4, 8
  %n.vec = and i64 %wide.trip.count30, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count30
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge19.split.us.us
  %indvars.iv27 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next28, %._crit_edge19.split.us.us ]
  br label %.lr.ph.us.us

.lr.ph.us.us:                                     ; preds = %._crit_edge.us.us, %.preheader.us
  %indvars.iv22 = phi i64 [ %indvars.iv.next23, %._crit_edge.us.us ], [ 0, %.preheader.us ]
  %6 = getelementptr inbounds [100 x [100 x i32]], ptr %0, i64 0, i64 %indvars.iv27, i64 %indvars.iv22
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.lr.ph.us.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.lr.ph.us.us ]
  %vec.phi = phi <4 x i32> [ %42, %vector.body ], [ zeroinitializer, %.lr.ph.us.us ]
  %vec.phi32 = phi <4 x i32> [ %43, %vector.body ], [ zeroinitializer, %.lr.ph.us.us ]
  %7 = or disjoint i64 %index, 1
  %8 = or disjoint i64 %index, 2
  %9 = or disjoint i64 %index, 3
  %10 = or disjoint i64 %index, 4
  %11 = or disjoint i64 %index, 5
  %12 = or disjoint i64 %index, 6
  %13 = or disjoint i64 %index, 7
  %14 = getelementptr inbounds [100 x [100 x i32]], ptr %1, i64 0, i64 %indvars.iv27, i64 %index
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %wide.load = load <4 x i32>, ptr %14, align 8, !tbaa !5
  %wide.load33 = load <4 x i32>, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %index, i64 %indvars.iv22
  %17 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %7, i64 %indvars.iv22
  %18 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %8, i64 %indvars.iv22
  %19 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %9, i64 %indvars.iv22
  %20 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %10, i64 %indvars.iv22
  %21 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %11, i64 %indvars.iv22
  %22 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %12, i64 %indvars.iv22
  %23 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %13, i64 %indvars.iv22
  %24 = load i32, ptr %16, align 4, !tbaa !5
  %25 = load i32, ptr %17, align 4, !tbaa !5
  %26 = load i32, ptr %18, align 4, !tbaa !5
  %27 = load i32, ptr %19, align 4, !tbaa !5
  %28 = insertelement <4 x i32> poison, i32 %24, i64 0
  %29 = insertelement <4 x i32> %28, i32 %25, i64 1
  %30 = insertelement <4 x i32> %29, i32 %26, i64 2
  %31 = insertelement <4 x i32> %30, i32 %27, i64 3
  %32 = load i32, ptr %20, align 4, !tbaa !5
  %33 = load i32, ptr %21, align 4, !tbaa !5
  %34 = load i32, ptr %22, align 4, !tbaa !5
  %35 = load i32, ptr %23, align 4, !tbaa !5
  %36 = insertelement <4 x i32> poison, i32 %32, i64 0
  %37 = insertelement <4 x i32> %36, i32 %33, i64 1
  %38 = insertelement <4 x i32> %37, i32 %34, i64 2
  %39 = insertelement <4 x i32> %38, i32 %35, i64 3
  %40 = mul nsw <4 x i32> %31, %wide.load
  %41 = mul nsw <4 x i32> %39, %wide.load33
  %42 = add <4 x i32> %vec.phi, %40
  %43 = add <4 x i32> %vec.phi32, %41
  %index.next = add nuw i64 %index, 8
  %44 = icmp eq i64 %index.next, %n.vec
  br i1 %44, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %43, %42
  %45 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us.us
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.us.us ], [ %n.vec, %middle.block ]
  %.ph = phi i32 [ 0, %.lr.ph.us.us ], [ %45, %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 ]
  %46 = phi i32 [ %52, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %47 = getelementptr inbounds [100 x [100 x i32]], ptr %1, i64 0, i64 %indvars.iv27, i64 %indvars.iv
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = getelementptr inbounds [100 x [100 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 %indvars.iv22
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = mul nsw i32 %50, %48
  %52 = add nsw i32 %46, %51
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count30
  br i1 %exitcond.not, label %._crit_edge.us.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us.us:                                ; preds = %scalar.ph, %middle.block
  %.lcssa = phi i32 [ %45, %middle.block ], [ %52, %scalar.ph ]
  store i32 %.lcssa, ptr %6, align 4, !tbaa !5
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %exitcond26.not = icmp eq i64 %indvars.iv.next23, %wide.trip.count30
  br i1 %exitcond26.not, label %._crit_edge19.split.us.us, label %.lr.ph.us.us, !llvm.loop !14

._crit_edge19.split.us.us:                        ; preds = %._crit_edge.us.us
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond31.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count30
  br i1 %exitcond31.not, label %._crit_edge, label %.preheader.us, !llvm.loop !15

._crit_edge:                                      ; preds = %._crit_edge19.split.us.us, %3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: 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 #1 = { 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


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