[llvm] [AArch64] Model shifts folded into scalar logical instructions (PR #229109)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 07:55:37 PDT 2026


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

Account for single-use constant shifts that can be folded into the
shifted-register operand of a scalar logical instruction.

Assisted-by: Codex


>From 20de85712bcfa3797591f49271bcbec3f731a366 Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Thu, 24 Sep 2026 14:17:27 +0000
Subject: [PATCH 1/4] Adjust SLP test threshold

---
 .../SLPVectorizer/AArch64/narrowed-reduction-chain-use.ll       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/narrowed-reduction-chain-use.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/narrowed-reduction-chain-use.ll
index c375ffe26a551..031eca3df3778 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/narrowed-reduction-chain-use.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/narrowed-reduction-chain-use.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt < %s -passes=slp-vectorizer -S -mtriple=aarch64-linux-gnu | FileCheck %s
+; RUN: opt < %s -passes=slp-vectorizer -slp-threshold=-1 -S -mtriple=aarch64-linux-gnu | FileCheck %s
 
 
 define ptr @test(ptr %q, ptr %z) {

>From 971a0944b1b19b2951031e857520872097dd0e3d Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Thu, 17 Sep 2026 14:53:19 +0000
Subject: [PATCH 2/4] Move loop invariant code out of loop in recurrence test

---
 .../AArch64/reduction-recurrence-costs-sve.ll | 664 ++++++++----------
 1 file changed, 275 insertions(+), 389 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/reduction-recurrence-costs-sve.ll b/llvm/test/Transforms/LoopVectorize/AArch64/reduction-recurrence-costs-sve.ll
index b330affc37dfa..22787392810e9 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/reduction-recurrence-costs-sve.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/reduction-recurrence-costs-sve.ll
@@ -9,169 +9,118 @@ target triple = "arm64-apple-macosx14.0.0"
 define i32 @chained_recurrences(i32 %x, i64 %y, ptr %src.1, i32 %z, ptr %src.2) #0 {
 ; DEFAULT-LABEL: define i32 @chained_recurrences(
 ; DEFAULT-SAME: i32 [[X:%.*]], i64 [[Y:%.*]], ptr [[SRC_1:%.*]], i32 [[Z:%.*]], ptr [[SRC_2:%.*]]) #[[ATTR0:[0-9]+]] {
-; DEFAULT-NEXT:  [[ENTRY:.*]]:
-; DEFAULT-NEXT:    [[TMP0:%.*]] = add i64 [[Y]], 1
-; DEFAULT-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 16
-; DEFAULT-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
-; DEFAULT:       [[VECTOR_PH]]:
-; DEFAULT-NEXT:    [[N_MOD_VF:%.*]] = and i64 [[TMP0]], 15
-; DEFAULT-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
-; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[X]], i64 0
-; DEFAULT-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
-; DEFAULT-NEXT:    [[TMP1:%.*]] = add i64 [[Y]], 1
-; DEFAULT-NEXT:    [[TMP2:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP1]]
-; DEFAULT-NEXT:    [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
-; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT7:%.*]] = insertelement <4 x i32> poison, i32 [[TMP3]], i64 0
-; DEFAULT-NEXT:    [[BROADCAST_SPLAT8:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT7]], <4 x i32> poison, <4 x i32> zeroinitializer
-; DEFAULT-NEXT:    [[TMP4:%.*]] = lshr <4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP5:%.*]] = shl <4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP6:%.*]] = or <4 x i32> [[TMP4]], [[TMP5]]
+; DEFAULT-NEXT:  [[VECTOR_PH:.*]]:
 ; DEFAULT-NEXT:    [[TMP7:%.*]] = or i32 [[Z]], [[X]]
-; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[TMP7]], i64 0
-; DEFAULT-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
-; DEFAULT-NEXT:    [[TMP8:%.*]] = and <4 x i32> [[BROADCAST_SPLAT2]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP9:%.*]] = xor <4 x i32> [[TMP8]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP10:%.*]] = zext <4 x i32> [[TMP9]] to <4 x i64>
-; DEFAULT-NEXT:    [[TMP11:%.*]] = extractelement <4 x i64> [[TMP10]], i64 0
-; DEFAULT-NEXT:    [[TMP12:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP11]]
-; DEFAULT-NEXT:    [[TMP13:%.*]] = extractelement <4 x i64> [[TMP10]], i64 1
-; DEFAULT-NEXT:    [[TMP14:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP13]]
-; DEFAULT-NEXT:    [[TMP15:%.*]] = extractelement <4 x i64> [[TMP10]], i64 2
-; DEFAULT-NEXT:    [[TMP16:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP15]]
-; DEFAULT-NEXT:    [[TMP17:%.*]] = extractelement <4 x i64> [[TMP10]], i64 3
-; DEFAULT-NEXT:    [[TMP18:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP17]]
-; DEFAULT-NEXT:    [[TMP19:%.*]] = load i32, ptr [[TMP12]], align 4
-; DEFAULT-NEXT:    [[TMP20:%.*]] = load i32, ptr [[TMP14]], align 4
-; DEFAULT-NEXT:    [[TMP21:%.*]] = load i32, ptr [[TMP16]], align 4
-; DEFAULT-NEXT:    [[TMP22:%.*]] = load i32, ptr [[TMP18]], align 4
-; DEFAULT-NEXT:    [[TMP23:%.*]] = insertelement <4 x i32> poison, i32 [[TMP19]], i64 0
-; DEFAULT-NEXT:    [[TMP24:%.*]] = insertelement <4 x i32> [[TMP23]], i32 [[TMP20]], i64 1
-; DEFAULT-NEXT:    [[TMP25:%.*]] = insertelement <4 x i32> [[TMP24]], i32 [[TMP21]], i64 2
-; DEFAULT-NEXT:    [[TMP26:%.*]] = insertelement <4 x i32> [[TMP25]], i32 [[TMP22]], i64 3
-; DEFAULT-NEXT:    br label %[[LOOP:.*]]
-; DEFAULT:       [[LOOP]]:
-; DEFAULT-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, %[[VECTOR_PH]] ], [ [[BROADCAST_SPLAT8]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VECTOR_RECUR3:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, %[[VECTOR_PH]] ], [ [[TMP28:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VEC_PHI:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP113:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VEC_PHI4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP114:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VEC_PHI5:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP115:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[VEC_PHI6:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP116:%.*]], %[[LOOP]] ]
-; DEFAULT-NEXT:    [[TMP27:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; DEFAULT-NEXT:    [[TMP28]] = shufflevector <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; DEFAULT-NEXT:    [[TMP29:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR3]], <4 x i32> [[TMP27]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; DEFAULT-NEXT:    [[TMP30:%.*]] = shufflevector <4 x i32> [[TMP27]], <4 x i32> [[TMP28]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; DEFAULT-NEXT:    [[TMP31:%.*]] = shufflevector <4 x i32> [[TMP28]], <4 x i32> [[TMP28]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; DEFAULT-NEXT:    [[TMP32:%.*]] = or <4 x i32> [[TMP29]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP33:%.*]] = or <4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP34:%.*]] = or <4 x i32> [[TMP31]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP35:%.*]] = shl <4 x i32> [[TMP32]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP36:%.*]] = shl <4 x i32> [[TMP33]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP37:%.*]] = shl <4 x i32> [[TMP34]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP38:%.*]] = or <4 x i32> [[TMP35]], splat (i32 2)
-; DEFAULT-NEXT:    [[TMP39:%.*]] = or <4 x i32> [[TMP36]], splat (i32 2)
-; DEFAULT-NEXT:    [[TMP40:%.*]] = or <4 x i32> [[TMP37]], splat (i32 2)
-; DEFAULT-NEXT:    [[TMP41:%.*]] = or <4 x i32> [[TMP6]], [[TMP38]]
-; DEFAULT-NEXT:    [[TMP42:%.*]] = or <4 x i32> [[TMP6]], [[TMP39]]
-; DEFAULT-NEXT:    [[TMP43:%.*]] = or <4 x i32> [[TMP6]], [[TMP40]]
-; DEFAULT-NEXT:    [[TMP44:%.*]] = or <4 x i32> [[TMP41]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP45:%.*]] = or <4 x i32> [[TMP42]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP46:%.*]] = or <4 x i32> [[TMP43]], [[BROADCAST_SPLAT]]
-; DEFAULT-NEXT:    [[TMP47:%.*]] = lshr <4 x i32> [[TMP44]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP48:%.*]] = lshr <4 x i32> [[TMP45]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP49:%.*]] = lshr <4 x i32> [[TMP46]], splat (i32 1)
-; DEFAULT-NEXT:    [[TMP50:%.*]] = zext <4 x i32> [[TMP47]] to <4 x i64>
-; DEFAULT-NEXT:    [[TMP51:%.*]] = zext <4 x i32> [[TMP48]] to <4 x i64>
-; DEFAULT-NEXT:    [[TMP52:%.*]] = zext <4 x i32> [[TMP49]] to <4 x i64>
-; DEFAULT-NEXT:    [[TMP53:%.*]] = extractelement <4 x i64> [[TMP50]], i64 0
-; DEFAULT-NEXT:    [[TMP54:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP53]]
-; DEFAULT-NEXT:    [[TMP55:%.*]] = extractelement <4 x i64> [[TMP50]], i64 1
-; DEFAULT-NEXT:    [[TMP56:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP55]]
-; DEFAULT-NEXT:    [[TMP57:%.*]] = extractelement <4 x i64> [[TMP50]], i64 2
-; DEFAULT-NEXT:    [[TMP58:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP57]]
-; DEFAULT-NEXT:    [[TMP59:%.*]] = extractelement <4 x i64> [[TMP50]], i64 3
-; DEFAULT-NEXT:    [[TMP60:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP59]]
-; DEFAULT-NEXT:    [[TMP61:%.*]] = extractelement <4 x i64> [[TMP51]], i64 0
-; DEFAULT-NEXT:    [[TMP62:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP61]]
-; DEFAULT-NEXT:    [[TMP63:%.*]] = extractelement <4 x i64> [[TMP51]], i64 1
-; DEFAULT-NEXT:    [[TMP64:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP63]]
-; DEFAULT-NEXT:    [[TMP65:%.*]] = extractelement <4 x i64> [[TMP51]], i64 2
-; DEFAULT-NEXT:    [[TMP66:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP65]]
-; DEFAULT-NEXT:    [[TMP120:%.*]] = extractelement <4 x i64> [[TMP51]], i64 3
-; DEFAULT-NEXT:    [[TMP121:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP120]]
-; DEFAULT-NEXT:    [[TMP122:%.*]] = extractelement <4 x i64> [[TMP52]], i64 0
-; DEFAULT-NEXT:    [[TMP123:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP122]]
-; DEFAULT-NEXT:    [[TMP124:%.*]] = extractelement <4 x i64> [[TMP52]], i64 1
-; DEFAULT-NEXT:    [[TMP125:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP124]]
-; DEFAULT-NEXT:    [[TMP126:%.*]] = extractelement <4 x i64> [[TMP52]], i64 2
-; DEFAULT-NEXT:    [[TMP127:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP126]]
-; DEFAULT-NEXT:    [[TMP128:%.*]] = extractelement <4 x i64> [[TMP52]], i64 3
+; DEFAULT-NEXT:    [[NOT:%.*]] = and i32 [[TMP7]], 1
+; DEFAULT-NEXT:    [[AND:%.*]] = xor i32 [[NOT]], 1
+; DEFAULT-NEXT:    [[TMP128:%.*]] = zext i32 [[AND]] to i64
 ; DEFAULT-NEXT:    [[TMP76:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP128]]
