This is an archive of the discontinued LLVM Phabricator instance.

[Reassociate] Group select of constants together with constants
Needs ReviewPublic

Authored by mgudim on May 30 2023, 3:23 PM.

Details

Reviewers
wristow
nikic
Summary

If we assign the same rank to select of constants as to constants, we enable InstCombine to absorb constant into the select.

Diff Detail

Event Timeline

mgudim created this revision.May 30 2023, 3:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 30 2023, 3:23 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
mgudim requested review of this revision.May 30 2023, 3:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 30 2023, 3:23 PM

Posting this as a draft to get some feedback / advice. I am not sure about the following points:

(1) If only one of the arguments of select is constant, putting select next to a constant could be still beneficial.
(2) How should we handle selects on different conditions?
(3) Would it be better to move selects next to constants in ReassociateExpression instead?

nikic added inline comments.May 31 2023, 12:58 AM
llvm/test/Transforms/Reassociate/select-of-constants.ll
19

This test produces the same result before and after your patch (https://llvm.godbolt.org/z/v6oe486Mc).

mgudim added inline comments.May 31 2023, 4:59 AM
llvm/test/Transforms/Reassociate/select-of-constants.ll
19

The output after reassociate is:

define dso_local signext i32 @select_of_constants(i32 noundef signext %a, i1 %c) {
entry:
  %select_ = select i1 %c, i32 8, i32 2
  %add4 = add i32 %select_, 4  ; note that now we add 4 to %select_
  %add_ = add i32 %add4, %a
  ret i32 %add_
}

So basically, the input corresponds to this expression: %select_ + (%a + 4) and output corresponds to this: (%select + 4) + %a. This allows InstCombine to "swallow` addition of 4 into the select. At the end of opt we'll have:

define dso_local signext i32 @select_of_constants(i32 noundef signext %a, i1 %c) local_unnamed_addr #0 {
entry:
  %add4 = select i1 %c, i32 12, i32 6
  %add_ = add i32 %add4, %a
  ret i32 %add_
}