[llvm] r361508 - [AsmPrinter] Treat a narrowing PtrToInt like Trunc

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Thu May 23 09:29:09 PDT 2019


Author: smeenai
Date: Thu May 23 09:29:09 2019
New Revision: 361508

URL: http://llvm.org/viewvc/llvm-project?rev=361508&view=rev
Log:
[AsmPrinter] Treat a narrowing PtrToInt like Trunc

When printing assembly for PtrToInt, AsmPrinter::lowerConstant
incorrectly assumed that if PtrToInt was not converting to an
int with exactly the same number of bits, it must be widening
to a larger int. But this isn't necessarily true; PtrToInt can
also shrink the size, which is useful when you want to produce
a known 32-bit pointer on a 64-bit platform (on x86_64 ELF
this yields a R_X86_64_32 relocation).

The old behavior of falling through to the widening case for a
narrowing PtrToInt yields bogus assembly code like this, which
fails to assemble because the no-op bit and it accidentally
creates is not a valid relocation:

```
        .long   a&-1
```

The fix is to treat a narrowing PtrToInt exactly the same as
it already treats Trunc: just emit the expression and let
the assembler deal with truncating it in the appropriate way.

Patch by Mat Hostetter <mjh at fb.com>.

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

Added:
    llvm/trunk/test/CodeGen/X86/ptrtoint-narrow.ll
Modified:
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=361508&r1=361507&r2=361508&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Thu May 23 09:29:09 2019
@@ -2231,7 +2231,10 @@ const MCExpr *AsmPrinter::lowerConstant(
 
     // We can emit the pointer value into this slot if the slot is an
     // integer slot equal to the size of the pointer.
-    if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
+    //
+    // If the pointer is larger than the resultant integer, then
+    // as with Trunc just depend on the assembler to truncate it.
+    if (DL.getTypeAllocSize(Ty) <= DL.getTypeAllocSize(Op->getType()))
       return OpExpr;
 
     // Otherwise the pointer is smaller than the resultant integer, mask off

Added: llvm/trunk/test/CodeGen/X86/ptrtoint-narrow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/ptrtoint-narrow.ll?rev=361508&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/ptrtoint-narrow.ll (added)
+++ llvm/trunk/test/CodeGen/X86/ptrtoint-narrow.ll Thu May 23 09:29:09 2019
@@ -0,0 +1,6 @@
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
+
+ at ptr = external global i8, align 1
+ at ref = constant i32 ptrtoint (i8* @ptr to i32), align 4
+
+; CHECK: .long  ptr{{$}}




More information about the llvm-commits mailing list