<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/93511>93511</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLVM treat  the use of available constant global as 0 incorrectly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:core,
            miscompilation,
            llvm:ir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ChuanqiXu9
      </td>
    </tr>
</table>

<pre>
    Comes from https://github.com/llvm/llvm-project/issues/93497

The reproducer (https://godbolt.org/z/P1f4v83Mr):

```LLVM IR
%struct.Thing = type { i32 }

@_ZN5Thing3OneE = available_externally constant %struct.Thing zeroinitializer, align 4
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_main.cpp, ptr null }]

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
entry:
  %t1 = alloca %struct.Thing, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %t1) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %t1, ptr align 4 @_ZN5Thing3OneE, i64 4, i1 false)
  %raw = getelementptr inbounds %struct.Thing, ptr %t1, i32 0, i32 0
  %0 = load i32, ptr %raw, align 4
  call void @consume(i32 noundef %0)
  call void @llvm.lifetime.end.p0(i64 4, ptr %t1) #5
  ret i32 0
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare void @consume(i32 noundef) #3

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

declare void @_ZGIW7bug1mod()

; Function Attrs: uwtable
define internal void @_GLOBAL__sub_I_main.cpp() #4 section ".text.startup" {
entry:
  call void @_ZGIW7bug1mod()
  ret void
}

attributes #0 = { mustprogress norecurse 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 #4 = { 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 #5 = { nounwind }

!llvm.linker.options = !{}
!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.0.0git (git@github.com:llvm/llvm-project.git 4d5a0a013e61e8c34ed0b481ee6b398fd80df909)"}
!5 = !{i64 0, i64 4, !6}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"_ZTS5Thing", !7, i64 0}
```

The code initialize a local variable `%t1` from an available constant global variable `@_ZN5Thing3OneE` and consume that. This is the code generated from the frontend. I feel it looks good.

However, the use of available constant global  variable is "optimized" to `0` in llc even if optimization is not turned on. Also if I use `opt` with `O1`, the use of  available constant global  variable  is also optimized as `0`. I feel this is somehow incorrect. Or do I misread something?

CC: @nikic @jyknight 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWEtv4zgS_jX0pRBBoh6WDz7kMZkNkNke7AS7i74YlFSSOaFILUnZcX79gnpZduxMz24f5tBAENFS1VdVH4tFFpkxvJKIaxLfkfhhwVq7VXp9v22Z_A__d7taZKo4rO9VjQZKrWrYWtsYEt4S-kjoY8Xtts28XNWEPgqxGx83jVa_Y24JfeTGtGgIfVyF0WpJ_Afi3_b_X7YIGhutijZHDYSmZ-CqyJSwntIVoY_vhD7-GpTRLg1_0YSunNgMjCR-__f8_M9f4Okfw1saG6vb3HovWy4rIOED2EODQJZ3wEMKZPlwghL5m69_jzvh8IvEnzoNtmNcsEzgBt8sasmEOECupLFMWji38Y5accktZ4K_oyb0HpjglYRosuEo8iqhMiY2uVXa9GaaBmXhIPpPQOK7AN5GXx1QY_Xw6DyPH5zM1e9TlEkch_H0IfI3Pz9_ubt93mxMm22eNjXj0subZpSQrRAD_gk54R08tjK3XEm4tVa7qYK6NbbRqtJoDEilMW-1QWj31jHWaxZYcolQGLURKmcCpGplgWU_BZHvHCA0JXQFhIa-87tXRGn1YZppcFzboCdLOKRz8j-QDZAzIWCneAEj8YKXaHmNnrFMW6_xCU15EkE0MeSsDM7E13FqrPPm4DW---NJRGjqtAf7I0oPOb08TzAnMBnnAZRMGHTpfYxYs30XcoUWBdYorUPkMnMkmksUzIK47zj2j4Mjrt-hCsWKWfoMBv-ISJf9bY2OuZBO0-lAZ75_Qj3K4luJ12jnrp-v2ItJKV2WiYzlryBVqRFBKnOQeefpnssC9lwIjbbVEmqslT4QmjJd1Vg7fY2s2GtujzNRYC6Yxm_LJF7XTE8z4ZxpbKtxiCz43_z_no5fSV2pmODMHD2GDktJcTgGcy7iTI4SHVSXxyMFXcR0HvG5QxdTaVAM_3pTfZK5f2Kiz1E3X39--tcya6ugVkV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