Skip to content

Commit 73bc383

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
2 parents 2d2d753 + a4c4127 commit 73bc383

File tree

25 files changed

+402
-48
lines changed

25 files changed

+402
-48
lines changed

llvm/docs/LangRef.rst

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,14 +3147,21 @@ as follows:
31473147
``A<address space>``
31483148
Specifies the address space of objects created by '``alloca``'.
31493149
Defaults to the default address space of 0.
3150-
``p[n]:<size>:<abi>[:<pref>][:<idx>]``
3151-
This specifies the *size* of a pointer and its ``<abi>`` and
3152-
``<pref>``\erred alignments for address space ``n``.
3153-
The fourth parameter ``<idx>`` is the size of the
3154-
index that used for address calculation, which must be less than or equal
3155-
to the pointer size. If not
3156-
specified, the default index size is equal to the pointer size. All sizes
3157-
are in bits. The address space, ``n``, is optional, and if not specified,
3150+
``p[n]:<size>:<abi>[:<pref>[:<idx>[:<addr>]]]``
3151+
This specifies the properties of a pointer in address space ``n``.
3152+
The ``<size>`` parameter specifies the size of the bitwise representation.
3153+
For :ref:`non-integral pointers <nointptrtype>` the representation size may
3154+
be larger than the address width of the underlying address space (e.g. to
3155+
accommodate additional metadata).
3156+
The alignment requirements are specified via the ``<abi>`` and
3157+
``<pref>``\erred alignments parameters.
3158+
The fourth parameter ``<idx>`` is the size of the index that used for
3159+
address calculations such as :ref:`getelementptr <i_getelementptr>`.
3160+
It must be less than or equal to the pointer size. If not specified, the
3161+
default index size is equal to the pointer size.
3162+
The index size also specifies the width of addresses in this address space.
3163+
All sizes are in bits.
3164+
The address space, ``n``, is optional, and if not specified,
31583165
denotes the default address space 0. The value of ``n`` must be
31593166
in the range [1,2^24).
31603167
``i<size>:<abi>[:<pref>]``
@@ -4266,6 +4273,16 @@ address spaces defined in the :ref:`datalayout string<langref_datalayout>`.
42664273
the default globals address space and ``addrspace("P")`` the program address
42674274
space.
42684275

4276+
The representation of pointers can be different for each address space and does
4277+
not necessarily need to be a plain integer address (e.g. for
4278+
:ref:`non-integral pointers <nointptrtype>`). In addition to a representation
4279+
bits size, pointers in each address space also have an index size which defines
4280+
the bitwidth of indexing operations as well as the size of `integer addresses`
4281+
in this address space. For example, CHERI capabilities are twice the size of the
4282+
underlying addresses to accommodate for additional metadata such as bounds and
4283+
permissions: on a 32-bit system the bitwidth of the pointer representation size
4284+
is 64, but the underlying address width remains 32 bits.
4285+
42694286
The default address space is number zero.
42704287

42714288
The semantics of non-zero address spaces are target-specific. Memory
@@ -12396,12 +12413,15 @@ Semantics:
1239612413
""""""""""
1239712414

1239812415
The '``ptrtoint``' instruction converts ``value`` to integer type
12399-
``ty2`` by interpreting the pointer value as an integer and either
12400-
truncating or zero extending that value to the size of the integer type.
12416+
``ty2`` by interpreting the all pointer representation bits as an integer
12417+
(equivalent to a ``bitcast``) and either truncating or zero extending that value
12418+
to the size of the integer type.
1240112419
If ``value`` is smaller than ``ty2`` then a zero extension is done. If
1240212420
``value`` is larger than ``ty2`` then a truncation is done. If they are
1240312421
the same size, then nothing is done (*no-op cast*) other than a type
1240412422
change.
12423+
The ``ptrtoint`` always :ref:`captures address and provenance <pointercapture>`
12424+
of the pointer argument.
1240512425

1240612426
Example:
1240712427
""""""""
@@ -12412,6 +12432,61 @@ Example:
1241212432
%Y = ptrtoint ptr %P to i64 ; yields zero extension on 32-bit architecture
1241312433
%Z = ptrtoint <4 x ptr> %P to <4 x i64>; yields vector zero extension for a vector of addresses on 32-bit architecture
1241412434

12435+
.. _i_ptrtoaddr:
12436+
12437+
'``ptrtoaddr .. to``' Instruction
12438+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12439+
12440+
Syntax:
12441+
"""""""
12442+
12443+
::
12444+
12445+
<result> = ptrtoaddr <ty> <value> to <ty2> ; yields ty2
12446+
12447+
Overview:
12448+
"""""""""
12449+
12450+
The '``ptrtoaddr``' instruction converts the pointer or a vector of
12451+
pointers ``value`` to the underlying integer address (or vector of integers) of
12452+
type ``ty2``. This is different from :ref:`ptrtoint <i_ptrtoint>` in that it
12453+
only operates on the index bits of the pointer and ignores all other bits.
12454+
12455+
Arguments:
12456+
""""""""""
12457+
12458+
The '``ptrtoaddr``' instruction takes a ``value`` to cast, which must be
12459+
a value of type :ref:`pointer <t_pointer>` or a vector of pointers, and a
12460+
type to cast it to ``ty2``, which must be an :ref:`integer <t_integer>` or
12461+
a vector of integers type.
12462+
12463+
Semantics:
12464+
""""""""""
12465+
12466+
The '``ptrtoaddr``' instruction converts ``value`` to integer type
12467+
``ty2`` by interpreting the lowest index-width pointer representation bits as an
12468+
integer and either truncating or zero extending that value to the size of the
12469+
integer type.
12470+
If the address of ``value`` is smaller than ``ty2`` then a zero extension is
12471+
done. If the address of ``value`` is larger than ``ty2`` then a truncation is
12472+
done. If the address size and the pointer representation size are the same and
12473+
``value`` and ``ty2`` are the same size, then nothing is done (*no-op cast*)
12474+
other than a type change.
12475+
12476+
The ``ptrtoaddr`` always :ref:`captures the address (but not provenance) <pointercapture>`
12477+
of the pointer argument.
12478+
12479+
Example:
12480+
""""""""
12481+
This example assumes pointers in address space 1 are 64 bits in size with an
12482+
address width of 32 bits (``p1:64:64:64:32`` :ref:`datalayout string<langref_datalayout>`)
12483+
.. code-block:: llvm
12484+
12485+
%X = ptrtoaddr ptr addrspace(1) %P to i8 ; extracts low 32 bits and truncates
12486+
%Y = ptrtoaddr ptr addrspace(1) %P to i64 ; extracts low 32 bits and zero extends
12487+
%Z = ptrtoaddr <4 x ptr addrspace(1)> %P to <4 x i64>; yields vector zero extension of low 32 bits for each pointer
12488+
12489+
1241512490
.. _i_inttoptr:
1241612491

1241712492
'``inttoptr .. to``' Instruction
@@ -12456,6 +12531,9 @@ of the integer ``value``. If ``value`` is larger than the size of a
1245612531
pointer then a truncation is done. If ``value`` is smaller than the size
1245712532
of a pointer then a zero extension is done. If they are the same size,
1245812533
nothing is done (*no-op cast*).
12534+
The behavior is equivalent to a ``bitcast``, however, the resulting value is not
12535+
guaranteed to be dereferenceable (e.g. if the result type is a
12536+
:ref:`non-integral pointers <nointptrtype>`).
1245912537

1246012538
Example:
1246112539
""""""""

llvm/include/llvm-c/Core.h

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

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,13 @@ class TargetTransformInfoImplBase {
732732
return 0;
733733
break;
734734
}
735+
case Instruction::PtrToAddr: {
736+
unsigned DstSize = Dst->getScalarSizeInBits();
737+
if (DL.isLegalInteger(DstSize) &&
738+
DstSize >= DL.getPointerAddressSizeInBits(Src))
739+
return 0;
740+
break;
741+
}
735742
case Instruction::PtrToInt: {
736743
unsigned DstSize = Dst->getScalarSizeInBits();
737744
if (DL.isLegalInteger(DstSize) &&
@@ -1441,6 +1448,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
14411448
Op2Info, Operands, I);
14421449
}
14431450
case Instruction::IntToPtr:
1451+
case Instruction::PtrToAddr:
14441452
case Instruction::PtrToInt:
14451453
case Instruction::SIToFP:
14461454
case Instruction::UIToFP:

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ enum Kind {
318318
kw_fptoui,
319319
kw_fptosi,
320320
kw_inttoptr,
321+
kw_ptrtoaddr,
321322
kw_ptrtoint,
322323
kw_bitcast,
323324
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ 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+
return translatePtrToInt(U, MIRBuilder);
491+
}
489492
bool translateTrunc(const User &U, MachineIRBuilder &MIRBuilder) {
490493
return translateCast(TargetOpcode::G_TRUNC, U, MIRBuilder);
491494
}

llvm/include/llvm/IR/DataLayout.h

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,38 @@ class DataLayout {
324324
/// the backends/clients are updated.
325325
Align getPointerPrefAlignment(unsigned AS = 0) const;
326326

327-
/// Layout pointer size in bytes, rounded up to a whole
328-
/// number of bytes.
327+
/// The pointer representation size in bytes, rounded up to a whole number of
328+
/// bytes. The difference between this function and getPointerAddressSize() is
329+
/// this one returns the size of the entire pointer type (this includes
330+
/// metadata bits for fat pointers) and the latter only returns the number of
331+
/// address bits.
332+
/// \sa DataLayout::getPointerAddressSizeInBits
329333
/// FIXME: The defaults need to be removed once all of
330334
/// the backends/clients are updated.
331335
unsigned getPointerSize(unsigned AS = 0) const;
332336

333-
// Index size in bytes used for address calculation,
334-
/// rounded up to a whole number of bytes.
337+
/// The index size in bytes used for address calculation, rounded up to a
338+
/// whole number of bytes. This not only defines the size used in
339+
/// getelementptr operations, but also the size of addresses in this \p AS.
340+
/// For example, a 64-bit CHERI-enabled target has 128-bit pointers of which
341+
/// only 64 are used to represent the address and the remaining ones are used
342+
/// for metadata such as bounds and access permissions. In this case
343+
/// getPointerSize() returns 16, but getIndexSize() returns 8.
344+
/// To help with code understanding, the alias getPointerAddressSize() can be
345+
/// used instead of getIndexSize() to clarify that an address width is needed.
335346
unsigned getIndexSize(unsigned AS) const;
336347

348+
/// The integral size of a pointer in a given address space in bytes, which
349+
/// is defined to be the same as getIndexSize(). This exists as a separate
350+
/// function to make it clearer when reading code that the size of an address
351+
/// is being requested. While targets exist where index size and the
352+
/// underlying address width are not identical (e.g. AMDGPU fat pointers with
353+
/// 48-bit addresses and 32-bit offsets indexing), there is currently no need
354+
/// to differentiate these properties in LLVM.
355+
/// \sa DataLayout::getIndexSize
356+
/// \sa DataLayout::getPointerAddressSizeInBits
357+
unsigned getPointerAddressSize(unsigned AS) const { return getIndexSize(AS); }
358+
337359
/// Return the address spaces containing non-integral pointers. Pointers in
338360
/// this address space don't have a well-defined bitwise representation.
339361
SmallVector<unsigned, 8> getNonIntegralAddressSpaces() const {
@@ -358,29 +380,53 @@ class DataLayout {
358380
return PTy && isNonIntegralPointerType(PTy);
359381
}
360382

361-
/// Layout pointer size, in bits
383+
/// The size in bits of the pointer representation in a given address space.
384+
/// This is not necessarily the same as the integer address of a pointer (e.g.
385+
/// for fat pointers).
386+
/// \sa DataLayout::getPointerAddressSizeInBits()
362387
/// FIXME: The defaults need to be removed once all of
363388
/// the backends/clients are updated.
364389
unsigned getPointerSizeInBits(unsigned AS = 0) const {
365390
return getPointerSpec(AS).BitWidth;
366391
}
367392

368-
/// Size in bits of index used for address calculation in getelementptr.
393+
/// The size in bits of indices used for address calculation in getelementptr
394+
/// and for addresses in the given AS. See getIndexSize() for more
395+
/// information.
396+
/// \sa DataLayout::getPointerAddressSizeInBits()
369397
unsigned getIndexSizeInBits(unsigned AS) const {
370398
return getPointerSpec(AS).IndexBitWidth;
371399
}
372400

373-
/// Layout pointer size, in bits, based on the type. If this function is
401+
/// The size in bits of an address in for the given AS. This is defined to
402+
/// return the same value as getIndexSizeInBits() since there is currently no
403+
/// target that requires these two properties to have different values. See
404+
/// getIndexSize() for more information.
405+
/// \sa DataLayout::getIndexSizeInBits()
406+
unsigned getPointerAddressSizeInBits(unsigned AS) const {
407+
return getIndexSizeInBits(AS);
408+
}
409+
410+
/// The pointer representation size in bits for this type. If this function is
374411
/// called with a pointer type, then the type size of the pointer is returned.
375412
/// If this function is called with a vector of pointers, then the type size
376413
/// of the pointer is returned. This should only be called with a pointer or
377414
/// vector of pointers.
378415
unsigned getPointerTypeSizeInBits(Type *) const;
379416

380-
/// Layout size of the index used in GEP calculation.
417+
/// The size in bits of the index used in GEP calculation for this type.
381418
/// The function should be called with pointer or vector of pointers type.
419+
/// This is defined to return the same value as getPointerAddressSizeInBits(),
420+
/// but separate functions exist for code clarity.
382421
unsigned getIndexTypeSizeInBits(Type *Ty) const;
383422

423+
/// The size in bits of an address for this type.
424+
/// This is defined to return the same value as getIndexTypeSizeInBits(),
425+
/// but separate functions exist for code clarity.
426+
unsigned getPointerAddressSizeInBits(Type *Ty) const {
427+
return getIndexTypeSizeInBits(Ty);
428+
}
429+
384430
unsigned getPointerTypeSize(Type *Ty) const {
385431
return getPointerTypeSizeInBits(Ty) / 8;
386432
}

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(PtrToIntInst);}
186187
RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
187188
RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
188189
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}

