[llvm-dev] [RFC] Should we add isa_or_null<>?

Aaron Ballman via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 4 10:39:31 PDT 2019


On Thu, Apr 4, 2019 at 12:58 PM Chris Lattner <clattner at nondot.org> wrote:
>
>
>
> > On Apr 4, 2019, at 5:37 AM, Don Hinton via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> >
> > I'd like to propose adding `isa_or_null<>` to replace the following usage pattern that's relatively common in conditionals:
> >
> >   var && isa<T>(var)  =>>  isa_or_null<T>(var)
> >
> > And in particular when `var` is a method call which might be expensive, e.g.:
> >
> >   X->foo() && isa<T>(X->foo())  =>>  isa_or_null<T>(X->foo())
> >
> > The implementation could be a simple wrapper around isa<>, and while the IR produced is only slightly more efficient, the elimination of an extra call could be worthwhile.
>
> I’d love to see this, I agree with downstream comments though that this name will be confusing.  isa_and_nonnull<>. ?

tbh, I don't think the proposed name will be all that confusing --
we're used to _or_null() returning "the right thing" when given null.
isa_and_nonnull<> is a bit of a weird name for me, but I could
probably live with it. We could spell it nonnull_and_isa<> to reflect
the order of the operations, but that sort of hides the important part
of the API (the "isa" bit).

~Aaron

>
> -Chris
>


More information about the llvm-dev mailing list