Skip to content

Commit 89c5753

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
2 parents fc2850f + a0dcd64 commit 89c5753

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+772
-92
lines changed

llvm/docs/LangRef.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12521,6 +12521,59 @@ Example:
1252112521
%Y = ptrtoint ptr %P to i64 ; yields zero extension on 32-bit architecture
1252212522
%Z = ptrtoint <4 x ptr> %P to <4 x i64>; yields vector zero extension for a vector of addresses on 32-bit architecture
1252312523

12524+
.. _i_ptrtoaddr:
12525+
12526+
'``ptrtoaddr .. to``' Instruction
12527+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12528+
12529+
Syntax:
12530+
"""""""
12531+
12532+
::
12533+
12534+
<result> = ptrtoaddr <ty> <value> to <ty2> ; yields ty2
12535+
12536+
Overview:
12537+
"""""""""
12538+
12539+
The '``ptrtoaddr``' instruction converts the pointer or a vector of
12540+
pointers ``value`` to the underlying integer address (or vector of integers) of
12541+
type ``ty2``. This is different from :ref:`ptrtoint <i_ptrtoint>` in that it
12542+
only operates on the index bits of the pointer and ignores all other bits.
12543+
``ty2`` must be the integer type (or vector of integers) matching the pointer
12544+
index width of the address space of ``ty``.
12545+
12546+
Arguments:
12547+
""""""""""
12548+
12549+
The '``ptrtoaddr``' instruction takes a ``value`` to cast, which must be
12550+
a value of type :ref:`pointer <t_pointer>` or a vector of pointers, and a
12551+
type to cast it to ``ty2``, which must be an :ref:`integer <t_integer>` or
12552+
a vector of integers type.
12553+
12554+
Semantics:
12555+
""""""""""
12556+
12557+
The '``ptrtoaddr``' instruction converts ``value`` to integer type ``ty2`` by
12558+
interpreting the lowest index-width pointer representation bits as an integer.
12559+
If the address size and the pointer representation size are the same and
12560+
``value`` and ``ty2`` are the same size, then nothing is done (*no-op cast*)
12561+
other than a type change.
12562+
12563+
The ``ptrtoaddr`` instruction always :ref:`captures the address but not the provenance <pointercapture>`
12564+
of the pointer argument.
12565+
12566+
Example:
12567+
""""""""
12568+
This example assumes pointers in address space 1 are 64 bits in size with an
12569+
address width of 32 bits (``p1:64:64:64:32`` :ref:`datalayout string<langref_datalayout>`)
12570+
.. code-block:: llvm
12571+
12572+
%X = ptrtoaddr ptr addrspace(1) %P to i8 ; extracts low 32 bits and truncates
12573+
%Y = ptrtoaddr ptr addrspace(1) %P to i64 ; extracts low 32 bits and zero extends
12574+
%Z = ptrtoaddr <4 x ptr addrspace(1)> %P to <4 x i64>; yields vector zero extension of low 32 bits for each pointer
12575+
12576+
1252412577
.. _i_inttoptr:
1252512578

1252612579
'``inttoptr .. to``' Instruction

llvm/docs/ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Makes programs 10x faster by doing Special New Thing.
5656
Changes to the LLVM IR
5757
----------------------
5858

59+
* The `ptrtoaddr` instruction was introduced. This instruction returns the
60+
address component of a pointer type variable but unlike `ptrtoint` does not
61+
capture provenance ([#125687](https://github.com/llvm/llvm-project/pull/125687)).
62+
5963
Changes to LLVM infrastructure
6064
------------------------------
6165

llvm/include/llvm-c/Core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ typedef enum {
111111
LLVMFPTrunc = 37,
112112
LLVMFPExt = 38,
113113
LLVMPtrToInt = 39,
114+
LLVMPtrToAddr = 69,
114115
LLVMIntToPtr = 40,
115116
LLVMBitCast = 41,
116117
LLVMAddrSpaceCast = 60,

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ class TargetTransformInfoImplBase {
731731
return 0;
732732
break;
733733
}
734+
case Instruction::PtrToAddr: {
735+
unsigned DstSize = Dst->getScalarSizeInBits();
736+
if (DL.isLegalInteger(DstSize) && DstSize >= DL.getAddressSizeInBits(Src))
737+
return 0;
738+
break;
739+
}
734740
case Instruction::PtrToInt: {
735741
unsigned DstSize = Dst->getScalarSizeInBits();
736742
if (DL.isLegalInteger(DstSize) &&
@@ -1437,6 +1443,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
14371443
Op2Info, Operands, I);
14381444
}
14391445
case Instruction::IntToPtr:
1446+
case Instruction::PtrToAddr:
14401447
case Instruction::PtrToInt:
14411448
case Instruction::SIToFP:
14421449
case Instruction::UIToFP:

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ enum Kind {
319319
kw_fptoui,
320320
kw_fptosi,
321321
kw_inttoptr,
322+
kw_ptrtoaddr,
322323
kw_ptrtoint,
323324
kw_bitcast,
324325
kw_addrspacecast,

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ enum CastOpcodes {
456456
CAST_PTRTOINT = 9,
457457
CAST_INTTOPTR = 10,
458458
CAST_BITCAST = 11,
459-
CAST_ADDRSPACECAST = 12
459+
CAST_ADDRSPACECAST = 12,
460+
CAST_PTRTOADDR = 13,
460461
};
461462

462463
/// UnaryOpcodes - These are values used in the bitcode files to encode which

llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ class IRTranslator : public MachineFunctionPass {
486486
bool translatePtrToInt(const User &U, MachineIRBuilder &MIRBuilder) {
487487
return translateCast(TargetOpcode::G_PTRTOINT, U, MIRBuilder);
488488
}
489+
bool translatePtrToAddr(const User &U, MachineIRBuilder &MIRBuilder) {
490+
// FIXME: this is not correct for pointers with addr width != pointer width
491+
return translatePtrToInt(U, MIRBuilder);
492+
}
489493
bool translateTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
490494
return translateCast(TargetOpcode::G_TRUNC, U, MIRBuilder);
491495
}

llvm/include/llvm/IR/Constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,8 @@ class ConstantExpr : public Constant {
11581158
LLVM_ABI static Constant *getXor(Constant *C1, Constant *C2);
11591159
LLVM_ABI static Constant *getTrunc(Constant *C, Type *Ty,
11601160
bool OnlyIfReduced = false);
1161+
LLVM_ABI static Constant *getPtrToAddr(Constant *C, Type *Ty,
1162+
bool OnlyIfReduced = false);
11611163
LLVM_ABI static Constant *getPtrToInt(Constant *C, Type *Ty,
11621164
bool OnlyIfReduced = false);
11631165
LLVM_ABI static Constant *getIntToPtr(Constant *C, Type *Ty,

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,10 @@ class IRBuilderBase {
21922192
return CreateCast(Instruction::FPExt, V, DestTy, Name, FPMathTag,
21932193
FMFSource);
21942194
}
2195-
2195+
Value *CreatePtrToAddr(Value *V, const Twine &Name = "") {
2196+
return CreateCast(Instruction::PtrToInt, V,
2197+
BB->getDataLayout().getAddressType(V->getType()), Name);
2198+
}
21962199
Value *CreatePtrToInt(Value *V, Type *DestTy,
21972200
const Twine &Name = "") {
21982201
return CreateCast(Instruction::PtrToInt, V, DestTy, Name);

llvm/include/llvm/IR/InstVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class InstVisitor {
183183
RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst);}
184184
RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst);}
185185
RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);}
186+
RetTy visitPtrToAddrInst(PtrToAddrInst &I) { DELEGATE(CastInst);}
186187
RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
187188
RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
188189
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}

0 commit comments

Comments
 (0)