; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/数学算法/快速幂运算.ll (Z3pow6matrixi)

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

@n = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZmlRK6matrixS1_(ptr dead_on_unwind noalias writable sret(%struct.matrix) align 4, ptr noundef nonnull align 4 dereferenceable(40000), ptr noundef nonnull align 4 dereferenceable(40000)) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6matrixC2Ev(ptr noundef nonnull align 4 dereferenceable(40000)) unnamed_addr #1 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6matrixixEi(ptr noundef nonnull align 4 dereferenceable(40000), i32 noundef) #1 align 2

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

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

8:                                                ; preds = %19, %3
  %9 = load i32, ptr %5, 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
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %22

13:                                               ; preds = %8
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = call noundef ptr @_ZN6matrixixEi(ptr noundef nonnull align 4 dereferenceable(40000) %0, i32 noundef %14)
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr %15, i64 %17
  store i32 1, ptr %18, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %13
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !9

22:                                               ; preds = %12
  br label %23

23:                                               ; preds = %31, %22
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %34

26:                                               ; preds = %23
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = and i32 %27, 1
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 40000, ptr %6) #4
  call void @_ZmlRK6matrixS1_(ptr dead_on_unwind writable sret(%struct.matrix) align 4 %6, ptr noundef nonnull align 4 dereferenceable(40000) %0, ptr noundef nonnull align 4 dereferenceable(40000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %0, ptr align 4 %6, i64 40000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 40000, ptr %6) #4
  br label %31

31:                                               ; preds = %30, %26
  call void @llvm.lifetime.start.p0(i64 40000, ptr %7) #4
  call void @_ZmlRK6matrixS1_(ptr dead_on_unwind writable sret(%struct.matrix) align 4 %7, ptr noundef nonnull align 4 dereferenceable(40000) %1, ptr noundef nonnull align 4 dereferenceable(40000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %1, ptr align 4 %7, i64 40000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 40000, ptr %7) #4
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = ashr i32 %32, 1
  store i32 %33, ptr %4, align 4, !tbaa !5
  br label %23, !llvm.loop !13

34:                                               ; preds = %23
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

attributes #0 = { 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 #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{i64 0, i64 40000, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !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.matrix = type { [100 x [100 x i32]] }

@n = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZmlRK6matrixS1_(ptr dead_on_unwind noalias writable sret(%struct.matrix) align 4, ptr noundef nonnull align 4 dereferenceable(40000), ptr noundef nonnull align 4 dereferenceable(40000)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6matrixC2Ev(ptr noundef nonnull align 4 dereferenceable(40000)) unnamed_addr #1 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN6matrixixEi(ptr noundef nonnull align 4 dereferenceable(40000), i32 noundef) local_unnamed_addr #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3pow6matrixi(ptr dead_on_unwind noalias nonnull writable sret(%struct.matrix) align 4 %0, ptr noundef byval(%struct.matrix) align 8 %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.matrix, align 4
  %5 = alloca %struct.matrix, align 4
  tail call void @_ZN6matrixC2Ev(ptr noundef nonnull align 4 dereferenceable(40000) %0)
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %3
  %.not10 = icmp eq i32 %2, 0
  br i1 %.not10, label %._crit_edge, label %.lr.ph12

.lr.ph:                                           ; preds = %3, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %3 ]
  %8 = trunc nuw nsw i64 %indvars.iv to i32
  %9 = tail call noundef ptr @_ZN6matrixixEi(ptr noundef nonnull align 4 dereferenceable(40000) %0, i32 noundef %8)
  %10 = getelementptr inbounds i32, ptr %9, i64 %indvars.iv
  store i32 1, ptr %10, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = icmp slt i64 %indvars.iv.next, %12
  br i1 %13, label %.lr.ph, label %.preheader, !llvm.loop !9

.lr.ph12:                                         ; preds = %.preheader, %16
  %.0711 = phi i32 [ %17, %16 ], [ %2, %.preheader ]
  %14 = and i32 %.0711, 1
  %.not8 = icmp eq i32 %14, 0
  br i1 %.not8, label %16, label %15

15:                                               ; preds = %.lr.ph12
  call void @llvm.lifetime.start.p0(i64 40000, ptr nonnull %4) #4
  call void @_ZmlRK6matrixS1_(ptr dead_on_unwind nonnull writable sret(%struct.matrix) align 4 %4, ptr noundef nonnull align 4 dereferenceable(40000) %0, ptr noundef nonnull align 4 dereferenceable(40000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(40000) %0, ptr noundef nonnull align 4 dereferenceable(40000) %4, i64 40000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 40000, ptr nonnull %4) #4
  br label %16

16:                                               ; preds = %15, %.lr.ph12
  call void @llvm.lifetime.start.p0(i64 40000, ptr nonnull %5) #4
  call void @_ZmlRK6matrixS1_(ptr dead_on_unwind nonnull writable sret(%struct.matrix) align 4 %5, ptr noundef nonnull align 4 dereferenceable(40000) %1, ptr noundef nonnull align 4 dereferenceable(40000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40000) %1, ptr noundef nonnull align 4 dereferenceable(40000) %5, i64 40000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 40000, ptr nonnull %5) #4
  %17 = ashr i32 %.0711, 1
  %.not = icmp ult i32 %.0711, 2
  br i1 %.not, label %._crit_edge, label %.lr.ph12, !llvm.loop !13

._crit_edge:                                      ; preds = %16, %.preheader
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

attributes #0 = { 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 #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{i64 0, i64 40000, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !10}


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass