aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
diff options
context:
space:
mode:
authorMichael Platings <michael.platings@arm.com>2019-03-08 10:44:06 +0000
committerMichael Platings <michael.platings@arm.com>2019-03-08 10:44:06 +0000
commit308e82ecebeef1342004637db9fdf11567a299b3 (patch)
treee1e40cd61cad2e514bc88d888424644ec1aac891 /llvm/lib/IR/ConstantFold.cpp
parent93110c2fe46c080bb016f5d9e8d9554b2d787ad7 (diff)
downloadllvm-308e82ecebeef1342004637db9fdf11567a299b3.zip
llvm-308e82ecebeef1342004637db9fdf11567a299b3.tar.gz
llvm-308e82ecebeef1342004637db9fdf11567a299b3.tar.bz2
[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/IR/ConstantFold.cpp')
-rw-r--r--llvm/lib/IR/ConstantFold.cpp28
1 files changed, 24 insertions, 4 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 037f120..3784405 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();