Skip to content

[InstCombine] infinite loop with select + compare canonicalization #52684

@clin111

Description

@clin111

The attached test case is causing instcombine to infinite loop. The two transformations that are going back and forth are:

InstCombineSelect.cpp:

static Instruction * tryToReuseConstantFromSelectInComparison(....)

and:

InstCombineCompares.cpp:

Instruction *InstCombinerImpl::foldICmpAddConstant(...)
....
  // The range test idiom can use either ult or ugt. Arbitrarily canonicalize
  // to the ult form.
  // X+C2 >u C -> X+(C2-C-1) <u ~C
  if (Pred == ICmpInst::ICMP_UGT)

The select transformation is doing this:

  %1 = icmp ult i32 %0, 2
  %spec.select = select i1 %1, i32 1, i32 -3

=>

  %.inv = icmp ugt i32 %0, 1
  %spec.select = select i1 %.inv, i32 -3, i32 1

It is flipping the compare so that the constant becomes the same as one of the
select operands.

The compare canonicalization then undoes this transformation, as it changes "ugt" back to ult". It looks like
one of these transformations should be prevented, but it is not immediately clear which way is "better".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions