diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -29,6 +29,7 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/Alignment.h" #include #include #include @@ -120,10 +121,10 @@ bool BigEndian; unsigned AllocaAddrSpace; - unsigned StackNaturalAlign; + MaybeAlign StackNaturalAlign; unsigned ProgramAddrSpace; - unsigned FunctionPtrAlign; + MaybeAlign FunctionPtrAlign; FunctionPtrAlignType TheFunctionPtrAlignType; enum ManglingModeT { @@ -261,17 +262,17 @@ bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); } /// Returns true if the given alignment exceeds the natural stack alignment. - bool exceedsNaturalStackAlignment(unsigned Align) const { - return (StackNaturalAlign != 0) && (Align > StackNaturalAlign); + bool exceedsNaturalStackAlignment(llvm::Align Align) const { + return StackNaturalAlign && (Align > StackNaturalAlign); } - unsigned getStackAlignment() const { return StackNaturalAlign; } + unsigned getStackAlignment() const { return StackNaturalAlign ? StackNaturalAlign->value() : 0; } unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } /// Returns the alignment of function pointers, which may or may not be /// related to the alignment of functions. /// \see getFunctionPtrAlignType - unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; } + MaybeAlign getFunctionPtrAlign() const { return FunctionPtrAlign; } /// Return the type of function pointer alignment. /// \see getFunctionPtrAlign diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -698,7 +698,7 @@ const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->needsStackRealignment(MF)) while (NewAlign > Align && - DL.exceedsNaturalStackAlignment(NewAlign)) + DL.exceedsNaturalStackAlignment(llvm::Align(NewAlign))) NewAlign /= 2; if (NewAlign > Align) { @@ -804,7 +804,7 @@ const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->needsStackRealignment(MF)) while (NewAlign > Align && - DL.exceedsNaturalStackAlignment(NewAlign)) + DL.exceedsNaturalStackAlignment(llvm::Align(NewAlign))) NewAlign /= 2; if (NewAlign > Align) { 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 @@ -5804,7 +5804,7 @@ const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); if (!TRI->needsStackRealignment(MF)) while (NewAlign > Align && - DL.exceedsNaturalStackAlignment(NewAlign)) + DL.exceedsNaturalStackAlignment(llvm::Align(NewAlign))) NewAlign /= 2; if (NewAlign > Align) { diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -182,9 +182,9 @@ LayoutMap = nullptr; BigEndian = false; AllocaAddrSpace = 0; - StackNaturalAlign = 0; + StackNaturalAlign.reset(); ProgramAddrSpace = 0; - FunctionPtrAlign = 0; + FunctionPtrAlign.reset(); TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; ManglingMode = MM_None; NonIntegralAddressSpaces.clear(); @@ -378,7 +378,7 @@ } break; case 'S': { // Stack natural alignment. - StackNaturalAlign = inBytes(getInt(Tok)); + StackNaturalAlign = MaybeAlign(inBytes(getInt(Tok))); break; } case 'F': { @@ -394,7 +394,7 @@ "datalayout string"); } Tok = Tok.substr(1); - FunctionPtrAlign = inBytes(getInt(Tok)); + FunctionPtrAlign = MaybeAlign(inBytes(getInt(Tok))); break; } case 'P': { // Function address space. diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -685,11 +685,13 @@ unsigned Align = 0; if (auto *GO = dyn_cast(this)) { if (isa(GO)) { + MaybeAlign FunctionPtrAlign = DL.getFunctionPtrAlign(); + unsigned Align = FunctionPtrAlign ? FunctionPtrAlign->value() : 0; switch (DL.getFunctionPtrAlignType()) { case DataLayout::FunctionPtrAlignType::Independent: - return DL.getFunctionPtrAlign(); + return Align; case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign: - return std::max(DL.getFunctionPtrAlign(), GO->getAlignment()); + return std::max(Align, GO->getAlignment()); } } Align = GO->getAlignment(); diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1152,7 +1152,7 @@ // If the preferred alignment is greater than the natural stack alignment // then don't round up. This avoids dynamic stack realignment. - if (DL.exceedsNaturalStackAlignment(PrefAlign)) + if (DL.exceedsNaturalStackAlignment(llvm::Align(PrefAlign))) return Align; AI->setAlignment(PrefAlign); return PrefAlign; diff --git a/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp --- a/llvm/unittests/IR/DataLayoutTest.cpp +++ b/llvm/unittests/IR/DataLayoutTest.cpp @@ -14,15 +14,15 @@ namespace { TEST(DataLayoutTest, FunctionPtrAlign) { - EXPECT_EQ(0U, DataLayout("").getFunctionPtrAlign()); - EXPECT_EQ(1U, DataLayout("Fi8").getFunctionPtrAlign()); - EXPECT_EQ(2U, DataLayout("Fi16").getFunctionPtrAlign()); - EXPECT_EQ(4U, DataLayout("Fi32").getFunctionPtrAlign()); - EXPECT_EQ(8U, DataLayout("Fi64").getFunctionPtrAlign()); - EXPECT_EQ(1U, DataLayout("Fn8").getFunctionPtrAlign()); - EXPECT_EQ(2U, DataLayout("Fn16").getFunctionPtrAlign()); - EXPECT_EQ(4U, DataLayout("Fn32").getFunctionPtrAlign()); - EXPECT_EQ(8U, DataLayout("Fn64").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign()); + EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign()); EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \ DataLayout("").getFunctionPtrAlignType()); EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \