[CGP] Fix infinite loop in icmp operand swapping
authorNikita Popov <npopov@redhat.com>
Fri, 16 Jun 2023 13:47:39 +0000 (15:47 +0200)
committerNikita Popov <npopov@redhat.com>
Fri, 16 Jun 2023 13:50:12 +0000 (15:50 +0200)
Don't swap the operands if they're the same. Fixes the issue reported
at https://reviews.llvm.org/D152541#4427017.

llvm/lib/CodeGen/CodeGenPrepare.cpp
llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll [new file with mode: 0644]

index ee4c193..0e2793a 100644 (file)
@@ -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 (file)
index 0000000..5ad54e5
--- /dev/null
@@ -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
+}