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

    <tr>
        <th>Summary</th>
        <td>
            [SLPVectorizer] Assertion `Gather->ReorderIndices.empty() && "Unexpected reordering of gathers."
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          vedantparanjape-amd
      </td>
    </tr>
</table>

<pre>
    I see a crash due to SLPVectorizer with the below testcase: opt -passes=slp-vectorizer <filename>

```
; ModuleID = 'reduced.bc'
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128"
target triple = "i386-unknown-linux-gnu"

%struct.BSS6 = type <{ [1792 x i8] }>

define internal fastcc void @test01() #0 {
  %1 = load i32, ptr getelementptr inbounds (%struct.BSS6, ptr null, i32 0, i32 0, i32 248), align 8
  %2 = load i32, ptr getelementptr inbounds (%struct.BSS6, ptr null, i32 0, i32 0, i32 252), align 4
  %3 = getelementptr double, ptr getelementptr inbounds (%struct.BSS6, ptr null, i32 0, i32 0, i32 376), i32 %2
  %4 = load double, ptr %3, align 8
  %5 = getelementptr double, ptr getelementptr inbounds (%struct.BSS6, ptr null, i32 0, i32 0, i32 376), i32 %1
  %6 = load double, ptr %5, align 8
  %7 = fadd double %6, %6
 %8 = fadd double %4, %7
  %9 = fadd double 0.000000e+00, 0.000000e+00
 %10 = fadd double %6, %9
  %11 = fadd double %10, %8
  br i1 true, label %12, label %temp_bb

temp_bb: ; No predecessors!
  br label %12

12: ; preds = %._crit_edge, %0
  %13 = phi double [ 0.000000e+00, %temp_bb ], [ %11, %0 ]
  ret void
}

attributes #0 = { "target-cpu"="skylake" }
```
I tried to debug the issue, it is mostly caused due to one of the nodes becoming ScatterVectorize nodes, which further adds it's operand to the tree. Here it leads to a malformed tree. I tried to do something like this and it helped.

```
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index b85becc4fdb7..611fb6445b45 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -4807,6 +4807,8 @@ void BoUpSLP::reorderBottomToTop(bool IgnoreReorder) {
       }
 // For gathers just need to reorder its scalars.
       for (TreeEntry *Gather : GatherOps) {
+        if (!Gather->ReorderIndices.empty())
+ reorderOrder(Gather->ReorderIndices, Mask);
 assert(Gather->ReorderIndices.empty() &&
                "Unexpected reordering of gathers.");
         if (!Gather->ReuseShuffleIndices.empty()) {
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV1tv47oR_jX0y0ACRermBz_Y8boNcE7PQZPT1wUljmxuaFEgqVz66wtSsiPvpkHbRVtBoDniXL4ZcmZo4Zw69ogbUuxIsV-J0Z-M3TyjFL0fhBX9NzFgIs5y1Rj5trkHhwgCWivcCeSI4A08_PL737D1xqq_o4UX5U_gTwgNavMCHp1vhUPCt2AGD8kgnENH-N7pIXl-lyP8rlMae3FGwr8Quid0O48lnd-J5Dv41chR4_0eCN8DYZVFObYo06YlrJrYvLBH9CCFF1q8mdHPvAyTM-FbTAbCt5zFIRlYRW_J7JYMszKPQ9LFCWczVU-SSV8Tvs3KiXjIWE0Yu4HirRo0XmAoXpfJ2D_15qVPtOrH1-TYj1eZeWSF83Zsfbp7eCijqH8bgo47Uu2AFLusWjN4BVWTYg-k2n8XO4md6hFU79H2QkMnnG9beDZKAslp2B2akYB1DYRxCqTaTZIAhBVZNKmNkKA4I-wOBm_hiB41nrH3gVJ9Y8ZeOohqlngv_P2odZgrzoD-MGF5sB4oodWxh3phn_0v7Bfsxn6-sM-j_Vt70oyNxv8OFl6VM5ZAhQAswOTvwbjFEIB-HL_i_40_W4ApP8FffIy_iiKdkBeRqCfwxt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