From f76a78bfee3b4125e6db9361ffbd7848e77252f6 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Mon, 5 Aug 2019 09:00:43 +0000 Subject: [PATCH] [LLVM][Alignment] Introduce Alignment Type in DataLayout Summary: This is patch is part of a serie 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, jfb, jakehehrlich Subscribers: hiraditya, dexonsmith, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D65521 Make getFunctionPtrAlign() return MaybeAlign git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367817 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/DataLayout.h | 13 +++++++------ lib/CodeGen/GlobalISel/CombinerHelper.cpp | 4 ++-- lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 2 +- lib/IR/DataLayout.cpp | 8 ++++---- lib/IR/Value.cpp | 6 ++++-- lib/Transforms/Utils/Local.cpp | 2 +- unittests/IR/DataLayoutTest.cpp | 18 +++++++++--------- 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/include/llvm/IR/DataLayout.h b/include/llvm/IR/DataLayout.h index ac9770a1512..175e779fc98 100644 --- a/include/llvm/IR/DataLayout.h +++ b/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 @@ private: bool BigEndian; unsigned AllocaAddrSpace; - unsigned StackNaturalAlign; + MaybeAlign StackNaturalAlign; unsigned ProgramAddrSpace; - unsigned FunctionPtrAlign; + MaybeAlign FunctionPtrAlign; FunctionPtrAlignType TheFunctionPtrAlignType; enum ManglingModeT { @@ -261,17 +262,17 @@ public: 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/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/lib/CodeGen/GlobalISel/CombinerHelper.cpp index 1ae454f4bd4..373d52fd1d4 100644 --- a/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -698,7 +698,7 @@ bool CombinerHelper::optimizeMemcpy(MachineInstr &MI, Register Dst, 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 @@ bool CombinerHelper::optimizeMemmove(MachineInstr &MI, Register Dst, 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/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 40f97eeeb79..9594dedb44b 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -5804,7 +5804,7 @@ static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, 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/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index 6e0ebbd4a73..ab90388fae3 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -182,9 +182,9 @@ void DataLayout::reset(StringRef Desc) { 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 @@ void DataLayout::parseSpecifier(StringRef Desc) { } break; case 'S': { // Stack natural alignment. - StackNaturalAlign = inBytes(getInt(Tok)); + StackNaturalAlign = MaybeAlign(inBytes(getInt(Tok))); break; } case 'F': { @@ -394,7 +394,7 @@ void DataLayout::parseSpecifier(StringRef Desc) { "datalayout string"); } Tok = Tok.substr(1); - FunctionPtrAlign = inBytes(getInt(Tok)); + FunctionPtrAlign = MaybeAlign(inBytes(getInt(Tok))); break; } case 'P': { // Function address space. diff --git a/lib/IR/Value.cpp b/lib/IR/Value.cpp index 763bc2099ef..b3dcaee766c 100644 --- a/lib/IR/Value.cpp +++ b/lib/IR/Value.cpp @@ -681,11 +681,13 @@ unsigned Value::getPointerAlignment(const DataLayout &DL) const { 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/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 725bc70c9ef..68443c15a78 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -1152,7 +1152,7 @@ static unsigned enforceKnownAlignment(Value *V, unsigned Align, // 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/unittests/IR/DataLayoutTest.cpp b/unittests/IR/DataLayoutTest.cpp index e7ed70b7de5..e24e8e045db 100644 --- a/unittests/IR/DataLayoutTest.cpp +++ b/unittests/IR/DataLayoutTest.cpp @@ -14,15 +14,15 @@ using namespace llvm; 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, \ -- 2.11.4.GIT