From 02c19658ffe935e09fc1d84b30f09beadb87fe52 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Mon, 30 Sep 2019 09:59:31 +0000 Subject: [PATCH] [Alignment][NFC] Adding a max function. Summary: This is patch is part of a series to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: courbet Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68201 llvm-svn: 373196 --- llvm/include/llvm/Support/Alignment.h | 8 ++++++++ llvm/unittests/Support/AlignmentTest.cpp | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h index c90d8d72d44c..3d8a4235b0e6 100644 --- a/llvm/include/llvm/Support/Alignment.h +++ b/llvm/include/llvm/Support/Alignment.h @@ -333,6 +333,14 @@ inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) { return Lhs ? Lhs.getValue() / Divisor : MaybeAlign(); } +inline Align max(MaybeAlign Lhs, Align Rhs) { + return Lhs && *Lhs > Rhs ? *Lhs : Rhs; +} + +inline Align max(Align Lhs, MaybeAlign Rhs) { + return Rhs && *Rhs > Lhs ? *Rhs : Lhs; +} + #undef ALIGN_CHECK_ISPOSITIVE #undef ALIGN_CHECK_ISSET diff --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp index f9cc27b8f423..0b1435912b93 100644 --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -227,6 +227,29 @@ TEST(AlignmentTest, AlignComparisons) { } } +TEST(AlignmentTest, Max) { + // We introduce std::max here to test ADL. + using std::max; + + // Uses llvm::max. + EXPECT_EQ(max(MaybeAlign(), Align(2)), Align(2)); + EXPECT_EQ(max(Align(2), MaybeAlign()), Align(2)); + + EXPECT_EQ(max(MaybeAlign(1), Align(2)), Align(2)); + EXPECT_EQ(max(Align(2), MaybeAlign(1)), Align(2)); + + EXPECT_EQ(max(MaybeAlign(2), Align(2)), Align(2)); + EXPECT_EQ(max(Align(2), MaybeAlign(2)), Align(2)); + + EXPECT_EQ(max(MaybeAlign(4), Align(2)), Align(4)); + EXPECT_EQ(max(Align(2), MaybeAlign(4)), Align(4)); + + // Uses std::max. + EXPECT_EQ(max(Align(2), Align(4)), Align(4)); + EXPECT_EQ(max(MaybeAlign(2), MaybeAlign(4)), MaybeAlign(4)); + EXPECT_EQ(max(MaybeAlign(), MaybeAlign()), MaybeAlign()); +} + TEST(AlignmentTest, AssumeAligned) { EXPECT_EQ(assumeAligned(0), Align(1)); EXPECT_EQ(assumeAligned(0), Align());