Index: docs/LangRef.rst =================================================================== --- docs/LangRef.rst +++ docs/LangRef.rst @@ -1945,6 +1945,8 @@ targets. ``a::`` This specifies the alignment for an object of aggregate type. +``F`` + This specifies the alignment for function pointers. ``m:`` If present, specifies that llvm names are mangled in the output. Symbols prefixed with the mangling escape character ``\01`` are passed through Index: include/llvm/IR/DataLayout.h =================================================================== --- include/llvm/IR/DataLayout.h +++ include/llvm/IR/DataLayout.h @@ -115,6 +115,7 @@ unsigned AllocaAddrSpace; unsigned StackNaturalAlign; + unsigned FunctionPtrAlign; unsigned ProgramAddrSpace; enum ManglingModeT { @@ -200,6 +201,7 @@ BigEndian = DL.isBigEndian(); AllocaAddrSpace = DL.AllocaAddrSpace; StackNaturalAlign = DL.StackNaturalAlign; + FunctionPtrAlign = DL.FunctionPtrAlign; ProgramAddrSpace = DL.ProgramAddrSpace; ManglingMode = DL.ManglingMode; LegalIntWidths = DL.LegalIntWidths; @@ -255,6 +257,7 @@ } unsigned getStackAlignment() const { return StackNaturalAlign; } + unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; } unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } Index: lib/IR/DataLayout.cpp =================================================================== --- lib/IR/DataLayout.cpp +++ lib/IR/DataLayout.cpp @@ -184,6 +184,7 @@ BigEndian = false; AllocaAddrSpace = 0; StackNaturalAlign = 0; + FunctionPtrAlign = 0; ProgramAddrSpace = 0; ManglingMode = MM_None; NonIntegralAddressSpaces.clear(); @@ -380,6 +381,10 @@ StackNaturalAlign = inBytes(getInt(Tok)); break; } + case 'F': { + FunctionPtrAlign = inBytes(getInt(Tok)); + break; + } case 'P': { // Function address space. ProgramAddrSpace = getAddrSpace(Tok); break; @@ -432,6 +437,7 @@ bool Ret = BigEndian == Other.BigEndian && AllocaAddrSpace == Other.AllocaAddrSpace && StackNaturalAlign == Other.StackNaturalAlign && + FunctionPtrAlign == Other.FunctionPtrAlign && ProgramAddrSpace == Other.ProgramAddrSpace && ManglingMode == Other.ManglingMode && LegalIntWidths == Other.LegalIntWidths && Index: lib/IR/Value.cpp =================================================================== --- lib/IR/Value.cpp +++ lib/IR/Value.cpp @@ -685,7 +685,10 @@ unsigned Align = 0; if (auto *GO = dyn_cast(this)) { - Align = GO->getAlignment(); + if (isa(GO)) + Align = DL.getFunctionPtrAlign(); + if (Align == 0) + Align = GO->getAlignment(); if (Align == 0) { if (auto *GVar = dyn_cast(GO)) { Type *ObjectType = GVar->getValueType(); Index: lib/Target/ARM/ARMTargetMachine.cpp =================================================================== --- lib/Target/ARM/ARMTargetMachine.cpp +++ lib/Target/ARM/ARMTargetMachine.cpp @@ -140,6 +140,10 @@ // 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 += "-F8"; + // ABIs other than APCS have 64 bit integers with natural alignment. if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS) Ret += "-i64:64"; Index: test/Analysis/ValueTracking/func-ptr-lsb.ll =================================================================== --- /dev/null +++ test/Analysis/ValueTracking/func-ptr-lsb.ll @@ -0,0 +1,15 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +target datalayout = "e-p:32:32-n32-S64-F8" + +; CHECK-LABEL: @foo_ptr +; CHECK: and +define i32 @foo_ptr() { +entry: + ret i32 and (i32 ptrtoint (void ()* @foo to i32), i32 -4) +} + +define internal void @foo() align 16 { +entry: + ret void +}