This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Simplify G_ADD when it has (0-X) on the LHS or RHS
ClosedPublic

Authored by paquette on Apr 3 2020, 8:30 PM.

Details

Summary

This implements the following combines:

((0-A) + B) -> B-A
(A + (0-B)) -> A-B

Porting over the basic algebraic combines from the DAGCombiner. There are several combines which fold adds away into subtracts. This is just the simplest one.

I noticed that add combines are some of the most commonly hit across CTMark, (via print statements when they fire), so I'm porting over some of the obvious ones.

This gives some minor code size improvements on CTMark at -O3 on AArch64.

Diff Detail

Event Timeline

paquette created this revision.Apr 3 2020, 8:30 PM
arsenm added inline comments.Apr 5 2020, 9:58 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
1701

Should this preserve any flags?

paquette marked an inline comment as done.Apr 6 2020, 11:17 AM
paquette added inline comments.
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
1701

In the DAGCombiner, it doesn't seem to:

// fold ((0-A) + B) -> B-A
if (N0.getOpcode() == ISD::SUB && isNullOrNullSplat(N0.getOperand(0)))
  return DAG.getNode(ISD::SUB, DL, VT, N1, N0.getOperand(1));

but maybe we should? It would make sense.

arsenm added inline comments.Apr 6 2020, 11:27 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
1701

Nobody ever really went back to preserve flags in DAGCombiner (to some degree for FP ops). I would check what InstCombine does

arsenm accepted this revision.Jun 5 2020, 6:24 AM

LGTM. InstCombine seems to not preserve flags in this case

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
1686–1687

We should add an m_Zero() at some point

This revision is now accepted and ready to land.Jun 5 2020, 6:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 5 2020, 6:24 AM
This revision was automatically updated to reflect the committed changes.