[PATCH] D143602: Allow 32-bit pointers to be written in 64-bit slots

David M. Lloyd via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 8 13:09:11 PST 2023


dmlloyd created this revision.
dmlloyd added reviewers: arsenm, smeenai.
Herald added subscribers: pmatos, asb, pengfei, jgravelle-google, sbc100, dschuff.
Herald added a project: All.
dmlloyd requested review of this revision.
Herald added subscribers: llvm-commits, aheejin, wdng.
Herald added a project: LLVM.

Pointer lowering will presently happily allow the assembler to truncate a pointer in order to fit it into a narrower slot. The assembler is also capable of extending a pointer into a larger slot, and this is needed for languages like Java which may initialize a 64-bit integer field with a 32-bit pointer value on the initial heap.

This change builds on D61325 <https://reviews.llvm.org/D61325> to allow the assembler to also extend pointers into larger integer slots.

Fixes #60453.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143602

Files:
  lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  test/CodeGen/WebAssembly/ptrtoint-widen.ll
  test/CodeGen/X86/ptrtoint-widen.ll


Index: test/CodeGen/X86/ptrtoint-widen.ll
===================================================================
--- /dev/null
+++ test/CodeGen/X86/ptrtoint-widen.ll
@@ -0,0 +1,6 @@
+; RUN: llc < %s -mtriple=i686-unknown-linux-gnu | FileCheck %s
+
+ at ptr = external global i8, align 1
+ at ref = constant i64 ptrtoint (ptr @ptr to i64), align 8
+
+; CHECK: .quad  ptr{{$}}
Index: test/CodeGen/WebAssembly/ptrtoint-widen.ll
===================================================================
--- /dev/null
+++ test/CodeGen/WebAssembly/ptrtoint-widen.ll
@@ -0,0 +1,6 @@
+; RUN: llc < %s -mtriple=wasm32-unknown-unknown | FileCheck %s
+
+ at ptr = external global i8, align 1
+ at ref = constant i64 ptrtoint (ptr @ptr to i64), align 8
+
+; CHECK: .int64  ptr{{$}}
Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2975,27 +2975,16 @@
     return lowerConstant(Op);
   }
 
-  case Instruction::PtrToInt: {
-    const DataLayout &DL = getDataLayout();
-
+  case Instruction::PtrToInt:
     // Support only foldable casts to/from pointers that can be eliminated by
     // changing the pointer to the appropriately sized integer type.
-    Constant *Op = CE->getOperand(0);
-    Type *Ty = CE->getType();
-
-    const MCExpr *OpExpr = lowerConstant(Op);
-
+    //
     // We can emit the pointer value into this slot if the slot is an
-    // integer slot equal to the size of the pointer.
+    // integer slot.
     //
-    // 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).getFixedValue() <=
-        DL.getTypeAllocSize(Op->getType()).getFixedValue())
-      return OpExpr;
-
-    break; // Error
-  }
+    // If the pointer is larger or smaller than the resultant integer, then
+    // just depend on the assembler to truncate or extend it.
+    return lowerConstant(CE->getOperand(0));
 
   case Instruction::Sub: {
     GlobalValue *LHSGV;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143602.495929.patch
Type: text/x-patch
Size: 2090 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230208/c441e0d8/attachment.bin>


More information about the llvm-commits mailing list