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

    <tr>
        <th>Summary</th>
        <td>
            [LICM] TSVC s113: LICM doesn't work for a load for an array with the constant index
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            missed-optimization,
            llvm:analysis
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          yus3710-fj
      </td>
    </tr>
</table>

<pre>
    LICM doesn't work for the loop in `s113` of [TSVC](https://www.netlib.org/benchmark/vectors).
This issue is related to #74262.

```fortran
      do 1 nl = 1,ntimes
      do 10 i = 2,n
        a(i) = a(1) + b(i)
   10 continue
      call dummy(ld,n,a,b,c,d,e,aa,bb,cc,1.)
   1 continue
```

```console
$ flang-new s113.f -Ofast -S -emit-llvm -v
flang-new version 19.0.0git (https://github.com/llvm/llvm-project.git f73ac218a666e2017565f2210b47332ddcf55f00)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Build config: +assertions
Found candidate GCC installation: /usr/lib/gcc/aarch64-redhat-linux/11
Selected GCC installation: /usr/lib/gcc/aarch64-redhat-linux/11
Candidate multilib: .;@m64
Selected multilib: .;@m64
 :
$
```

```llvm
vector.body: ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %29 = load float, ptr %5, align 4, !tbaa !11  ;; this can be hoisted
  %broadcast.splatinsert14 = insertelement <4 x float> poison, float %29, i64 0
  %broadcast.splat15 = shufflevector <4 x float> %broadcast.splatinsert14, <4 x float> poison, <4 x i32> zeroinitializer
  %30 = or disjoint i64 %index, 1
  %31 = getelementptr float, ptr %6, i64 %30
  %32 = getelementptr i8, ptr %31, i64 16
  %wide.load = load <4 x float>, ptr %31, align 4, !tbaa !13
 %wide.load13 = load <4 x float>, ptr %32, align 4, !tbaa !13
  %33 = fadd fast <4 x float> %wide.load, %broadcast.splat15
  %34 = fadd fast <4 x float> %wide.load13, %broadcast.splat15
  %35 = getelementptr float, ptr %5, i64 %30
  %36 = getelementptr i8, ptr %35, i64 16
  store <4 x float> %33, ptr %35, align 4, !tbaa !11
  store <4 x float> %34, ptr %36, align 4, !tbaa !11
  %index.next = add nuw i64 %index, 8
  %37 = icmp eq i64 %index.next, %n.vec
  br i1 %37, label %middle.block, label %vector.body, !llvm.loop !15
```

We can say `a(i)` and `a(1)` never alias, but BasicAA doesn't.

FYI: It can be reproduced with the following C code. (Reproducer: https://godbolt.org/z/Kso3c7dsj)

```c
// C version
for (int nl = 0; nl < ntimes; nl++) {
  for (int i = 2; i <= n; i++) {
    a[i-1] = a[0] + b[i-1];
  }
  dummy(a, b, c, d, e, aa, bb, cc, 0.);
}
```

Actually, Flang generates LLVM IR like this C code.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV1uP6ygS_jXkpRTLgC_JQx467s2qtWe00pyjWe0jNjjhNIEs4E53__pVYTudvs30kSaKElOXD4oqis8iBL23Sm1IuSXl7UIM8eD85mkIvKb5sv-5aJ182ny7a34D6VSwhNURzs7fQ-88xIMC49wJtAVS5YFSTqocXA-k3P74_kdDylvCVocYT4HwG8J2hO3O53NmVTS6zZzfE7Zrle0OR-HvCds9qC46HwhbZyS_JfnNj4MOoEMYFOgAXhkRlYTogDBeF6xik930W-Xjt3c-emFHKaSPdEDBGiD8FihhjY36qMJbixx0smBoca0EEIStNGHrpMcBTQO2hXbSXOxpDp2zUdtBXWN0whiQw_H4RNjKyDQFawRhTUtY0xHWoEyhLAmTFMU0e4X-BvwS9Ydb0TkbnJlNWQG9EXa_tOoMmLCsh-W_exEiLL_DUh11XBrzcITlw-jxYv2gfNDOAl1neZbvdYR3ud3reBjarHNHwnYIM_0tT979VF3M0KuvuegYXYmqqhTLaV1WZc8Yzdui5pxJ2fVl2ef5JeQfwu9VJPwGhPDdoSqWg7237myXRtvhc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