This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Use dominate tree to get dominate predecessor condtion
Needs ReviewPublic

Authored by bcl5980 on Mar 29 2023, 12:22 AM.

Details

Summary

For now isImpliedByDomCondition only check single predecessor to check if exist dominate condition. For example:

if (x ==y)
  if (z > 0)
     return x ^ y;

We can't detect the pattern x ^y is 0.
This patch invovle DomConditionInfo to maintain a list for every basic block to record all dominate conditions for this basic block.

Even though this patch increase some CPU overhead:
http://llvm-compile-time-tracker.com/compare.php?from=c508e933270d457166c3226b53d8c81619c3e350&to=d4b071718aae1f9f9a8fee31cd2fbee55b5e5d60&stat=instructions%3Au

It help to detect a lot of patterns in SPEC2017 and llvm-test-suite:

MultiSourc.../Benchmarks/nbench/nbench.test     45460.00   45508.00  0.1%
Bitcode/Be...an/halide_local_laplacian.test    199880.00  199976.00  0.0%
MultiSourc...nsumer-jpeg/consumer-jpeg.test    161822.00  161886.00  0.0%
MultiSourc...abench/jpeg/jpeg-6a/cjpeg.test    164107.00  164171.00  0.0%
MultiSourc...lications/SIBsim4/SIBsim4.test     46139.00   46155.00  0.0%
MultiSource/Benchmarks/PAQ8p/paq8p.test         95843.00   95875.00  0.0%
MultiSourc...pplications/oggenc/oggenc.test    257042.00  257074.00  0.0%
MultiSourc...marks/7zip/7zip-benchmark.test    907698.00  907794.00  0.0%
External/S...rlbench_r/500.perlbench_r.test   1697416.00 1697544.00  0.0%
External/S...rlbench_s/600.perlbench_s.test   1697416.00 1697544.00  0.0%
External/S...0.omnetpp_s/620.omnetpp_s.test   1236181.00 1236229.00  0.0%
External/S...0.omnetpp_r/520.omnetpp_r.test   1236181.00 1236229.00  0.0%
MultiSourc...ProxyApps-C++/CLAMR/CLAMR.test    705856.00  705872.00  0.0%
External/S...8.imagick_r/538.imagick_r.test   1718889.00 1718921.00  0.0%
External/S...8.imagick_s/638.imagick_s.test   1718889.00 1718921.00  0.0%
External/S...7rate/502.gcc_r/502.gcc_r.test   6969643.00 6969771.00  0.0%
External/S...speed/602.gcc_s/602.gcc_s.test   6969643.00 6969771.00  0.0%
External/S...511.povray_r/511.povray_r.test    905099.00  905115.00  0.0%
External/S...510.parest_r/510.parest_r.test   8515521.00 8515649.00  0.0%
External/S...6.blender_r/526.blender_r.test   8704919.00 8705047.00  0.0%
External/S...lancbmk_s/623.xalancbmk_s.test   2959781.00 2959813.00  0.0%
External/S...lancbmk_r/523.xalancbmk_r.test   2959781.00 2959813.00  0.0%

Diff Detail

Event Timeline

bcl5980 created this revision.Mar 29 2023, 12:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2023, 12:22 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Mar 29 2023, 12:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2023, 12:22 AM
bcl5980 updated this revision to Diff 509639.Mar 30 2023, 6:07 AM
bcl5980 updated this revision to Diff 509883.Mar 30 2023, 7:44 PM
bcl5980 updated this revision to Diff 513481.Apr 14 2023, 1:42 AM
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 added reviewers: nikic, fhahn, goldstein.w.n.
bcl5980 updated this revision to Diff 513486.Apr 14 2023, 1:47 AM

rebase code

bcl5980 edited the summary of this revision. (Show Details)Apr 14 2023, 2:36 AM

Can you be a little clearer about what cases we now cover that we didn't before?

llvm/lib/Analysis/DomConditionAnalysis.cpp
106

We have a vec of dominating conditions. Why are we only using the first one? The value of the this patch IIUC is that we should be able to get more conditions and more information, but returning the first one defeats that purpose.

llvm/test/Transforms/InstSimplify/domcondition.ll
219

Can you precommit tests so we can see diff this patch causes?

bcl5980 added inline comments.Apr 14 2023, 7:25 PM
llvm/lib/Analysis/DomConditionAnalysis.cpp
106

If we have many dominate conditions for the same LHS and RHS, we can actually merge them into one. So return the first one is enough. For example:

if (x >= y)
  if ( x == y)
      return x ^ y;

We only need to check the dominate condition x == y. For now we will have two elements in the vector x > y, x == y.
Actually we just need the dominate condition x==y here. We can optimize this part in initialization later to make sure every LHS cmp RHS only have one element in the vector.
I can add a TODO in the code but the initialize cost is a already a little high now.

bcl5980 edited the summary of this revision. (Show Details)Apr 14 2023, 7:32 PM
bcl5980 updated this revision to Diff 513858.Apr 15 2023, 1:16 AM

rebase precommit tests.

craig.topper added inline comments.
llvm/lib/Analysis/DomConditionAnalysis.cpp
11

Is the BreadFirstIterator used?

llvm/lib/Analysis/ValueTracking.cpp
7947

You don't need to explicitly pass std::nulloptr. It will default construct that way.

7953

!IsImplied.has_value()

bcl5980 updated this revision to Diff 515640.Apr 21 2023, 1:25 AM
bcl5980 added a reviewer: craig.topper.

address comments.