-; DEFAULT-NEXT:    [[TMP77:%.*]] = load i32, ptr [[TMP54]], align 4
-; DEFAULT-NEXT:    [[TMP78:%.*]] = load i32, ptr [[TMP56]], align 4
-; DEFAULT-NEXT:    [[TMP79:%.*]] = load i32, ptr [[TMP58]], align 4
-; DEFAULT-NEXT:    [[TMP80:%.*]] = load i32, ptr [[TMP60]], align 4
-; DEFAULT-NEXT:    [[TMP81:%.*]] = insertelement <4 x i32> poison, i32 [[TMP77]], i64 0
-; DEFAULT-NEXT:    [[TMP82:%.*]] = insertelement <4 x i32> [[TMP81]], i32 [[TMP78]], i64 1
-; DEFAULT-NEXT:    [[TMP83:%.*]] = insertelement <4 x i32> [[TMP82]], i32 [[TMP79]], i64 2
-; DEFAULT-NEXT:    [[TMP84:%.*]] = insertelement <4 x i32> [[TMP83]], i32 [[TMP80]], i64 3
-; DEFAULT-NEXT:    [[TMP85:%.*]] = load i32, ptr [[TMP62]], align 4
-; DEFAULT-NEXT:    [[TMP86:%.*]] = load i32, ptr [[TMP64]], align 4
-; DEFAULT-NEXT:    [[TMP87:%.*]] = load i32, ptr [[TMP66]], align 4
-; DEFAULT-NEXT:    [[TMP88:%.*]] = load i32, ptr [[TMP121]], align 4
-; DEFAULT-NEXT:    [[TMP89:%.*]] = insertelement <4 x i32> poison, i32 [[TMP85]], i64 0
-; DEFAULT-NEXT:    [[TMP90:%.*]] = insertelement <4 x i32> [[TMP89]], i32 [[TMP86]], i64 1
-; DEFAULT-NEXT:    [[TMP91:%.*]] = insertelement <4 x i32> [[TMP90]], i32 [[TMP87]], i64 2
-; DEFAULT-NEXT:    [[TMP92:%.*]] = insertelement <4 x i32> [[TMP91]], i32 [[TMP88]], i64 3
-; DEFAULT-NEXT:    [[TMP93:%.*]] = load i32, ptr [[TMP123]], align 4
-; DEFAULT-NEXT:    [[TMP94:%.*]] = load i32, ptr [[TMP125]], align 4
-; DEFAULT-NEXT:    [[TMP95:%.*]] = load i32, ptr [[TMP127]], align 4
-; DEFAULT-NEXT:    [[TMP96:%.*]] = load i32, ptr [[TMP76]], align 4
-; DEFAULT-NEXT:    [[TMP97:%.*]] = insertelement <4 x i32> poison, i32 [[TMP93]], i64 0
-; DEFAULT-NEXT:    [[TMP98:%.*]] = insertelement <4 x i32> [[TMP97]], i32 [[TMP94]], i64 1
-; DEFAULT-NEXT:    [[TMP99:%.*]] = insertelement <4 x i32> [[TMP98]], i32 [[TMP95]], i64 2
-; DEFAULT-NEXT:    [[TMP100:%.*]] = insertelement <4 x i32> [[TMP99]], i32 [[TMP96]], i64 3
-; DEFAULT-NEXT:    [[TMP101:%.*]] = load i32, ptr [[TMP123]], align 4
-; DEFAULT-NEXT:    [[TMP102:%.*]] = load i32, ptr [[TMP125]], align 4
-; DEFAULT-NEXT:    [[TMP103:%.*]] = load i32, ptr [[TMP127]], align 4
 ; DEFAULT-NEXT:    [[TMP104:%.*]] = load i32, ptr [[TMP76]], align 4
-; DEFAULT-NEXT:    [[TMP105:%.*]] = insertelement <4 x i32> poison, i32 [[TMP101]], i64 0
-; DEFAULT-NEXT:    [[TMP106:%.*]] = insertelement <4 x i32> [[TMP105]], i32 [[TMP102]], i64 1
-; DEFAULT-NEXT:    [[TMP107:%.*]] = insertelement <4 x i32> [[TMP106]], i32 [[TMP103]], i64 2
-; DEFAULT-NEXT:    [[TMP108:%.*]] = insertelement <4 x i32> [[TMP107]], i32 [[TMP104]], i64 3
-; DEFAULT-NEXT:    [[TMP109:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI]]
-; DEFAULT-NEXT:    [[TMP110:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI4]]
-; DEFAULT-NEXT:    [[TMP111:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI5]]
-; DEFAULT-NEXT:    [[TMP112:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI6]]
-; DEFAULT-NEXT:    [[TMP113]] = or <4 x i32> [[TMP109]], [[TMP84]]
-; DEFAULT-NEXT:    [[TMP114]] = or <4 x i32> [[TMP110]], [[TMP92]]
-; DEFAULT-NEXT:    [[TMP115]] = or <4 x i32> [[TMP111]], [[TMP100]]
-; DEFAULT-NEXT:    [[TMP116]] = or <4 x i32> [[TMP112]], [[TMP108]]
-; DEFAULT-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 16
-; DEFAULT-NEXT:    [[TMP117:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; DEFAULT-NEXT:    br i1 [[TMP117]], label %[[MIDDLE_BLOCK:.*]], label %[[LOOP]], !llvm.loop [[LOOP0:![0-9]+]]
+; DEFAULT-NEXT:    [[TMP0:%.*]] = add i64 [[Y]], 1
+; DEFAULT-NEXT:    [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
+; DEFAULT-NEXT:    [[TMP2:%.*]] = shl nuw i64 [[TMP1]], 4
+; DEFAULT-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], [[TMP2]]
+; DEFAULT-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH1:.*]]
+; DEFAULT:       [[VECTOR_PH1]]:
+; DEFAULT-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], [[TMP2]]
+; DEFAULT-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
+; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[X]], i64 0
+; DEFAULT-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; DEFAULT-NEXT:    [[TMP3:%.*]] = add i64 [[Y]], 1
+; DEFAULT-NEXT:    [[TMP4:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP3]]
+; DEFAULT-NEXT:    [[TMP5:%.*]] = load i32, ptr [[TMP4]], align 4
+; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT8:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP5]], i64 0
+; DEFAULT-NEXT:    [[BROADCAST_SPLAT9:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT8]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; DEFAULT-NEXT:    [[TMP6:%.*]] = lshr <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP55:%.*]] = shl <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP8:%.*]] = or <vscale x 4 x i32> [[TMP6]], [[TMP55]]
+; DEFAULT-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP104]], i64 0
+; DEFAULT-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; DEFAULT-NEXT:    [[TMP9:%.*]] = call i32 @llvm.vscale.i32()
+; DEFAULT-NEXT:    [[TMP10:%.*]] = mul nuw i32 [[TMP9]], 4
+; DEFAULT-NEXT:    [[TMP11:%.*]] = sub i32 [[TMP10]], 1
+; DEFAULT-NEXT:    [[VECTOR_RECUR_INIT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP11]]
+; DEFAULT-NEXT:    [[TMP12:%.*]] = call i32 @llvm.vscale.i32()
+; DEFAULT-NEXT:    [[TMP13:%.*]] = mul nuw i32 [[TMP12]], 4
+; DEFAULT-NEXT:    [[TMP14:%.*]] = sub i32 [[TMP13]], 1
+; DEFAULT-NEXT:    [[VECTOR_RECUR_INIT3:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP14]]
+; DEFAULT-NEXT:    br label %[[LOOP1:.*]]
+; DEFAULT:       [[LOOP1]]:
+; DEFAULT-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VECTOR_RECUR:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT]], %[[VECTOR_PH1]] ], [ [[BROADCAST_SPLAT9]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VECTOR_RECUR4:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT3]], %[[VECTOR_PH1]] ], [ [[TMP16:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP45:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VEC_PHI5:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP46:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VEC_PHI6:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP47:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[VEC_PHI7:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP48:%.*]], %[[LOOP1]] ]
+; DEFAULT-NEXT:    [[TMP15:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR]], <vscale x 4 x i32> [[BROADCAST_SPLAT9]], i32 1)
+; DEFAULT-NEXT:    [[TMP16]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[BROADCAST_SPLAT9]], <vscale x 4 x i32> [[BROADCAST_SPLAT9]], i32 1)
+; DEFAULT-NEXT:    [[TMP17:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR4]], <vscale x 4 x i32> [[TMP15]], i32 1)
+; DEFAULT-NEXT:    [[TMP18:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[TMP15]], <vscale x 4 x i32> [[TMP16]], i32 1)
+; DEFAULT-NEXT:    [[TMP19:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[TMP16]], <vscale x 4 x i32> [[TMP16]], i32 1)
+; DEFAULT-NEXT:    [[TMP20:%.*]] = or <vscale x 4 x i32> [[TMP17]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP21:%.*]] = or <vscale x 4 x i32> [[TMP18]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP22:%.*]] = or <vscale x 4 x i32> [[TMP19]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP23:%.*]] = shl <vscale x 4 x i32> [[TMP20]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP24:%.*]] = shl <vscale x 4 x i32> [[TMP21]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP25:%.*]] = shl <vscale x 4 x i32> [[TMP22]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP26:%.*]] = or <vscale x 4 x i32> [[TMP23]], splat (i32 2)
+; DEFAULT-NEXT:    [[TMP27:%.*]] = or <vscale x 4 x i32> [[TMP24]], splat (i32 2)
+; DEFAULT-NEXT:    [[TMP28:%.*]] = or <vscale x 4 x i32> [[TMP25]], splat (i32 2)
+; DEFAULT-NEXT:    [[TMP29:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP26]]
+; DEFAULT-NEXT:    [[TMP30:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP27]]
+; DEFAULT-NEXT:    [[TMP31:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP28]]
+; DEFAULT-NEXT:    [[TMP32:%.*]] = or <vscale x 4 x i32> [[TMP29]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP33:%.*]] = or <vscale x 4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP34:%.*]] = or <vscale x 4 x i32> [[TMP31]], [[BROADCAST_SPLAT]]
+; DEFAULT-NEXT:    [[TMP35:%.*]] = lshr <vscale x 4 x i32> [[TMP32]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP36:%.*]] = lshr <vscale x 4 x i32> [[TMP33]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP37:%.*]] = lshr <vscale x 4 x i32> [[TMP34]], splat (i32 1)
+; DEFAULT-NEXT:    [[TMP38:%.*]] = zext <vscale x 4 x i32> [[TMP35]] to <vscale x 4 x i64>
+; DEFAULT-NEXT:    [[TMP39:%.*]] = zext <vscale x 4 x i32> [[TMP36]] to <vscale x 4 x i64>
+; DEFAULT-NEXT:    [[TMP40:%.*]] = zext <vscale x 4 x i32> [[TMP37]] to <vscale x 4 x i64>
+; DEFAULT-NEXT:    [[WIDE_GEP:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP38]]
+; DEFAULT-NEXT:    [[WIDE_GEP10:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP39]]
+; DEFAULT-NEXT:    [[WIDE_GEP11:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP40]]
+; DEFAULT-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; DEFAULT-NEXT:    [[WIDE_MASKED_GATHER12:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP10]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; DEFAULT-NEXT:    [[WIDE_MASKED_GATHER13:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP11]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; DEFAULT-NEXT:    [[TMP41:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI]]
+; DEFAULT-NEXT:    [[TMP42:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI5]]
+; DEFAULT-NEXT:    [[TMP43:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI6]]
+; DEFAULT-NEXT:    [[TMP44:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI7]]
+; DEFAULT-NEXT:    [[TMP45]] = or <vscale x 4 x i32> [[TMP41]], [[WIDE_MASKED_GATHER]]
+; DEFAULT-NEXT:    [[TMP46]] = or <vscale x 4 x i32> [[TMP42]], [[WIDE_MASKED_GATHER12]]
+; DEFAULT-NEXT:    [[TMP47]] = or <vscale x 4 x i32> [[TMP43]], [[WIDE_MASKED_GATHER13]]
+; DEFAULT-NEXT:    [[TMP48]] = or <vscale x 4 x i32> [[TMP44]], [[WIDE_MASKED_GATHER13]]
+; DEFAULT-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP2]]
+; DEFAULT-NEXT:    [[TMP49:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; DEFAULT-NEXT:    br i1 [[TMP49]], label %[[MIDDLE_BLOCK:.*]], label %[[LOOP1]], !llvm.loop [[LOOP0:![0-9]+]]
 ; DEFAULT:       [[MIDDLE_BLOCK]]:
-; DEFAULT-NEXT:    [[BIN_RDX:%.*]] = or <4 x i32> [[TMP114]], [[TMP113]]
-; DEFAULT-NEXT:    [[BIN_RDX9:%.*]] = or <4 x i32> [[TMP115]], [[BIN_RDX]]
-; DEFAULT-NEXT:    [[BIN_RDX10:%.*]] = or <4 x i32> [[TMP116]], [[BIN_RDX9]]
-; DEFAULT-NEXT:    [[TMP118:%.*]] = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> [[BIN_RDX10]])
-; DEFAULT-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <4 x i32> [[TMP28]], i64 3
+; DEFAULT-NEXT:    [[BIN_RDX:%.*]] = or <vscale x 4 x i32> [[TMP46]], [[TMP45]]
+; DEFAULT-NEXT:    [[BIN_RDX14:%.*]] = or <vscale x 4 x i32> [[TMP47]], [[BIN_RDX]]
+; DEFAULT-NEXT:    [[BIN_RDX15:%.*]] = or <vscale x 4 x i32> [[TMP48]], [[BIN_RDX14]]
+; DEFAULT-NEXT:    [[TMP50:%.*]] = call i32 @llvm.vector.reduce.or.nxv4i32(<vscale x 4 x i32> [[BIN_RDX15]])
+; DEFAULT-NEXT:    [[TMP51:%.*]] = call i32 @llvm.vscale.i32()
+; DEFAULT-NEXT:    [[TMP52:%.*]] = mul nuw i32 [[TMP51]], 4
+; DEFAULT-NEXT:    [[TMP53:%.*]] = sub i32 [[TMP52]], 1
+; DEFAULT-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <vscale x 4 x i32> [[TMP16]], i32 [[TMP53]]
 ; DEFAULT-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
 ; DEFAULT-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
 ; DEFAULT:       [[SCALAR_PH]]:
-; DEFAULT-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; DEFAULT-NEXT:    [[SCALAR_RECUR_INIT11:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; DEFAULT-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; DEFAULT-NEXT:    [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP118]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; DEFAULT-NEXT:    br label %[[LOOP1:.*]]
-; DEFAULT:       [[LOOP1]]:
-; DEFAULT-NEXT:    [[TMP129:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[TMP68:%.*]], %[[LOOP1]] ]
-; DEFAULT-NEXT:    [[SCALAR_RECUR15:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT11]], %[[SCALAR_PH]] ], [ [[TMP129]], %[[LOOP1]] ]
-; DEFAULT-NEXT:    [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP1]] ]
-; DEFAULT-NEXT:    [[SUM_RED:%.*]] = phi i32 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_2:%.*]], %[[LOOP1]] ]
-; DEFAULT-NEXT:    [[TMP67:%.*]] = add i64 [[Y]], 1
-; DEFAULT-NEXT:    [[GEP_1:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP67]]
-; DEFAULT-NEXT:    [[TMP68]] = load i32, ptr [[GEP_1]], align 4
+; DEFAULT-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP5]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; DEFAULT-NEXT:    [[SCALAR_RECUR_INIT16:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; DEFAULT-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; DEFAULT-NEXT:    [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP50]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; DEFAULT-NEXT:    br label %[[LOOP:.*]]
+; DEFAULT:       [[LOOP]]:
+; DEFAULT-NEXT:    [[TMP54:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[TMP57:%.*]], %[[LOOP]] ]
+; DEFAULT-NEXT:    [[SCALAR_RECUR15:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT16]], %[[SCALAR_PH]] ], [ [[TMP54]], %[[LOOP]] ]
+; DEFAULT-NEXT:    [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
+; DEFAULT-NEXT:    [[SUM_RED:%.*]] = phi i32 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_2:%.*]], %[[LOOP]] ]
+; DEFAULT-NEXT:    [[TMP56:%.*]] = add i64 [[Y]], 1
+; DEFAULT-NEXT:    [[GEP_1:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP56]]
+; DEFAULT-NEXT:    [[TMP57]] = load i32, ptr [[GEP_1]], align 4
 ; DEFAULT-NEXT:    [[OR3:%.*]] = or i32 [[SCALAR_RECUR15]], [[X]]
 ; DEFAULT-NEXT:    [[IV_NEXT]] = add i64 [[IV]], 1
 ; DEFAULT-NEXT:    [[SHR:%.*]] = lshr i32 [[X]], 1
