From: Shoaib Meenai Date: Thu, 23 May 2019 16:29:09 +0000 (+0000) Subject: [AsmPrinter] Treat a narrowing PtrToInt like Trunc X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c76c82bdda6d8fd37d3816d1dcac8c474cbeb976;p=llvm [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 . Differential Revision: https://reviews.llvm.org/D61325 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361508 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index b57eac3d72e..bf7776b1dc0 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2231,7 +2231,10 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) { // 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 diff --git a/test/CodeGen/X86/ptrtoint-narrow.ll b/test/CodeGen/X86/ptrtoint-narrow.ll new file mode 100644 index 00000000000..c34e7b1ec51 --- /dev/null +++ b/test/CodeGen/X86/ptrtoint-narrow.ll @@ -0,0 +1,6 @@ +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s + +@ptr = external global i8, align 1 +@ref = constant i32 ptrtoint (i8* @ptr to i32), align 4 + +; CHECK: .long ptr{{$}}