This is an archive of the discontinued LLVM Phabricator instance.

[ConstantRange] Add full() + empty() named constructors (NFC)
ClosedPublic

Authored by nikic on Mar 22 2019, 2:00 PM.

Details

Summary

This adds ConstantRange::full(BitWidth) and ConstantRange::empty(BitWidth) named constructors as more readable alternatives to the current ConstantRange(BitWidth, /* full */ false) and similar. Additionally private full() and empty() member functions are added which return a full/empty range with the same bit width -- these are commonly needed inside ConstantRange.cpp.

I've also made the IsFullSet argument in the ConstantRange(BitWidth, IsFullSet) constructor mandatory, though I could drop that part if there is a BC concern here.

Diff Detail

Repository
rL LLVM

Event Timeline

nikic created this revision.Mar 22 2019, 2:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 22 2019, 2:01 PM

Looks like a nice cleanup! The naming choice is a bit controversial i think.
Is it fully obvious that full()/empty() don't check that it's full/empty and return bool, but return new full/empty range?
Any more obvious choices? Or am i imagining this?

nikic added a comment.Mar 23 2019, 6:00 AM

Looks like a nice cleanup! The naming choice is a bit controversial i think.
Is it fully obvious that full()/empty() don't check that it's full/empty and return bool, but return new full/empty range?
Any more obvious choices? Or am i imagining this?

I think the static constructors ConstantRange::full(BW) and ConstantRange::empty(BW) are unambiguous, they can't really be interpreted as a boolean check. The CR.full() and CR.empty() methods do look like boolean checks when written in that way. This is part of the reason why I made those methods private: Inside ConstantRange.cpp it's clear (imho) that something like return empty() returns an empty range, while this would not necessarily be obvious outside of it.

lebedev.ri accepted this revision.Mar 23 2019, 6:14 AM

After looking into existing headers (esp. APInt) i think it will be best to name all of these getFull() and getEmpty().
https://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly agrees:
Function names should be verb phrases (as they represent actions), and command-like function should be imperative. The name should be camel case, and start with a lower case letter (e.g. openFile() or isFoo()).

Otherwise LG.

This revision is now accepted and ready to land.Mar 23 2019, 6:14 AM
This revision was automatically updated to reflect the committed changes.
nikic added a comment.Mar 24 2019, 2:37 AM

@lebedev.ri Thanks for the suggestion, I agree that getFull() and getEmpty() make more sense given that APInt uses getXXX() constructors so extensively.

dsanders added inline comments.
llvm/trunk/include/llvm/IR/ConstantRange.h
61

The comment needs updating to match the removal of the default