@@ -181,186 +130,129 @@ define i32 @chained_recurrences(i32 %x, i64 %y, ptr %src.1, i32 %z, ptr %src.2)
 ; DEFAULT-NEXT:    [[TMP71:%.*]] = or i32 [[SHR]], [[SHL19]]
 ; DEFAULT-NEXT:    [[TMP72:%.*]] = or i32 [[TMP71]], [[TMP70]]
 ; DEFAULT-NEXT:    [[TMP73:%.*]] = or i32 [[TMP72]], [[X]]
-; DEFAULT-NEXT:    [[OR20:%.*]] = or i32 [[Z]], [[X]]
-; DEFAULT-NEXT:    [[NOT:%.*]] = and i32 [[OR20]], 1
-; DEFAULT-NEXT:    [[AND:%.*]] = xor i32 [[NOT]], 1
-; DEFAULT-NEXT:    [[IDX_EXT_1:%.*]] = zext i32 [[AND]] to i64
-; DEFAULT-NEXT:    [[GEP_2:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[IDX_EXT_1]]
-; DEFAULT-NEXT:    [[TMP74:%.*]] = load i32, ptr [[GEP_2]], align 4
 ; DEFAULT-NEXT:    [[SHR24:%.*]] = lshr i32 [[TMP73]], 1
 ; DEFAULT-NEXT:    [[IDX_EXT_2:%.*]] = zext i32 [[SHR24]] to i64
 ; DEFAULT-NEXT:    [[GEP_3:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[IDX_EXT_2]]
 ; DEFAULT-NEXT:    [[TMP75:%.*]] = load i32, ptr [[GEP_3]], align 4
-; DEFAULT-NEXT:    [[RED_1:%.*]] = or i32 [[TMP74]], [[SUM_RED]]
+; DEFAULT-NEXT:    [[RED_1:%.*]] = or i32 [[TMP104]], [[SUM_RED]]
 ; DEFAULT-NEXT:    [[RED_2]] = or i32 [[RED_1]], [[TMP75]]
 ; DEFAULT-NEXT:    [[EC:%.*]] = icmp eq i64 [[IV]], [[Y]]
-; DEFAULT-NEXT:    br i1 [[EC]], label %[[EXIT]], label %[[LOOP1]], !llvm.loop [[LOOP3:![0-9]+]]
+; DEFAULT-NEXT:    br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP3:![0-9]+]]
 ; DEFAULT:       [[EXIT]]:
-; DEFAULT-NEXT:    [[RED_2_LCSSA:%.*]] = phi i32 [ [[RED_2]], %[[LOOP1]] ], [ [[TMP118]], %[[MIDDLE_BLOCK]] ]
+; DEFAULT-NEXT:    [[RED_2_LCSSA:%.*]] = phi i32 [ [[RED_2]], %[[LOOP]] ], [ [[TMP50]], %[[MIDDLE_BLOCK]] ]
 ; DEFAULT-NEXT:    ret i32 [[RED_2_LCSSA]]
 ;
 ; VSCALEFORTUNING2-LABEL: define i32 @chained_recurrences(
 ; VSCALEFORTUNING2-SAME: i32 [[X:%.*]], i64 [[Y:%.*]], ptr [[SRC_1:%.*]], i32 [[Z:%.*]], ptr [[SRC_2:%.*]]) #[[ATTR0:[0-9]+]] {
-; VSCALEFORTUNING2-NEXT:  [[ENTRY:.*]]:
-; VSCALEFORTUNING2-NEXT:    [[TMP0:%.*]] = add i64 [[Y]], 1
-; VSCALEFORTUNING2-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 16
-; VSCALEFORTUNING2-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
-; VSCALEFORTUNING2:       [[VECTOR_PH]]:
-; VSCALEFORTUNING2-NEXT:    [[N_MOD_VF:%.*]] = and i64 [[TMP0]], 15
-; VSCALEFORTUNING2-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[X]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
-; VSCALEFORTUNING2-NEXT:    [[TMP7:%.*]] = add i64 [[Y]], 1
-; VSCALEFORTUNING2-NEXT:    [[TMP8:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP7]]
-; VSCALEFORTUNING2-NEXT:    [[TMP3:%.*]] = load i32, ptr [[TMP8]], align 4
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT7:%.*]] = insertelement <4 x i32> poison, i32 [[TMP3]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT8:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT7]], <4 x i32> poison, <4 x i32> zeroinitializer
-; VSCALEFORTUNING2-NEXT:    [[TMP4:%.*]] = lshr <4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP5:%.*]] = shl <4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP6:%.*]] = or <4 x i32> [[TMP4]], [[TMP5]]
+; VSCALEFORTUNING2-NEXT:  [[VECTOR_PH:.*]]:
 ; VSCALEFORTUNING2-NEXT:    [[TMP16:%.*]] = or i32 [[Z]], [[X]]
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[TMP16]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
-; VSCALEFORTUNING2-NEXT:    [[TMP117:%.*]] = and <4 x i32> [[BROADCAST_SPLAT2]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP9:%.*]] = xor <4 x i32> [[TMP117]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP10:%.*]] = zext <4 x i32> [[TMP9]] to <4 x i64>
-; VSCALEFORTUNING2-NEXT:    [[TMP11:%.*]] = extractelement <4 x i64> [[TMP10]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP12:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP11]]
-; VSCALEFORTUNING2-NEXT:    [[TMP13:%.*]] = extractelement <4 x i64> [[TMP10]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP14:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP13]]
-; VSCALEFORTUNING2-NEXT:    [[TMP15:%.*]] = extractelement <4 x i64> [[TMP10]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP120:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP15]]
-; VSCALEFORTUNING2-NEXT:    [[TMP17:%.*]] = extractelement <4 x i64> [[TMP10]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP18:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP17]]
-; VSCALEFORTUNING2-NEXT:    [[TMP19:%.*]] = load i32, ptr [[TMP12]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP20:%.*]] = load i32, ptr [[TMP14]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP21:%.*]] = load i32, ptr [[TMP120]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP22:%.*]] = load i32, ptr [[TMP18]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP23:%.*]] = insertelement <4 x i32> poison, i32 [[TMP19]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP24:%.*]] = insertelement <4 x i32> [[TMP23]], i32 [[TMP20]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP25:%.*]] = insertelement <4 x i32> [[TMP24]], i32 [[TMP21]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP26:%.*]] = insertelement <4 x i32> [[TMP25]], i32 [[TMP22]], i64 3
-; VSCALEFORTUNING2-NEXT:    br label %[[VECTOR_BODY:.*]]
-; VSCALEFORTUNING2:       [[VECTOR_BODY]]:
-; VSCALEFORTUNING2-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, %[[VECTOR_PH]] ], [ [[BROADCAST_SPLAT8]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR3:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, %[[VECTOR_PH]] ], [ [[TMP28:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VEC_PHI:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP113:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VEC_PHI4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP114:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VEC_PHI5:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP115:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[VEC_PHI6:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP116:%.*]], %[[VECTOR_BODY]] ]
-; VSCALEFORTUNING2-NEXT:    [[TMP27:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; VSCALEFORTUNING2-NEXT:    [[TMP28]] = shufflevector <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> [[BROADCAST_SPLAT8]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; VSCALEFORTUNING2-NEXT:    [[TMP29:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR3]], <4 x i32> [[TMP27]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; VSCALEFORTUNING2-NEXT:    [[TMP30:%.*]] = shufflevector <4 x i32> [[TMP27]], <4 x i32> [[TMP28]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; VSCALEFORTUNING2-NEXT:    [[TMP31:%.*]] = shufflevector <4 x i32> [[TMP28]], <4 x i32> [[TMP28]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
-; VSCALEFORTUNING2-NEXT:    [[TMP32:%.*]] = or <4 x i32> [[TMP29]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP33:%.*]] = or <4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP34:%.*]] = or <4 x i32> [[TMP31]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP35:%.*]] = shl <4 x i32> [[TMP32]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP36:%.*]] = shl <4 x i32> [[TMP33]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP37:%.*]] = shl <4 x i32> [[TMP34]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP38:%.*]] = or <4 x i32> [[TMP35]], splat (i32 2)
-; VSCALEFORTUNING2-NEXT:    [[TMP39:%.*]] = or <4 x i32> [[TMP36]], splat (i32 2)
-; VSCALEFORTUNING2-NEXT:    [[TMP40:%.*]] = or <4 x i32> [[TMP37]], splat (i32 2)
-; VSCALEFORTUNING2-NEXT:    [[TMP41:%.*]] = or <4 x i32> [[TMP6]], [[TMP38]]
-; VSCALEFORTUNING2-NEXT:    [[TMP42:%.*]] = or <4 x i32> [[TMP6]], [[TMP39]]
-; VSCALEFORTUNING2-NEXT:    [[TMP43:%.*]] = or <4 x i32> [[TMP6]], [[TMP40]]
-; VSCALEFORTUNING2-NEXT:    [[TMP44:%.*]] = or <4 x i32> [[TMP41]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP45:%.*]] = or <4 x i32> [[TMP42]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP46:%.*]] = or <4 x i32> [[TMP43]], [[BROADCAST_SPLAT]]
-; VSCALEFORTUNING2-NEXT:    [[TMP47:%.*]] = lshr <4 x i32> [[TMP44]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP48:%.*]] = lshr <4 x i32> [[TMP45]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP121:%.*]] = lshr <4 x i32> [[TMP46]], splat (i32 1)
-; VSCALEFORTUNING2-NEXT:    [[TMP50:%.*]] = zext <4 x i32> [[TMP47]] to <4 x i64>
-; VSCALEFORTUNING2-NEXT:    [[TMP51:%.*]] = zext <4 x i32> [[TMP48]] to <4 x i64>
-; VSCALEFORTUNING2-NEXT:    [[TMP52:%.*]] = zext <4 x i32> [[TMP121]] to <4 x i64>
-; VSCALEFORTUNING2-NEXT:    [[TMP53:%.*]] = extractelement <4 x i64> [[TMP50]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP54:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP53]]
-; VSCALEFORTUNING2-NEXT:    [[TMP122:%.*]] = extractelement <4 x i64> [[TMP50]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP123:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP122]]
-; VSCALEFORTUNING2-NEXT:    [[TMP124:%.*]] = extractelement <4 x i64> [[TMP50]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP125:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP124]]
-; VSCALEFORTUNING2-NEXT:    [[TMP126:%.*]] = extractelement <4 x i64> [[TMP50]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP127:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP126]]
-; VSCALEFORTUNING2-NEXT:    [[TMP128:%.*]] = extractelement <4 x i64> [[TMP51]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP129:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP128]]
-; VSCALEFORTUNING2-NEXT:    [[TMP130:%.*]] = extractelement <4 x i64> [[TMP51]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP131:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP130]]
-; VSCALEFORTUNING2-NEXT:    [[TMP65:%.*]] = extractelement <4 x i64> [[TMP51]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP66:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP65]]
-; VSCALEFORTUNING2-NEXT:    [[TMP67:%.*]] = extractelement <4 x i64> [[TMP51]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP68:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP67]]
-; VSCALEFORTUNING2-NEXT:    [[TMP69:%.*]] = extractelement <4 x i64> [[TMP52]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP70:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP69]]
-; VSCALEFORTUNING2-NEXT:    [[TMP71:%.*]] = extractelement <4 x i64> [[TMP52]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP72:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP71]]
-; VSCALEFORTUNING2-NEXT:    [[TMP73:%.*]] = extractelement <4 x i64> [[TMP52]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP74:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP73]]
-; VSCALEFORTUNING2-NEXT:    [[TMP75:%.*]] = extractelement <4 x i64> [[TMP52]], i64 3
+; VSCALEFORTUNING2-NEXT:    [[NOT:%.*]] = and i32 [[TMP16]], 1
+; VSCALEFORTUNING2-NEXT:    [[AND:%.*]] = xor i32 [[NOT]], 1
+; VSCALEFORTUNING2-NEXT:    [[TMP75:%.*]] = zext i32 [[AND]] to i64
 ; VSCALEFORTUNING2-NEXT:    [[TMP76:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[TMP75]]
-; VSCALEFORTUNING2-NEXT:    [[TMP77:%.*]] = load i32, ptr [[TMP54]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP78:%.*]] = load i32, ptr [[TMP123]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP79:%.*]] = load i32, ptr [[TMP125]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP80:%.*]] = load i32, ptr [[TMP127]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP81:%.*]] = insertelement <4 x i32> poison, i32 [[TMP77]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP82:%.*]] = insertelement <4 x i32> [[TMP81]], i32 [[TMP78]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP83:%.*]] = insertelement <4 x i32> [[TMP82]], i32 [[TMP79]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP84:%.*]] = insertelement <4 x i32> [[TMP83]], i32 [[TMP80]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP85:%.*]] = load i32, ptr [[TMP129]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP86:%.*]] = load i32, ptr [[TMP131]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP87:%.*]] = load i32, ptr [[TMP66]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP88:%.*]] = load i32, ptr [[TMP68]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP89:%.*]] = insertelement <4 x i32> poison, i32 [[TMP85]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP90:%.*]] = insertelement <4 x i32> [[TMP89]], i32 [[TMP86]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP91:%.*]] = insertelement <4 x i32> [[TMP90]], i32 [[TMP87]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP92:%.*]] = insertelement <4 x i32> [[TMP91]], i32 [[TMP88]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP93:%.*]] = load i32, ptr [[TMP70]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP94:%.*]] = load i32, ptr [[TMP72]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP95:%.*]] = load i32, ptr [[TMP74]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP96:%.*]] = load i32, ptr [[TMP76]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP97:%.*]] = insertelement <4 x i32> poison, i32 [[TMP93]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP98:%.*]] = insertelement <4 x i32> [[TMP97]], i32 [[TMP94]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP99:%.*]] = insertelement <4 x i32> [[TMP98]], i32 [[TMP95]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP100:%.*]] = insertelement <4 x i32> [[TMP99]], i32 [[TMP96]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP101:%.*]] = load i32, ptr [[TMP70]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP102:%.*]] = load i32, ptr [[TMP72]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP103:%.*]] = load i32, ptr [[TMP74]], align 4
 ; VSCALEFORTUNING2-NEXT:    [[TMP104:%.*]] = load i32, ptr [[TMP76]], align 4
