@@ -281,18 +281,21 @@ class ValueLatticeElement {
281281 return std::nullopt ;
282282 }
283283
284- ConstantRange asConstantRange (Type *Ty, bool UndefAllowed = false ) const {
285- assert (Ty->isIntOrIntVectorTy () && " Must be integer type" );
284+ ConstantRange asConstantRange (unsigned BW, bool UndefAllowed = false ) const {
286285 if (isConstantRange (UndefAllowed))
287286 return getConstantRange ();
288287 if (isConstant ())
289288 return getConstant ()->toConstantRange ();
290- unsigned BW = Ty->getScalarSizeInBits ();
291289 if (isUnknown ())
292290 return ConstantRange::getEmpty (BW);
293291 return ConstantRange::getFull (BW);
294292 }
295293
294+ ConstantRange asConstantRange (Type *Ty, bool UndefAllowed = false ) const {
295+ assert (Ty->isIntOrIntVectorTy () && " Must be integer type" );
296+ return asConstantRange (Ty->getScalarSizeInBits (), UndefAllowed);
297+ }
298+
296299 bool markOverdefined () {
297300 if (isOverdefined ())
298301 return false ;
@@ -384,7 +387,9 @@ class ValueLatticeElement {
384387 return true ;
385388 }
386389
387- assert (isUnknown () || isUndef ());
390+ assert (isUnknown () || isUndef () || isConstant ());
391+ assert ((!isConstant () || NewR.contains (getConstant ()->toConstantRange ())) &&
392+ " Constant must be subset of new range" );
388393
389394 NumRangeExtensions = 0 ;
390395 Tag = NewTag;
@@ -426,6 +431,16 @@ class ValueLatticeElement {
426431 return false ;
427432 if (RHS.isUndef ())
428433 return false ;
434+ // If the constant is a vector of integers, try to treat it as a range.
435+ if (getConstant ()->getType ()->isVectorTy () &&
436+ getConstant ()->getType ()->getScalarType ()->isIntegerTy ()) {
437+ ConstantRange L = getConstant ()->toConstantRange ();
438+ ConstantRange NewR = L.unionWith (
439+ RHS.asConstantRange (L.getBitWidth (), /* UndefAllowed=*/ true ));
440+ return markConstantRange (
441+ std::move (NewR),
442+ Opts.setMayIncludeUndef (RHS.isConstantRangeIncludingUndef ()));
443+ }
429444 markOverdefined ();
430445 return true ;
431446 }
@@ -444,14 +459,9 @@ class ValueLatticeElement {
444459 return OldTag != Tag;
445460 }
446461
447- if (!RHS.isConstantRange ()) {
448- // We can get here if we've encountered a constantexpr of integer type
449- // and merge it with a constantrange.
450- markOverdefined ();
451- return true ;
452- }
453-
454- ConstantRange NewR = getConstantRange ().unionWith (RHS.getConstantRange ());
462+ const ConstantRange &L = getConstantRange ();
463+ ConstantRange NewR = L.unionWith (
464+ RHS.asConstantRange (L.getBitWidth (), /* UndefAllowed=*/ true ));
455465 return markConstantRange (
456466 std::move (NewR),
457467 Opts.setMayIncludeUndef (RHS.isConstantRangeIncludingUndef ()));
0 commit comments