From 03036061c7716f88cef32ebe16f2c10307383ef8 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Mon, 20 Jun 2022 09:33:09 +0000 Subject: [PATCH] [Alignment] Use 'previous()' method instead of scalar division This is in preparation of integration with D128052. Differential Revision: https://reviews.llvm.org/D128169 --- llvm/include/llvm/Support/Alignment.h | 15 ++++++++------- llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp | 4 ++-- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 2 +- llvm/unittests/Support/AlignmentTest.cpp | 9 +-------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h index 24a2f62..f12d8ed 100644 --- a/llvm/include/llvm/Support/Alignment.h +++ b/llvm/include/llvm/Support/Alignment.h @@ -84,6 +84,14 @@ public: /// Needed to interact with C for instance. uint64_t value() const { return uint64_t(1) << ShiftValue; } + // Returns the previous alignment. + Align previous() const { + assert(ShiftValue != 0 && "Undefined operation"); + Align Out; + Out.ShiftValue = ShiftValue - 1; + return Out; + } + /// Allow constructions of constexpr Align. template constexpr static LogValue Constant() { return LogValue{static_cast(CTLog2())}; @@ -314,13 +322,6 @@ bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; -inline Align operator/(Align Lhs, uint64_t Divisor) { - assert(llvm::isPowerOf2_64(Divisor) && - "Divisor must be positive and a power of 2"); - assert(Lhs != 1 && "Can't halve byte alignment"); - return Align(Lhs.value() / Divisor); -} - #ifndef NDEBUG // For usage in LLVM_DEBUG macros. inline std::string DebugStr(const Align &A) { diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp index e8030df..5226816 100644 --- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -7662,7 +7662,7 @@ LegalizerHelper::lowerMemcpy(MachineInstr &MI, Register Dst, Register Src, const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->hasStackRealignment(MF)) while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) - NewAlign = NewAlign / 2; + NewAlign = NewAlign.previous(); if (NewAlign > Alignment) { Alignment = NewAlign; @@ -7770,7 +7770,7 @@ LegalizerHelper::lowerMemmove(MachineInstr &MI, Register Dst, Register Src, const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->hasStackRealignment(MF)) while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) - NewAlign = NewAlign / 2; + NewAlign = NewAlign.previous(); if (NewAlign > Alignment) { Alignment = NewAlign; diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 3b54916..7a72999 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -6751,7 +6751,7 @@ static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->hasStackRealignment(MF)) while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign)) - NewAlign = NewAlign / 2; + NewAlign = NewAlign.previous(); if (NewAlign > Alignment) { // Give the stack frame object a larger alignment if needed. diff --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp index 9841e09..f5f0d5e 100644 --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -77,7 +77,7 @@ TEST(AlignmentTest, CheckMaybeAlignHasValue) { TEST(AlignmentTest, Division) { for (uint64_t Value : getValidAlignments()) { if (Value > 1) { - EXPECT_EQ(Align(Value) / 2, Value / 2); + EXPECT_EQ(Align(Value).previous(), Value / 2); } } } @@ -292,13 +292,6 @@ TEST(AlignmentDeathTest, CantConvertUnsetMaybe) { EXPECT_DEATH((MaybeAlign(0).getValue()), ".*"); } -TEST(AlignmentDeathTest, Division) { - EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment"); - - EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2"); - EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2"); -} - TEST(AlignmentDeathTest, InvalidCTors) { EXPECT_DEATH((Align(0)), "Value must not be 0"); for (uint64_t Value : getNonPowerOfTwo()) { -- 2.7.4