-; VSCALEFORTUNING2-NEXT:    [[TMP105:%.*]] = insertelement <4 x i32> poison, i32 [[TMP101]], i64 0
-; VSCALEFORTUNING2-NEXT:    [[TMP106:%.*]] = insertelement <4 x i32> [[TMP105]], i32 [[TMP102]], i64 1
-; VSCALEFORTUNING2-NEXT:    [[TMP107:%.*]] = insertelement <4 x i32> [[TMP106]], i32 [[TMP103]], i64 2
-; VSCALEFORTUNING2-NEXT:    [[TMP108:%.*]] = insertelement <4 x i32> [[TMP107]], i32 [[TMP104]], i64 3
-; VSCALEFORTUNING2-NEXT:    [[TMP109:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI]]
-; VSCALEFORTUNING2-NEXT:    [[TMP110:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI4]]
-; VSCALEFORTUNING2-NEXT:    [[TMP111:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI5]]
-; VSCALEFORTUNING2-NEXT:    [[TMP112:%.*]] = or <4 x i32> [[TMP26]], [[VEC_PHI6]]
-; VSCALEFORTUNING2-NEXT:    [[TMP113]] = or <4 x i32> [[TMP109]], [[TMP84]]
-; VSCALEFORTUNING2-NEXT:    [[TMP114]] = or <4 x i32> [[TMP110]], [[TMP92]]
-; VSCALEFORTUNING2-NEXT:    [[TMP115]] = or <4 x i32> [[TMP111]], [[TMP100]]
-; VSCALEFORTUNING2-NEXT:    [[TMP116]] = or <4 x i32> [[TMP112]], [[TMP108]]
-; VSCALEFORTUNING2-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 16
+; VSCALEFORTUNING2-NEXT:    [[TMP0:%.*]] = add i64 [[Y]], 1
+; VSCALEFORTUNING2-NEXT:    [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
+; VSCALEFORTUNING2-NEXT:    [[TMP2:%.*]] = shl nuw i64 [[TMP1]], 4
+; VSCALEFORTUNING2-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], [[TMP2]]
+; VSCALEFORTUNING2-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH1:.*]]
+; VSCALEFORTUNING2:       [[VECTOR_PH1]]:
+; VSCALEFORTUNING2-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], [[TMP2]]
+; VSCALEFORTUNING2-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[X]], i64 0
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; VSCALEFORTUNING2-NEXT:    [[TMP3:%.*]] = add i64 [[Y]], 1
+; VSCALEFORTUNING2-NEXT:    [[TMP4:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP3]]
+; VSCALEFORTUNING2-NEXT:    [[TMP5:%.*]] = load i32, ptr [[TMP4]], align 4
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT8:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP5]], i64 0
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT9:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT8]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; VSCALEFORTUNING2-NEXT:    [[TMP6:%.*]] = lshr <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP7:%.*]] = shl <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP8:%.*]] = or <vscale x 4 x i32> [[TMP6]], [[TMP7]]
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP104]], i64 0
+; VSCALEFORTUNING2-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; VSCALEFORTUNING2-NEXT:    [[TMP9:%.*]] = call i32 @llvm.vscale.i32()
+; VSCALEFORTUNING2-NEXT:    [[TMP10:%.*]] = mul nuw i32 [[TMP9]], 4
+; VSCALEFORTUNING2-NEXT:    [[TMP11:%.*]] = sub i32 [[TMP10]], 1
+; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR_INIT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP11]]
+; VSCALEFORTUNING2-NEXT:    [[TMP12:%.*]] = call i32 @llvm.vscale.i32()
+; VSCALEFORTUNING2-NEXT:    [[TMP13:%.*]] = mul nuw i32 [[TMP12]], 4
+; VSCALEFORTUNING2-NEXT:    [[TMP14:%.*]] = sub i32 [[TMP13]], 1
+; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR_INIT3:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP14]]
+; VSCALEFORTUNING2-NEXT:    br label %[[LOOP:.*]]
+; VSCALEFORTUNING2:       [[LOOP]]:
+; VSCALEFORTUNING2-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT]], %[[VECTOR_PH1]] ], [ [[BROADCAST_SPLAT9]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR4:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT3]], %[[VECTOR_PH1]] ], [ [[TMP63:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP45:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VEC_PHI5:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP46:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VEC_PHI6:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP47:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[VEC_PHI7:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH1]] ], [ [[TMP48:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[TMP15:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR]], <vscale x 4 x i32> [[BROADCAST_SPLAT9]], i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP63]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[BROADCAST_SPLAT9]], <vscale x 4 x i32> [[BROADCAST_SPLAT9]], i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP17:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR4]], <vscale x 4 x i32> [[TMP15]], i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP18:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[TMP15]], <vscale x 4 x i32> [[TMP63]], i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP19:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[TMP63]], <vscale x 4 x i32> [[TMP63]], i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP20:%.*]] = or <vscale x 4 x i32> [[TMP17]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP21:%.*]] = or <vscale x 4 x i32> [[TMP18]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP22:%.*]] = or <vscale x 4 x i32> [[TMP19]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP23:%.*]] = shl <vscale x 4 x i32> [[TMP20]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP24:%.*]] = shl <vscale x 4 x i32> [[TMP21]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP25:%.*]] = shl <vscale x 4 x i32> [[TMP22]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP26:%.*]] = or <vscale x 4 x i32> [[TMP23]], splat (i32 2)
+; VSCALEFORTUNING2-NEXT:    [[TMP27:%.*]] = or <vscale x 4 x i32> [[TMP24]], splat (i32 2)
+; VSCALEFORTUNING2-NEXT:    [[TMP28:%.*]] = or <vscale x 4 x i32> [[TMP25]], splat (i32 2)
+; VSCALEFORTUNING2-NEXT:    [[TMP29:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP26]]
+; VSCALEFORTUNING2-NEXT:    [[TMP30:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP27]]
+; VSCALEFORTUNING2-NEXT:    [[TMP31:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP28]]
+; VSCALEFORTUNING2-NEXT:    [[TMP32:%.*]] = or <vscale x 4 x i32> [[TMP29]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP33:%.*]] = or <vscale x 4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP34:%.*]] = or <vscale x 4 x i32> [[TMP31]], [[BROADCAST_SPLAT]]
+; VSCALEFORTUNING2-NEXT:    [[TMP35:%.*]] = lshr <vscale x 4 x i32> [[TMP32]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP36:%.*]] = lshr <vscale x 4 x i32> [[TMP33]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP37:%.*]] = lshr <vscale x 4 x i32> [[TMP34]], splat (i32 1)
+; VSCALEFORTUNING2-NEXT:    [[TMP38:%.*]] = zext <vscale x 4 x i32> [[TMP35]] to <vscale x 4 x i64>
+; VSCALEFORTUNING2-NEXT:    [[TMP39:%.*]] = zext <vscale x 4 x i32> [[TMP36]] to <vscale x 4 x i64>
+; VSCALEFORTUNING2-NEXT:    [[TMP40:%.*]] = zext <vscale x 4 x i32> [[TMP37]] to <vscale x 4 x i64>
+; VSCALEFORTUNING2-NEXT:    [[WIDE_GEP:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP38]]
+; VSCALEFORTUNING2-NEXT:    [[WIDE_GEP10:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP39]]
+; VSCALEFORTUNING2-NEXT:    [[WIDE_GEP11:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP40]]
+; VSCALEFORTUNING2-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; VSCALEFORTUNING2-NEXT:    [[WIDE_MASKED_GATHER12:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP10]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; VSCALEFORTUNING2-NEXT:    [[WIDE_MASKED_GATHER13:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP11]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; VSCALEFORTUNING2-NEXT:    [[TMP41:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI]]
+; VSCALEFORTUNING2-NEXT:    [[TMP42:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI5]]
+; VSCALEFORTUNING2-NEXT:    [[TMP43:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI6]]
+; VSCALEFORTUNING2-NEXT:    [[TMP44:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI7]]
+; VSCALEFORTUNING2-NEXT:    [[TMP45]] = or <vscale x 4 x i32> [[TMP41]], [[WIDE_MASKED_GATHER]]
+; VSCALEFORTUNING2-NEXT:    [[TMP46]] = or <vscale x 4 x i32> [[TMP42]], [[WIDE_MASKED_GATHER12]]
+; VSCALEFORTUNING2-NEXT:    [[TMP47]] = or <vscale x 4 x i32> [[TMP43]], [[WIDE_MASKED_GATHER13]]
+; VSCALEFORTUNING2-NEXT:    [[TMP48]] = or <vscale x 4 x i32> [[TMP44]], [[WIDE_MASKED_GATHER13]]
+; VSCALEFORTUNING2-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP2]]
 ; VSCALEFORTUNING2-NEXT:    [[TMP49:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; VSCALEFORTUNING2-NEXT:    br i1 [[TMP49]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; VSCALEFORTUNING2-NEXT:    br i1 [[TMP49]], label %[[MIDDLE_BLOCK:.*]], label %[[LOOP]], !llvm.loop [[LOOP0:![0-9]+]]
 ; VSCALEFORTUNING2:       [[MIDDLE_BLOCK]]:
-; VSCALEFORTUNING2-NEXT:    [[BIN_RDX:%.*]] = or <4 x i32> [[TMP114]], [[TMP113]]
-; VSCALEFORTUNING2-NEXT:    [[BIN_RDX9:%.*]] = or <4 x i32> [[TMP115]], [[BIN_RDX]]
-; VSCALEFORTUNING2-NEXT:    [[BIN_RDX10:%.*]] = or <4 x i32> [[TMP116]], [[BIN_RDX9]]
-; VSCALEFORTUNING2-NEXT:    [[TMP118:%.*]] = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> [[BIN_RDX10]])
-; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <4 x i32> [[TMP28]], i64 3
+; VSCALEFORTUNING2-NEXT:    [[BIN_RDX:%.*]] = or <vscale x 4 x i32> [[TMP46]], [[TMP45]]
+; VSCALEFORTUNING2-NEXT:    [[BIN_RDX14:%.*]] = or <vscale x 4 x i32> [[TMP47]], [[BIN_RDX]]
+; VSCALEFORTUNING2-NEXT:    [[BIN_RDX15:%.*]] = or <vscale x 4 x i32> [[TMP48]], [[BIN_RDX14]]
+; VSCALEFORTUNING2-NEXT:    [[TMP50:%.*]] = call i32 @llvm.vector.reduce.or.nxv4i32(<vscale x 4 x i32> [[BIN_RDX15]])
+; VSCALEFORTUNING2-NEXT:    [[TMP51:%.*]] = call i32 @llvm.vscale.i32()
+; VSCALEFORTUNING2-NEXT:    [[TMP52:%.*]] = mul nuw i32 [[TMP51]], 4
+; VSCALEFORTUNING2-NEXT:    [[TMP53:%.*]] = sub i32 [[TMP52]], 1
+; VSCALEFORTUNING2-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <vscale x 4 x i32> [[TMP63]], i32 [[TMP53]]
 ; VSCALEFORTUNING2-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
 ; VSCALEFORTUNING2-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
 ; VSCALEFORTUNING2:       [[SCALAR_PH]]:
-; VSCALEFORTUNING2-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; VSCALEFORTUNING2-NEXT:    [[SCALAR_RECUR_INIT11:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; VSCALEFORTUNING2-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; VSCALEFORTUNING2-NEXT:    [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP118]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
-; VSCALEFORTUNING2-NEXT:    br label %[[LOOP:.*]]
-; VSCALEFORTUNING2:       [[LOOP]]:
-; VSCALEFORTUNING2-NEXT:    [[TMP132:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[TMP57:%.*]], %[[LOOP]] ]
-; VSCALEFORTUNING2-NEXT:    [[TMP55:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT11]], %[[SCALAR_PH]] ], [ [[TMP132]], %[[LOOP]] ]
-; VSCALEFORTUNING2-NEXT:    [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
-; VSCALEFORTUNING2-NEXT:    [[SUM_RED:%.*]] = phi i32 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_2:%.*]], %[[LOOP]] ]
+; VSCALEFORTUNING2-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP5]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; VSCALEFORTUNING2-NEXT:    [[SCALAR_RECUR_INIT16:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; VSCALEFORTUNING2-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; VSCALEFORTUNING2-NEXT:    [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP50]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_PH]] ]
+; VSCALEFORTUNING2-NEXT:    br label %[[LOOP1:.*]]
+; VSCALEFORTUNING2:       [[LOOP1]]:
+; VSCALEFORTUNING2-NEXT:    [[TMP54:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[TMP57:%.*]], %[[LOOP1]] ]
+; VSCALEFORTUNING2-NEXT:    [[TMP55:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT16]], %[[SCALAR_PH]] ], [ [[TMP54]], %[[LOOP1]] ]
+; VSCALEFORTUNING2-NEXT:    [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP1]] ]
+; VSCALEFORTUNING2-NEXT:    [[SUM_RED:%.*]] = phi i32 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_2:%.*]], %[[LOOP1]] ]
 ; VSCALEFORTUNING2-NEXT:    [[TMP56:%.*]] = add i64 [[Y]], 1
 ; VSCALEFORTUNING2-NEXT:    [[GEP_1:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP56]]
 ; VSCALEFORTUNING2-NEXT:    [[TMP57]] = load i32, ptr [[GEP_1]], align 4
