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

    <tr>
        <th>Summary</th>
        <td>
            loop-vectorize miscompile
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    llvm commit: 6955a7d134e7
Reproduce with:
```
opt -passes="loop-vectorize" bbi-107525_lv.ll -S -o - -mtriple=aarch64-none-linux-gnu --data-layout="p:16:16"
```
The loop-vectorize output contains
```
pred.sdiv.continue: ; preds = %pred.sdiv.if, %vector.body
  %3 = phi i16 [ poison, %vector.body ], [ %2, %pred.sdiv.if ]
  [...]

pred.sdiv.continue2: ; preds = %pred.sdiv.if1, %pred.sdiv.continue
 [...]
  %7 = shl i16 %3, 14
  %8 = sub i16 %6, %7
  %9 = getelementptr [16 x i64], ptr @g_855, i16 0, i16 %8
  %10 = getelementptr i64, ptr %9, i32 0
 %wide.load = load <2 x i64>, ptr %10, align 1
```

and the problem here is that we will get to the %3 phi from %vector.body and then continue to pred.sdiv.continue2 where the %3 poison value will make %7, %8, %9 and %10 poison, and then we will do a load from %10 which is instant UB.

The input program has no UB.

Before 0d61ffd350
```
[Loads] Support SCEVAddExpr as start for pointer AddRec.
```
the result is ok. Then with 0d61ffd350 it crashes with
```
opt: ../include/llvm/Support/Casting.h:109: static bool llvm::isa_impl_cl<llvm::Instruction, const llvm::Value *>::doit(const From *) [To = llvm::Instruction, From = const llvm::Value *]: Assertion `Val && "isa<> used on a null pointer"' failed.
```
and that crash goes away with fd82b5b287
```
[LV] Support recieps without underlying instr in collectPoisonGenRec.

Support recipes without underlying instruction in
collectPoisonGeneratingRecipes by directly trying to dyn_cast_or_null
the underlying value.

Fixes https://github.com/llvm/llvm-project/issues/70590.
```
but then we get the miscompile instead.

I also tested to cherry-pick the fd82b5b287 to directly after 0d61ffd350 and we get the miscompile then too, so it doesn't seem to be some other patch between them that causes the miscompile.

[bbi-107525_lv.ll.gz](https://github.com/user-attachments/files/20610502/bbi-107525_lv.ll.gz)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVlGPmzgQ_jXOywhkTIDkIQ9kszlVuodT29vXyOBJ8K2xkW02m_76kw1p0nTbk05abcCe-Wbmm2-MuXPypBE3pNiSYrfgo--M3fTylaPqjOpRLxojLhul3npoTd9LT_IaynVR8Epk-RIrQuvPOFgjxhbhLH1H8prQmpR0_qO1GTwkA3cOHcl3hDFlzJC8YeuNld-QMAZNI5OMVgUrDuotVQqSL5AYSCDpvZWDQpLvOLdtVy4TbTQmSurxPTnpEZJEcM8TxS9m9BP-QPI6K6d_jD1k87VD-DEBMKMfRg-t0Z5L7R4cBosidUK-pcFA6hEDByTfQthxQPIdEFbczOSRsKewNEVIA4WE1hCW8mg-dBJkVgIptjAY6Yz-2QNIsYurxTbssNniPkw0icDFNk3T6e3DjNl_ppz9hP-9Wlr_ECDWUUUM16mpDlbkwT9bXvdX0_7YXPfLGb-6WqyjxQk9KuxR-8HbECYr4R1kuZyrj6tLejqsiiK8BzR6fQhxrnAZ_QAv4FxBWLGOfjkDGktixVkKTJXhIrrOD09sTiB_vvPNYlCu5ElD9iAQQmuuBfgOYbCmUdhDhxZBOvAd93AOg6FUyA28iXZRCUEFR2v6x8bPYBquHQheH_QUzjHMDS8qCd64GueIPX_FifSJ_NX8u44xJtZu-vse95qwMMAnWq5pZhTOnWy7UJvUznPt4e9tOpEQJkvqMEmDNSfLe-i4A21uFls8GotARZkdjyIv6COTxfZPw4UjxQ6-jMNgrIcvT88vtRDP74MF7sB5bj0cjQ2Ja48WaiE-Y5s-QAVWLLpR-ZCreU3hayxN-u4uPkgPreWuQzcdXj-dXGFw0pSwvdStGgUStg-nIWH7OUHC9k_cealPaTj7MroOLs5zL1tojFEQ7fOa5LV0_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