[PATCH] D88937: [KnownBits] Add a sextOrTrunc method
Quentin Colombet via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 15:01:00 PDT 2020
qcolombet updated this revision to Diff 296807.
qcolombet added a comment.
- Add a test for the new method
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D88937/new/
https://reviews.llvm.org/D88937
Files:
llvm/include/llvm/Support/KnownBits.h
llvm/unittests/Support/KnownBitsTest.cpp
Index: llvm/unittests/Support/KnownBitsTest.cpp
===================================================================
--- llvm/unittests/Support/KnownBitsTest.cpp
+++ llvm/unittests/Support/KnownBitsTest.cpp
@@ -203,4 +203,33 @@
});
}
+TEST(KnownBitsTest, SExtOrTrunc) {
+ const unsigned NarrowerSize = 4;
+ const unsigned BaseSize = 6;
+ const unsigned WiderSize = 8;
+ APInt NegativeFitsNarrower(BaseSize, -4, /*isSigned*/ true);
+ APInt NegativeDoesntFitNarrower(BaseSize, -28, /*isSigned*/ true);
+ APInt PositiveFitsNarrower(BaseSize, 14);
+ APInt PositiveDoesntFitNarrower(BaseSize, 36);
+ auto InitKnownBits = [&](KnownBits &Res, const APInt &Input) {
+ Res = KnownBits(Input.getBitWidth());
+ Res.One = Input;
+ Res.Zero = ~Input;
+ };
+
+ for (unsigned Size : {NarrowerSize, BaseSize, WiderSize}) {
+ for (const APInt &Input :
+ {NegativeFitsNarrower, NegativeDoesntFitNarrower, PositiveFitsNarrower,
+ PositiveDoesntFitNarrower}) {
+ KnownBits Test;
+ InitKnownBits(Test, Input);
+ KnownBits Baseline;
+ InitKnownBits(Baseline, Input.sextOrTrunc(Size));
+ Test = Test.sextOrTrunc(Size);
+ EXPECT_EQ(Test.One, Baseline.One);
+ EXPECT_EQ(Test.Zero, Baseline.Zero);
+ }
+ }
+}
+
} // end anonymous namespace
Index: llvm/include/llvm/Support/KnownBits.h
===================================================================
--- llvm/include/llvm/Support/KnownBits.h
+++ llvm/include/llvm/Support/KnownBits.h
@@ -166,6 +166,16 @@
return *this;
}
+ /// Return known bits for a sign extension or truncation of the value we're
+ /// tracking.
+ KnownBits sextOrTrunc(unsigned BitWidth) const {
+ if (BitWidth > getBitWidth())
+ return sext(BitWidth);
+ if (BitWidth < getBitWidth())
+ return trunc(BitWidth);
+ return *this;
+ }
+
/// Return a KnownBits with the extracted bits
/// [bitPosition,bitPosition+numBits).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88937.296807.patch
Type: text/x-patch
Size: 2012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201007/83a39e07/attachment.bin>
More information about the llvm-commits
mailing list