@@ -373,92 +265,92 @@ define i32 @chained_recurrences(i32 %x, i64 %y, ptr %src.1, i32 %z, ptr %src.2)
 ; VSCALEFORTUNING2-NEXT:    [[TMP60:%.*]] = or i32 [[SHR]], [[SHL19]]
 ; VSCALEFORTUNING2-NEXT:    [[TMP61:%.*]] = or i32 [[TMP60]], [[TMP59]]
 ; VSCALEFORTUNING2-NEXT:    [[TMP62:%.*]] = or i32 [[TMP61]], [[X]]
-; VSCALEFORTUNING2-NEXT:    [[OR20:%.*]] = or i32 [[Z]], [[X]]
-; VSCALEFORTUNING2-NEXT:    [[NOT:%.*]] = and i32 [[OR20]], 1
-; VSCALEFORTUNING2-NEXT:    [[AND:%.*]] = xor i32 [[NOT]], 1
-; VSCALEFORTUNING2-NEXT:    [[IDX_EXT_1:%.*]] = zext i32 [[AND]] to i64
-; VSCALEFORTUNING2-NEXT:    [[GEP_2:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[IDX_EXT_1]]
-; VSCALEFORTUNING2-NEXT:    [[TMP63:%.*]] = load i32, ptr [[GEP_2]], align 4
 ; VSCALEFORTUNING2-NEXT:    [[SHR24:%.*]] = lshr i32 [[TMP62]], 1
 ; VSCALEFORTUNING2-NEXT:    [[IDX_EXT_2:%.*]] = zext i32 [[SHR24]] to i64
 ; VSCALEFORTUNING2-NEXT:    [[GEP_3:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[IDX_EXT_2]]
 ; VSCALEFORTUNING2-NEXT:    [[TMP64:%.*]] = load i32, ptr [[GEP_3]], align 4
-; VSCALEFORTUNING2-NEXT:    [[RED_1:%.*]] = or i32 [[TMP63]], [[SUM_RED]]
+; VSCALEFORTUNING2-NEXT:    [[RED_1:%.*]] = or i32 [[TMP104]], [[SUM_RED]]
 ; VSCALEFORTUNING2-NEXT:    [[RED_2]] = or i32 [[RED_1]], [[TMP64]]
 ; VSCALEFORTUNING2-NEXT:    [[EC:%.*]] = icmp eq i64 [[IV]], [[Y]]
-; VSCALEFORTUNING2-NEXT:    br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP3:![0-9]+]]
+; VSCALEFORTUNING2-NEXT:    br i1 [[EC]], label %[[EXIT]], label %[[LOOP1]], !llvm.loop [[LOOP3:![0-9]+]]
 ; VSCALEFORTUNING2:       [[EXIT]]:
-; VSCALEFORTUNING2-NEXT:    [[RED_2_LCSSA:%.*]] = phi i32 [ [[RED_2]], %[[LOOP]] ], [ [[TMP118]], %[[MIDDLE_BLOCK]] ]
+; VSCALEFORTUNING2-NEXT:    [[RED_2_LCSSA:%.*]] = phi i32 [ [[RED_2]], %[[LOOP1]] ], [ [[TMP50]], %[[MIDDLE_BLOCK]] ]
 ; VSCALEFORTUNING2-NEXT:    ret i32 [[RED_2_LCSSA]]
 ;
 ; PRED-LABEL: define i32 @chained_recurrences(
 ; PRED-SAME: i32 [[X:%.*]], i64 [[Y:%.*]], ptr [[SRC_1:%.*]], i32 [[Z:%.*]], ptr [[SRC_2:%.*]]) #[[ATTR0:[0-9]+]] {
-; PRED-NEXT:  [[ENTRY:.*:]]
+; PRED-NEXT:  [[VECTOR_PH:.*:]]
+; PRED-NEXT:    [[TMP20:%.*]] = or i32 [[Z]], [[X]]
+; PRED-NEXT:    [[NOT:%.*]] = and i32 [[TMP20]], 1
+; PRED-NEXT:    [[AND:%.*]] = xor i32 [[NOT]], 1
+; PRED-NEXT:    [[IDX_EXT_1:%.*]] = zext i32 [[AND]] to i64
+; PRED-NEXT:    [[GEP_2:%.*]] = getelementptr i32, ptr [[SRC_2]], i64 [[IDX_EXT_1]]
+; PRED-NEXT:    [[INVARIANT_LOAD_2:%.*]] = load i32, ptr [[GEP_2]], align 4
 ; PRED-NEXT:    [[TMP0:%.*]] = add i64 [[Y]], 1
