[llvm] [SLP] Improve handling of sibling loop BTC mismatch (PR #208164)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 01:07:18 PDT 2026


https://github.com/hazzlim created https://github.com/llvm/llvm-project/pull/208164

When building a tree that spans sibling loops, SLP currently bails out to
gather as soon as the sibling BTCs are represented by different SCEVs.

Instead of making that decision unconditionally, evaluate both candidates:
the conservative gather tree and an always-merge sibling tree. Compute
costs for both and select the more profitable one.


>From 14f137451e284509e7d7c329a3b418490b470982 Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Sat, 4 Jul 2026 12:12:51 +0000
Subject: [PATCH 1/2] Precommit test

---
 .../sibling-loops-mismatched-tripcount.ll     | 330 ++++++++++++++++++
 1 file changed, 330 insertions(+)
 create mode 100644 llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll

diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll
new file mode 100644
index 0000000000000..689f91a2470c4
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll
@@ -0,0 +1,330 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=slp-vectorizer -S %s | FileCheck %s
+
+target triple = "aarch64-unknown-linux-gnu"
+
+define void @sibling_mismatch_stores(ptr %out, double %a, double %b, double %c, i32 %na, i32 %nb, i32 %nh, i1 %sel) {
+; CHECK-LABEL: define void @sibling_mismatch_stores(
+; CHECK-SAME: ptr [[OUT:%.*]], double [[A:%.*]], double [[B:%.*]], double [[C:%.*]], i32 [[NA:%.*]], i32 [[NB:%.*]], i32 [[NH:%.*]], i1 [[SEL:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[SEL]], label %[[A_PREHEADER:.*]], label %[[B_PREHEADER:.*]]
+; CHECK:       [[A_PREHEADER]]:
+; CHECK-NEXT:    br label %[[A_HEADER:.*]]
+; CHECK:       [[A_HEADER]]:
+; CHECK-NEXT:    [[IA:%.*]] = phi i32 [ 0, %[[A_PREHEADER]] ], [ [[IA_NEXT:%.*]], %[[A_LATCH:.*]] ]
+; CHECK-NEXT:    [[AA0:%.*]] = phi double [ [[A]], %[[A_PREHEADER]] ], [ [[AA0N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[AA1:%.*]] = phi double [ [[B]], %[[A_PREHEADER]] ], [ [[AA1N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[AA2:%.*]] = phi double [ [[C]], %[[A_PREHEADER]] ], [ [[AA2N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[AA3:%.*]] = phi double [ [[A]], %[[A_PREHEADER]] ], [ [[AA3N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[AA4:%.*]] = phi double [ [[B]], %[[A_PREHEADER]] ], [ [[AA4N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[AA5:%.*]] = phi double [ [[C]], %[[A_PREHEADER]] ], [ [[AA5N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    br label %[[A_BODY:.*]]
+; CHECK:       [[A_BODY]]:
+; CHECK-NEXT:    [[AA0N]] = fadd double [[AA0]], [[A]]
+; CHECK-NEXT:    [[AA1N]] = fadd double [[AA1]], [[B]]
+; CHECK-NEXT:    [[AA2N]] = fadd double [[AA2]], [[C]]
+; CHECK-NEXT:    [[AA3N]] = fadd double [[AA3]], [[A]]
+; CHECK-NEXT:    [[AA4N]] = fadd double [[AA4]], [[B]]
+; CHECK-NEXT:    [[AA5N]] = fadd double [[AA5]], [[C]]
+; CHECK-NEXT:    br label %[[A_LATCH]]
+; CHECK:       [[A_LATCH]]:
+; CHECK-NEXT:    [[IA_NEXT]] = add nuw nsw i32 [[IA]], 1
+; CHECK-NEXT:    [[A_DONE:%.*]] = icmp eq i32 [[IA_NEXT]], [[NA]]
+; CHECK-NEXT:    br i1 [[A_DONE]], label %[[A_EXIT:.*]], label %[[A_HEADER]]
+; CHECK:       [[B_PREHEADER]]:
+; CHECK-NEXT:    br label %[[B_HEADER:.*]]
+; CHECK:       [[B_HEADER]]:
+; CHECK-NEXT:    [[IB:%.*]] = phi i32 [ 0, %[[B_PREHEADER]] ], [ [[IB_NEXT:%.*]], %[[B_LATCH:.*]] ]
+; CHECK-NEXT:    [[BB0:%.*]] = phi double [ [[B]], %[[B_PREHEADER]] ], [ [[BB0N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[BB1:%.*]] = phi double [ [[C]], %[[B_PREHEADER]] ], [ [[BB1N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[BB2:%.*]] = phi double [ [[A]], %[[B_PREHEADER]] ], [ [[BB2N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[BB3:%.*]] = phi double [ [[B]], %[[B_PREHEADER]] ], [ [[BB3N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[BB4:%.*]] = phi double [ [[C]], %[[B_PREHEADER]] ], [ [[BB4N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[BB5:%.*]] = phi double [ [[A]], %[[B_PREHEADER]] ], [ [[BB5N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    br label %[[B_BODY:.*]]
+; CHECK:       [[B_BODY]]:
+; CHECK-NEXT:    [[BB0N]] = fadd double [[BB0]], [[B]]
+; CHECK-NEXT:    [[BB1N]] = fadd double [[BB1]], [[C]]
+; CHECK-NEXT:    [[BB2N]] = fadd double [[BB2]], [[A]]
+; CHECK-NEXT:    [[BB3N]] = fadd double [[BB3]], [[B]]
+; CHECK-NEXT:    [[BB4N]] = fadd double [[BB4]], [[C]]
+; CHECK-NEXT:    [[BB5N]] = fadd double [[BB5]], [[A]]
+; CHECK-NEXT:    br label %[[B_LATCH]]
+; CHECK:       [[B_LATCH]]:
+; CHECK-NEXT:    [[IB_NEXT]] = add nuw nsw i32 [[IB]], 1
+; CHECK-NEXT:    [[B_DONE:%.*]] = icmp eq i32 [[IB_NEXT]], [[NB]]
+; CHECK-NEXT:    br i1 [[B_DONE]], label %[[B_EXIT:.*]], label %[[B_HEADER]]
+; CHECK:       [[A_EXIT]]:
+; CHECK-NEXT:    br label %[[JOIN:.*]]
+; CHECK:       [[B_EXIT]]:
+; CHECK-NEXT:    br label %[[JOIN]]
+; CHECK:       [[JOIN]]:
+; CHECK-NEXT:    [[S0:%.*]] = phi double [ [[AA0N]], %[[A_EXIT]] ], [ [[BB0N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[S1:%.*]] = phi double [ [[AA1N]], %[[A_EXIT]] ], [ [[BB1N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[S2:%.*]] = phi double [ [[AA2N]], %[[A_EXIT]] ], [ [[BB2N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[S3:%.*]] = phi double [ [[AA3N]], %[[A_EXIT]] ], [ [[BB3N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[S4:%.*]] = phi double [ [[AA4N]], %[[A_EXIT]] ], [ [[BB4N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[S5:%.*]] = phi double [ [[AA5N]], %[[A_EXIT]] ], [ [[BB5N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    br label %[[HOT_HEADER:.*]]
+; CHECK:       [[HOT_HEADER]]:
+; CHECK-NEXT:    [[IH:%.*]] = phi i32 [ 0, %[[JOIN]] ], [ [[IH_NEXT:%.*]], %[[HOT_LATCH:.*]] ]
+; CHECK-NEXT:    [[P0:%.*]] = phi double [ [[S0]], %[[JOIN]] ], [ [[N0:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[P1:%.*]] = phi double [ [[S1]], %[[JOIN]] ], [ [[N1:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[P2:%.*]] = phi double [ [[S2]], %[[JOIN]] ], [ [[N2:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[P3:%.*]] = phi double [ [[S3]], %[[JOIN]] ], [ [[N3:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[P4:%.*]] = phi double [ [[S4]], %[[JOIN]] ], [ [[N4:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[P5:%.*]] = phi double [ [[S5]], %[[JOIN]] ], [ [[N5:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    br label %[[HOT_BODY:.*]]
+; CHECK:       [[HOT_BODY]]:
+; CHECK-NEXT:    [[N0]] = fadd double [[P0]], [[S0]]
+; CHECK-NEXT:    [[N1]] = fadd double [[P1]], [[S1]]
+; CHECK-NEXT:    [[N2]] = fadd double [[P2]], [[S2]]
+; CHECK-NEXT:    [[N3]] = fadd double [[P3]], [[S3]]
+; CHECK-NEXT:    [[N4]] = fadd double [[P4]], [[S4]]
+; CHECK-NEXT:    [[N5]] = fadd double [[P5]], [[S5]]
+; CHECK-NEXT:    br label %[[HOT_LATCH]]
+; CHECK:       [[HOT_LATCH]]:
+; CHECK-NEXT:    [[IH_NEXT]] = add nuw nsw i32 [[IH]], 1
+; CHECK-NEXT:    [[H_DONE:%.*]] = icmp eq i32 [[IH_NEXT]], [[NH]]
+; CHECK-NEXT:    br i1 [[H_DONE]], label %[[EXIT:.*]], label %[[HOT_HEADER]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[R0:%.*]] = phi double [ [[N0]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[R1:%.*]] = phi double [ [[N1]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[R2:%.*]] = phi double [ [[N2]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[R3:%.*]] = phi double [ [[N3]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[R4:%.*]] = phi double [ [[N4]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[R5:%.*]] = phi double [ [[N5]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[OUT1:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 1
+; CHECK-NEXT:    [[OUT2:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 2
+; CHECK-NEXT:    [[OUT3:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 3
+; CHECK-NEXT:    [[OUT4:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 4
+; CHECK-NEXT:    [[OUT5:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 5
+; CHECK-NEXT:    store double [[R0]], ptr [[OUT]], align 8
+; CHECK-NEXT:    store double [[R1]], ptr [[OUT1]], align 8
+; CHECK-NEXT:    store double [[R2]], ptr [[OUT2]], align 8
+; CHECK-NEXT:    store double [[R3]], ptr [[OUT3]], align 8
+; CHECK-NEXT:    store double [[R4]], ptr [[OUT4]], align 8
+; CHECK-NEXT:    store double [[R5]], ptr [[OUT5]], align 8
+; CHECK-NEXT:    ret void
+;
+entry:
+  br i1 %sel, label %A.preheader, label %B.preheader
+
+A.preheader:
+  br label %A.header
+
+A.header:
+  %ia = phi i32 [ 0, %A.preheader ], [ %ia.next, %A.latch ]
+  %aa0 = phi double [ %a, %A.preheader ], [ %aa0n, %A.latch ]
+  %aa1 = phi double [ %b, %A.preheader ], [ %aa1n, %A.latch ]
+  %aa2 = phi double [ %c, %A.preheader ], [ %aa2n, %A.latch ]
+  %aa3 = phi double [ %a, %A.preheader ], [ %aa3n, %A.latch ]
+  %aa4 = phi double [ %b, %A.preheader ], [ %aa4n, %A.latch ]
+  %aa5 = phi double [ %c, %A.preheader ], [ %aa5n, %A.latch ]
+  br label %A.body
+
+A.body:
+  %aa0n = fadd double %aa0, %a
+  %aa1n = fadd double %aa1, %b
+  %aa2n = fadd double %aa2, %c
+  %aa3n = fadd double %aa3, %a
+  %aa4n = fadd double %aa4, %b
+  %aa5n = fadd double %aa5, %c
+  br label %A.latch
+
+A.latch:
+  %ia.next = add nuw nsw i32 %ia, 1
+  %a.done = icmp eq i32 %ia.next, %na
+  br i1 %a.done, label %A.exit, label %A.header
+
+B.preheader:
+  br label %B.header
+
+B.header:
+  %ib = phi i32 [ 0, %B.preheader ], [ %ib.next, %B.latch ]
+  %bb0 = phi double [ %b, %B.preheader ], [ %bb0n, %B.latch ]
+  %bb1 = phi double [ %c, %B.preheader ], [ %bb1n, %B.latch ]
+  %bb2 = phi double [ %a, %B.preheader ], [ %bb2n, %B.latch ]
+  %bb3 = phi double [ %b, %B.preheader ], [ %bb3n, %B.latch ]
+  %bb4 = phi double [ %c, %B.preheader ], [ %bb4n, %B.latch ]
+  %bb5 = phi double [ %a, %B.preheader ], [ %bb5n, %B.latch ]
+  br label %B.body
+
+B.body:
+  %bb0n = fadd double %bb0, %b
+  %bb1n = fadd double %bb1, %c
+  %bb2n = fadd double %bb2, %a
+  %bb3n = fadd double %bb3, %b
+  %bb4n = fadd double %bb4, %c
+  %bb5n = fadd double %bb5, %a
+  br label %B.latch
+
+B.latch:
+  %ib.next = add nuw nsw i32 %ib, 1
+  %b.done = icmp eq i32 %ib.next, %nb
+  br i1 %b.done, label %B.exit, label %B.header
+
+A.exit:
+  br label %join
+
+B.exit:
+  br label %join
+
+join:
+  %s0 = phi double [ %aa0n, %A.exit ], [ %bb0n, %B.exit ]
+  %s1 = phi double [ %aa1n, %A.exit ], [ %bb1n, %B.exit ]
+  %s2 = phi double [ %aa2n, %A.exit ], [ %bb2n, %B.exit ]
+  %s3 = phi double [ %aa3n, %A.exit ], [ %bb3n, %B.exit ]
+  %s4 = phi double [ %aa4n, %A.exit ], [ %bb4n, %B.exit ]
+  %s5 = phi double [ %aa5n, %A.exit ], [ %bb5n, %B.exit ]
+  br label %hot.header
+
+hot.header:
+  %ih = phi i32 [ 0, %join ], [ %ih.next, %hot.latch ]
+  %p0 = phi double [ %s0, %join ], [ %n0, %hot.latch ]
+  %p1 = phi double [ %s1, %join ], [ %n1, %hot.latch ]
+  %p2 = phi double [ %s2, %join ], [ %n2, %hot.latch ]
+  %p3 = phi double [ %s3, %join ], [ %n3, %hot.latch ]
+  %p4 = phi double [ %s4, %join ], [ %n4, %hot.latch ]
+  %p5 = phi double [ %s5, %join ], [ %n5, %hot.latch ]
+  br label %hot.body
+
+hot.body:
+  %n0 = fadd double %p0, %s0
+  %n1 = fadd double %p1, %s1
+  %n2 = fadd double %p2, %s2
+  %n3 = fadd double %p3, %s3
+  %n4 = fadd double %p4, %s4
+  %n5 = fadd double %p5, %s5
+  br label %hot.latch
+
+hot.latch:
+  %ih.next = add nuw nsw i32 %ih, 1
+  %h.done = icmp eq i32 %ih.next, %nh
+  br i1 %h.done, label %exit, label %hot.header
+
+exit:
+  %r0 = phi double [ %n0, %hot.latch ]
+  %r1 = phi double [ %n1, %hot.latch ]
+  %r2 = phi double [ %n2, %hot.latch ]
+  %r3 = phi double [ %n3, %hot.latch ]
+  %r4 = phi double [ %n4, %hot.latch ]
+  %r5 = phi double [ %n5, %hot.latch ]
+  %out1 = getelementptr inbounds double, ptr %out, i64 1
+  %out2 = getelementptr inbounds double, ptr %out, i64 2
+  %out3 = getelementptr inbounds double, ptr %out, i64 3
+  %out4 = getelementptr inbounds double, ptr %out, i64 4
+  %out5 = getelementptr inbounds double, ptr %out, i64 5
+  store double %r0, ptr %out, align 8
+  store double %r1, ptr %out1, align 8
+  store double %r2, ptr %out2, align 8
+  store double %r3, ptr %out3, align 8
+  store double %r4, ptr %out4, align 8
+  store double %r5, ptr %out5, align 8
+  ret void
+}
+
+define dso_local void @sibling_mismatch_reduction(ptr noundef %base, ptr noundef %end1, ptr noundef %end2, ptr nofree noundef readnone captures(none) %end3, ptr nofree noundef readonly captures(none) %arr, ptr nofree noundef writeonly captures(none) %out) local_unnamed_addr {
+; CHECK-LABEL: define dso_local void @sibling_mismatch_reduction(
+; CHECK-SAME: ptr noundef [[BASE:%.*]], ptr noundef [[END1:%.*]], ptr noundef [[END2:%.*]], ptr nofree noundef readnone captures(none) [[END3:%.*]], ptr nofree noundef readonly captures(none) [[ARR:%.*]], ptr nofree noundef writeonly captures(none) [[OUT:%.*]]) local_unnamed_addr {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[END1]] to i64
+; CHECK-NEXT:    [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[BASE]] to i64
+; CHECK-NEXT:    [[SUB_PTR_SUB:%.*]] = sub i64 [[SUB_PTR_LHS_CAST]], [[SUB_PTR_RHS_CAST]]
+; CHECK-NEXT:    [[SUB_PTR_DIV:%.*]] = ashr exact i64 [[SUB_PTR_SUB]], 1
+; CHECK-NEXT:    [[SUB_PTR_LHS_CAST1:%.*]] = ptrtoint ptr [[END2]] to i64
+; CHECK-NEXT:    [[SUB_PTR_SUB3:%.*]] = sub i64 [[SUB_PTR_LHS_CAST1]], [[SUB_PTR_RHS_CAST]]
+; CHECK-NEXT:    [[SUB_PTR_DIV4:%.*]] = ashr exact i64 [[SUB_PTR_SUB3]], 1
+; CHECK-NEXT:    [[CMP42:%.*]] = icmp ugt i64 [[SUB_PTR_DIV]], 1
+; CHECK-NEXT:    br i1 [[CMP42]], label %[[FOR_BODY:.*]], label %[[FOR_COND9_PREHEADER:.*]]
+; CHECK:       [[FOR_COND9_PREHEADER]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x double> [ zeroinitializer, %[[ENTRY]] ], [ [[TMP5:%.*]], %[[FOR_BODY]] ]
+; CHECK-NEXT:    [[CMP1148:%.*]] = icmp ugt i64 [[SUB_PTR_DIV4]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = extractelement <2 x double> [[TMP0]], i32 0
+; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <2 x double> [[TMP0]], i32 1
+; CHECK-NEXT:    br i1 [[CMP1148]], label %[[FOR_BODY13:.*]], label %[[FOR_COND_CLEANUP12:.*]]
+; CHECK:       [[FOR_BODY]]:
+; CHECK-NEXT:    [[I_043:%.*]] = phi i64 [ [[ADD7:%.*]], %[[FOR_BODY]] ], [ 0, %[[ENTRY]] ]
+; CHECK-NEXT:    [[TMP3:%.*]] = phi <2 x double> [ [[TMP5]], %[[FOR_BODY]] ], [ zeroinitializer, %[[ENTRY]] ]
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds nuw [8 x i8], ptr [[ARR]], i64 [[I_043]]
+; CHECK-NEXT:    [[TMP4:%.*]] = load <2 x double>, ptr [[ARRAYIDX]], align 8
+; CHECK-NEXT:    [[TMP5]] = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> [[TMP4]], <2 x double> splat (double 2.000000e+00), <2 x double> [[TMP3]])
+; CHECK-NEXT:    [[ADD7]] = add nuw i64 [[I_043]], 2
+; CHECK-NEXT:    [[ADD:%.*]] = or disjoint i64 [[ADD7]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i64 [[ADD]], [[SUB_PTR_DIV]]
+; CHECK-NEXT:    br i1 [[CMP]], label %[[FOR_BODY]], label %[[FOR_COND9_PREHEADER]]
+; CHECK:       [[FOR_COND_CLEANUP12]]:
+; CHECK-NEXT:    [[ACC_SROA_8_1_LCSSA:%.*]] = phi double [ [[TMP2]], %[[FOR_COND9_PREHEADER]] ], [ [[TMP9:%.*]], %[[FOR_BODY13]] ]
+; CHECK-NEXT:    [[ACC_SROA_0_1_LCSSA:%.*]] = phi double [ [[TMP1]], %[[FOR_COND9_PREHEADER]] ], [ [[TMP8:%.*]], %[[FOR_BODY13]] ]
+; CHECK-NEXT:    [[ADD26:%.*]] = fadd double [[ACC_SROA_8_1_LCSSA]], [[ACC_SROA_0_1_LCSSA]]
+; CHECK-NEXT:    store double [[ADD26]], ptr [[OUT]], align 8
+; CHECK-NEXT:    ret void
+; CHECK:       [[FOR_BODY13]]:
+; CHECK-NEXT:    [[ACC_SROA_0_151:%.*]] = phi double [ [[TMP8]], %[[FOR_BODY13]] ], [ [[TMP1]], %[[FOR_COND9_PREHEADER]] ]
+; CHECK-NEXT:    [[ACC_SROA_8_150:%.*]] = phi double [ [[TMP9]], %[[FOR_BODY13]] ], [ [[TMP2]], %[[FOR_COND9_PREHEADER]] ]
+; CHECK-NEXT:    [[I8_049:%.*]] = phi i64 [ [[ADD22:%.*]], %[[FOR_BODY13]] ], [ 0, %[[FOR_COND9_PREHEADER]] ]
+; CHECK-NEXT:    [[ARRAYIDX15:%.*]] = getelementptr inbounds nuw [8 x i8], ptr [[ARR]], i64 [[I8_049]]
+; CHECK-NEXT:    [[TMP6:%.*]] = load double, ptr [[ARRAYIDX15]], align 8
+; CHECK-NEXT:    [[ARRAYIDX18:%.*]] = getelementptr inbounds nuw i8, ptr [[ARRAYIDX15]], i64 8
+; CHECK-NEXT:    [[TMP7:%.*]] = load double, ptr [[ARRAYIDX18]], align 8
+; CHECK-NEXT:    [[TMP8]] = tail call double @llvm.fmuladd.f64(double [[TMP6]], double 4.000000e+00, double [[ACC_SROA_0_151]])
+; CHECK-NEXT:    [[TMP9]] = tail call double @llvm.fmuladd.f64(double [[TMP7]], double 4.000000e+00, double [[ACC_SROA_8_150]])
+; CHECK-NEXT:    [[ADD22]] = add nuw i64 [[I8_049]], 2
+; CHECK-NEXT:    [[ADD10:%.*]] = or disjoint i64 [[ADD22]], 1
+; CHECK-NEXT:    [[CMP11:%.*]] = icmp ult i64 [[ADD10]], [[SUB_PTR_DIV4]]
+; CHECK-NEXT:    br i1 [[CMP11]], label %[[FOR_BODY13]], label %[[FOR_COND_CLEANUP12]]
+;
+entry:
+  %sub.ptr.lhs.cast = ptrtoint ptr %end1 to i64
+  %sub.ptr.rhs.cast = ptrtoint ptr %base to i64
+  %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
+  %sub.ptr.div = ashr exact i64 %sub.ptr.sub, 1
+  %sub.ptr.lhs.cast1 = ptrtoint ptr %end2 to i64
+  %sub.ptr.sub3 = sub i64 %sub.ptr.lhs.cast1, %sub.ptr.rhs.cast
+  %sub.ptr.div4 = ashr exact i64 %sub.ptr.sub3, 1
+  %cmp42 = icmp ugt i64 %sub.ptr.div, 1
+  br i1 %cmp42, label %for.body, label %for.cond9.preheader
+
+for.cond9.preheader:                              ; preds = %for.body, %entry
+  %acc.sroa.8.0.lcssa = phi double [ 0.000000e+00, %entry ], [ %3, %for.body ]
+  %acc.sroa.0.0.lcssa = phi double [ 0.000000e+00, %entry ], [ %2, %for.body ]
+  %cmp1148 = icmp ugt i64 %sub.ptr.div4, 1
+  br i1 %cmp1148, label %for.body13, label %for.cond.cleanup12
+
+for.body:                                         ; preds = %entry, %for.body
+  %acc.sroa.0.045 = phi double [ %2, %for.body ], [ 0.000000e+00, %entry ]
+  %acc.sroa.8.044 = phi double [ %3, %for.body ], [ 0.000000e+00, %entry ]
+  %i.043 = phi i64 [ %add7, %for.body ], [ 0, %entry ]
+  %arrayidx = getelementptr inbounds nuw [8 x i8], ptr %arr, i64 %i.043
+  %0 = load double, ptr %arrayidx, align 8
+  %arrayidx6 = getelementptr inbounds nuw i8, ptr %arrayidx, i64 8
+  %1 = load double, ptr %arrayidx6, align 8
+  %2 = tail call double @llvm.fmuladd.f64(double %0, double 2.000000e+00, double %acc.sroa.0.045)
+  %3 = tail call double @llvm.fmuladd.f64(double %1, double 2.000000e+00, double %acc.sroa.8.044)
+  %add7 = add nuw i64 %i.043, 2
+  %add = or disjoint i64 %add7, 1
+  %cmp = icmp ult i64 %add, %sub.ptr.div
+  br i1 %cmp, label %for.body, label %for.cond9.preheader
+
+for.cond.cleanup12:                               ; preds = %for.body13, %for.cond9.preheader
+  %acc.sroa.8.1.lcssa = phi double [ %acc.sroa.8.0.lcssa, %for.cond9.preheader ], [ %7, %for.body13 ]
+  %acc.sroa.0.1.lcssa = phi double [ %acc.sroa.0.0.lcssa, %for.cond9.preheader ], [ %6, %for.body13 ]
+  %add26 = fadd double %acc.sroa.8.1.lcssa, %acc.sroa.0.1.lcssa
+  store double %add26, ptr %out, align 8
+  ret void
+
+for.body13:                                       ; preds = %for.cond9.preheader, %for.body13
+  %acc.sroa.0.151 = phi double [ %6, %for.body13 ], [ %acc.sroa.0.0.lcssa, %for.cond9.preheader ]
+  %acc.sroa.8.150 = phi double [ %7, %for.body13 ], [ %acc.sroa.8.0.lcssa, %for.cond9.preheader ]
+  %i8.049 = phi i64 [ %add22, %for.body13 ], [ 0, %for.cond9.preheader ]
+  %arrayidx15 = getelementptr inbounds nuw [8 x i8], ptr %arr, i64 %i8.049
+  %4 = load double, ptr %arrayidx15, align 8
+  %arrayidx18 = getelementptr inbounds nuw i8, ptr %arrayidx15, i64 8
+  %5 = load double, ptr %arrayidx18, align 8
+  %6 = tail call double @llvm.fmuladd.f64(double %4, double 4.000000e+00, double %acc.sroa.0.151)
+  %7 = tail call double @llvm.fmuladd.f64(double %5, double 4.000000e+00, double %acc.sroa.8.150)
+  %add22 = add nuw i64 %i8.049, 2
+  %add10 = or disjoint i64 %add22, 1
+  %cmp11 = icmp ult i64 %add10, %sub.ptr.div4
+  br i1 %cmp11, label %for.body13, label %for.cond.cleanup12
+}

>From 5cbf98a41cf69003867297669e976d56cb93b98f Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Tue, 7 Jul 2026 22:15:13 +0000
Subject: [PATCH 2/2] [SLP] Improve handling of sibling loop BTC mismatch

When building a tree that spans sibling loops, SLP currently bails out to
gather as soon as the sibling BTCs are represented by different SCEVs.

Instead of making that decision unconditionally, evaluate both candidates:
the conservative gather tree and an always-merge sibling tree. Compute
costs for both and select the more profitable one.
---
 .../Transforms/Vectorize/SLPVectorizer.cpp    | 372 +++++++++++-------
 .../sibling-loops-mismatched-tripcount.ll     |  93 ++---
 2 files changed, 249 insertions(+), 216 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index ee909f3fd1b43..36f2c9216c839 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -2212,7 +2212,9 @@ class slpvectorizer::BoUpSLP {
                  const SmallDenseSet<Value *> &UserIgnoreLst);
 
   /// Construct a vectorizable tree that starts at \p Roots.
-  void buildTree(ArrayRef<Value *> Roots);
+  void buildTree(ArrayRef<Value *> Roots, bool AlwaysMergeSiblingLoops = false);
+
+  bool sawSiblingMismatchBailout() const { return SawSiblingMismatchBailout; }
 
   /// Return the scalars of the root node.
   ArrayRef<Value *> getRootNodeScalars() const {
@@ -2300,7 +2302,7 @@ class slpvectorizer::BoUpSLP {
   buildExternalUses(const ExtraValueToDebugLocsMap &ExternallyUsedValues = {});
 
   /// Transforms graph nodes to target specific representations, if profitable.
-  void transformNodes();
+  void transformNodes(bool AlwaysMergeSiblingLoops = false);
 
   /// Clear the internal data structures that are created by 'buildTree'.
   void deleteTree() {
@@ -2337,6 +2339,7 @@ class slpvectorizer::BoUpSLP {
     TreeEntryToStridedPtrInfoMap.clear();
     CurrentLoopNest.clear();
     MergedLoopBTCs.clear();
+    SawSiblingMismatchBailout = false;
   }
 
   unsigned getTreeSize() const { return VectorizableTree.size(); }
@@ -4067,6 +4070,7 @@ class slpvectorizer::BoUpSLP {
 
   /// This is the recursive part of buildTree.
   void buildTreeRec(ArrayRef<Value *> Roots, unsigned Depth, const EdgeInfo &EI,
+                    bool AlwaysMergeSiblingLoops = false,
                     unsigned InterleaveFactor = 0);
 
   /// \returns true if the ExtractElement/ExtractValue instructions in \p VL can
@@ -4183,7 +4187,8 @@ class slpvectorizer::BoUpSLP {
       const SmallMapVector<
           std::tuple<BasicBlock *, Value *, Type *>,
           SmallVector<SmallVector<std::pair<LoadInst *, int64_t>>>, 8>
-          &GatheredLoads);
+          &GatheredLoads,
+      bool AlwaysMergeSiblingLoops);
 
   /// Helper for `findExternalStoreUsersReorderIndices()`. It iterates over the
   /// users of \p TE and collects the stores. It returns the map from the store
@@ -5023,6 +5028,8 @@ class slpvectorizer::BoUpSLP {
   /// Per-depth SCEVs trip counts at every loop level where the tree builder has
   /// joined diverging sibling loops.
   SmallVector<const SCEV *> MergedLoopBTCs;
+  /// True if this tree build hit a sibling-trip-count mismatch bailout.
+  bool SawSiblingMismatchBailout = false;
 
   /// Maps the loops to their loop nests.
   SmallDenseMap<const Loop *, SmallVector<const Loop *>> LoopToLoopNest;
@@ -10022,13 +10029,13 @@ void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
   buildTreeRec(Roots, 0, EdgeInfo());
 }
 
-void BoUpSLP::buildTree(ArrayRef<Value *> Roots) {
+void BoUpSLP::buildTree(ArrayRef<Value *> Roots, bool AlwaysMergeSiblingLoops) {
   deleteTree();
   assert(TreeEntryToStridedPtrInfoMap.empty() &&
          "TreeEntryToStridedPtrInfoMap is not cleared");
   if (!allSameType(Roots))
     return;
-  buildTreeRec(Roots, 0, EdgeInfo());
+  buildTreeRec(Roots, 0, EdgeInfo(), AlwaysMergeSiblingLoops);
 }
 
 /// Tries to find subvector of loads and builds new vector of only loads if can
@@ -10189,7 +10196,8 @@ void BoUpSLP::tryToVectorizeGatheredLoads(
     const SmallMapVector<
         std::tuple<BasicBlock *, Value *, Type *>,
         SmallVector<SmallVector<std::pair<LoadInst *, int64_t>>>, 8>
-        &GatheredLoads) {
+        &GatheredLoads,
+    bool AlwaysMergeSiblingLoops) {
   GatheredLoadsEntriesFirst = VectorizableTree.size();
 
   SmallVector<SmallPtrSet<const Value *, 4>> LoadSetsToVectorize(
@@ -10583,7 +10591,8 @@ void BoUpSLP::tryToVectorizeGatheredLoads(
                            }))
                   continue;
                 unsigned Sz = VectorizableTree.size();
-                buildTreeRec(SubSlice, 0, EdgeInfo(), InterleaveFactor);
+                buildTreeRec(SubSlice, 0, EdgeInfo(), AlwaysMergeSiblingLoops,
+                             InterleaveFactor);
                 if (Sz == VectorizableTree.size()) {
                   IsVectorized = false;
                   // Try non-interleaved vectorization with smaller vector
@@ -10638,7 +10647,7 @@ void BoUpSLP::tryToVectorizeGatheredLoads(
       inversePermutation(E.ReorderIndices, ReorderMask);
       reorderScalars(GatheredScalars, ReorderMask);
     }
-    buildTreeRec(GatheredScalars, 0, EdgeInfo());
+    buildTreeRec(GatheredScalars, 0, EdgeInfo(), AlwaysMergeSiblingLoops);
   }
   // If no new entries created, consider it as no gathered loads entries must be
   // handled.
@@ -13101,6 +13110,7 @@ BoUpSLP::getScalarsVectorizationLegality(ArrayRef<Value *> VL, unsigned Depth,
 
 void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
                            const EdgeInfo &UserTreeIdx,
+                           bool AlwaysMergeSiblingLoops,
                            unsigned InterleaveFactor) {
   assert((allConstant(VLRef) || allSameType(VLRef)) && "Invalid types!");
 
@@ -13129,7 +13139,7 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       } else {
         TE->CombinedEntriesWithIndices.emplace_back(VectorizableTree.size(),
                                                     Idx == 0 ? 0 : Op1.size());
-        buildTreeRec(Op, Depth, {TE, Idx});
+        buildTreeRec(Op, Depth, {TE, Idx}, AlwaysMergeSiblingLoops);
       }
     };
     AddNode(Op1, 0);
@@ -13202,104 +13212,107 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
     return;
   }
 
-  // Check the loop nest. We need to be sure we handle a single loop nest at a
-  // time to avoid incorrect cost estimation because of the loop aware cost
-  // model.
-  if (VectorizableTree.empty()) {
-    assert(CurrentLoopNest.empty() && "Expected empty loop nest");
-    // Process the first node? Initial fill of the loop nest.
-    BasicBlock *Parent = S.getMainOp()->getParent();
-    if (const Loop *L = LI->getLoopFor(Parent)) {
-      L = findInnermostNonInvariantLoop(L, VL);
-      if (L)
-        CurrentLoopNest.assign(getLoopNest(L));
-    }
-  } else if (!UserTreeIdx ||
-             UserTreeIdx.UserTE->State == TreeEntry::SplitVectorize ||
-             UserTreeIdx.UserTE->isGather() ||
-             UserTreeIdx.UserTE->getMainOp()->getParent() !=
-                 S.getMainOp()->getParent()) {
-    BasicBlock *Parent = S.getMainOp()->getParent();
-    if (const Loop *L = LI->getLoopFor(Parent)) {
-      // Check that the new loop nest shares the same outer structure as the
-      // tree's current loop nest. Completely disjoint nests (different
-      // outermost loops) are forced to gather because their scales cannot be
-      // meaningfully combined. Sibling inner loops (inside a common outer
-      // loop or outside any loops at all) are allowed: the cost model scales
-      // each entry by its own loop via getScaleToLoopIterations(), so a tree
-      // that spans sibling inner loops (e.g. a PHI at their merge block) can
-      // still be costed correctly. Contract CurrentLoopNest to the longest
-      // common prefix with the new entry's nest so subsequent entries in yet
-      // another sibling can also be admitted.
-      L = findInnermostNonInvariantLoop(L, VL);
-      if (L) {
-        SmallVector<const Loop *> NewLoopNest(getLoopNest(L));
-        unsigned CommonLen = 0;
-        for (const auto [L1, L2] : zip(CurrentLoopNest, NewLoopNest)) {
-          if (L1 != L2)
-            break;
-          ++CommonLen;
-        }
-        auto ValidateMergedBTCs = [&](unsigned StartDepth) -> bool {
-          unsigned EndDepth =
-              std::min<unsigned>(NewLoopNest.size(), MergedLoopBTCs.size());
-          for (unsigned D = StartDepth; D < EndDepth; ++D) {
-            const SCEV *Constraint = MergedLoopBTCs[D];
-            if (!Constraint)
-              continue;
-            const SCEV *NewBTC = SE->getBackedgeTakenCount(NewLoopNest[D]);
-            if (isa<SCEVCouldNotCompute>(NewBTC) || NewBTC != Constraint)
-              return false;
-          }
-          return true;
-        };
-        auto BailOutToGather = [&]() {
-          LLVM_DEBUG(dbgs()
-                     << "SLP: Sibling loops have different trip counts.\n");
-          newGatherTreeEntry(VL, S, UserTreeIdx, ReuseShuffleIndices);
-        };
-        if (CurrentLoopNest.empty()) {
-          if (!ValidateMergedBTCs(0)) {
-            BailOutToGather();
-            return;
-          }
-          CurrentLoopNest.assign(NewLoopNest);
-        } else if (CommonLen < CurrentLoopNest.size() &&
-                   CommonLen < NewLoopNest.size()) {
-          // Divergence below the common prefix: the tree now spans sibling
-          // loops at depth CommonLen. Admitting them into one tree makes
-          // the profitability decision JOINT across both siblings, so a
-          // very hot sibling could otherwise let an unprofitable cold
-          // sibling ride along "for free" (per-entry scaling of the cold
-          // sibling's entries would be dwarfed by the hot one). Require
-          // SCEV-proven equal backedge-taken counts for the diverging
-          // siblings before joining; otherwise force gather.
-          const Loop *SibA = CurrentLoopNest[CommonLen];
-          const Loop *SibB = NewLoopNest[CommonLen];
-          const SCEV *BecA = SE->getBackedgeTakenCount(SibA);
-          const SCEV *BecB = SE->getBackedgeTakenCount(SibB);
-          if (isa<SCEVCouldNotCompute>(BecA) || BecA != BecB) {
-            BailOutToGather();
-            return;
-          }
-          if (!ValidateMergedBTCs(CommonLen + 1)) {
-            BailOutToGather();
-            return;
+  if (!AlwaysMergeSiblingLoops) {
+    // Check the loop nest. We need to be sure we handle a single loop nest at a
+    // time to avoid incorrect cost estimation because of the loop aware cost
+    // model.
+    if (VectorizableTree.empty()) {
+      assert(CurrentLoopNest.empty() && "Expected empty loop nest");
+      // Process the first node? Initial fill of the loop nest.
+      BasicBlock *Parent = S.getMainOp()->getParent();
+      if (const Loop *L = LI->getLoopFor(Parent)) {
+        L = findInnermostNonInvariantLoop(L, VL);
+        if (L)
+          CurrentLoopNest.assign(getLoopNest(L));
+      }
+    } else if (!UserTreeIdx ||
+               UserTreeIdx.UserTE->State == TreeEntry::SplitVectorize ||
+               UserTreeIdx.UserTE->isGather() ||
+               UserTreeIdx.UserTE->getMainOp()->getParent() !=
+                   S.getMainOp()->getParent()) {
+      BasicBlock *Parent = S.getMainOp()->getParent();
+      if (const Loop *L = LI->getLoopFor(Parent)) {
+        // Check that the new loop nest shares the same outer structure as the
+        // tree's current loop nest. Completely disjoint nests (different
+        // outermost loops) are forced to gather because their scales cannot be
+        // meaningfully combined. Sibling inner loops (inside a common outer
+        // loop or outside any loops at all) are allowed: the cost model scales
+        // each entry by its own loop via getScaleToLoopIterations(), so a tree
+        // that spans sibling inner loops (e.g. a PHI at their merge block) can
+        // still be costed correctly. Contract CurrentLoopNest to the longest
+        // common prefix with the new entry's nest so subsequent entries in yet
+        // another sibling can also be admitted.
+        L = findInnermostNonInvariantLoop(L, VL);
+        if (L) {
+          SmallVector<const Loop *> NewLoopNest(getLoopNest(L));
+          unsigned CommonLen = 0;
+          for (const auto [L1, L2] : zip(CurrentLoopNest, NewLoopNest)) {
+            if (L1 != L2)
+              break;
+            ++CommonLen;
           }
-          if (MergedLoopBTCs.size() <= CommonLen)
-            MergedLoopBTCs.resize(CommonLen + 1, nullptr);
-          MergedLoopBTCs[CommonLen] = BecA;
-          CurrentLoopNest.truncate(CommonLen);
-        } else if (NewLoopNest.size() > CurrentLoopNest.size()) {
-          if (!ValidateMergedBTCs(CurrentLoopNest.size())) {
-            BailOutToGather();
-            return;
+          auto ValidateMergedBTCs = [&](unsigned StartDepth) -> bool {
+            unsigned EndDepth =
+                std::min<unsigned>(NewLoopNest.size(), MergedLoopBTCs.size());
+            for (unsigned D = StartDepth; D < EndDepth; ++D) {
+              const SCEV *Constraint = MergedLoopBTCs[D];
+              if (!Constraint)
+                continue;
+              const SCEV *NewBTC = SE->getBackedgeTakenCount(NewLoopNest[D]);
+              if (isa<SCEVCouldNotCompute>(NewBTC) || NewBTC != Constraint)
+                return false;
+            }
+            return true;
+          };
+          auto BailOutToGather = [&]() {
+            SawSiblingMismatchBailout = true;
+            LLVM_DEBUG(dbgs()
+                       << "SLP: Sibling loops have different trip counts.\n");
+            newGatherTreeEntry(VL, S, UserTreeIdx, ReuseShuffleIndices);
+          };
+          if (CurrentLoopNest.empty()) {
+            if (!ValidateMergedBTCs(0)) {
+              BailOutToGather();
+              return;
+            }
+            CurrentLoopNest.assign(NewLoopNest);
+          } else if (CommonLen < CurrentLoopNest.size() &&
+                     CommonLen < NewLoopNest.size()) {
+            // Divergence below the common prefix: the tree now spans sibling
+            // loops at depth CommonLen. Admitting them into one tree makes
+            // the profitability decision JOINT across both siblings, so a
+            // very hot sibling could otherwise let an unprofitable cold
+            // sibling ride along "for free" (per-entry scaling of the cold
+            // sibling's entries would be dwarfed by the hot one). Require
+            // SCEV-proven equal backedge-taken counts for the diverging
+            // siblings before joining; otherwise force gather.
+            const Loop *SibA = CurrentLoopNest[CommonLen];
+            const Loop *SibB = NewLoopNest[CommonLen];
+            const SCEV *BecA = SE->getBackedgeTakenCount(SibA);
+            const SCEV *BecB = SE->getBackedgeTakenCount(SibB);
+            if (isa<SCEVCouldNotCompute>(BecA) || BecA != BecB) {
+              BailOutToGather();
+              return;
+            }
+            if (!ValidateMergedBTCs(CommonLen + 1)) {
+              BailOutToGather();
+              return;
+            }
+            if (MergedLoopBTCs.size() <= CommonLen)
+              MergedLoopBTCs.resize(CommonLen + 1, nullptr);
+            MergedLoopBTCs[CommonLen] = BecA;
+            CurrentLoopNest.truncate(CommonLen);
+          } else if (NewLoopNest.size() > CurrentLoopNest.size()) {
+            if (!ValidateMergedBTCs(CurrentLoopNest.size())) {
+              BailOutToGather();
+              return;
+            }
+            CurrentLoopNest.append(
+                std::next(NewLoopNest.begin(), CurrentLoopNest.size()),
+                NewLoopNest.end());
           }
-          CurrentLoopNest.append(
-              std::next(NewLoopNest.begin(), CurrentLoopNest.size()),
-              NewLoopNest.end());
+          // Otherwise NewLoopNest is a prefix of CurrentLoopNest: keep as-is.
         }
-        // Otherwise NewLoopNest is a prefix of CurrentLoopNest: keep as-is.
       }
     }
   }
@@ -13355,12 +13368,12 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
         continue;
       InstructionsState S = getSameOpcode(Op, *TLI);
       if ((!S || S.getOpcode() != Instruction::PHI) || S.isAltShuffle())
-        buildTreeRec(Op, Depth + 1, {TE, I});
+        buildTreeRec(Op, Depth + 1, {TE, I}, AlwaysMergeSiblingLoops);
       else
         PHIOps.push_back(I);
     }
     for (unsigned I : PHIOps)
-      buildTreeRec(Operands[I], Depth + 1, {TE, I});
+      buildTreeRec(Operands[I], Depth + 1, {TE, I}, AlwaysMergeSiblingLoops);
   };
   switch (ShuffleOrOp) {
     case Instruction::PHI: {
@@ -13402,7 +13415,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
         SmallVector<Value *> Calls;
         if (checkEVsForVecCalls(VL, S, *TLI, Indices, Calls)) {
           TE->StructEVIndices = std::move(Indices);
-          buildTreeRec(Operands.front(), Depth + 1, {TE, 0});
+          buildTreeRec(Operands.front(), Depth + 1, {TE, 0},
+                       AlwaysMergeSiblingLoops);
         }
       }
       return;
@@ -13437,7 +13451,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
                  TE->dump());
 
       TE->setOperands(Operands);
-      buildTreeRec(TE->getOperand(1), Depth + 1, {TE, 1});
+      buildTreeRec(TE->getOperand(1), Depth + 1, {TE, 1},
+                   AlwaysMergeSiblingLoops);
       return;
     }
     case Instruction::Load: {
@@ -13501,7 +13516,7 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       }
       TE->setOperands(Operands);
       if (State == TreeEntry::ScatterVectorize)
-        buildTreeRec(PointerOps, Depth + 1, {TE, 0});
+        buildTreeRec(PointerOps, Depth + 1, {TE, 0}, AlwaysMergeSiblingLoops);
       return;
     }
     case Instruction::ZExt:
@@ -13542,7 +13557,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
 
       TE->setOperands(Operands);
       for (unsigned I : seq<unsigned>(VL0->getNumOperands()))
-        buildTreeRec(TE->getOperand(I), Depth, {TE, I});
+        buildTreeRec(TE->getOperand(I), Depth, {TE, I},
+                     AlwaysMergeSiblingLoops);
       if (ShuffleOrOp == Instruction::Trunc) {
         ExtraBitWidthNodes.insert(getOperandEntry(TE, 0)->Idx);
       } else if (ShuffleOrOp == Instruction::SIToFP ||
@@ -13588,8 +13604,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
         }
       }
       TE->setOperands(Operands);
-      buildTreeRec(Operands.front(), Depth, {TE, 0});
-      buildTreeRec(Operands.back(), Depth, {TE, 1});
+      buildTreeRec(Operands.front(), Depth, {TE, 0}, AlwaysMergeSiblingLoops);
+      buildTreeRec(Operands.back(), Depth, {TE, 1}, AlwaysMergeSiblingLoops);
       if (ShuffleOrOp == Instruction::ICmp) {
         unsigned NumSignBits0 =
             ComputeNumSignBits(VL0->getOperand(0), *DL, AC, nullptr, DT);
@@ -13640,7 +13656,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       }
       TE->setOperands(Operands);
       for (unsigned I : seq<unsigned>(VL0->getNumOperands()))
-        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I});
+        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I},
+                     AlwaysMergeSiblingLoops);
       return;
     }
     case Instruction::GetElementPtr: {
@@ -13651,7 +13668,7 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       TE->setOperands(Operands);
 
       for (unsigned I = 0, Ops = Operands.size(); I < Ops; ++I)
-        buildTreeRec(Operands[I], Depth + 1, {TE, I});
+        buildTreeRec(Operands[I], Depth + 1, {TE, I}, AlwaysMergeSiblingLoops);
       return;
     }
     case Instruction::Store: {
@@ -13666,7 +13683,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
             dbgs() << "SLP: added a new TreeEntry (strided StoreInst).\n";
             TE->dump());
         TE->setOperands(Operands);
-        buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0});
+        buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0},
+                     AlwaysMergeSiblingLoops);
         return;
       }
       if (State == TreeEntry::ExpandVectorize) {
@@ -13681,7 +13699,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
                 << "SLP: added a new TreeEntry (expanded masked StoreInst).\n";
             TE->dump());
         TE->setOperands(Operands);
-        buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0});
+        buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0},
+                     AlwaysMergeSiblingLoops);
         return;
       }
       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
@@ -13689,7 +13708,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       LLVM_DEBUG(dbgs() << "SLP: added a new TreeEntry (StoreInst).\n";
                  TE->dump());
       TE->setOperands(Operands);
-      buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0});
+      buildTreeRec(TE->getOperand(0), Depth + 1, {TE, 0},
+                   AlwaysMergeSiblingLoops);
       return;
     }
     case Instruction::Call: {
@@ -13714,7 +13734,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
         // vectorize it.
         if (isVectorIntrinsicWithScalarOpAtArg(ID, I, TTI))
           continue;
-        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I});
+        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I},
+                     AlwaysMergeSiblingLoops);
       }
       return;
     }
@@ -13758,8 +13779,10 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
           }
         }
         TE->setOperands(Operands);
-        buildTreeRec(Operands.front(), Depth + 1, {TE, 0});
-        buildTreeRec(Operands.back(), Depth + 1, {TE, 1});
+        buildTreeRec(Operands.front(), Depth + 1, {TE, 0},
+                     AlwaysMergeSiblingLoops);
+        buildTreeRec(Operands.back(), Depth + 1, {TE, 1},
+                     AlwaysMergeSiblingLoops);
         return;
       }
 
@@ -13771,7 +13794,8 @@ void BoUpSLP::buildTreeRec(ArrayRef<Value *> VLRef, unsigned Depth,
       }
       TE->setOperands(Operands);
       for (unsigned I : seq<unsigned>(VL0->getNumOperands()))
-        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I});
+        buildTreeRec(TE->getOperand(I), Depth + 1, {TE, I},
+                     AlwaysMergeSiblingLoops);
       return;
     }
     default:
@@ -15222,7 +15246,7 @@ bool BoUpSLP::matchesSelectOfBits(const TreeEntry &SelectTE) const {
   return BitcastCost <= SelectCost;
 }
 
-void BoUpSLP::transformNodes() {
+void BoUpSLP::transformNodes(bool AlwaysMergeSiblingLoops) {
   constexpr TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
   BaseGraphSize = VectorizableTree.size();
   // Turn graph transforming mode on and off, when done.
@@ -15484,7 +15508,8 @@ void BoUpSLP::transformNodes() {
           unsigned PrevSize = VectorizableTree.size();
           [[maybe_unused]] unsigned PrevEntriesSize =
               LoadEntriesToVectorize.size();
-          buildTreeRec(Slice, 0, EdgeInfo(&E, UINT_MAX));
+          buildTreeRec(Slice, 0, EdgeInfo(&E, UINT_MAX),
+                       AlwaysMergeSiblingLoops);
           if (PrevSize + 1 == VectorizableTree.size() && !SameTE &&
               VectorizableTree[PrevSize]->isGather() &&
               VectorizableTree[PrevSize]->hasState() &&
@@ -15831,7 +15856,7 @@ void BoUpSLP::transformNodes() {
   }
   // Try to vectorize gathered loads if this is not just a gather of loads.
   if (!GatheredLoads.empty())
-    tryToVectorizeGatheredLoads(GatheredLoads);
+    tryToVectorizeGatheredLoads(GatheredLoads, AlwaysMergeSiblingLoops);
 }
 
 /// Merges shuffle masks and emits final shuffle instruction, if required. It
@@ -27980,6 +28005,23 @@ bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
   return Changed;
 }
 
+static InstructionCost prepareCurrentTree(BoUpSLP &R,
+                                          bool AlwaysMergeSiblingLoops,
+                                          const Value *Op = nullptr) {
+  if (R.isProfitableToReorder()) {
+    R.reorderTopToBottom();
+    R.reorderBottomToTop(Op != nullptr &&
+                         !isa<InsertElementInst, InsertValueInst>(Op));
+  }
+  R.transformNodes(AlwaysMergeSiblingLoops);
+  R.computeMinimumValueSizes();
+
+  InstructionCost TreeCost = R.calculateTreeCostAndTrimNonProfitable();
+  R.buildExternalUses();
+
+  return TreeCost;
+}
+
 std::optional<bool>
 SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
                                        unsigned Idx, unsigned MinVF,
@@ -28031,7 +28073,22 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
       return false;
     }
   }
+
+  auto PrepareAndCostCurrentTree =
+      [](BoUpSLP &R, const InstructionsState &S,
+         bool AlwaysMergeSiblingLoops =
+             false) -> std::pair<InstructionCost, unsigned> {
+    InstructionCost TreeCost = prepareCurrentTree(R, AlwaysMergeSiblingLoops);
+
+    unsigned CanonicalSize = R.getCanonicalGraphSize();
+    if (S && S.getOpcode() == Instruction::Load)
+      CanonicalSize = 2; // cut off masked gather small trees
+
+    return {R.getTreeCost(TreeCost), CanonicalSize};
+  };
+
   R.buildTree(Chain);
+
   // Check if tree tiny and store itself or its value is not vectorized.
   if (R.isTreeTinyAndNotFullyVectorizable()) {
     if (R.isGathered(Chain.front()) ||
@@ -28040,23 +28097,33 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
     Size = R.getCanonicalGraphSize();
     return false;
   }
-  if (R.isProfitableToReorder()) {
-    R.reorderTopToBottom();
-    R.reorderBottomToTop();
-  }
-  R.transformNodes();
-  R.computeMinimumValueSizes();
 
-  InstructionCost TreeCost = R.calculateTreeCostAndTrimNonProfitable();
-  R.buildExternalUses();
+  auto [Cost, BestSize] = PrepareAndCostCurrentTree(R, S);
 
-  Size = R.getCanonicalGraphSize();
-  if (S && S.getOpcode() == Instruction::Load)
-    Size = 2; // cut off masked gather small trees
-  InstructionCost Cost = R.getTreeCost(TreeCost);
+  bool RebuildTreeRequired = false;
+  if (R.sawSiblingMismatchBailout()) {
+    R.buildTree(Chain, /*AlwaysMergeSiblingLoops=*/true);
+    RebuildTreeRequired = true;
+
+    if (!R.isTreeTinyAndNotFullyVectorizable()) {
+      auto [AllowCost, AllowSize] =
+          PrepareAndCostCurrentTree(R, S, /*AlwaysMergeSiblingLoops=*/true);
+      if (AllowCost < Cost) {
+        Cost = AllowCost;
+        BestSize = AllowSize;
+        RebuildTreeRequired = false;
+      }
+    }
+  }
+  Size = BestSize;
 
   LLVM_DEBUG(dbgs() << "SLP: Found cost = " << Cost << " for VF=" << VF << "\n");
   if (Cost < -SLPCostThreshold) {
+    if (RebuildTreeRequired) {
+      R.buildTree(Chain);
+      PrepareAndCostCurrentTree(R, S);
+    }
+
     LLVM_DEBUG(dbgs() << "SLP: Decided to vectorize cost = " << Cost << "\n");
 
     using namespace ore;
@@ -29024,23 +29091,36 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
       R.buildTree(Ops);
       if (R.isTreeTinyAndNotFullyVectorizable())
         continue;
-      if (R.isProfitableToReorder()) {
-        R.reorderTopToBottom();
-        R.reorderBottomToTop(
-            !isa<InsertElementInst, InsertValueInst>(Ops.front()));
-      }
-      R.transformNodes();
-      R.computeMinimumValueSizes();
-      InstructionCost TreeCost = R.calculateTreeCostAndTrimNonProfitable();
-      R.buildExternalUses();
 
-      InstructionCost Cost = R.getTreeCost(TreeCost);
+      InstructionCost Cost = R.getTreeCost(prepareCurrentTree(
+          R, /*AlwaysMergeSiblingLoops=*/false, Ops.front()));
+
+      bool RebuildTreeRequired = false;
+      if (R.sawSiblingMismatchBailout()) {
+        R.buildTree(Ops, /*AlwaysMergeSiblingLoops=*/true);
+        RebuildTreeRequired = true;
+
+        if (!R.isTreeTinyAndNotFullyVectorizable()) {
+          InstructionCost AllowCost = R.getTreeCost(prepareCurrentTree(
+              R, /*AlwaysMergeSiblingLoops=*/true, Ops.front()));
+          if (AllowCost < Cost) {
+            Cost = AllowCost;
+            RebuildTreeRequired = false;
+          }
+        }
+      }
       CandidateFound = true;
       MinCost = std::min(MinCost, Cost);
 
       LLVM_DEBUG(dbgs() << "SLP: Found cost = " << Cost
                         << " for VF=" << ActualVF << "\n");
       if (Cost < -SLPCostThreshold) {
+        if (RebuildTreeRequired) {
+          R.buildTree(Ops);
+          R.getTreeCost(prepareCurrentTree(R, /*AlwaysMergeSiblingLoops=*/false,
+                                           Ops.front()));
+        }
+
         LLVM_DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
         R.getORE()->emit(OptimizationRemark(SV_NAME, "VectorizedList",
                                                     cast<Instruction>(Ops[0]))
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll
index 689f91a2470c4..04453a71aee34 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/sibling-loops-mismatched-tripcount.ll
@@ -9,46 +9,34 @@ define void @sibling_mismatch_stores(ptr %out, double %a, double %b, double %c,
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    br i1 [[SEL]], label %[[A_PREHEADER:.*]], label %[[B_PREHEADER:.*]]
 ; CHECK:       [[A_PREHEADER]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = insertelement <6 x double> poison, double [[A]], i32 0
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <6 x double> [[TMP0]], double [[B]], i32 1
+; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <6 x double> [[TMP1]], double [[C]], i32 2
+; CHECK-NEXT:    [[TMP3:%.*]] = shufflevector <6 x double> [[TMP2]], <6 x double> poison, <6 x i32> <i32 0, i32 1, i32 2, i32 0, i32 1, i32 2>
 ; CHECK-NEXT:    br label %[[A_HEADER:.*]]
 ; CHECK:       [[A_HEADER]]:
 ; CHECK-NEXT:    [[IA:%.*]] = phi i32 [ 0, %[[A_PREHEADER]] ], [ [[IA_NEXT:%.*]], %[[A_LATCH:.*]] ]
-; CHECK-NEXT:    [[AA0:%.*]] = phi double [ [[A]], %[[A_PREHEADER]] ], [ [[AA0N:%.*]], %[[A_LATCH]] ]
-; CHECK-NEXT:    [[AA1:%.*]] = phi double [ [[B]], %[[A_PREHEADER]] ], [ [[AA1N:%.*]], %[[A_LATCH]] ]
-; CHECK-NEXT:    [[AA2:%.*]] = phi double [ [[C]], %[[A_PREHEADER]] ], [ [[AA2N:%.*]], %[[A_LATCH]] ]
-; CHECK-NEXT:    [[AA3:%.*]] = phi double [ [[A]], %[[A_PREHEADER]] ], [ [[AA3N:%.*]], %[[A_LATCH]] ]
-; CHECK-NEXT:    [[AA4:%.*]] = phi double [ [[B]], %[[A_PREHEADER]] ], [ [[AA4N:%.*]], %[[A_LATCH]] ]
-; CHECK-NEXT:    [[AA5:%.*]] = phi double [ [[C]], %[[A_PREHEADER]] ], [ [[AA5N:%.*]], %[[A_LATCH]] ]
+; CHECK-NEXT:    [[TMP4:%.*]] = phi <6 x double> [ [[TMP3]], %[[A_PREHEADER]] ], [ [[TMP5:%.*]], %[[A_LATCH]] ]
 ; CHECK-NEXT:    br label %[[A_BODY:.*]]
 ; CHECK:       [[A_BODY]]:
-; CHECK-NEXT:    [[AA0N]] = fadd double [[AA0]], [[A]]
-; CHECK-NEXT:    [[AA1N]] = fadd double [[AA1]], [[B]]
-; CHECK-NEXT:    [[AA2N]] = fadd double [[AA2]], [[C]]
-; CHECK-NEXT:    [[AA3N]] = fadd double [[AA3]], [[A]]
-; CHECK-NEXT:    [[AA4N]] = fadd double [[AA4]], [[B]]
-; CHECK-NEXT:    [[AA5N]] = fadd double [[AA5]], [[C]]
+; CHECK-NEXT:    [[TMP5]] = fadd <6 x double> [[TMP4]], [[TMP3]]
 ; CHECK-NEXT:    br label %[[A_LATCH]]
 ; CHECK:       [[A_LATCH]]:
 ; CHECK-NEXT:    [[IA_NEXT]] = add nuw nsw i32 [[IA]], 1
 ; CHECK-NEXT:    [[A_DONE:%.*]] = icmp eq i32 [[IA_NEXT]], [[NA]]
 ; CHECK-NEXT:    br i1 [[A_DONE]], label %[[A_EXIT:.*]], label %[[A_HEADER]]
 ; CHECK:       [[B_PREHEADER]]:
+; CHECK-NEXT:    [[TMP6:%.*]] = insertelement <6 x double> poison, double [[B]], i32 0
+; CHECK-NEXT:    [[TMP7:%.*]] = insertelement <6 x double> [[TMP6]], double [[C]], i32 1
+; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <6 x double> [[TMP7]], double [[A]], i32 2
+; CHECK-NEXT:    [[TMP9:%.*]] = shufflevector <6 x double> [[TMP8]], <6 x double> poison, <6 x i32> <i32 0, i32 1, i32 2, i32 0, i32 1, i32 2>
 ; CHECK-NEXT:    br label %[[B_HEADER:.*]]
 ; CHECK:       [[B_HEADER]]:
 ; CHECK-NEXT:    [[IB:%.*]] = phi i32 [ 0, %[[B_PREHEADER]] ], [ [[IB_NEXT:%.*]], %[[B_LATCH:.*]] ]
-; CHECK-NEXT:    [[BB0:%.*]] = phi double [ [[B]], %[[B_PREHEADER]] ], [ [[BB0N:%.*]], %[[B_LATCH]] ]
-; CHECK-NEXT:    [[BB1:%.*]] = phi double [ [[C]], %[[B_PREHEADER]] ], [ [[BB1N:%.*]], %[[B_LATCH]] ]
-; CHECK-NEXT:    [[BB2:%.*]] = phi double [ [[A]], %[[B_PREHEADER]] ], [ [[BB2N:%.*]], %[[B_LATCH]] ]
-; CHECK-NEXT:    [[BB3:%.*]] = phi double [ [[B]], %[[B_PREHEADER]] ], [ [[BB3N:%.*]], %[[B_LATCH]] ]
-; CHECK-NEXT:    [[BB4:%.*]] = phi double [ [[C]], %[[B_PREHEADER]] ], [ [[BB4N:%.*]], %[[B_LATCH]] ]
-; CHECK-NEXT:    [[BB5:%.*]] = phi double [ [[A]], %[[B_PREHEADER]] ], [ [[BB5N:%.*]], %[[B_LATCH]] ]
+; CHECK-NEXT:    [[TMP10:%.*]] = phi <6 x double> [ [[TMP9]], %[[B_PREHEADER]] ], [ [[TMP11:%.*]], %[[B_LATCH]] ]
 ; CHECK-NEXT:    br label %[[B_BODY:.*]]
 ; CHECK:       [[B_BODY]]:
-; CHECK-NEXT:    [[BB0N]] = fadd double [[BB0]], [[B]]
-; CHECK-NEXT:    [[BB1N]] = fadd double [[BB1]], [[C]]
-; CHECK-NEXT:    [[BB2N]] = fadd double [[BB2]], [[A]]
-; CHECK-NEXT:    [[BB3N]] = fadd double [[BB3]], [[B]]
-; CHECK-NEXT:    [[BB4N]] = fadd double [[BB4]], [[C]]
-; CHECK-NEXT:    [[BB5N]] = fadd double [[BB5]], [[A]]
+; CHECK-NEXT:    [[TMP11]] = fadd <6 x double> [[TMP10]], [[TMP9]]
 ; CHECK-NEXT:    br label %[[B_LATCH]]
 ; CHECK:       [[B_LATCH]]:
 ; CHECK-NEXT:    [[IB_NEXT]] = add nuw nsw i32 [[IB]], 1
@@ -59,52 +47,22 @@ define void @sibling_mismatch_stores(ptr %out, double %a, double %b, double %c,
 ; CHECK:       [[B_EXIT]]:
 ; CHECK-NEXT:    br label %[[JOIN]]
 ; CHECK:       [[JOIN]]:
-; CHECK-NEXT:    [[S0:%.*]] = phi double [ [[AA0N]], %[[A_EXIT]] ], [ [[BB0N]], %[[B_EXIT]] ]
-; CHECK-NEXT:    [[S1:%.*]] = phi double [ [[AA1N]], %[[A_EXIT]] ], [ [[BB1N]], %[[B_EXIT]] ]
-; CHECK-NEXT:    [[S2:%.*]] = phi double [ [[AA2N]], %[[A_EXIT]] ], [ [[BB2N]], %[[B_EXIT]] ]
-; CHECK-NEXT:    [[S3:%.*]] = phi double [ [[AA3N]], %[[A_EXIT]] ], [ [[BB3N]], %[[B_EXIT]] ]
-; CHECK-NEXT:    [[S4:%.*]] = phi double [ [[AA4N]], %[[A_EXIT]] ], [ [[BB4N]], %[[B_EXIT]] ]
-; CHECK-NEXT:    [[S5:%.*]] = phi double [ [[AA5N]], %[[A_EXIT]] ], [ [[BB5N]], %[[B_EXIT]] ]
+; CHECK-NEXT:    [[TMP12:%.*]] = phi <6 x double> [ [[TMP5]], %[[A_EXIT]] ], [ [[TMP11]], %[[B_EXIT]] ]
 ; CHECK-NEXT:    br label %[[HOT_HEADER:.*]]
 ; CHECK:       [[HOT_HEADER]]:
 ; CHECK-NEXT:    [[IH:%.*]] = phi i32 [ 0, %[[JOIN]] ], [ [[IH_NEXT:%.*]], %[[HOT_LATCH:.*]] ]
-; CHECK-NEXT:    [[P0:%.*]] = phi double [ [[S0]], %[[JOIN]] ], [ [[N0:%.*]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[P1:%.*]] = phi double [ [[S1]], %[[JOIN]] ], [ [[N1:%.*]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[P2:%.*]] = phi double [ [[S2]], %[[JOIN]] ], [ [[N2:%.*]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[P3:%.*]] = phi double [ [[S3]], %[[JOIN]] ], [ [[N3:%.*]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[P4:%.*]] = phi double [ [[S4]], %[[JOIN]] ], [ [[N4:%.*]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[P5:%.*]] = phi double [ [[S5]], %[[JOIN]] ], [ [[N5:%.*]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    [[TMP13:%.*]] = phi <6 x double> [ [[TMP12]], %[[JOIN]] ], [ [[TMP14:%.*]], %[[HOT_LATCH]] ]
 ; CHECK-NEXT:    br label %[[HOT_BODY:.*]]
 ; CHECK:       [[HOT_BODY]]:
-; CHECK-NEXT:    [[N0]] = fadd double [[P0]], [[S0]]
-; CHECK-NEXT:    [[N1]] = fadd double [[P1]], [[S1]]
-; CHECK-NEXT:    [[N2]] = fadd double [[P2]], [[S2]]
-; CHECK-NEXT:    [[N3]] = fadd double [[P3]], [[S3]]
-; CHECK-NEXT:    [[N4]] = fadd double [[P4]], [[S4]]
-; CHECK-NEXT:    [[N5]] = fadd double [[P5]], [[S5]]
+; CHECK-NEXT:    [[TMP14]] = fadd <6 x double> [[TMP13]], [[TMP12]]
 ; CHECK-NEXT:    br label %[[HOT_LATCH]]
 ; CHECK:       [[HOT_LATCH]]:
 ; CHECK-NEXT:    [[IH_NEXT]] = add nuw nsw i32 [[IH]], 1
 ; CHECK-NEXT:    [[H_DONE:%.*]] = icmp eq i32 [[IH_NEXT]], [[NH]]
 ; CHECK-NEXT:    br i1 [[H_DONE]], label %[[EXIT:.*]], label %[[HOT_HEADER]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    [[R0:%.*]] = phi double [ [[N0]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[R1:%.*]] = phi double [ [[N1]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[R2:%.*]] = phi double [ [[N2]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[R3:%.*]] = phi double [ [[N3]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[R4:%.*]] = phi double [ [[N4]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[R5:%.*]] = phi double [ [[N5]], %[[HOT_LATCH]] ]
-; CHECK-NEXT:    [[OUT1:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 1
-; CHECK-NEXT:    [[OUT2:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 2
-; CHECK-NEXT:    [[OUT3:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 3
-; CHECK-NEXT:    [[OUT4:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 4
-; CHECK-NEXT:    [[OUT5:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 5
-; CHECK-NEXT:    store double [[R0]], ptr [[OUT]], align 8
-; CHECK-NEXT:    store double [[R1]], ptr [[OUT1]], align 8
-; CHECK-NEXT:    store double [[R2]], ptr [[OUT2]], align 8
-; CHECK-NEXT:    store double [[R3]], ptr [[OUT3]], align 8
-; CHECK-NEXT:    store double [[R4]], ptr [[OUT4]], align 8
-; CHECK-NEXT:    store double [[R5]], ptr [[OUT5]], align 8
+; CHECK-NEXT:    [[TMP15:%.*]] = phi <6 x double> [ [[TMP14]], %[[HOT_LATCH]] ]
+; CHECK-NEXT:    store <6 x double> [[TMP15]], ptr [[OUT]], align 8
 ; CHECK-NEXT:    ret void
 ;
 entry:
@@ -240,8 +198,6 @@ define dso_local void @sibling_mismatch_reduction(ptr noundef %base, ptr noundef
 ; CHECK:       [[FOR_COND9_PREHEADER]]:
 ; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x double> [ zeroinitializer, %[[ENTRY]] ], [ [[TMP5:%.*]], %[[FOR_BODY]] ]
 ; CHECK-NEXT:    [[CMP1148:%.*]] = icmp ugt i64 [[SUB_PTR_DIV4]], 1
-; CHECK-NEXT:    [[TMP1:%.*]] = extractelement <2 x double> [[TMP0]], i32 0
-; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <2 x double> [[TMP0]], i32 1
 ; CHECK-NEXT:    br i1 [[CMP1148]], label %[[FOR_BODY13:.*]], label %[[FOR_COND_CLEANUP12:.*]]
 ; CHECK:       [[FOR_BODY]]:
 ; CHECK-NEXT:    [[I_043:%.*]] = phi i64 [ [[ADD7:%.*]], %[[FOR_BODY]] ], [ 0, %[[ENTRY]] ]
@@ -254,21 +210,18 @@ define dso_local void @sibling_mismatch_reduction(ptr noundef %base, ptr noundef
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i64 [[ADD]], [[SUB_PTR_DIV]]
 ; CHECK-NEXT:    br i1 [[CMP]], label %[[FOR_BODY]], label %[[FOR_COND9_PREHEADER]]
 ; CHECK:       [[FOR_COND_CLEANUP12]]:
-; CHECK-NEXT:    [[ACC_SROA_8_1_LCSSA:%.*]] = phi double [ [[TMP2]], %[[FOR_COND9_PREHEADER]] ], [ [[TMP9:%.*]], %[[FOR_BODY13]] ]
-; CHECK-NEXT:    [[ACC_SROA_0_1_LCSSA:%.*]] = phi double [ [[TMP1]], %[[FOR_COND9_PREHEADER]] ], [ [[TMP8:%.*]], %[[FOR_BODY13]] ]
+; CHECK-NEXT:    [[TMP6:%.*]] = phi <2 x double> [ [[TMP0]], %[[FOR_COND9_PREHEADER]] ], [ [[TMP9:%.*]], %[[FOR_BODY13]] ]
+; CHECK-NEXT:    [[ACC_SROA_0_1_LCSSA:%.*]] = extractelement <2 x double> [[TMP6]], i32 0
+; CHECK-NEXT:    [[ACC_SROA_8_1_LCSSA:%.*]] = extractelement <2 x double> [[TMP6]], i32 1
 ; CHECK-NEXT:    [[ADD26:%.*]] = fadd double [[ACC_SROA_8_1_LCSSA]], [[ACC_SROA_0_1_LCSSA]]
 ; CHECK-NEXT:    store double [[ADD26]], ptr [[OUT]], align 8
 ; CHECK-NEXT:    ret void
 ; CHECK:       [[FOR_BODY13]]:
-; CHECK-NEXT:    [[ACC_SROA_0_151:%.*]] = phi double [ [[TMP8]], %[[FOR_BODY13]] ], [ [[TMP1]], %[[FOR_COND9_PREHEADER]] ]
-; CHECK-NEXT:    [[ACC_SROA_8_150:%.*]] = phi double [ [[TMP9]], %[[FOR_BODY13]] ], [ [[TMP2]], %[[FOR_COND9_PREHEADER]] ]
 ; CHECK-NEXT:    [[I8_049:%.*]] = phi i64 [ [[ADD22:%.*]], %[[FOR_BODY13]] ], [ 0, %[[FOR_COND9_PREHEADER]] ]
+; CHECK-NEXT:    [[TMP7:%.*]] = phi <2 x double> [ [[TMP9]], %[[FOR_BODY13]] ], [ [[TMP0]], %[[FOR_COND9_PREHEADER]] ]
 ; CHECK-NEXT:    [[ARRAYIDX15:%.*]] = getelementptr inbounds nuw [8 x i8], ptr [[ARR]], i64 [[I8_049]]
-; CHECK-NEXT:    [[TMP6:%.*]] = load double, ptr [[ARRAYIDX15]], align 8
-; CHECK-NEXT:    [[ARRAYIDX18:%.*]] = getelementptr inbounds nuw i8, ptr [[ARRAYIDX15]], i64 8
-; CHECK-NEXT:    [[TMP7:%.*]] = load double, ptr [[ARRAYIDX18]], align 8
-; CHECK-NEXT:    [[TMP8]] = tail call double @llvm.fmuladd.f64(double [[TMP6]], double 4.000000e+00, double [[ACC_SROA_0_151]])
-; CHECK-NEXT:    [[TMP9]] = tail call double @llvm.fmuladd.f64(double [[TMP7]], double 4.000000e+00, double [[ACC_SROA_8_150]])
+; CHECK-NEXT:    [[TMP8:%.*]] = load <2 x double>, ptr [[ARRAYIDX15]], align 8
+; CHECK-NEXT:    [[TMP9]] = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> [[TMP8]], <2 x double> splat (double 4.000000e+00), <2 x double> [[TMP7]])
 ; CHECK-NEXT:    [[ADD22]] = add nuw i64 [[I8_049]], 2
 ; CHECK-NEXT:    [[ADD10:%.*]] = or disjoint i64 [[ADD22]], 1
 ; CHECK-NEXT:    [[CMP11:%.*]] = icmp ult i64 [[ADD10]], [[SUB_PTR_DIV4]]



More information about the llvm-commits mailing list