<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70348>70348</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LSR][SCEV] llc crashes with Assertion '!Ops.empty() && "Cannot get empty add!"'
</td>
</tr>
<tr>
<th>Labels</th>
<td>
loopoptim,
llvm:crash,
llvm:SCEV
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
danilaml
</td>
</tr>
</table>
<pre>
For the following IR:
```llvm
define void @foo() {
bb:
br label %bb4
bb4: ; preds = %bb4, %bb
%phi = phi i32 [ 1, %bb ], [ 0, %bb4 ]
br i1 false, label %bb5, label %bb4
bb5: ; preds = %bb4
%trunc = trunc i64 poison to i32
br label %bb6
bb6: ; preds = %bb6, %bb5
%phi7 = phi i32 [ 1, %bb5 ], [ %add44, %bb6 ]
%phi8 = phi i32 [ 0, %bb5 ], [ 1, %bb6 ]
%mul = mul i32 %phi8, %trunc
%add10 = add i32 %phi, %mul
%mul11 = mul i32 %add10, %trunc
%mul13 = mul i32 %phi7, %phi
%add15 = add i32 %mul13, %mul11
%mul16 = mul i32 %add15, %trunc
%add17 = add i32 poison, %mul16
%mul18 = mul i32 %add17, %trunc
%add19 = add i32 poison, %mul18
%mul20 = mul i32 %add19, %trunc
%add21 = add i32 poison, %mul20
%mul22 = mul i32 %add21, %trunc
%add23 = add i32 poison, %mul22
%mul24 = mul i32 %add23, %trunc
%add25 = add i32 poison, %mul24
%mul26 = mul i32 %add25, %trunc
%add27 = add i32 poison, %mul26
%mul28 = mul i32 %add27, %trunc
%add29 = add i32 poison, %mul28
%mul30 = mul i32 %add29, %trunc
%add31 = add i32 poison, %mul30
%mul32 = mul i32 %add31, %trunc
%mul34 = mul i32 %mul32, %trunc
%add35 = add i32 poison, %mul34
%mul37 = mul i32 %add35, %trunc
%add42 = add i32 %add35, %mul37
%add44 = add i32 %phi7, 1
%add45 = add i32 %add42, poison
%add50 = or i32 %add45, %phi7
br i1 true, label %bb51, label %bb6
bb51: ; preds = %bb6
unreachable
}
```
llc crashes with `const llvm::SCEV* llvm::ScalarEvolution::getAddExpr(llvm::SmallVectorImpl<const llvm::SCEV*>&, llvm::SCEV::NoWrapFlags, unsigned int): Assertion `!Ops.empty() && "Cannot get empty add!"' failed.` during LSR pass.
godbolt link: https://godbolt.org/z/zocK4zebz
Seems to be a very old issue that goes back to at least LLVM 11 release: https://godbolt.org/z/xsb9osT6j
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysllFvozgQxz-N8zLaCMYYwkMekqaRVte7k9pT79lgJ_GuwQib7raf_mSTNoRCdHfaqhVg_v55Zv6uGW6tOtZSrgnbErZb8M6dTLsWvFaaV3pRGPG63psW3EnCwWhtfqj6CF8fCd2QaEeiDUmj_lfrl6ofEvKgagkvRgkgSXQwhuCKYA4k2_aKoviYD1C0oHkhNRBkRZGcsWddQugG_tMPoVtoWiksELp7Z-Jdf_e-JkHWnFRQ-KuiCIRtIf4QAmG78MC2EH2MJmH4EreK4cC1lV4wyIGNnkc5sV-T0yUV13Z1GV71dypNoDHKmhqc8clNlzq9Div1YU2slH6kz67Ll92oHxsWkCDjQiQXG9JhHXvY6hMsmobFs5Sq0wHirwHSg8_6UJqBmgsRR0HPhRjoz_Kq02dx_xDHY3YAzMD9BDoRTHbW-3U-6B7ExpEEwiWWOB7R06lw2EQ451fZ1QL97hjg0xF-NYXP5vH5bfzqGo_RFD6fxWN8E4_RCI8TeIzn8fQ2Hkf4ZApP5_HsNj4Z4aesxXlr8ba1OLIWp6zFeWvxtrU4spZOWYvz1tLb1tKRtXTKWjpjrdd_8ipA5s8Eetssmlzjs6lwprx6PwRx_I8-nBCIV-VJkokjKpgVj8CfTpCwmleec7iSs94m0w7V7HI6ZddfOdd2nz5y8Whg9Dlh8ez3pEd3dSt5eeKFlueZ2W7UU_SPWpdQttyepIUfyp2ApFFpausg9Bx0Q-jm6e7-meBmOFJ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