-; PRED-NEXT:    br label %[[VECTOR_PH:.*]]
-; PRED:       [[VECTOR_PH]]:
+; PRED-NEXT:    br label %[[VECTOR_BODY:.*]]
+; PRED:       [[VECTOR_BODY]]:
 ; PRED-NEXT:    [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
 ; PRED-NEXT:    [[TMP2:%.*]] = shl nuw i64 [[TMP1]], 2
-; PRED-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[X]], i64 0
-; PRED-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
-; PRED-NEXT:    [[TMP11:%.*]] = add i64 [[Y]], 1
-; PRED-NEXT:    [[TMP12:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP11]]
-; PRED-NEXT:    [[TMP5:%.*]] = load i32, ptr [[TMP12]], align 4
-; PRED-NEXT:    [[BROADCAST_SPLATINSERT5:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP5]], i64 0
+; PRED-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[X]], i64 0
+; PRED-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; PRED-NEXT:    [[TMP3:%.*]] = add i64 [[Y]], 1
+; PRED-NEXT:    [[GEP_3:%.*]] = getelementptr i32, ptr [[SRC_1]], i64 [[TMP3]]
+; PRED-NEXT:    [[TMP6:%.*]] = load i32, ptr [[GEP_3]], align 4
+; PRED-NEXT:    [[BROADCAST_SPLATINSERT5:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP6]], i64 0
 ; PRED-NEXT:    [[BROADCAST_SPLAT6:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT5]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
-; PRED-NEXT:    [[TMP13:%.*]] = lshr <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; PRED-NEXT:    [[TMP14:%.*]] = shl <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
-; PRED-NEXT:    [[TMP15:%.*]] = or <vscale x 4 x i32> [[TMP13]], [[TMP14]]
-; PRED-NEXT:    [[TMP20:%.*]] = or i32 [[Z]], [[X]]
-; PRED-NEXT:    [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP20]], i64 0
-; PRED-NEXT:    [[TMP16:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT2]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
-; PRED-NEXT:    [[TMP17:%.*]] = and <vscale x 4 x i32> [[TMP16]], splat (i32 1)
-; PRED-NEXT:    [[TMP18:%.*]] = xor <vscale x 4 x i32> [[TMP17]], splat (i32 1)
-; PRED-NEXT:    [[TMP19:%.*]] = zext <vscale x 4 x i32> [[TMP18]] to <vscale x 4 x i64>
-; PRED-NEXT:    [[DOTSPLAT:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP19]]
+; PRED-NEXT:    [[TMP29:%.*]] = lshr <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; PRED-NEXT:    [[TMP7:%.*]] = shl <vscale x 4 x i32> [[BROADCAST_SPLAT]], splat (i32 1)
+; PRED-NEXT:    [[TMP8:%.*]] = or <vscale x 4 x i32> [[TMP29]], [[TMP7]]
 ; PRED-NEXT:    [[ACTIVE_LANE_MASK_ENTRY:%.*]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 0, i64 [[TMP0]])
-; PRED-NEXT:    [[TMP22:%.*]] = call i32 @llvm.vscale.i32()
-; PRED-NEXT:    [[TMP23:%.*]] = mul nuw i32 [[TMP22]], 4
-; PRED-NEXT:    [[TMP24:%.*]] = sub i32 [[TMP23]], 1
-; PRED-NEXT:    [[VECTOR_RECUR_INIT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP24]]
-; PRED-NEXT:    [[TMP25:%.*]] = call i32 @llvm.vscale.i32()
-; PRED-NEXT:    [[TMP26:%.*]] = mul nuw i32 [[TMP25]], 4
-; PRED-NEXT:    [[TMP27:%.*]] = sub i32 [[TMP26]], 1
-; PRED-NEXT:    [[VECTOR_RECUR_INIT3:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP27]]
-; PRED-NEXT:    br label %[[VECTOR_BODY:.*]]
-; PRED:       [[VECTOR_BODY]]:
-; PRED-NEXT:    [[IV:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; PRED-NEXT:    [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], %[[VECTOR_PH]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; PRED-NEXT:    [[VECTOR_RECUR:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT]], %[[VECTOR_PH]] ], [ [[BROADCAST_SPLAT6]], %[[VECTOR_BODY]] ]
-; PRED-NEXT:    [[VECTOR_RECUR4:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT3]], %[[VECTOR_PH]] ], [ [[TMP29:%.*]], %[[VECTOR_BODY]] ]
-; PRED-NEXT:    [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP41:%.*]], %[[VECTOR_BODY]] ]
-; PRED-NEXT:    [[TMP29]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR]], <vscale x 4 x i32> [[BROADCAST_SPLAT6]], i32 1)
-; PRED-NEXT:    [[TMP30:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR4]], <vscale x 4 x i32> [[TMP29]], i32 1)
-; PRED-NEXT:    [[TMP31:%.*]] = or <vscale x 4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
-; PRED-NEXT:    [[TMP32:%.*]] = shl <vscale x 4 x i32> [[TMP31]], splat (i32 1)
-; PRED-NEXT:    [[TMP33:%.*]] = or <vscale x 4 x i32> [[TMP32]], splat (i32 2)
-; PRED-NEXT:    [[TMP34:%.*]] = or <vscale x 4 x i32> [[TMP15]], [[TMP33]]
-; PRED-NEXT:    [[TMP35:%.*]] = or <vscale x 4 x i32> [[TMP34]], [[BROADCAST_SPLAT]]
-; PRED-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[DOTSPLAT]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)
-; PRED-NEXT:    [[TMP36:%.*]] = lshr <vscale x 4 x i32> [[TMP35]], splat (i32 1)
-; PRED-NEXT:    [[TMP37:%.*]] = zext <vscale x 4 x i32> [[TMP36]] to <vscale x 4 x i64>
-; PRED-NEXT:    [[TMP38:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP37]]
-; PRED-NEXT:    [[WIDE_MASKED_GATHER7:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP38]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)
-; PRED-NEXT:    [[TMP39:%.*]] = or <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], [[VEC_PHI]]
-; PRED-NEXT:    [[TMP40:%.*]] = or <vscale x 4 x i32> [[TMP39]], [[WIDE_MASKED_GATHER7]]
-; PRED-NEXT:    [[TMP41]] = select <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> [[TMP40]], <vscale x 4 x i32> [[VEC_PHI]]
-; PRED-NEXT:    [[INDEX_NEXT]] = add i64 [[IV]], [[TMP2]]
-; PRED-NEXT:    [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX_NEXT]], i64 [[TMP0]])
-; PRED-NEXT:    [[TMP43:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i64 0
-; PRED-NEXT:    [[TMP42:%.*]] = xor i1 [[TMP43]], true
-; PRED-NEXT:    br i1 [[TMP42]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
-; PRED:       [[MIDDLE_BLOCK]]:
-; PRED-NEXT:    [[TMP44:%.*]] = call i32 @llvm.vector.reduce.or.nxv4i32(<vscale x 4 x i32> [[TMP41]])
+; PRED-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[INVARIANT_LOAD_2]], i64 0
+; PRED-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; PRED-NEXT:    [[TMP9:%.*]] = call i32 @llvm.vscale.i32()
+; PRED-NEXT:    [[TMP10:%.*]] = mul nuw i32 [[TMP9]], 4
+; PRED-NEXT:    [[TMP11:%.*]] = sub i32 [[TMP10]], 1
+; PRED-NEXT:    [[VECTOR_RECUR_INIT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP11]]
+; PRED-NEXT:    [[TMP12:%.*]] = call i32 @llvm.vscale.i32()
+; PRED-NEXT:    [[TMP13:%.*]] = mul nuw i32 [[TMP12]], 4
+; PRED-NEXT:    [[TMP14:%.*]] = sub i32 [[TMP13]], 1
+; PRED-NEXT:    [[VECTOR_RECUR_INIT3:%.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 [[TMP14]]
 ; PRED-NEXT:    br label %[[EXIT:.*]]
 ; PRED:       [[EXIT]]:
+; PRED-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_BODY]] ], [ [[INDEX_NEXT:%.*]], %[[EXIT]] ]
+; PRED-NEXT:    [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], %[[VECTOR_BODY]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], %[[EXIT]] ]
+; PRED-NEXT:    [[VECTOR_RECUR:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT]], %[[VECTOR_BODY]] ], [ [[BROADCAST_SPLAT6]], %[[EXIT]] ]
+; PRED-NEXT:    [[VECTOR_RECUR4:%.*]] = phi <vscale x 4 x i32> [ [[VECTOR_RECUR_INIT3]], %[[VECTOR_BODY]] ], [ [[TMP15:%.*]], %[[EXIT]] ]
+; PRED-NEXT:    [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP26:%.*]], %[[EXIT]] ]
+; PRED-NEXT:    [[TMP15]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR]], <vscale x 4 x i32> [[BROADCAST_SPLAT6]], i32 1)
+; PRED-NEXT:    [[TMP16:%.*]] = call <vscale x 4 x i32> @llvm.vector.splice.right.nxv4i32(<vscale x 4 x i32> [[VECTOR_RECUR4]], <vscale x 4 x i32> [[TMP15]], i32 1)
+; PRED-NEXT:    [[TMP17:%.*]] = or <vscale x 4 x i32> [[TMP16]], [[BROADCAST_SPLAT]]
+; PRED-NEXT:    [[TMP18:%.*]] = shl <vscale x 4 x i32> [[TMP17]], splat (i32 1)
+; PRED-NEXT:    [[TMP19:%.*]] = or <vscale x 4 x i32> [[TMP18]], splat (i32 2)
+; PRED-NEXT:    [[TMP30:%.*]] = or <vscale x 4 x i32> [[TMP8]], [[TMP19]]
+; PRED-NEXT:    [[TMP21:%.*]] = or <vscale x 4 x i32> [[TMP30]], [[BROADCAST_SPLAT]]
+; PRED-NEXT:    [[TMP22:%.*]] = lshr <vscale x 4 x i32> [[TMP21]], splat (i32 1)
+; PRED-NEXT:    [[TMP23:%.*]] = zext <vscale x 4 x i32> [[TMP22]] to <vscale x 4 x i64>
+; PRED-NEXT:    [[WIDE_GEP:%.*]] = getelementptr i32, ptr [[SRC_2]], <vscale x 4 x i64> [[TMP23]]
+; PRED-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[WIDE_GEP]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)
+; PRED-NEXT:    [[TMP24:%.*]] = or <vscale x 4 x i32> [[BROADCAST_SPLAT2]], [[VEC_PHI]]
+; PRED-NEXT:    [[TMP25:%.*]] = or <vscale x 4 x i32> [[TMP24]], [[WIDE_MASKED_GATHER]]
+; PRED-NEXT:    [[TMP26]] = select <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> [[TMP25]], <vscale x 4 x i32> [[VEC_PHI]]
+; PRED-NEXT:    [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP2]]
+; PRED-NEXT:    [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX_NEXT]], i64 [[TMP0]])
+; PRED-NEXT:    [[TMP27:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i64 0
+; PRED-NEXT:    [[TMP28:%.*]] = xor i1 [[TMP27]], true
+; PRED-NEXT:    br i1 [[TMP28]], label %[[MIDDLE_BLOCK:.*]], label %[[EXIT]], !llvm.loop [[LOOP0:![0-9]+]]
+; PRED:       [[MIDDLE_BLOCK]]:
+; PRED-NEXT:    [[TMP44:%.*]] = call i32 @llvm.vector.reduce.or.nxv4i32(<vscale x 4 x i32> [[TMP26]])
+; PRED-NEXT:    br label %[[EXIT1:.*]]
+; PRED:       [[EXIT1]]:
 ; PRED-NEXT:    ret i32 [[TMP44]]
 ;
 entry:
+  %or20 = or i32 %z, %x
+  %not = and i32 %or20, 1
+  %and = xor i32 %not, 1
+  %idx.ext.1 = zext i32 %and to i64
+  %gep.2 = getelementptr i32, ptr %src.2, i64 %idx.ext.1
+  %invariant.load.2 = load i32, ptr %gep.2, align 4
   br label %loop
 
 loop:
@@ -478,17 +370,11 @@ loop:
   %8 = or i32 %shr, %shl19
   %9 = or i32 %8, %7
   %10 = or i32 %9, %x
-  %or20 = or i32 %z, %x
-  %not = and i32 %or20, 1
-  %and = xor i32 %not, 1
-  %idx.ext.1 = zext i32 %and to i64
-  %gep.2 = getelementptr i32, ptr %src.2, i64 %idx.ext.1
-  %11 = load i32, ptr %gep.2, align 4
   %shr24 = lshr i32 %10, 1
   %idx.ext.2 = zext i32 %shr24 to i64
   %gep.3 = getelementptr i32, ptr %src.2, i64 %idx.ext.2
   %12 = load i32, ptr %gep.3, align 4
-  %red.1 = or i32 %11, %sum.red
+  %red.1 = or i32 %invariant.load.2, %sum.red
   %red.2 = or i32 %red.1, %12
   %ec = icmp eq i64 %iv, %y
   br i1 %ec, label %exit, label %loop

>From 57d2e2e188c4f366fac224a52cd156f00ddac700 Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Thu, 17 Sep 2026 13:34:42 +0000
Subject: [PATCH 3/4] Add test

---
 .../CostModel/AArch64/folded-shift.ll         | 141 ++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 llvm/test/Analysis/CostModel/AArch64/folded-shift.ll

diff --git a/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll b/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll
new file mode 100644
index 0000000000000..b4928b814ec68
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll
@@ -0,0 +1,141 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes='print<cost-model>' -disable-output -mtriple=aarch64 2>&1 < %s | FileCheck %s
+
+; A constant scalar shift with a single, same-block logical user folds into
+; that user's shifted-register operand.
+
+define i64 @folded(i64 %x, i64 %y) {
+; CHECK-LABEL: 'folded'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i64 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i64 %shl, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i64 %and, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i64 %lshr, %x
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %ashr = ashr i64 %or, 11
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %xor = xor i64 %ashr, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i64 %xor
+;
+  %shl = shl i64 %x, 7
+  %and = and i64 %shl, %y
+  %lshr = lshr i64 %and, 9
+  %or = or i64 %lshr, %x
+  %ashr = ashr i64 %or, 11
+  %xor = xor i64 %ashr, %y
+  ret i64 %xor
+}
+
+; Do not arbitrarily assign the single shifted-register operand when both
+; operands are shifts.
+define i64 @two_shifts(i64 %x, i64 %y) {
+; CHECK-LABEL: 'two_shifts'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lhs = shl i64 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %rhs = lshr i64 %y, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i64 %lhs, %rhs
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i64 %or
+;
+  %lhs = shl i64 %x, 7
+  %rhs = lshr i64 %y, 9
+  %or = or i64 %lhs, %rhs
+  ret i64 %or
+}
+
+define i32 @folded_i32(i32 %x, i32 %y) {
+; CHECK-LABEL: 'folded_i32'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shift = shl i32 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i32 %shift, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i32 %and
+;
+  %shift = shl i32 %x, 7
+  %and = and i32 %shift, %y
+  ret i32 %and
+}
+
+define i64 @not_folded(i64 %x, i64 %y, i64 %amount, i1 %cond) {
+; CHECK-LABEL: 'not_folded'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %multiuse = shl i64 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i64 %multiuse, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i64 %multiuse, %and
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %nonlogical = shl i64 %x, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %add = add i64 %or, %nonlogical
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %variable = lshr i64 %x, %amount
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %xor = xor i64 %add, %variable
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %crossblock = ashr i64 %x, 11
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %constant.user = shl i64 %x, 13
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %constant.logical = or i64 %constant.user, 255
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %with.constant = xor i64 %xor, %constant.logical
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: br i1 %cond, label %then, label %exit
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %fold.user = and i64 %crossblock, %with.constant
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: br label %exit
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %result = phi i64 [ %with.constant, %entry ], [ %fold.user, %then ]
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i64 %result
+;
+entry:
+  %multiuse = shl i64 %x, 7
+  %and = and i64 %multiuse, %y
+  %or = or i64 %multiuse, %and
+  %nonlogical = shl i64 %x, 9
+  %add = add i64 %or, %nonlogical
+  %variable = lshr i64 %x, %amount
+  %xor = xor i64 %add, %variable
+  %crossblock = ashr i64 %x, 11
+  %constant.user = shl i64 %x, 13
+  %constant.logical = or i64 %constant.user, 255
+  %with.constant = xor i64 %xor, %constant.logical
+  br i1 %cond, label %then, label %exit
+
+then:
+  %fold.user = and i64 %crossblock, %with.constant
+  br label %exit
+
+exit:
+  %result = phi i64 [ %with.constant, %entry ], [ %fold.user, %then ]
+  ret i64 %result
+}
+
+define i16 @folded_i16(i16 %x, i16 %y) {
+; CHECK-LABEL: 'folded_i16'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i16 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i16 %shl, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i16 %and, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i16 %lshr, %x
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %ashr = ashr i16 %or, 11
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %xor = xor i16 %ashr, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i16 %xor
+;
+  %shl = shl i16 %x, 7
+  %and = and i16 %shl, %y
+  %lshr = lshr i16 %and, 9
+  %or = or i16 %lshr, %x
+  %ashr = ashr i16 %or, 11
+  %xor = xor i16 %ashr, %y
+  ret i16 %xor
+}
+
+define i8 @folded_i8(i8 %x, i8 %y) {
+; CHECK-LABEL: 'folded_i8'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i8 %x, 3
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i8 %shl, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i8 %and, 5
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i8 %lshr, %x
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %ashr = ashr i8 %or, 1
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %xor = xor i8 %ashr, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i8 %xor
+;
+  %shl = shl i8 %x, 3
+  %and = and i8 %shl, %y
+  %lshr = lshr i8 %and, 5
+  %or = or i8 %lshr, %x
+  %ashr = ashr i8 %or, 1
+  %xor = xor i8 %ashr, %y
+  ret i8 %xor
+}
+
+define <2 x i64> @vector_not_folded(<2 x i64> %x, <2 x i64> %y) {
+; CHECK-LABEL: 'vector_not_folded'
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shift = shl <2 x i64> %x, splat (i64 7)
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and <2 x i64> %shift, %y
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret <2 x i64> %and
+;
+  %shift = shl <2 x i64> %x, splat (i64 7)
+  %and = and <2 x i64> %shift, %y
+  ret <2 x i64> %and
+}

>From afc02f231cdcab10363f1404994d0bac95637f8c Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Thu, 17 Sep 2026 13:34:59 +0000
Subject: [PATCH 4/4] [AArch64] Model shifts folded into scalar logical
 instructions

Account for single-use constant shifts that can be folded into the
shifted-register operand of a scalar logical instruction.

Assisted-by: Codex
---
 .../AArch64/AArch64TargetTransformInfo.cpp    | 43 ++++++++++++++++
 .../CostModel/AArch64/folded-shift.ll         | 14 +++---
 .../CodeGen/AArch64/logical_shifted_reg.ll    | 35 ++++++-------
 llvm/test/CodeGen/AArch64/tbz-tbnz.ll         | 36 ++++++--------
 ...ather-buildvector-with-minbitwidth-user.ll | 49 ++++++++++---------
 5 files changed, 110 insertions(+), 67 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index 1c815c439aca0..11a7270d9f405 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -3513,6 +3513,45 @@ static std::optional<Instruction *> instCombineSVEAnd(InstCombiner &IC,
   return IC.replaceInstUsesWith(II, NewCmphs);
 }
 
+static bool canFoldShiftIntoUser(unsigned Opcode, Type *Ty,
+                                 const Instruction *CtxI) {
+
+  auto IsFoldableShift = [](const Instruction &I) {
+    unsigned Width = I.getType()->getIntegerBitWidth();
+    if (!I.isShift() || !is_contained({8u, 16u, 32u, 64u}, Width))
+      return false;
+
+    // These still require mask/sign-extension.
+    if (I.getOpcode() != Instruction::Shl && is_contained({8u, 16u}, Width))
+      return false;
+
+    auto *ShiftAmount = dyn_cast<ConstantInt>(I.getOperand(1));
+    return ShiftAmount && ShiftAmount->getValue().ult(Width);
+  };
+
+  if (!Ty->isIntegerTy() || !CtxI || CtxI->getOpcode() != Opcode ||
+      CtxI->getType() != Ty || !CtxI->hasOneUse() || !IsFoldableShift(*CtxI))
+    return false;
+
+  auto *UserI = dyn_cast<BinaryOperator>(*CtxI->user_begin());
+  if (!UserI || UserI->getParent() != CtxI->getParent() ||
+      !is_contained({Instruction::And, Instruction::Or, Instruction::Xor},
+                    UserI->getOpcode()))
+    return false;
+
+  Value *Other = UserI->getOperand(UserI->getOperand(0) == CtxI ? 1 : 0);
+  if (isa<Constant>(Other))
+    return false;
+  // A logical instruction has only one shifted-register operand. If both
+  // operands are shifts, consistently assign the fold to operand 1.
+  if (auto *OtherShift = dyn_cast<Instruction>(Other);
+      OtherShift && OtherShift->getParent() == UserI->getParent() &&
+      IsFoldableShift(*OtherShift) && OtherShift->hasOneUse())
+    return UserI->getOperand(1) == CtxI;
+
+  return true;
+}
+
 std::optional<Instruction *>
 AArch64TTIImpl::instCombineIntrinsic(InstCombiner &IC,
                                      IntrinsicInst &II) const {
@@ -5268,6 +5307,10 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
   case ISD::SRL:
   case ISD::SRA:
   case ISD::SHL: {
+
+    if (canFoldShiftIntoUser(Opcode, Ty, CtxI))
+      return TTI::TCC_Free;
+
     // Immediate vector shifts require uniform shift amounts. Non-uniform
     // constants therefore use variable shifts and require materializing the
     // shift vector. Account for a shift and materialization per legalized
diff --git a/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll b/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll
index b4928b814ec68..17c960f0b7f0d 100644
--- a/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll
+++ b/llvm/test/Analysis/CostModel/AArch64/folded-shift.ll
@@ -6,11 +6,11 @@
 
 define i64 @folded(i64 %x, i64 %y) {
 ; CHECK-LABEL: 'folded'
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i64 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %shl = shl i64 %x, 7
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i64 %shl, %y
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i64 %and, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %lshr = lshr i64 %and, 9
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i64 %lshr, %x
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %ashr = ashr i64 %or, 11
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %ashr = ashr i64 %or, 11
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %xor = xor i64 %ashr, %y
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i64 %xor
 ;
@@ -28,7 +28,7 @@ define i64 @folded(i64 %x, i64 %y) {
 define i64 @two_shifts(i64 %x, i64 %y) {
 ; CHECK-LABEL: 'two_shifts'
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lhs = shl i64 %x, 7
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %rhs = lshr i64 %y, 9
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %rhs = lshr i64 %y, 9
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i64 %lhs, %rhs
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i64 %or
 ;
@@ -40,7 +40,7 @@ define i64 @two_shifts(i64 %x, i64 %y) {
 
 define i32 @folded_i32(i32 %x, i32 %y) {
 ; CHECK-LABEL: 'folded_i32'
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shift = shl i32 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %shift = shl i32 %x, 7
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i32 %shift, %y
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret i32 %and
 ;
@@ -93,7 +93,7 @@ exit:
 
 define i16 @folded_i16(i16 %x, i16 %y) {
 ; CHECK-LABEL: 'folded_i16'
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i16 %x, 7
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %shl = shl i16 %x, 7
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i16 %shl, %y
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i16 %and, 9
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i16 %lshr, %x
@@ -112,7 +112,7 @@ define i16 @folded_i16(i16 %x, i16 %y) {
 
 define i8 @folded_i8(i8 %x, i8 %y) {
 ; CHECK-LABEL: 'folded_i8'
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i8 %x, 3
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %shl = shl i8 %x, 3
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %and = and i8 %shl, %y
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %lshr = lshr i8 %and, 5
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %or = or i8 %lshr, %x
diff --git a/llvm/test/CodeGen/AArch64/logical_shifted_reg.ll b/llvm/test/CodeGen/AArch64/logical_shifted_reg.ll
index 3b43b4209b08f..dd0ec0937e6f4 100644
--- a/llvm/test/CodeGen/AArch64/logical_shifted_reg.ll
+++ b/llvm/test/CodeGen/AArch64/logical_shifted_reg.ll
@@ -252,17 +252,17 @@ define void @flag_setting() {
 ; CHECK-SD-NEXT:    ldr x10, [x10, :got_lo12:var2_64]
 ; CHECK-SD-NEXT:    ldr x9, [x8]
 ; CHECK-SD-NEXT:    ldr x10, [x10]
-; CHECK-SD-NEXT:    tst x9, x10
-; CHECK-SD-NEXT:    b.gt .LBB2_4
-; CHECK-SD-NEXT:  // %bb.1: // %test2
-; CHECK-SD-NEXT:    tst x9, x10, lsl #63
-; CHECK-SD-NEXT:    b.mi .LBB2_4
-; CHECK-SD-NEXT:  // %bb.2: // %test3
-; CHECK-SD-NEXT:    tst x9, x10, asr #12
-; CHECK-SD-NEXT:    b.gt .LBB2_4
-; CHECK-SD-NEXT:  // %bb.3: // %other_exit
+; CHECK-SD-NEXT:    and x11, x9, x10, lsl #63
+; CHECK-SD-NEXT:    and x12, x9, x10, asr #12
+; CHECK-SD-NEXT:    and x10, x9, x10
+; CHECK-SD-NEXT:    cmn x11, #1
+; CHECK-SD-NEXT:    ccmp x12, #1, #0, gt
+; CHECK-SD-NEXT:    ccmp x10, #1, #0, lt
+; CHECK-SD-NEXT:    b.lt .LBB2_2
+; CHECK-SD-NEXT:  // %bb.1: // %common.ret
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB2_2: // %other_exit
 ; CHECK-SD-NEXT:    str x9, [x8]
-; CHECK-SD-NEXT:  .LBB2_4: // %common.ret
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: flag_setting:
@@ -274,17 +274,18 @@ define void @flag_setting() {
 ; CHECK-GI-NEXT:    ldr x9, [x8]
 ; CHECK-GI-NEXT:    ldr x10, [x10]
 ; CHECK-GI-NEXT:    tst x9, x10
-; CHECK-GI-NEXT:    b.gt .LBB2_4
-; CHECK-GI-NEXT:  // %bb.1: // %test2
+; CHECK-GI-NEXT:    b.gt .LBB2_3
+; CHECK-GI-NEXT:  // %bb.1:
 ; CHECK-GI-NEXT:    tst x9, x10, lsl #63
-; CHECK-GI-NEXT:    b.mi .LBB2_4
-; CHECK-GI-NEXT:  // %bb.2: // %test3
+; CHECK-GI-NEXT:    b.mi .LBB2_3
+; CHECK-GI-NEXT:  // %bb.2:
 ; CHECK-GI-NEXT:    asr x10, x10, #12
 ; CHECK-GI-NEXT:    tst x10, x9
-; CHECK-GI-NEXT:    b.gt .LBB2_4
-; CHECK-GI-NEXT:  // %bb.3: // %other_exit
+; CHECK-GI-NEXT:    b.le .LBB2_4
+; CHECK-GI-NEXT:  .LBB2_3: // %common.ret
+; CHECK-GI-NEXT:    ret
+; CHECK-GI-NEXT:  .LBB2_4: // %other_exit
 ; CHECK-GI-NEXT:    str x9, [x8]
-; CHECK-GI-NEXT:  .LBB2_4: // %common.ret
 ; CHECK-GI-NEXT:    ret
   %val1 = load i64, ptr @var1_64
   %val2 = load i64, ptr @var2_64
diff --git a/llvm/test/CodeGen/AArch64/tbz-tbnz.ll b/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
index 55954db64f8d6..7c69c18e8748a 100644
--- a/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
+++ b/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
@@ -199,48 +199,44 @@ if.end:
 define void @test8(i64 %val1, i64 %val2, i64 %val3) {
 ; CHECK-SD-LABEL: test8:
 ; CHECK-SD:       // %bb.0:
-; CHECK-SD-NEXT:    and x8, x1, x2
+; CHECK-SD-NEXT:    and x8, x0, x1, lsl #63
 ; CHECK-SD-NEXT:    and x9, x0, x1
+; CHECK-SD-NEXT:    and x10, x0, x1, lsl #62
+; CHECK-SD-NEXT:    and x8, x9, x8
+; CHECK-SD-NEXT:    and x9, x1, x2
+; CHECK-SD-NEXT:    orr x9, x9, x10
 ; CHECK-SD-NEXT:    cmn x8, #1
-; CHECK-SD-NEXT:    ccmp x9, #0, #0, gt
-; CHECK-SD-NEXT:    b.pl .LBB7_2
-; CHECK-SD-NEXT:  // %bb.1: // %if.then2
-; CHECK-SD-NEXT:    tst x0, x1, lsl #63
-; CHECK-SD-NEXT:    b.mi .LBB7_3
-; CHECK-SD-NEXT:  .LBB7_2: // %if.end
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB7_3: // %if.then3
-; CHECK-SD-NEXT:    tst x0, x1, lsl #62
+; CHECK-SD-NEXT:    ccmp x9, #0, #8, le
 ; CHECK-SD-NEXT:    b.mi .LBB7_2
-; CHECK-SD-NEXT:  // %bb.4: // %if.then4
+; CHECK-SD-NEXT:  // %bb.1: // %if.then4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl t
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-SD-NEXT:  .LBB7_2: // %if.end
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test8:
 ; CHECK-GI:       // %bb.0:
 ; CHECK-GI-NEXT:    tst x0, x1
-; CHECK-GI-NEXT:    b.pl .LBB7_3
+; CHECK-GI-NEXT:    b.pl .LBB7_5
 ; CHECK-GI-NEXT:  // %bb.1:
 ; CHECK-GI-NEXT:    tst x1, x2
-; CHECK-GI-NEXT:    b.mi .LBB7_3
-; CHECK-GI-NEXT:  // %bb.2: // %if.then2
+; CHECK-GI-NEXT:    b.mi .LBB7_5
+; CHECK-GI-NEXT:  // %bb.2:
 ; CHECK-GI-NEXT:    tst x0, x1, lsl #63
-; CHECK-GI-NEXT:    b.mi .LBB7_4
-; CHECK-GI-NEXT:  .LBB7_3: // %if.end
-; CHECK-GI-NEXT:    ret
-; CHECK-GI-NEXT:  .LBB7_4: // %if.then3
+; CHECK-GI-NEXT:    b.pl .LBB7_5
+; CHECK-GI-NEXT:  // %bb.3:
 ; CHECK-GI-NEXT:    tst x0, x1, lsl #62
-; CHECK-GI-NEXT:    b.mi .LBB7_3
-; CHECK-GI-NEXT:  // %bb.5: // %if.then4
+; CHECK-GI-NEXT:    b.mi .LBB7_5
+; CHECK-GI-NEXT:  // %bb.4: // %if.then4
 ; CHECK-GI-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-GI-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-GI-NEXT:    .cfi_offset w30, -16
 ; CHECK-GI-NEXT:    bl t
 ; CHECK-GI-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-GI-NEXT:  .LBB7_5: // %if.end
 ; CHECK-GI-NEXT:    ret
   %and1 = and i64 %val1, %val2
   %tst1 = icmp slt i64 %and1, 0
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/gather-buildvector-with-minbitwidth-user.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/gather-buildvector-with-minbitwidth-user.ll
index ae6aede2273d9..1ec3e235f7a3e 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/gather-buildvector-with-minbitwidth-user.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/gather-buildvector-with-minbitwidth-user.ll
@@ -12,34 +12,37 @@ define void @h(i16 %a, i16 %b, i16 %c, i16 %d, i16 %e, i16 %f, i16 %g, i16 %h, i
 ; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[I]], i64 2
 ; CHECK-NEXT:    [[TMP3:%.*]] = insertelement <4 x i16> [[TMP2]], i16 [[M]], i64 3
 ; CHECK-NEXT:    [[TMP4:%.*]] = zext <4 x i16> [[TMP3]] to <4 x i32>
-; CHECK-NEXT:    [[TMP5:%.*]] = insertelement <8 x i16> poison, i16 [[D]], i64 0
-; CHECK-NEXT:    [[TMP6:%.*]] = insertelement <8 x i16> [[TMP5]], i16 [[G]], i64 1
-; CHECK-NEXT:    [[TMP7:%.*]] = insertelement <8 x i16> [[TMP6]], i16 [[K]], i64 2
-; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <8 x i16> [[TMP7]], i16 [[O]], i64 3
-; CHECK-NEXT:    [[TMP9:%.*]] = insertelement <8 x i16> [[TMP8]], i16 [[C]], i64 4
-; CHECK-NEXT:    [[TMP10:%.*]] = insertelement <8 x i16> [[TMP9]], i16 [[F]], i64 5
-; CHECK-NEXT:    [[TMP11:%.*]] = insertelement <8 x i16> [[TMP10]], i16 [[J]], i64 6
-; CHECK-NEXT:    [[TMP12:%.*]] = insertelement <8 x i16> [[TMP11]], i16 [[N]], i64 7
-; CHECK-NEXT:    [[TMP13:%.*]] = insertelement <8 x i32> poison, i32 [[ARG]], i64 0
-; CHECK-NEXT:    [[TMP14:%.*]] = shufflevector <8 x i32> [[TMP13]], <8 x i32> poison, <8 x i32> zeroinitializer
-; CHECK-NEXT:    [[TMP15:%.*]] = ashr <8 x i32> [[TMP14]], splat (i32 5)
-; CHECK-NEXT:    [[TMP16:%.*]] = insertelement <2 x i16> poison, i16 [[H]], i64 0
-; CHECK-NEXT:    [[TMP17:%.*]] = insertelement <2 x i16> [[TMP16]], i16 [[L]], i64 1
-; CHECK-NEXT:    [[TMP18:%.*]] = zext <2 x i16> [[TMP17]] to <2 x i32>
-; CHECK-NEXT:    [[TMP19:%.*]] = trunc <8 x i32> [[TMP15]] to <8 x i16>
-; CHECK-NEXT:    [[TMP20:%.*]] = or <8 x i16> [[TMP19]], [[TMP12]]
+; CHECK-NEXT:    [[TMP5:%.*]] = insertelement <4 x i16> poison, i16 [[C]], i64 0
+; CHECK-NEXT:    [[TMP6:%.*]] = insertelement <4 x i16> [[TMP5]], i16 [[F]], i64 1
+; CHECK-NEXT:    [[TMP7:%.*]] = insertelement <4 x i16> [[TMP6]], i16 [[J]], i64 2
+; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <4 x i16> [[TMP7]], i16 [[N]], i64 3
+; CHECK-NEXT:    [[TMP9:%.*]] = sext <4 x i16> [[TMP8]] to <4 x i32>
+; CHECK-NEXT:    [[TMP10:%.*]] = insertelement <4 x i32> poison, i32 [[ARG]], i64 0
+; CHECK-NEXT:    [[TMP11:%.*]] = shufflevector <4 x i32> [[TMP10]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[TMP12:%.*]] = ashr <4 x i32> [[TMP11]], splat (i32 5)
+; CHECK-NEXT:    [[ARRAYIDX18:%.*]] = getelementptr i8, ptr null, i64 24
+; CHECK-NEXT:    [[TMP13:%.*]] = insertelement <4 x i16> poison, i16 [[D]], i64 0
+; CHECK-NEXT:    [[TMP14:%.*]] = insertelement <4 x i16> [[TMP13]], i16 [[G]], i64 1
+; CHECK-NEXT:    [[TMP15:%.*]] = insertelement <4 x i16> [[TMP14]], i16 [[K]], i64 2
+; CHECK-NEXT:    [[TMP16:%.*]] = insertelement <4 x i16> [[TMP15]], i16 [[O]], i64 3
+; CHECK-NEXT:    [[TMP17:%.*]] = sext <4 x i16> [[TMP16]] to <4 x i32>
+; CHECK-NEXT:    [[TMP19:%.*]] = ashr <4 x i32> [[TMP11]], splat (i32 5)
+; CHECK-NEXT:    [[TMP24:%.*]] = insertelement <2 x i16> poison, i16 [[H]], i64 0
+; CHECK-NEXT:    [[TMP20:%.*]] = insertelement <2 x i16> [[TMP24]], i16 [[L]], i64 1
+; CHECK-NEXT:    [[TMP18:%.*]] = zext <2 x i16> [[TMP20]] to <2 x i32>
 ; CHECK-NEXT:    [[TMP21:%.*]] = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 [[CONV9]], i64 0
 ; CHECK-NEXT:    [[TMP22:%.*]] = or <4 x i32> [[TMP21]], [[TMP4]]
-; CHECK-NEXT:    [[TMP23:%.*]] = trunc <4 x i32> [[TMP22]] to <4 x i16>
-; CHECK-NEXT:    [[TMP24:%.*]] = shufflevector <4 x i16> [[TMP23]], <4 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 poison, i32 poison, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP28:%.*]] = or <4 x i32> [[TMP12]], [[TMP17]]
+; CHECK-NEXT:    [[TMP29:%.*]] = or <4 x i32> [[TMP22]], [[TMP28]]
+; CHECK-NEXT:    [[TMP23:%.*]] = trunc <4 x i32> [[TMP29]] to <4 x i16>
+; CHECK-NEXT:    store <4 x i16> [[TMP23]], ptr [[ARRAYIDX2]], align 2
 ; CHECK-NEXT:    [[TMP25:%.*]] = shufflevector <2 x i32> [[TMP18]], <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
 ; CHECK-NEXT:    [[TMP26:%.*]] = shufflevector <4 x i32> [[TMP4]], <4 x i32> [[TMP25]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
 ; CHECK-NEXT:    [[TMP27:%.*]] = or <4 x i32> [[TMP26]], [[TMP21]]
-; CHECK-NEXT:    [[TMP28:%.*]] = trunc <4 x i32> [[TMP27]] to <4 x i16>
-; CHECK-NEXT:    [[TMP29:%.*]] = shufflevector <4 x i16> [[TMP28]], <4 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 poison, i32 poison, i32 poison, i32 poison>
-; CHECK-NEXT:    [[TMP30:%.*]] = shufflevector <8 x i16> [[TMP24]], <8 x i16> [[TMP29]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
-; CHECK-NEXT:    [[TMP31:%.*]] = or <8 x i16> [[TMP30]], [[TMP20]]
-; CHECK-NEXT:    store <8 x i16> [[TMP31]], ptr [[ARRAYIDX2]], align 2
+; CHECK-NEXT:    [[TMP30:%.*]] = or <4 x i32> [[TMP19]], [[TMP9]]
+; CHECK-NEXT:    [[TMP31:%.*]] = or <4 x i32> [[TMP27]], [[TMP30]]
+; CHECK-NEXT:    [[TMP32:%.*]] = trunc <4 x i32> [[TMP31]] to <4 x i16>
+; CHECK-NEXT:    store <4 x i16> [[TMP32]], ptr [[ARRAYIDX18]], align 2
 ; CHECK-NEXT:    ret void
 ;
 entry:



More information about the llvm-commits mailing list