[IR] Do not assume that function pointers are aligned
authorMikhail Maltsev <mikhail.maltsev@arm.com>
Fri, 27 Apr 2018 09:12:12 +0000 (09:12 +0000)
committerMikhail Maltsev <mikhail.maltsev@arm.com>
Fri, 27 Apr 2018 09:12:12 +0000 (09:12 +0000)
Summary:
The value tracking analysis uses function alignment to infer that the
least significant bits of function pointers are known to be zero.
Unfortunately, this is not correct for ARM targets: the least
significant bit of a function pointer stores the ARM/Thumb state
information (i.e., the LSB is set for Thumb functions and cleared for
ARM functions).

The original approach (https://reviews.llvm.org/D44781) introduced a
new field for function pointer alignment in the DataLayout structure
to address this. But it seems unlikely that optimizations based on
function pointer alignment would bring much benefit in practice to
justify the additional maintenance burden, so this patch simply
assumes that function pointer alignment is always unknown.

Reviewers: javed.absar, efriedma

Reviewed By: efriedma

Subscribers: kristof.beyls, llvm-commits, hfinkel, rogfer01

Differential Revision: https://reviews.llvm.org/D46110

llvm-svn: 331025

llvm/lib/IR/Value.cpp
llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll [new file with mode: 0644]

index 8c6658f..a479d04 100644 (file)
@@ -685,6 +685,10 @@ 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;
     Align = GO->getAlignment();
     if (Align == 0) {
       if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
diff --git a/llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll b/llvm/test/Analysis/ValueTracking/func-ptr-lsb.ll
new file mode 100644 (file)
index 0000000..0162f00
--- /dev/null
@@ -0,0 +1,18 @@
+; RUN: opt -instcombine -S < %s | FileCheck %s
+
+target datalayout = "e-p:32:32-n32-S64"
+
+; CHECK-LABEL: @foo_ptr
+; CHECK: and
+define i32 @foo_ptr() {
+entry:
+  ; Even though the address of @foo is aligned, we cannot assume that the
+  ; pointer has the same alignment. This is not true for e.g. ARM targets
+  ; which store ARM/Thumb state in the LSB
+  ret i32 and (i32 ptrtoint (void ()* @foo to i32), i32 -4)
+}
+
+define internal void @foo() align 16 {
+entry:
+  ret void
+}