This is an archive of the discontinued LLVM Phabricator instance.

[X86] Teach computeKnownBitsForTargetNode about MUL_IMM
ClosedPublic

Authored by kazu on Mar 24 2023, 12:11 AM.

Details

Summary

This patch teaches computeKnownBitsForTargetNode about MUL_IMM.

MUL_IMM comes up in certain select of constants. Specifically, it is
used to multiply the result of SETCC. Computing the known zero bits
of MUL_IMM allows matchAddressRecursively us to convert some OR into
ADD, which eventually becomes a part of LEA.

This patch fixes:

https://github.com/llvm/llvm-project/issues/61365

Diff Detail

Event Timeline

kazu created this revision.Mar 24 2023, 12:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2023, 12:11 AM
kazu requested review of this revision.Mar 24 2023, 12:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2023, 12:11 AM
kazu updated this revision to Diff 507979.Mar 24 2023, 12:23 AM

Guard against a potential integer overflow.

craig.topper added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
38108

Why can't we use KnownBits.mul?

foad added inline comments.Mar 24 2023, 1:01 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
38105–38106

You can write this as ~Known2.Zero or Known2.getMaxValue(). But I agree with @craig.topper.

kazu updated this revision to Diff 508134.Mar 24 2023, 9:27 AM

Switched to KnownBits::mul.

kazu marked 2 inline comments as done.Mar 24 2023, 9:27 AM

Please take a look. Thanks!

RKSimon added inline comments.Mar 26 2023, 7:23 AM
llvm/test/CodeGen/X86/select-constant-lea.ll
20

Please can you confirm what's caused us to use the ORLIke pattern?

kazu added inline comments.Mar 26 2023, 8:37 PM
llvm/test/CodeGen/X86/select-constant-lea.ll
20

combineSelectOfTwoConstants expands ISD::SELECT as:

// select Cond, TC, FC --> (zext(Cond) * (TC - FC)) + FC

with ISD::ADD. Then DAGCombiner::visitADD does:

// fold (a+b) -> (a|b) iff a and b share no bits.                                                 
if ((!LegalOperations || TLI.isOperationLegal(ISD::OR, VT)) &&
    DAG.haveNoCommonBitsSet(N0, N1))
  return DAG.getNode(ISD::OR, DL, VT, N0, N1);

This is where the ISD::OR comes from.

RKSimon accepted this revision.Mar 27 2023, 3:14 AM

LGTM - cheers

This revision is now accepted and ready to land.Mar 27 2023, 3:14 AM
This revision was automatically updated to reflect the committed changes.