Files
clang-p2996/llvm/test/CodeGen/X86/select-neg.ll
Maurice Heumann d1fc8f7211 [X86] Prevent infinite loop in SelectionDAG when lowering negations
In certain cases, lowering negations can cause an infinite loop in SelectionDAG on X86.

The following snippet shows that behaviour:
https://godbolt.org/z/5hP45T4hY

What happens is that ADD(XOR(..., -1), 1) is detected as the two's complement and transformed into SUB(0, ...)
However, immediates can not be encoded as the LHS of a SUB on X86.
Therefore it is transformed back into an ADD/XOR pair, which is then again transformed into a SUB and so on.

In that specific case, I still think it is valid to display this as a SUB(0,...) , because it should eventually be lowered as a NEG.
Which seems better than an ADD/XOR pair.

Adding an exception to the X86 specific handling for SUBs with 0 LHS operand fixes this infinite loop.

Differential Revision: https://reviews.llvm.org/D154575
2023-07-13 12:20:44 +01:00

18 lines
522 B
LLVM

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=i386-unknown-unknown | FileCheck %s
@value1 = external hidden constant i32
define i32 @function(i32 %arg1) {
; CHECK-LABEL: function:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl $value1, %eax
; CHECK-NEXT: xorl {{[0-9]+}}(%esp), %eax
; CHECK-NEXT: negl %eax
; CHECK-NEXT: retl
entry:
%0 = xor i32 %arg1, xor (i32 ptrtoint (i32* @value1 to i32), i32 -1)
%.neg = add i32 %0, 1
ret i32 %.neg
}