[PATCH] D46110: [IR] Do not assume that function pointers are aligned

Mikhail Maltsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 26 02:43:40 PDT 2018


miyuki created this revision.
miyuki added a reviewer: eli.friedman.
Herald added a reviewer: javed.absar.
Herald added a subscriber: kristof.beyls.

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.


https://reviews.llvm.org/D46110

Files:
  lib/IR/Value.cpp
  test/Analysis/ValueTracking/func-ptr-lsb.ll


Index: test/Analysis/ValueTracking/func-ptr-lsb.ll
===================================================================
--- /dev/null
+++ test/Analysis/ValueTracking/func-ptr-lsb.ll
@@ -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
+}
Index: lib/IR/Value.cpp
===================================================================
--- lib/IR/Value.cpp
+++ lib/IR/Value.cpp
@@ -685,6 +685,8 @@
 
   unsigned Align = 0;
   if (auto *GO = dyn_cast<GlobalObject>(this)) {
+    if (isa<Function>(GO))
+      return 0;
     Align = GO->getAlignment();
     if (Align == 0) {
       if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46110.144085.patch
Type: text/x-patch
Size: 1072 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180426/31a64a29/attachment.bin>


More information about the llvm-commits mailing list