; LLVM IR from alibaba/BladeDISC :: tao_compiler/mlir/ral/ral_context.ll (ZN3tao3ral16ThreadLocalIndex3GetEv)

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

@_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index = external hidden thread_local global i32, align 4
@_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index = external hidden thread_local global i8, align 1
@_ZN3tao3ral12_GLOBAL__N_113nextThreadIdxE = external hidden global { i32 }, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN3tao3ral16ThreadLocalIndex3GetEv() #0 align 2 {
  %1 = load i8, ptr @_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index, align 1
  %2 = icmp eq i8 %1, 0
  br i1 %2, label %3, label %6, !prof !5

3:                                                ; preds = %0
  %4 = call noundef i32 @_ZNSt13__atomic_baseIiEppEi(ptr noundef nonnull align 4 dereferenceable(4) @_ZN3tao3ral12_GLOBAL__N_113nextThreadIdxE, i32 noundef 0) #2
  %5 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index)
  store i32 %4, ptr %5, align 4, !tbaa !6
  store i8 1, ptr @_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index, align 1
  br label %6

6:                                                ; preds = %3, %0
  %7 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index)
  %8 = load i32, ptr %7, align 4, !tbaa !6
  ret i32 %8
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #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 speculatable willreturn memory(none) }
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 = !{!"branch_weights", i32 1, i32 1023}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}


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

@_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index = external hidden thread_local local_unnamed_addr global i32, align 4
@_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index = external hidden thread_local local_unnamed_addr global i8, align 1
@_ZN3tao3ral12_GLOBAL__N_113nextThreadIdxE = external hidden global { i32 }, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN3tao3ral16ThreadLocalIndex3GetEv() local_unnamed_addr #0 align 2 {
  %1 = load i8, ptr @_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index, align 1
  %2 = icmp eq i8 %1, 0
  br i1 %2, label %3, label %._crit_edge, !prof !5

._crit_edge:                                      ; preds = %0
  %.pre = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index)
  br label %6

3:                                                ; preds = %0
  %4 = tail call noundef i32 @_ZNSt13__atomic_baseIiEppEi(ptr noundef nonnull align 4 dereferenceable(4) @_ZN3tao3ral12_GLOBAL__N_113nextThreadIdxE, i32 noundef 0) #2
  %5 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @_ZZN3tao3ral16ThreadLocalIndex3GetEvE5index)
  store i32 %4, ptr %5, align 4, !tbaa !6
  store i8 1, ptr @_ZGVZN3tao3ral16ThreadLocalIndex3GetEvE5index, align 1
  br label %6

6:                                                ; preds = %._crit_edge, %3
  %.pre-phi = phi ptr [ %.pre, %._crit_edge ], [ %5, %3 ]
  %7 = load i32, ptr %.pre-phi, align 4, !tbaa !6
  ret i32 %7
}

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"branch_weights", i32 1, i32 1023}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,TailCallElimPass