[PATCH] D153356: [Align] Add isAligned taking an APInt
Guillaume Chatelet via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 26 06:58:51 PDT 2023
gchatelet updated this revision to Diff 534538.
gchatelet added a comment.
- Remove comment
- Rename variable
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D153356/new/
https://reviews.llvm.org/D153356
Files:
llvm/include/llvm/ADT/APInt.h
llvm/lib/Support/APInt.cpp
llvm/unittests/ADT/APIntTest.cpp
Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -11,6 +11,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/Support/Alignment.h"
#include "gtest/gtest.h"
#include <array>
#include <optional>
@@ -1852,6 +1853,25 @@
}
}
+TEST(APIntTest, isAligned) {
+ struct {
+ uint64_t alignment;
+ uint64_t offset;
+ bool isAligned;
+ } Tests[] = {
+ {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
+ {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
+ {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
+ };
+ for (const auto &T : Tests)
+ EXPECT_EQ(APInt(32, T.offset).isAligned(Align(T.alignment)), T.isAligned);
+ // Tests for APInt that can't represent the alignment.
+ // Here APInt(4, I) can represent values from 0 to 15.
+ EXPECT_TRUE(APInt(4, 0).isAligned(Align(32))); // zero is always aligned.
+ for (int I = 1; I < 16; ++I)
+ EXPECT_FALSE(APInt(4, I).isAligned(Align(32)));
+}
+
// Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which
// does self-move on some platforms.
#ifdef EXPENSIVE_CHECKS
Index: llvm/lib/Support/APInt.cpp
===================================================================
--- llvm/lib/Support/APInt.cpp
+++ llvm/lib/Support/APInt.cpp
@@ -19,6 +19,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/bit.h"
#include "llvm/Config/llvm-config.h"
+#include "llvm/Support/Alignment.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
@@ -164,6 +165,14 @@
ID.AddInteger(U.pVal[i]);
}
+bool APInt::isAligned(Align A) const {
+ if (isZero())
+ return true;
+ const unsigned TrailingZeroes = countr_zero();
+ const unsigned MinimumTrailingZeroes = Log2(A);
+ return TrailingZeroes >= MinimumTrailingZeroes;
+}
+
/// Prefix increment operator. Increments the APInt by one.
APInt& APInt::operator++() {
if (isSingleWord())
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -28,6 +28,7 @@
class StringRef;
class hash_code;
class raw_ostream;
+struct Align;
template <typename T> class SmallVectorImpl;
template <typename T> class ArrayRef;
@@ -433,6 +434,10 @@
return (LO + TZ) == BitWidth;
}
+ /// Checks if this APInt -interpreted as an address- is aligned to the
+ /// provided value.
+ bool isAligned(Align A) const;
+
/// Check if the APInt's value is returned by getSignMask.
///
/// \returns true if this is the value returned by getSignMask.
@@ -1570,8 +1575,8 @@
/// Count the number of trailing zero bits.
///
- /// This function is an APInt version of std::countr_zero. It counts the number
- /// of zeros from the least significant bit to the first set bit.
+ /// This function is an APInt version of std::countr_zero. It counts the
+ /// number of zeros from the least significant bit to the first set bit.
///
/// \returns BitWidth if the value is zero, otherwise returns the number of
/// zeros from the least significant bit to the first one bit.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153356.534538.patch
Type: text/x-patch
Size: 3349 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230626/0d4e8d4e/attachment.bin>
More information about the llvm-commits
mailing list