This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Add Transform for `(and/or (eq/ne A,Pow2),(eq/ne A,-Pow2))`->`(eq/ne (and (and A,Pow2),~(Pow2*2)), 0)`
ClosedPublic

Authored by goldstein.w.n on Jan 23 2023, 3:24 AM.

Details

Summary

In many instances this can be preferable if the icmp -> i1 cannot be
done in one instruction (such as X86 for scalars).

At the moment guarded behind TLI.isDesirableToCombineLogicOpOfSETCC.

alive2 links:
https://alive2.llvm.org/ce/z/nLm5sN
https://alive2.llvm.org/ce/z/moEcyE

Diff Detail

Event Timeline

goldstein.w.n created this revision.Jan 23 2023, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2023, 3:24 AM
goldstein.w.n requested review of this revision.Jan 23 2023, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2023, 3:24 AM

Note, I think this is probably also worth turning on for aarch64: https://godbolt.org/z/8odYov4c1 hence when I put this in DAGCombiner instead of x86 only.

RKSimon added inline comments.Jan 23 2023, 3:41 AM
llvm/include/llvm/CodeGen/TargetLowering.h
4018

Drop the const from the SDNode args?

goldstein.w.n added inline comments.Jan 23 2023, 11:40 AM
llvm/include/llvm/CodeGen/TargetLowering.h
4018

Drop the const from the SDNode args?

Really? All the surrounding "should I do this" functions seem to use consts:

  virtual bool isDesirableToCommuteWithShift(const SDNode *N,
                                             CombineLevel Level) const {
    return true;
  }
...
  virtual bool isDesirableToCombineLogicOpOfSETCC(const SDNode *LogicOp,
                                                  const SDNode *SETCC0,
                                                  const SDNode *SETCC1) const {
    return false;
  }

...
  virtual bool isDesirableToCommuteXorWithShift(const SDNode *N) const {
    return true;
  }

Remove redundant iszero check

Rebase + fix typo + make only oneuse

RKSimon added inline comments.Jan 29 2023, 7:41 AM
llvm/include/llvm/CodeGen/TargetLowering.h
4018

OK - never mind

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
2562

remove extra newline

5906

Swap over to reduce calling (expensive) hasOneUse calls unnecessarily (or maybe move to the end of the if()):

LHS0 == RHS0 && LHS1C && RHS1C && LHS.hasOneUse() && RHS.hasOneUse() &&
5915

const APInt &C = Pow2->getAPIntValue();

goldstein.w.n marked 5 inline comments as done.

Fix style issues, make code a bit better (perf)

Fixed up in next version (won't let me submit "done" w.o message).

RKSimon accepted this revision.Feb 5 2023, 9:14 AM

LGTM

llvm/include/llvm/CodeGen/TargetLowering.h
4010

remove trailing w?

This revision is now accepted and ready to land.Feb 5 2023, 9:14 AM
This revision was landed with ongoing or failed builds.Feb 14 2023, 4:59 PM
This revision was automatically updated to reflect the committed changes.