diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h --- a/llvm/include/llvm/Support/Alignment.h +++ b/llvm/include/llvm/Support/Alignment.h @@ -84,6 +84,14 @@ /// 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; -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 --- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -7662,7 +7662,7 @@ 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 @@ 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 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -6751,7 +6751,7 @@ 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 --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -77,7 +77,7 @@ 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 @@ 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()) {