This is an archive of the discontinued LLVM Phabricator instance.

Refactor instruction simplification code in visitors. NFC.
ClosedPublic

Authored by eraman on Feb 17 2017, 10:44 AM.

Details

Summary

Several visitors check if operands to the instruction are constants, either as it is or after looking up SimplifiedValues, check if the result is a constant and update the SimplifiedValues map. This refactoring splits it into a common function that does the checking of whether the operands are constants and updating of the SimplifiedValues table, and an instruction specific part that is implemented by each instruction visitor as a lambda and passed to the common function.

Diff Detail

Repository
rL LLVM

Event Timeline

eraman created this revision.Feb 17 2017, 10:44 AM
eraman updated this revision to Diff 88925.Feb 17 2017, 10:52 AM

Fix a stale comment

chandlerc added inline comments.Feb 17 2017, 2:58 PM
lib/Analysis/InlineCost.cpp
211 ↗(On Diff #88925)

I'd keep this above with the general implementation routines rather than with the visitors.

Also, I'd keep the comment on the implementation rather than the declaration.

216 ↗(On Diff #88925)

Using a function_ref seems like a fairly heavy abstraction. Maybe make this a template and take an arbitrary callable?

465 ↗(On Diff #88925)

What if there are more than two operands? Maybe you want to require one of th eexpression subclasses here? Or maybe just make COps be a small vector so this is generically correct? It should't be any less efficient, and I find the N++ bit below somewhat easy to miss anyways.

473–476 ↗(On Diff #88925)

I would invert and use early return here as well:

auto *C = Evaluate(COpts);
if (!C)
  return false;

SimplifiedValues[&I] = C;
return true;
507–509 ↗(On Diff #88925)

For all of these where you only use the lambda once, put the lambda in the arguments?

Also for all of these, can you let the return type be deduced? I always prefer that when it is unambiguous. This might be made easier by the routine accepting a generic callable.

eraman marked 5 inline comments as done.Feb 17 2017, 3:55 PM

Thanks for the comments. Will update the revised patch in a bit.

lib/Analysis/InlineCost.cpp
465 ↗(On Diff #88925)

Changed it to use SmallVector

507–509 ↗(On Diff #88925)

Made the suggested change in all but one place where the body of the law is multiple lines and to me makes the surrounding if more readable.

eraman updated this revision to Diff 88983.Feb 17 2017, 3:57 PM
eraman marked 2 inline comments as done.

Address Chandler's comments.

chandlerc accepted this revision.Feb 17 2017, 5:57 PM

LGTM, really nice cleanup.

This revision is now accepted and ready to land.Feb 17 2017, 5:57 PM
This revision was automatically updated to reflect the committed changes.