diff options
author | Michael Platings <michael.platings@arm.com> | 2019-03-08 10:44:06 +0000 |
---|---|---|
committer | Michael Platings <michael.platings@arm.com> | 2019-03-08 10:44:06 +0000 |
commit | 308e82ecebeef1342004637db9fdf11567a299b3 (patch) | |
tree | e1e40cd61cad2e514bc88d888424644ec1aac891 /llvm/lib | |
parent | 93110c2fe46c080bb016f5d9e8d9554b2d787ad7 (diff) | |
download | bcm5719-llvm-308e82ecebeef1342004637db9fdf11567a299b3.tar.gz bcm5719-llvm-308e82ecebeef1342004637db9fdf11567a299b3.zip |
[IR][ARM] Add function pointer alignment to datalayout
Use this feature to fix a bug on ARM where 4 byte alignment is
incorrectly assumed.
Differential Revision: https://reviews.llvm.org/D57335
llvm-svn: 355685
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 28 | ||||
-rw-r--r-- | llvm/lib/IR/DataLayout.cpp | 20 | ||||
-rw-r--r-- | llvm/lib/IR/Value.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/Target/ARM/ARMTargetMachine.cpp | 4 |
4 files changed, 56 insertions, 8 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 037f120f47d..3784405ecb0 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -26,6 +26,7 @@ #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/PatternMatch.h" #include "llvm/Support/ErrorHandling.h" @@ -1076,10 +1077,29 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, isa<GlobalValue>(CE1->getOperand(0))) { GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); - // Functions are at least 4-byte aligned. - unsigned GVAlign = GV->getAlignment(); - if (isa<Function>(GV)) - GVAlign = std::max(GVAlign, 4U); + unsigned GVAlign; + + if (Module *TheModule = GV->getParent()) { + GVAlign = GV->getPointerAlignment(TheModule->getDataLayout()); + + // If the function alignment is not specified then assume that it + // is 4. + // This is dangerous; on x86, the alignment of the pointer + // corresponds to the alignment of the function, but might be less + // than 4 if it isn't explicitly specified. + // However, a fix for this behaviour was reverted because it + // increased code size (see https://reviews.llvm.org/D55115) + // FIXME: This code should be deleted once existing targets have + // appropriate defaults + if (GVAlign == 0U && isa<Function>(GV)) + GVAlign = 4U; + } else if (isa<Function>(GV)) { + // Without a datalayout we have to assume the worst case: that the + // function pointer isn't aligned at all. + GVAlign = 0U; + } else { + GVAlign = GV->getAlignment(); + } if (GVAlign > 1) { unsigned DstWidth = CI2->getType()->getBitWidth(); diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp index 2644214a49c..943f5381c64 100644 --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -184,6 +184,8 @@ void DataLayout::reset(StringRef Desc) { AllocaAddrSpace = 0; StackNaturalAlign = 0; ProgramAddrSpace = 0; + FunctionPtrAlign = 0; + TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; ManglingMode = MM_None; NonIntegralAddressSpaces.clear(); @@ -379,6 +381,22 @@ void DataLayout::parseSpecifier(StringRef Desc) { StackNaturalAlign = inBytes(getInt(Tok)); break; } + case 'F': { + switch (Tok.front()) { + case 'i': + TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; + break; + case 'n': + TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign; + break; + default: + report_fatal_error("Unknown function pointer alignment type in " + "datalayout string"); + } + Tok = Tok.substr(1); + FunctionPtrAlign = inBytes(getInt(Tok)); + break; + } case 'P': { // Function address space. ProgramAddrSpace = getAddrSpace(Tok); break; @@ -432,6 +450,8 @@ bool DataLayout::operator==(const DataLayout &Other) const { AllocaAddrSpace == Other.AllocaAddrSpace && StackNaturalAlign == Other.StackNaturalAlign && ProgramAddrSpace == Other.ProgramAddrSpace && + FunctionPtrAlign == Other.FunctionPtrAlign && + TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType && ManglingMode == Other.ManglingMode && LegalIntWidths == Other.LegalIntWidths && Alignments == Other.Alignments && Pointers == Other.Pointers; diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index 38eed76fe45..cf32a66c901 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -648,10 +648,14 @@ unsigned Value::getPointerAlignment(const DataLayout &DL) const { unsigned Align = 0; if (auto *GO = dyn_cast<GlobalObject>(this)) { - // Don't make any assumptions about function pointer alignment. Some - // targets use the LSBs to store additional information. - if (isa<Function>(GO)) - return 0; + if (isa<Function>(GO)) { + switch (DL.getFunctionPtrAlignType()) { + case DataLayout::FunctionPtrAlignType::Independent: + return DL.getFunctionPtrAlign(); + case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign: + return std::max(DL.getFunctionPtrAlign(), GO->getAlignment()); + } + } Align = GO->getAlignment(); if (Align == 0) { if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.cpp b/llvm/lib/Target/ARM/ARMTargetMachine.cpp index 9954eee2e5f..401843c1e0f 100644 --- a/llvm/lib/Target/ARM/ARMTargetMachine.cpp +++ b/llvm/lib/Target/ARM/ARMTargetMachine.cpp @@ -141,6 +141,10 @@ static std::string computeDataLayout(const Triple &TT, StringRef CPU, // Pointers are 32 bits and aligned to 32 bits. Ret += "-p:32:32"; + // Function pointers are aligned to 8 bits (because the LSB stores the + // ARM/Thumb state). + Ret += "-Fi8"; + // ABIs other than APCS have 64 bit integers with natural alignment. if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS) Ret += "-i64:64"; |