Index: llvm/docs/LangRef.rst =================================================================== --- llvm/docs/LangRef.rst +++ llvm/docs/LangRef.rst @@ -2003,6 +2003,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: llvm/include/llvm/IR/DataLayout.h =================================================================== --- llvm/include/llvm/IR/DataLayout.h +++ llvm/include/llvm/IR/DataLayout.h @@ -114,6 +114,7 @@ unsigned AllocaAddrSpace; unsigned StackNaturalAlign; + unsigned FunctionPtrAlign; unsigned ProgramAddrSpace; enum ManglingModeT { @@ -199,6 +200,7 @@ BigEndian = DL.isBigEndian(); AllocaAddrSpace = DL.AllocaAddrSpace; StackNaturalAlign = DL.StackNaturalAlign; + FunctionPtrAlign = DL.FunctionPtrAlign; ProgramAddrSpace = DL.ProgramAddrSpace; ManglingMode = DL.ManglingMode; LegalIntWidths = DL.LegalIntWidths; @@ -254,6 +256,7 @@ } unsigned getStackAlignment() const { return StackNaturalAlign; } + unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; } unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } Index: llvm/lib/IR/ConstantFold.cpp =================================================================== --- llvm/lib/IR/ConstantFold.cpp +++ 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,14 @@ isa(CE1->getOperand(0))) { GlobalValue *GV = cast(CE1->getOperand(0)); - // Functions are at least 4-byte aligned. - unsigned GVAlign = GV->getAlignment(); - if (isa(GV)) - GVAlign = std::max(GVAlign, 4U); + unsigned GVAlign = + GV->getParent() + ? GV->getPointerAlignment(GV->getParent()->getDataLayout()) + : 0U; + + // If the function alignment is not specified then assume that it is 4 + if (GVAlign == 0U && isa(GV)) + GVAlign = 4U; if (GVAlign > 1) { unsigned DstWidth = CI2->getType()->getBitWidth(); Index: llvm/lib/IR/DataLayout.cpp =================================================================== --- llvm/lib/IR/DataLayout.cpp +++ llvm/lib/IR/DataLayout.cpp @@ -183,6 +183,7 @@ BigEndian = false; AllocaAddrSpace = 0; StackNaturalAlign = 0; + FunctionPtrAlign = 0; ProgramAddrSpace = 0; ManglingMode = MM_None; NonIntegralAddressSpaces.clear(); @@ -379,6 +380,10 @@ StackNaturalAlign = inBytes(getInt(Tok)); break; } + case 'F': { + FunctionPtrAlign = inBytes(getInt(Tok)); + break; + } case 'P': { // Function address space. ProgramAddrSpace = getAddrSpace(Tok); break; @@ -431,6 +436,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: llvm/lib/IR/Value.cpp =================================================================== --- llvm/lib/IR/Value.cpp +++ llvm/lib/IR/Value.cpp @@ -647,11 +647,10 @@ unsigned Align = 0; if (auto *GO = dyn_cast(this)) { - // Don't make any assumptions about function pointer alignment. Some - // targets use the LSBs to store additional information. if (isa(GO)) - return 0; - Align = GO->getAlignment(); + Align = DL.getFunctionPtrAlign(); + if (Align == 0) + Align = GO->getAlignment(); if (Align == 0) { if (auto *GVar = dyn_cast(GO)) { Type *ObjectType = GVar->getValueType(); Index: llvm/lib/Target/ARM/ARMTargetMachine.cpp =================================================================== --- llvm/lib/Target/ARM/ARMTargetMachine.cpp +++ llvm/lib/Target/ARM/ARMTargetMachine.cpp @@ -141,6 +141,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: llvm/test/Analysis/ConstantFolding/func-and-folding.ll =================================================================== --- /dev/null +++ llvm/test/Analysis/ConstantFolding/func-and-folding.ll @@ -0,0 +1,43 @@ +; RUN: opt < %s -constprop -S -o - | FileCheck %s + +; Function Attrs: minsize norecurse nounwind optsize readnone +define void @align1() align 1 { +entry: + ret void +} + +; Function Attrs: minsize norecurse nounwind optsize readnone +define void @align4() align 4 { +entry: + ret void +} + +; Function Attrs: minsize norecurse nounwind optsize readnone +define void @alignunspecified() { +entry: + ret void +} + +; Function Attrs: minsize nounwind optsize +define i32 @main() local_unnamed_addr { +entry: +; We expect that this line won't be optimised as we know that the address of +; align1 may be any number. +; CHECK: ptrtoint + %call = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @align1 to i32), i32 2)) + +; We expect that this line will be optimised as we know that the alignment of +; align4 is 4 and therefore the ANDing the address of align4 with 2 is 0 +; CHECK-NOT: ptrtoint + %call2 = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @align4 to i32), i32 2)) + +; We expect that this line will be optimised because if the alignment is +; unspecified then we assume it to be 4. +; CHECK-NOT: ptrtoint + %call3 = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @alignunspecified to i32), i32 2)) + + ret i32 0 +} + +; Function Attrs: minsize optsize +declare i32 @process(...) local_unnamed_addr Index: llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll =================================================================== --- llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll +++ llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll @@ -1,6 +1,6 @@ ; RUN: opt -instcombine -S < %s | FileCheck %s -target datalayout = "e-p:32:32-n32-S64" +target datalayout = "e-p:32:32-n32-S64-F8" ; CHECK-LABEL: @foo_ptr ; CHECK: and Index: llvm/test/Assembler/2004-03-07-FunctionAddressAlignment.ll =================================================================== --- llvm/test/Assembler/2004-03-07-FunctionAddressAlignment.ll +++ /dev/null @@ -1,16 +0,0 @@ -; RUN: llvm-as < %s | llvm-dis | not grep ptrtoint -; RUN: verify-uselistorder %s -; All of these should be eliminable - - -define i32 @foo() { - ret i32 and (i32 ptrtoint (i32()* @foo to i32), i32 1) -} - -define i32 @foo2() { - ret i32 and (i32 1, i32 ptrtoint (i32()* @foo2 to i32)) -} - -define i1 @foo3() { - ret i1 icmp ne (i1()* @foo3, i1()* null) -}