[llvm] [SCEV][LoopStrengthReduce] Avoid querying SCEV for non-SCEVable operands (PR #214902)
Jinpeng Wang via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 11 05:14:10 PDT 2026
https://github.com/jjppp updated https://github.com/llvm/llvm-project/pull/214902
>From acf7ac885f8e2185632341aad813f822b4274897 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Sat, 8 Aug 2026 08:43:35 +0800
Subject: [PATCH 1/2] [SCEV] Avoid querying SCEV for non-SCEVable operands
---
llvm/lib/Analysis/ScalarEvolution.cpp | 4 ++++
.../LoopStrengthReduce/scev-vector-shl-crash.ll | 17 +++++++++++++++++
2 files changed, 21 insertions(+)
create mode 100644 llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 27a1a20bcdf79..03e18b4606efd 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2433,6 +2433,10 @@ ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp(
bool Deduced = false;
+ if (!isSCEVable(OBO->getOperand(0)->getType()) ||
+ !isSCEVable(OBO->getOperand(1)->getType()))
+ return std::nullopt;
+
Instruction::BinaryOps Opcode = (Instruction::BinaryOps)OBO->getOpcode();
const SCEV *LHS = getSCEV(OBO->getOperand(0));
const SCEV *RHS = getSCEV(OBO->getOperand(1));
diff --git a/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll b/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
new file mode 100644
index 0000000000000..6726231c24633
--- /dev/null
+++ b/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
@@ -0,0 +1,17 @@
+; RUN: llc -mtriple=x86_64 < %s -o /dev/null
+define void @snork(i64 %arg) {
+bbl:
+ %shl = shl nsw <2 x i64> zeroinitializer, zeroinitializer
+ %extractelement = extractelement <2 x i64> %shl, i64 0
+ %sub = sub i64 %arg, %extractelement
+ %and = and i64 %arg, 1
+ %sub1 = sub i64 %sub, %and
+ %add = add i64 %extractelement, %sub1
+ br label %bbl2
+
+bbl2:
+ %phi = phi i64 [ %add3, %bbl2 ], [ %add, %bbl ]
+ %add3 = add i64 %phi, 1
+ %icmp = icmp eq i64 %add3, 0
+ br label %bbl2
+}
>From c55a57da92b9f18aefe09cbeb7024b99b2763e85 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Tue, 11 Aug 2026 20:13:46 +0800
Subject: [PATCH 2/2] update positive checks for test
---
.../LoopStrengthReduce/scev-vector-shl-crash.ll | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll b/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
index 6726231c24633..6b5e0756d50d3 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/scev-vector-shl-crash.ll
@@ -1,5 +1,19 @@
-; RUN: llc -mtriple=x86_64 < %s -o /dev/null
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=x86_64 < %s | FileCheck %s
+
+; Ensure LSR does not crash by querying ScalarEvolution for the non-SCEVable
+; vector shl while strengthening its nowrap flags.
+
define void @snork(i64 %arg) {
+; CHECK-LABEL: snork:
+; CHECK: # %bb.0: # %bbl
+; CHECK-NEXT: notq %rdi
+; CHECK-NEXT: orq $1, %rdi
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB0_1: # %bbl2
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: decq %rdi
+; CHECK-NEXT: jmp .LBB0_1
bbl:
%shl = shl nsw <2 x i64> zeroinitializer, zeroinitializer
%extractelement = extractelement <2 x i64> %shl, i64 0
More information about the llvm-commits
mailing list