Skip to content

Commit

Permalink
[CGP] Fix infinite loop in icmp operand swapping
Browse files Browse the repository at this point in the history
Don't swap the operands if they're the same. Fixes the issue reported
at https://reviews.llvm.org/D152541#4427017.
  • Loading branch information
nikic committed Jun 16, 2023
1 parent 5e73fda commit b7bd3a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Transforms/CodeGenPrepare/X86/icmp-swap-loop.ll
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b7bd3a7

Please sign in to comment.