[llvm] b7bd3a7 - [CGP] Fix infinite loop in icmp operand swapping

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 16 06:50:19 PDT 2023


Author: Nikita Popov
Date: 2023-06-16T15:50:12+02:00
New Revision: b7bd3a734c7c08e1a703a4f23e3e854ba94a1bf2

URL: https://github.com/llvm/llvm-project/commit/b7bd3a734c7c08e1a703a4f23e3e854ba94a1bf2
DIFF: https://github.com/llvm/llvm-project/commit/b7bd3a734c7c08e1a703a4f23e3e854ba94a1bf2.diff

LOG: [CGP] Fix infinite loop in icmp operand swapping

Don't swap the operands if they're the same. Fixes the issue reported
at https://reviews.llvm.org/D152541#4427017.

Added: 
    llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll

Modified: 
    llvm/lib/CodeGen/CodeGenPrepare.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index ee4c19394d854..0e2793a8c3504 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -1837,7 +1837,7 @@ static bool swapICmpOperandsToExposeCSEOpportunities(CmpInst *Cmp) {
   Value *Op0 = Cmp->getOperand(0);
   Value *Op1 = Cmp->getOperand(1);
   if (!Op0->getType()->isIntegerTy() || isa<Constant>(Op0) ||
-      isa<Constant>(Op1))
+      isa<Constant>(Op1) || Op0 == Op1)
     return false;
 
   // If a subtract already has the same operands as a compare, swapping would be

diff  --git a/llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll b/llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll
new file mode 100644
index 0000000000000..5ad54e596ca6d
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -S -mtriple=x86_64-- -codegenprepare < %s | FileCheck %s
+
+define i1 @test(i32 %arg) {
+; CHECK-LABEL: define i1 @test
+; CHECK-SAME: (i32 [[ARG:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[ARG]], [[ARG]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[ARG]], [[ARG]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp ne i32 %arg, %arg
+  %sub = sub i32 %arg, %arg
+  ret i1 %cmp
+}


        


More information about the llvm-commits mailing list