; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/int/sc_signed.ll (ZNK5sc_dt9sc_signed9to_doubleEv)

; 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.sc_dt::sc_signed" = type { %"class.sc_dt::sc_value_base", i32, i32, i32, ptr }
%"class.sc_dt::sc_value_base" = type { ptr }

; 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 nounwind uwtable
define dso_local noundef double @_ZNK5sc_dt9sc_signed9to_doubleEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #1 align 2 {
  %2 = alloca double, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store double 0.000000e+00, ptr %2, align 8
  br label %39

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %13 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 3
  %14 = load i32, ptr %13, align 8, !tbaa !13
  store i32 %14, ptr %4, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store double 0.000000e+00, ptr %5, align 8, !tbaa !15
  br label %15

15:                                               ; preds = %19, %12
  %16 = load i32, ptr %4, align 4, !tbaa !14
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %4, align 4, !tbaa !14
  %18 = icmp sge i32 %17, 0
  br i1 %18, label %19, label %29

19:                                               ; preds = %15
  %20 = load double, ptr %5, align 8, !tbaa !15
  %21 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 4
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  %23 = load i32, ptr %4, align 4, !tbaa !14
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = uitofp i32 %26 to double
  %28 = call double @llvm.fmuladd.f64(double %20, double 0x41D0000000000000, double %27)
  store double %28, ptr %5, align 8, !tbaa !15
  br label %15, !llvm.loop !18

29:                                               ; preds = %15
  %30 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 1
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = icmp eq i32 %31, -1
  br i1 %32, label %33, label %36

33:                                               ; preds = %29
  %34 = load double, ptr %5, align 8, !tbaa !15
  %35 = fneg double %34
  store double %35, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %38

36:                                               ; preds = %29
  %37 = load double, ptr %5, align 8, !tbaa !15
  store double %37, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %38

38:                                               ; preds = %36, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %39

39:                                               ; preds = %38, %11
  %40 = load double, ptr %2, align 8
  ret double %40
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 speculatable willreturn memory(none) }
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, !12, i64 8}
!10 = !{!"_ZTSN5sc_dt9sc_signedE", !11, i64 0, !12, i64 8, !12, i64 12, !12, i64 16, !6, i64 24}
!11 = !{!"_ZTSN5sc_dt13sc_value_baseE"}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 16}
!14 = !{!12, !12, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = !{!10, !6, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZNK5sc_dt9sc_signed9to_doubleEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %35, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i32, ptr %6, align 8, !tbaa !12
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = zext nneg i32 %7 to i64
  %xtraiter = and i64 %11, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %.lr.ph, %.prol.preheader
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.prol.preheader ], [ %11, %.lr.ph ]
  %.010.prol = phi double [ %15, %.prol.preheader ], [ 0.000000e+00, %.lr.ph ]
  %prol.iter = phi i64 [ %prol.iter.next, %.prol.preheader ], [ 0, %.lr.ph ]
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, -1
  %12 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv.next.prol
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = uitofp i32 %13 to double
  %15 = tail call double @llvm.fmuladd.f64(double %.010.prol, double 0x41D0000000000000, double %14)
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !15

.prol.loopexit:                                   ; preds = %.prol.preheader, %.lr.ph
  %indvars.iv.unr = phi i64 [ %11, %.lr.ph ], [ %indvars.iv.next.prol, %.prol.preheader ]
  %.010.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %15, %.prol.preheader ]
  %.lcssa.unr = phi double [ poison, %.lr.ph ], [ %15, %.prol.preheader ]
  %16 = icmp ult i32 %7, 4
  br i1 %16, label %._crit_edge, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %10, i64 -4
  %invariant.gep14 = getelementptr i8, ptr %10, i64 -8
  br label %17

17:                                               ; preds = %17, %.lr.ph.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.new ], [ %indvars.iv.next.3, %17 ]
  %.010 = phi double [ %.010.unr, %.lr.ph.new ], [ %31, %17 ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  %18 = load i32, ptr %gep, align 4, !tbaa !14
  %19 = uitofp i32 %18 to double
  %20 = tail call double @llvm.fmuladd.f64(double %.010, double 0x41D0000000000000, double %19)
  %gep15 = getelementptr i32, ptr %invariant.gep14, i64 %indvars.iv
  %21 = load i32, ptr %gep15, align 4, !tbaa !14
  %22 = uitofp i32 %21 to double
  %23 = tail call double @llvm.fmuladd.f64(double %20, double 0x41D0000000000000, double %22)
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %24 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv.next.2
  %25 = load i32, ptr %24, align 4, !tbaa !14
  %26 = uitofp i32 %25 to double
  %27 = tail call double @llvm.fmuladd.f64(double %23, double 0x41D0000000000000, double %26)
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %28 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv.next.3
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = uitofp i32 %29 to double
  %31 = tail call double @llvm.fmuladd.f64(double %27, double 0x41D0000000000000, double %30)
  %32 = icmp ugt i64 %indvars.iv.next.2, 1
  br i1 %32, label %17, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %.prol.loopexit, %17, %5
  %.0.lcssa = phi double [ 0.000000e+00, %5 ], [ %.lcssa.unr, %.prol.loopexit ], [ %31, %17 ]
  %33 = icmp eq i32 %3, -1
  %34 = fneg double %.0.lcssa
  %.07 = select i1 %33, double %34, double %.0.lcssa
  br label %35

35:                                               ; preds = %1, %._crit_edge
  %.1 = phi double [ %.07, %._crit_edge ], [ 0.000000e+00, %1 ]
  ret double %.1
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = { mustprogress 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, !8, i64 8}
!6 = !{!"_ZTSN5sc_dt9sc_signedE", !7, i64 0, !8, i64 8, !8, i64 12, !8, i64 16, !11, i64 24}
!7 = !{!"_ZTSN5sc_dt13sc_value_baseE"}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!6, !8, i64 16}
!13 = !{!6, !11, i64 24}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass