[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

llvm-svn: 367817
This commit is contained in:
Guillaume Chatelet 2019-08-05 09:00:43 +00:00
parent 3046ef5c11
commit 65e4b47aad
7 changed files with 28 additions and 25 deletions

View File

@ -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 <cassert>
#include <cstdint>
#include <string>
@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -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.

View File

@ -681,11 +681,13 @@ unsigned Value::getPointerAlignment(const DataLayout &DL) const {
unsigned Align = 0;
if (auto *GO = dyn_cast<GlobalObject>(this)) {
if (isa<Function>(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();

View File

@ -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;

View File

@ -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, \