Skip to content

[InstCombine] Queue Xor for deletion after replacing its uses in freelyInvertAllUsersOf. #72445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2023

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Nov 15, 2023

Fixes #72433

@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2023

@llvm/pr-subscribers-llvm-transforms

Author: Craig Topper (topperc)

Changes

Fixes #72433


Full diff: https://github.com/llvm/llvm-project/pull/72445.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+2)
  • (added) llvm/test/Transforms/InstCombine/pr72433.ll (+27)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 16de4e3ffe1f51a..463a7b5bb1bb588 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1185,6 +1185,8 @@ void InstCombinerImpl::freelyInvertAllUsersOf(Value *I, Value *IgnoredUser) {
       break;
     case Instruction::Xor:
       replaceInstUsesWith(cast<Instruction>(*U), I);
+      // Add to worklist for DCE.
+      addToWorklist(cast<Instruction>(U));
       break;
     default:
       llvm_unreachable("Got unexpected user - out of sync with "
diff --git a/llvm/test/Transforms/InstCombine/pr72433.ll b/llvm/test/Transforms/InstCombine/pr72433.ll
new file mode 100644
index 000000000000000..ec3fcfcfea46fcf
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr72433.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i32 @widget(i32 %arg, i32 %arg1) {
+; CHECK-LABEL: define i32 @widget(
+; CHECK-SAME: i32 [[ARG:%.*]], i32 [[ARG1:%.*]]) {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    [[ICMP:%.*]] = icmp ne i32 [[ARG]], 0
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i1 [[ICMP]] to i32
+; CHECK-NEXT:    [[MUL:%.*]] = shl nuw nsw i32 20, [[TMP0]]
+; CHECK-NEXT:    [[XOR:%.*]] = zext i1 [[ICMP]] to i32
+; CHECK-NEXT:    [[ADD9:%.*]] = or i32 [[MUL]], [[XOR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i1 [[ICMP]] to i32
+; CHECK-NEXT:    [[MUL2:%.*]] = shl nuw nsw i32 [[ADD9]], [[TMP1]]
+; CHECK-NEXT:    ret i32 [[MUL2]]
+;
+bb:
+  %icmp = icmp eq i32 %arg, 0
+  %zext = zext i1 %icmp to i32
+  %sub = sub i32 2, %zext
+  %mul = mul i32 20, %sub
+  %zext8 = zext i1 %icmp to i32
+  %xor = xor i32 %zext8, 1
+  %add9 = add i32 %mul, %xor
+  %mul2 = mul i32 %add9, %sub
+  ret i32 %mul2
+}

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@topperc topperc merged commit 2fbd088 into llvm:main Nov 16, 2023
@topperc topperc deleted the pr/pr72433 branch November 16, 2023 17:46
sr-tream pushed a commit to sr-tream/llvm-project that referenced this pull request Nov 20, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InstCombine fails to reach fixpoint after 1 iteration
3 participants