[llvm] 4a6adf4 - [InstSimplify] Fix Compilation Hang in simplifyExtractValueInst (#190279)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 7 08:48:50 PDT 2026
Author: Sharjeel Khan
Date: 2026-04-07T08:48:45-07:00
New Revision: 4a6adf42d80a91f79c9c7c8d0c7801f9575d40dd
URL: https://github.com/llvm/llvm-project/commit/4a6adf42d80a91f79c9c7c8d0c7801f9575d40dd
DIFF: https://github.com/llvm/llvm-project/commit/4a6adf42d80a91f79c9c7c8d0c7801f9575d40dd.diff
LOG: [InstSimplify] Fix Compilation Hang in simplifyExtractValueInst (#190279)
Jump Threading can create self-referential insertvalues which are
allowed by the verifier in unreachable code. These self-referential
insertvalues cause the compilation to hang in simplifyExtractValueInst.
This PR adds a check to break out of the loop if it detects it is a
self-referential insertvalue and adds the reproducer's bitcode as a
test.
Fixes: https://github.com/llvm/llvm-project/issues/187381
Added:
llvm/test/Transforms/CorrelatedValuePropagation/pr187381.ll
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 12c3edc0ced30..4b7e23d3025aa 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5455,8 +5455,13 @@ static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
// extractvalue x, (insertvalue y, elt, n), n -> elt
unsigned NumIdxs = Idxs.size();
+ SmallPtrSet<InsertValueInst *, 8> VisitedSet;
for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
+ // Protect against insertvalue cycles in unreachable code.
+ if (!VisitedSet.insert(IVI).second)
+ break;
+
ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
unsigned NumInsertValueIdxs = InsertValueIdxs.size();
unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/pr187381.ll b/llvm/test/Transforms/CorrelatedValuePropagation/pr187381.ll
new file mode 100644
index 0000000000000..80ddf0a6aedd8
--- /dev/null
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/pr187381.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
+
+define i32 @insertvalue_selfreferential() {
+; CHECK-LABEL: define i32 @insertvalue_selfreferential() {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[UNREACHABLE_BLOCK:.*]]:
+; CHECK-NEXT: [[TMP0:%.*]] = insertvalue [2 x i32] [[TMP0]], i32 0, 1
+; CHECK-NEXT: [[EXTRACT_VALUE:%.*]] = extractvalue [2 x i32] [[TMP0]], 0
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[SINK_VALUE:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[EXTRACT_VALUE]], %[[UNREACHABLE_BLOCK]] ]
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ br label %exit
+
+unreachable.block: ; No predecessors!
+ %0 = insertvalue [2 x i32] %0, i32 0, 1
+ %extract.value = extractvalue [2 x i32] %0, 0
+ br label %exit
+
+exit: ; preds = %unreachable.block, %entry
+ %sink.value = phi i32 [ 0, %entry ], [ %extract.value, %unreachable.block ]
+ ret i32 0
+}
+
+define i32 @insertvalue_longchain() {
+; CHECK-LABEL: define i32 @insertvalue_longchain() {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[UNREACHABLE_BLOCK:.*]]:
+; CHECK-NEXT: [[TMP0:%.*]] = insertvalue [2 x i32] [[TMP3:%.*]], i32 0, 1
+; CHECK-NEXT: [[TMP1:%.*]] = insertvalue [2 x i32] [[TMP0]], i32 0, 1
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue [2 x i32] [[TMP1]], i32 0, 1
+; CHECK-NEXT: [[TMP3]] = insertvalue [2 x i32] [[TMP2]], i32 0, 1
+; CHECK-NEXT: [[EXTRACT_VALUE:%.*]] = extractvalue [2 x i32] [[TMP3]], 0
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[SINK_VALUE:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[EXTRACT_VALUE]], %[[UNREACHABLE_BLOCK]] ]
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ br label %exit
+
+unreachable.block: ; No predecessors!
+ %0 = insertvalue [2 x i32] %3, i32 0, 1
+ %1 = insertvalue [2 x i32] %0, i32 0, 1
+ %2 = insertvalue [2 x i32] %1, i32 0, 1
+ %3 = insertvalue [2 x i32] %2, i32 0, 1
+ %extract.value = extractvalue [2 x i32] %3, 0
+ br label %exit
+
+exit: ; preds = %unreachable.block, %entry
+ %sink.value = phi i32 [ 0, %entry ], [ %extract.value, %unreachable.block ]
+ ret i32 0
+}
More information about the llvm-commits
mailing list