Inverse of ConstantFP::get and similar functions?

Hi,

I noticed that ConstantFP::get automatically returns the appropriately
types Constant depending on the LLVM type passed in (i.e. if called
with a vector, it returns a splat vector with the given constant).

Is there any simple way to do the inverse of this function? i.e.,
given a llvm::Value, check whether it is either a scalar of the given
constant value or a splat vector with the given constant value? I
can't seem to find any, and it doesn't look like the pattern matching
interface provides something similar to this either.

If this doesn't exist, then I propose adding static versions of all
the various ConstantFoo::isBar() functions, which take a value as a
parameter and check that the value is of a constant of the appropriate
type and value (checking for vectors matching the predicate in the
vector case).

For example:

   static bool ConstantFP::isExactlyValue(Value *V, double D);

would return true is V is ConstantFP, a splat ConstantVector, or a
ConstantDataVector with the appropriate type. Similarly,

   static bool ConstantFP::isZero(Value *V);

would return true if V is a ConstantFP with zero of either sign, a
ConstantVector or ConstantDataVector with all zeros of either sign, or
a zero initializer...

Anyone have any thoughts, and/or can point me to somewhere where this
kind of thing is already implemented?

Thanks,
Stephen

Hi,

I noticed that ConstantFP::get automatically returns the
appropriately
types Constant depending on the LLVM type passed in (i.e. if called
with a vector, it returns a splat vector with the given constant).

Is there any simple way to do the inverse of this function? i.e.,
given a llvm::Value, check whether it is either a scalar of the given
constant value or a splat vector with the given constant value? I
can't seem to find any, and it doesn't look like the pattern matching
interface provides something similar to this either.

If this doesn't exist, then I propose adding static versions of all
the various ConstantFoo::isBar() functions, which take a value as a
parameter and check that the value is of a constant of the
appropriate
type and value (checking for vectors matching the predicate in the
vector case).

For example:

   static bool ConstantFP::isExactlyValue(Value *V, double D);

You can currently do this:
  if (const ConstantVector *CV = dyn_cast<ConstantVector>(X))
    if (Constant *Splat = CV->getSplatValue())
      // Now you know that Splat is a splatted value, so check it for something.

-Hal

You can currently do this:
  if (const ConstantVector *CV = dyn_cast<ConstantVector>(X))
    if (Constant *Splat = CV->getSplatValue())
      // Now you know that Splat is a splatted value, so check it for something.

Yes, but that's only if you want to check the vector case only; I
would like to check for either the scalar constant or the splat
vector, in the same way that ConstantFP::get creates either the scalar
constant or splat vector.

Also, that doesn't check for ConstantDataVector, either, which is the
usual canonical form.

Basically, I want the alternative to the following:

static bool IsExactlyValueConstantFP(Value *V, double D) {
  ConstantFP *CFP;
  ConstantDataVector *CDV;
  ConstantVector *CV;
  return ((CFP = dyn_cast<ConstantFP>(V)) && CFP->isExactlyValue(D)) ||
         ((CDV = dyn_cast<ConstantDataVector>(V)) &&
          (CFP = dyn_cast_or_null<ConstantFP>(CDV->getSplatValue())) &&
          CFP->isExactlyValue(D)) ||
         ((CV = dyn_cast<ConstantVector>(V)) &&
          (CFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue())) &&
          CFP->isExactlyValue(D));
}

Stephen

We do already have Constant::isZeroValue(). There's also m_SpecificFP
and m_AnyZero in PatternMatch.h.

-Eli

We do already have Constant::isZeroValue(). There's also m_SpecificFP
and m_AnyZero in PatternMatch.h.

-Eli

Oh, the latter two are what I need, more or less. Thanks!

Stephen

Hi Stephen,

Well, Eli already pointed me in the direction of m_SpecificFP, so no
big deal now, but getUniqueInteger isn't what I needed because:

1. It's only for integers
2. It asserts when the vector does not contain all of the same
integer, rather than returning some kind of failure code.

Anyway, it's not a big deal now, but it does seem kind of odd to me
that the Constant hierarchy of classes contain so many functions that
allow easy abstraction of scalar and vector types in one direction
(when creating constants), but not in the other direction (when
detecting constants). Using the pattern matches work okay, though.

Stephen