This is an archive of the discontinued LLVM Phabricator instance.

[KnownBits] Add a sextOrTrunc method
ClosedPublic

Authored by qcolombet on Oct 6 2020, 4:32 PM.

Details

Summary

We already offer zextOrTrunc and it seems natural to offer the
same capability for sign extension.

This patch is a preparatory addition useful for future computeKnownBits
developments.

Diff Detail

Event Timeline

qcolombet created this revision.Oct 6 2020, 4:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 6 2020, 4:32 PM
lebedev.ri accepted this revision.Oct 6 2020, 11:27 PM

Could use a test, but it generally makes sense to have such a method.

This revision is now accepted and ready to land.Oct 6 2020, 11:27 PM
RKSimon accepted this revision.Oct 7 2020, 12:56 AM

LGTM

qcolombet updated this revision to Diff 296807.Oct 7 2020, 3:00 PM
  • Add a test for the new method

Hi @lebedev.ri ,

Could use a test, but it generally makes sense to have such a method.

I didn't see a test for zextOrTrunc that I could mimic so I come up with something.

Let me know what do you think.

Cheers,
-Quentin

Seems fine

llvm/unittests/Support/KnownBitsTest.cpp
205–234

I guess i was thinking of something like this:

TEST(KnownBitsTest, GetMinMaxVal) {
  const unsigned NarrowerSize = 4;
  const unsigned BaseSize = 6;
  const unsigned WiderSize = 8;
  ForeachKnownBits(BaseSize, [&](const KnownBits &KnownBase) {
    for (unsigned Size : {NarrowerSize, BaseSize, WiderSize}) {
      KnownBits Test;
      if(Size == BaseSize)
        Test = KnownBase
      else
        Test = Size < BaseSize ? KnownBase.trunc(Size) :  KnownBase.sext(Size);
      EXPECT_EQ(Test, KnownBase.sextOrTrunc(Size));
    }
  });
}

which seems pretty tautological i guess, so maybe we should go with your test.

This revision was landed with ongoing or failed builds.Oct 8 2020, 11:35 AM
This revision was automatically updated to reflect the committed changes.