[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
Thu Feb 9 15:16:49 PST 2023


dmlloyd updated this revision to Diff 496254.
dmlloyd added a comment.

Updated to test more integer sizes per @arsenm.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143602/new/

https://reviews.llvm.org/D143602

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


Index: llvm/test/CodeGen/X86/ptrtoint-widen.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/ptrtoint-widen.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple=i686-unknown-linux-gnu | FileCheck %s
+
+ at ptr = external global i8, align 1
+ at wide = constant i64 ptrtoint (ptr @ptr to i64), align 8
+ at normal = constant i32 ptrtoint (ptr @ptr to i32), align 4
+ at narrow = constant i16 ptrtoint (ptr @ptr to i16), align 2
+
+; CHECK: wide:
+; CHECK-NEXT: .quad ptr{{$}}
+; CHECK: normal:
+; CHECK-NEXT: .long ptr{{$}}
+; CHECK: narrow:
+; CHECK-NEXT: .short ptr{{$}}
+
Index: llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
===================================================================
--- llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
+++ /dev/null
@@ -1,10 +0,0 @@
-; RUN: not --crash llc < %s -mtriple=i386-linux 2>&1 | FileCheck %s
-
-; ptrtoint expressions that cast to a wider integer type are not supported.
-; A frontend can achieve a similar result by casting to the correct integer
-; type and explicitly zeroing any additional bytes.
-; { i32, i32 } { i32 ptrtoint (ptr @r to i32), i32 0 }
-
-; CHECK: LLVM ERROR: Unsupported expression in static initializer: ptrtoint (ptr @r to i64)
-
- at r = global i64 ptrtoint (ptr @r to i64)
Index: llvm/test/CodeGen/WebAssembly/ptrtoint-widen.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/ptrtoint-widen.ll
@@ -0,0 +1,16 @@
+; RUN: llc < %s -mtriple=wasm32-unknown-unknown | FileCheck %s
+
+ at ptr = external global i8, align 1
+ at wide = constant i64 ptrtoint (ptr @ptr to i64), align 8
+ at normal = constant i32 ptrtoint (ptr @ptr to i32), align 4
+ at narrow = constant i16 ptrtoint (ptr @ptr to i16), align 2
+
+; CHECK: wide:
+; CHECK-NEXT: .int64 ptr{{$}}
+; CHECK: normal:
+; CHECK-NEXT: .int32 ptr{{$}}
+; CHECK: narrow:
+; CHECK-NEXT: .int16 ptr{{$}}
+
+
+
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3007,25 +3007,15 @@
   }
 
   case Instruction::PtrToInt: {
-    const DataLayout &DL = getDataLayout();
-
     // 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: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143602.496254.patch
Type: text/x-patch
Size: 3212 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230209/33072abc/attachment.bin>


More information about the llvm-commits mailing list