Commit 32aac256 authored by Jay Foad's avatar Jay Foad
Browse files

[KnownBits] Introduce anyext instead of passing a flag into zext

Summary:
This was a very odd API, where you had to pass a flag into a zext
function to say whether the extended bits really were zero or not. All
callers passed in a literal true or false.

I think it's much clearer to make the function name reflect the
operation being performed on the value we're tracking (rather than on
the KnownBits Zero and One fields), so zext means the value is being
zero extended and new function anyext means the value is being extended
with unknown bits.

NFC.

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D74482
parent 137415ad
Loading
Loading
Loading
Loading
+28 −19
Original line number Diff line number Diff line
@@ -122,39 +122,48 @@ public:
    return ~Zero;
  }

  /// Truncate the underlying known Zero and One bits. This is equivalent
  /// to truncating the value we're tracking.
  /// Return known bits for a truncation of the value we're tracking.
  KnownBits trunc(unsigned BitWidth) const {
    return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
  }

  /// Extends the underlying known Zero and One bits.
  /// By setting ExtendedBitsAreKnownZero=true this will be equivalent to
  /// zero extending the value we're tracking.
  /// With ExtendedBitsAreKnownZero=false the extended bits are set to unknown.
  KnownBits zext(unsigned BitWidth, bool ExtendedBitsAreKnownZero) const {
  /// Return known bits for an "any" extension of the value we're tracking,
  /// where we don't know anything about the extended bits.
  KnownBits anyext(unsigned BitWidth) const {
    return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
  }

  /// Return known bits for a zero extension of the value we're tracking.
  KnownBits zext(unsigned BitWidth) const {
    unsigned OldBitWidth = getBitWidth();
    APInt NewZero = Zero.zext(BitWidth);
    if (ExtendedBitsAreKnownZero)
    NewZero.setBitsFrom(OldBitWidth);
    return KnownBits(NewZero, One.zext(BitWidth));
  }

  /// Sign extends the underlying known Zero and One bits. This is equivalent
  /// to sign extending the value we're tracking.
  /// Return known bits for a sign extension of the value we're tracking.
  KnownBits sext(unsigned BitWidth) const {
    return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
  }

  /// Extends or truncates the underlying known Zero and One bits. When
  /// extending the extended bits can either be set as known zero (if
  /// ExtendedBitsAreKnownZero=true) or as unknown (if
  /// ExtendedBitsAreKnownZero=false).
  KnownBits zextOrTrunc(unsigned BitWidth,
                        bool ExtendedBitsAreKnownZero) const {
  /// Return known bits for an "any" extension or truncation of the value we're
  /// tracking.
  KnownBits anyextOrTrunc(unsigned BitWidth) const {
    if (BitWidth > getBitWidth())
      return anyext(BitWidth);
    if (BitWidth < getBitWidth())
      return trunc(BitWidth);
    return *this;
  }

  /// Return known bits for a zero extension or truncation of the value we're
  /// tracking.
  KnownBits zextOrTrunc(unsigned BitWidth) const {
    if (BitWidth > getBitWidth())
      return zext(BitWidth, ExtendedBitsAreKnownZero);
    return KnownBits(Zero.zextOrTrunc(BitWidth), One.zextOrTrunc(BitWidth));
      return zext(BitWidth);
    if (BitWidth < getBitWidth())
      return trunc(BitWidth);
    return *this;
  }

  /// Return a KnownBits with the extracted bits
+1 −1
Original line number Diff line number Diff line
@@ -5736,7 +5736,7 @@ ScalarEvolution::getRangeRef(const SCEV *S,
      // For a SCEVUnknown, ask ValueTracking.
      KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
      if (Known.getBitWidth() != BitWidth)
        Known = Known.zextOrTrunc(BitWidth, true);
        Known = Known.zextOrTrunc(BitWidth);
      // If Known does not result in full-set, intersect with it.
      if (Known.getMinValue() != Known.getMaxValue() + 1)
        ConservativeResult = ConservativeResult.intersectWith(
+2 −2
Original line number Diff line number Diff line
@@ -1254,9 +1254,9 @@ static void computeKnownBitsFromOperator(const Operator *I,
      Q.DL.getTypeSizeInBits(ScalarTy);

    assert(SrcBitWidth && "SrcBitWidth can't be zero");
    Known = Known.zextOrTrunc(SrcBitWidth, false);
    Known = Known.anyextOrTrunc(SrcBitWidth);
    computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
    Known = Known.zextOrTrunc(BitWidth, true /* ExtendedBitsAreKnownZero */);
    Known = Known.zextOrTrunc(BitWidth);
    break;
  }
  case Instruction::BitCast: {
+3 −3
Original line number Diff line number Diff line
@@ -294,7 +294,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
  case TargetOpcode::G_ANYEXT: {
    computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
                         Depth + 1);
    Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */);
    Known = Known.zext(BitWidth);
    break;
  }
  case TargetOpcode::G_LOAD: {
@@ -360,9 +360,9 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
                               ? DL.getIndexSizeInBits(SrcTy.getAddressSpace())
                               : SrcTy.getSizeInBits();
    assert(SrcBitWidth && "SrcBitWidth can't be zero");
    Known = Known.zextOrTrunc(SrcBitWidth, true);
    Known = Known.zextOrTrunc(SrcBitWidth);
    computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
    Known = Known.zextOrTrunc(BitWidth, true);
    Known = Known.zextOrTrunc(BitWidth);
    if (BitWidth > SrcBitWidth)
      Known.Zero.setBitsFrom(SrcBitWidth);
    break;
+1 −1
Original line number Diff line number Diff line
@@ -407,7 +407,7 @@ FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {

  if (BitWidth > LOI->Known.getBitWidth()) {
    LOI->NumSignBits = 1;
    LOI->Known = LOI->Known.zext(BitWidth, false /* => any extend */);
    LOI->Known = LOI->Known.anyext(BitWidth);
  }

  return LOI;
Loading