[PATCH] D139783: [JT] check xor operand is exactly the same in processBranchOnXOR

Yingchi Long via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 10 21:43:12 PST 2022


inclyc created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
inclyc requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Reproducer:

  ; RUN: opt -S -jump-threading < %s
  define void @test() {
  entry:
  br i1 false, label %loop, label %exit
  
  loop:
  %bool = phi i1 [ %xor, %loop.latch ], [ false, %entry ]
  %cmp = icmp eq i16 0, 1
  %xor = xor i1 %cmp, %bool
  br i1 %bool, label %loop.latch, label %exit
  
  loop.latch:
  %dummy = phi i16 [ 0, %loop ]
  br label %loop
  
  exit:
  ret void
  }

On this occassion, phi node %bool is actually %xor, and doing substitution causes assertion failure.

Fixes: https://github.com/llvm/llvm-project/issues/58812


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139783

Files:
  llvm/lib/Transforms/Scalar/JumpThreading.cpp
  llvm/test/Transforms/JumpThreading/phi-xor-branch.ll


Index: llvm/test/Transforms/JumpThreading/phi-xor-branch.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/JumpThreading/phi-xor-branch.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=jump-threading %s | FileCheck %s
+
+; https://github.com/llvm/llvm-project/issues/58812
+
+define void @test() {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[EXIT:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    [[DUMMY:%.*]] = phi i16 [ 0, [[LOOP:%.*]] ]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 0, 1
+; CHECK-NEXT:    [[XOR:%.*]] = xor i1 false, [[XOR]]
+; CHECK-NEXT:    br i1 [[XOR]], label [[LOOP]], label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br i1 false, label %loop, label %exit
+
+loop:
+  %bool = phi i1 [ %xor, %loop.latch ], [ false, %entry ]
+  %cmp = icmp eq i16 0, 1
+  %xor = xor i1 %cmp, %bool
+  br i1 %bool, label %loop.latch, label %exit
+
+loop.latch:
+  %dummy = phi i16 [ 0, %loop ]
+  br label %loop
+
+exit:
+  ret void
+}
Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1932,7 +1932,7 @@
       // If all preds provide undef, just nuke the xor, because it is undef too.
       BO->replaceAllUsesWith(UndefValue::get(BO->getType()));
       BO->eraseFromParent();
-    } else if (SplitVal->isZero()) {
+    } else if (SplitVal->isZero() && BO != BO->getOperand(isLHS)) {
       // If all preds provide 0, replace the xor with the other input.
       BO->replaceAllUsesWith(BO->getOperand(isLHS));
       BO->eraseFromParent();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139783.481894.patch
Type: text/x-patch
Size: 1804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221211/ee099905/attachment.bin>


More information about the llvm-commits mailing list