llvm/include/llvm/IR/Instruction.def

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,35 +190,36 @@ HANDLE_CAST_INST(43, UIToFP , UIToFPInst ) // UInt -> floating point
190190
HANDLE_CAST_INST(44, SIToFP , SIToFPInst ) // SInt -> floating point
191191
HANDLE_CAST_INST(45, FPTrunc , FPTruncInst ) // Truncate floating point
192192
HANDLE_CAST_INST(46, FPExt , FPExtInst ) // Extend floating point
193-
HANDLE_CAST_INST(47, PtrToInt, PtrToIntInst) // Pointer -> Integer
194-
HANDLE_CAST_INST(48, IntToPtr, IntToPtrInst) // Integer -> Pointer
195-
HANDLE_CAST_INST(49, BitCast , BitCastInst ) // Type cast
196-
HANDLE_CAST_INST(50, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
197-
LAST_CAST_INST(50)
193+
HANDLE_CAST_INST(47, PtrToInt, PtrToIntInst) // Pointer -> Integer (bitcast)
194+
HANDLE_CAST_INST(48, PtrToAddr, PtrToAddrInst) // Pointer -> Address
195+
HANDLE_CAST_INST(49, IntToPtr, IntToPtrInst) // Integer -> Pointer
196+
HANDLE_CAST_INST(50, BitCast , BitCastInst ) // Type cast
197+
HANDLE_CAST_INST(51, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
198+
LAST_CAST_INST(51)
198199

199-
FIRST_FUNCLETPAD_INST(51)
200-
HANDLE_FUNCLETPAD_INST(51, CleanupPad, CleanupPadInst)
201-
HANDLE_FUNCLETPAD_INST(52, CatchPad , CatchPadInst)
202-
LAST_FUNCLETPAD_INST(52)
200+
FIRST_FUNCLETPAD_INST(52)
201+
HANDLE_FUNCLETPAD_INST(52, CleanupPad, CleanupPadInst)
202+
HANDLE_FUNCLETPAD_INST(53, CatchPad , CatchPadInst)
203+
LAST_FUNCLETPAD_INST(53)
203204

204205
// Other operators...
205-
FIRST_OTHER_INST(53)
206-
HANDLE_OTHER_INST(53, ICmp , ICmpInst ) // Integer comparison instruction
207-
HANDLE_OTHER_INST(54, FCmp , FCmpInst ) // Floating point comparison instr.
208-
HANDLE_OTHER_INST(55, PHI , PHINode ) // PHI node instruction
209-
HANDLE_OTHER_INST(56, Call , CallInst ) // Call a function
210-
HANDLE_OTHER_INST(57, Select , SelectInst ) // select instruction
211-
HANDLE_USER_INST (58, UserOp1, Instruction) // May be used internally in a pass
212-
HANDLE_USER_INST (59, UserOp2, Instruction) // Internal to passes only
213-
HANDLE_OTHER_INST(60, VAArg , VAArgInst ) // vaarg instruction
214-
HANDLE_OTHER_INST(61, ExtractElement, ExtractElementInst)// extract from vector
215-
HANDLE_OTHER_INST(62, InsertElement, InsertElementInst) // insert into vector
216-
HANDLE_OTHER_INST(63, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
217-
HANDLE_OTHER_INST(64, ExtractValue, ExtractValueInst)// extract from aggregate
218-
HANDLE_OTHER_INST(65, InsertValue, InsertValueInst) // insert into aggregate
219-
HANDLE_OTHER_INST(66, LandingPad, LandingPadInst) // Landing pad instruction.
220-
HANDLE_OTHER_INST(67, Freeze, FreezeInst) // Freeze instruction.
221-
LAST_OTHER_INST(67)
206+
FIRST_OTHER_INST(54)
207+
HANDLE_OTHER_INST(54, ICmp , ICmpInst ) // Integer comparison instruction
208+
HANDLE_OTHER_INST(55, FCmp , FCmpInst ) // Floating point comparison instr.
209+
HANDLE_OTHER_INST(56, PHI , PHINode ) // PHI node instruction
210+
HANDLE_OTHER_INST(57, Call , CallInst ) // Call a function
211+
HANDLE_OTHER_INST(58, Select , SelectInst ) // select instruction
212+
HANDLE_USER_INST (59, UserOp1, Instruction) // May be used internally in a pass
213+
HANDLE_USER_INST (60, UserOp2, Instruction) // Internal to passes only
214+
HANDLE_OTHER_INST(61, VAArg , VAArgInst ) // vaarg instruction
215+
HANDLE_OTHER_INST(62, ExtractElement, ExtractElementInst)// extract from vector
216+
HANDLE_OTHER_INST(63, InsertElement, InsertElementInst) // insert into vector
217+
HANDLE_OTHER_INST(64, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
218+
HANDLE_OTHER_INST(65, ExtractValue, ExtractValueInst)// extract from aggregate
219+
HANDLE_OTHER_INST(66, InsertValue, InsertValueInst) // insert into aggregate
220+
HANDLE_OTHER_INST(67, LandingPad, LandingPadInst) // Landing pad instruction.
221+
HANDLE_OTHER_INST(68, Freeze, FreezeInst) // Freeze instruction.
222+
LAST_OTHER_INST(68)
222223

223224
#undef FIRST_TERM_INST
224225
#undef HANDLE_TERM_INST

0 commit comments

Comments
 (0)