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

    <tr>
        <th>Summary</th>
        <td>
            [SLP] crash with -fprofile-generate: Instruction::PHI && "Expected PHI"
        </td>
    </tr>

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

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

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

<pre>
    One of our internal tests picked up a new assertion failure since #131625

Here's a reduced test-case
https://godbolt.org/z/rKT64of5b
```
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-unknown"

define void @_Z1sv() #0 {
entry:
 br label %"_ZZ1svEN3$_08__invokeEii.exit"

if.then.i.i: ; No predecessors!
  br label %3

"_ZZ1svEN3$_08__invokeEii.exit":                  ; preds = %entry
 %0 = zext i1 false to i64
  %1 = add i64 0, %0
  %2 = add i64 0, 0
  br i1 false, label %3, label %while.cond.while.end_crit_edge

3: ; preds = %"_ZZ1svEN3$_08__invokeEii.exit", %if.then.i.i
  %pgocount51962 = phi i64 [ 0, %"_ZZ1svEN3$_08__invokeEii.exit" ], [ 0, %if.then.i.i ]
  %pgocount62360 = phi i64 [ 0, %"_ZZ1svEN3$_08__invokeEii.exit" ], [ 0, %if.then.i.i ]
 %pgocount83056 = phi i64 [ %1, %"_ZZ1svEN3$_08__invokeEii.exit" ], [ 0, %if.then.i.i ]
  %pgocount93354 = phi i64 [ %2, %"_ZZ1svEN3$_08__invokeEii.exit" ], [ 0, %if.then.i.i ]
  br label %while.cond.while.end_crit_edge

while.cond.while.end_crit_edge: ; preds = %3, %"_ZZ1svEN3$_08__invokeEii.exit"
  %pgocount51961 = phi i64 [ %pgocount51962, %3 ], [ 0, %"_ZZ1svEN3$_08__invokeEii.exit" ]
 %pgocount62359 = phi i64 [ %pgocount62360, %3 ], [ 0, %"_ZZ1svEN3$_08__invokeEii.exit" ]
  %pgocount83055 = phi i64 [ %pgocount83056, %3 ], [ %1, %"_ZZ1svEN3$_08__invokeEii.exit" ]
 %pgocount93353 = phi i64 [ %pgocount93354, %3 ], [ %2, %"_ZZ1svEN3$_08__invokeEii.exit" ]
  store i64 %pgocount51961, ptr getelementptr inbounds nuw (i8, ptr null, i64 40), align 8
  store i64 %pgocount62359, ptr getelementptr inbounds nuw (i8, ptr null, i64 48), align 8
  store i64 %pgocount83055, ptr getelementptr inbounds nuw (i8, ptr null, i64 56), align 8
  store i64 %pgocount93353, ptr getelementptr inbounds nuw (i8, ptr null, i64 64), align 8
  ret void
}

; uselistorder directives
uselistorder ptr null, { 3, 2, 1, 0 }

attributes #0 = { "target-cpu"="znver2" }
```

```
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:15064: llvm::Instruction& llvm::slpvectorizer::BoUpSLP::getLastInstructionInBundle(const llvm::slpvectorizer::BoUpSLP::TreeEntry*): Assertion `E->getOpcode() == Instruction::PHI && "Expected PHI"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes slp-vectorizer <source>
1.      Running pass "function(slp-vectorizer)" on module "<source>"
2.      Running pass "slp-vectorizer" on function "_Z1sv"
```

This is derived from the following reduced C++ code:

https://godbolt.org/z/aqYnvnGdj
```
int b, a, c, d, e, g, h, i;
int j(const char *, const char *);
int q();
struct ag {
  friend bool r(ag) {
    const char *f, *k = q() ? 0 : "";
    f = q() ? 0 : "";
    return j(k, f);
  }
} l;
bool m;
void s() {
  auto n = [](int, int) {
    if (c) {
      if (a)
        return true;
    } else if (a)
      if (d)
        if (e) {
          bool o = r(l);
 return o;
        }
    return m;
  };
  bool (*p)(int, int) = n;
 while (h)
    g = p(b, i) ?: g;
}
```

```
clang++: /root/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:15064: llvm::Instruction& llvm::slpvectorizer::BoUpSLP::getLastInstructionInBundle(const llvm::slpvectorizer::BoUpSLP::TreeEntry*): Assertion `E->getOpcode() == Instruction::PHI && "Expected PHI"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O2 -fprofile-generate -march=znver2 <source>
1.      <eof> parser at end of file
2.      Optimizer
3.      Running pass "function<eager-inv>(float2int,lower-constant-intrinsics,loop(loop-rotate<header-duplication;no-prepare-for-lto>,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,infer-alignment,loop-load-elim,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,slp-vectorizer,vector-combine,instcombine<max-iterations=1;no-verify-fixpoint>,loop-unroll<O2>,transform-warning,sroa<preserve-cfg>,infer-alignment,instcombine<max-iterations=1;no-verify-fixpoint>,loop-mssa(licm<allowspeculation>),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,tailcallelim,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>)" on module "<source>"
4.      Running pass "slp-vectorizer" on function "_Z1sv"

```
</pre>
<img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