[llvm] [InstCombine] Invalidate changes to `DoesConsume` if the first try fails (PR #82973)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 4 09:31:07 PST 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/82973
>From 60c91d1ab87315f2b366b9ff1fc7761cb592b44f Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 5 Mar 2024 01:30:24 +0800
Subject: [PATCH] [InstCombine] Fix infinite loop due to incorrect
`DoesConsume`
---
.../InstCombine/InstructionCombining.cpp | 11 ++++--
llvm/test/Transforms/InstCombine/pr82877.ll | 34 +++++++++++++++++++
2 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/Transforms/InstCombine/pr82877.ll
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 87c8dca7efed89..80ce0c9275b2cb 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2341,11 +2341,13 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
!shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(V));
// Selects/min/max with invertible operands are freely invertible
if (IsSelect || match(V, m_MaxOrMin(m_Value(A), m_Value(B)))) {
+ bool LocalDoesConsume = DoesConsume;
if (!getFreelyInvertedImpl(B, B->hasOneUse(), /*Builder*/ nullptr,
- DoesConsume, Depth))
+ LocalDoesConsume, Depth))
return nullptr;
if (Value *NotA = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
- DoesConsume, Depth)) {
+ LocalDoesConsume, Depth)) {
+ DoesConsume = LocalDoesConsume;
if (Builder != nullptr) {
Value *NotB = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
DoesConsume, Depth);
@@ -2361,12 +2363,13 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
}
if (PHINode *PN = dyn_cast<PHINode>(V)) {
+ bool LocalDoesConsume = DoesConsume;
SmallVector<std::pair<Value *, BasicBlock *>, 8> IncomingValues;
for (Use &U : PN->operands()) {
BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
Value *NewIncomingVal = getFreelyInvertedImpl(
U.get(), /*WillInvertAllUses=*/false,
- /*Builder=*/nullptr, DoesConsume, MaxAnalysisRecursionDepth - 1);
+ /*Builder=*/nullptr, LocalDoesConsume, MaxAnalysisRecursionDepth - 1);
if (NewIncomingVal == nullptr)
return nullptr;
// Make sure that we can safely erase the original PHI node.
@@ -2375,6 +2378,8 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
if (Builder != nullptr)
IncomingValues.emplace_back(NewIncomingVal, IncomingBlock);
}
+
+ DoesConsume = LocalDoesConsume;
if (Builder != nullptr) {
IRBuilderBase::InsertPointGuard Guard(*Builder);
Builder->SetInsertPoint(PN);
diff --git a/llvm/test/Transforms/InstCombine/pr82877.ll b/llvm/test/Transforms/InstCombine/pr82877.ll
new file mode 100644
index 00000000000000..8594bb68b8e4cc
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr82877.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define i64 @func(i32 %p, i1 %cmp1) {
+; CHECK-LABEL: define i64 @func(
+; CHECK-SAME: i32 [[P:%.*]], i1 [[CMP1:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[P]], -1
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: [[P0:%.*]] = phi i32 [ [[NOT]], [[ENTRY:%.*]] ], [ [[CONV:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP1]], i32 0, i32 -1231558963
+; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[COND]], [[P0]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[XOR]], 1
+; CHECK-NEXT: [[CONV]] = zext i1 [[CMP2]] to i32
+; CHECK-NEXT: br i1 [[CMP2]], label [[FOR_BODY]], label [[FOR_EXIT:%.*]]
+; CHECK: for.exit:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %not = xor i32 %p, -1
+ br label %for.body
+
+for.body:
+ %p0 = phi i32 [ %not, %entry ], [ %conv, %for.body ]
+ %cond = select i1 %cmp1, i32 0, i32 -1231558963
+ %xor = xor i32 %cond, %p0
+ %cmp2 = icmp ne i32 %xor, 1
+ %conv = zext i1 %cmp2 to i32
+ br i1 %cmp2, label %for.body, label %for.exit
+
+for.exit:
+ ret i64 0
+}
More information about the llvm-commits
mailing list