Skip to content

Commit 9b0761d

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
2 parents c2f0af5 + e9843c8 commit 9b0761d

33 files changed

+714
-81
lines changed

llvm/docs/LangRef.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12466,6 +12466,61 @@ Example:
1246612466
%Y = ptrtoint ptr %P to i64 ; yields zero extension on 32-bit architecture
1246712467
%Z = ptrtoint <4 x ptr> %P to <4 x i64>; yields vector zero extension for a vector of addresses on 32-bit architecture
1246812468

12469+
.. _i_ptrtoaddr:
12470+
12471+
'``ptrtoaddr .. to``' Instruction
12472+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12473+
12474+
Syntax:
12475+
"""""""
12476+
12477+
::
12478+
12479+
<result> = ptrtoaddr <ty> <value> to <ty2> ; yields ty2
12480+
12481+
Overview:
12482+
"""""""""
12483+
12484+
The '``ptrtoaddr``' instruction converts the pointer or a vector of
12485+
pointers ``value`` to the underlying integer address (or vector of integers) of
12486+
type ``ty2``. This is different from :ref:`ptrtoint <i_ptrtoint>` in that it
12487+
only operates on the index bits of the pointer and ignores all other bits.
12488+
12489+
Arguments:
12490+
""""""""""
12491+
12492+
The '``ptrtoaddr``' instruction takes a ``value`` to cast, which must be
12493+
a value of type :ref:`pointer <t_pointer>` or a vector of pointers, and a
12494+
type to cast it to ``ty2``, which must be an :ref:`integer <t_integer>` or
12495+
a vector of integers type.
12496+
12497+
Semantics:
12498+
""""""""""
12499+
12500+
The '``ptrtoaddr``' instruction converts ``value`` to integer type
12501+
``ty2`` by interpreting the lowest index-width pointer representation bits as an
12502+
integer and either truncating or zero extending that value to the size of the
12503+
integer type.
12504+
If the address of ``value`` is smaller than ``ty2`` then a zero extension is
12505+
done. If the address of ``value`` is larger than ``ty2`` then a truncation is
12506+
done. If the address size and the pointer representation size are the same and
12507+
``value`` and ``ty2`` are the same size, then nothing is done (*no-op cast*)
12508+
other than a type change.
12509+
12510+
The ``ptrtoaddr`` always :ref:`captures the address (but not provenance) <pointercapture>`
12511+
of the pointer argument.
12512+
12513+
Example:
12514+
""""""""
12515+
This example assumes pointers in address space 1 are 64 bits in size with an
12516+
address width of 32 bits (``p1:64:64:64:32`` :ref:`datalayout string<langref_datalayout>`)
12517+
.. code-block:: llvm
12518+
12519+
%X = ptrtoaddr ptr addrspace(1) %P to i8 ; extracts low 32 bits and truncates
12520+
%Y = ptrtoaddr ptr addrspace(1) %P to i64 ; extracts low 32 bits and zero extends
12521+
%Z = ptrtoaddr <4 x ptr addrspace(1)> %P to <4 x i64>; yields vector zero extension of low 32 bits for each pointer
12522+
12523+
1246912524
.. _i_inttoptr:
1247012525

1247112526
'``inttoptr .. to``' Instruction

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ class TargetTransformInfoImplBase {
732732
return 0;
733733
break;
734734
}
735+
case Instruction::PtrToAddr: {
736+
unsigned DstSize = Dst->getScalarSizeInBits();
737+
if (DL.isLegalInteger(DstSize) && DstSize >= DL.getAddressSizeInBits(Src))
738+
return 0;
739+
break;
740+
}
735741
case Instruction::PtrToInt: {
736742
unsigned DstSize = Dst->getScalarSizeInBits();
737743
if (DL.isLegalInteger(DstSize) &&
@@ -1438,6 +1444,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
14381444
Op2Info, Operands, I);
14391445
}
14401446
case Instruction::IntToPtr:
1447+
case Instruction::PtrToAddr:
14411448
case Instruction::PtrToInt:
14421449
case Instruction::SIToFP:
14431450
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: 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/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);}

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

llvm/include/llvm/IR/Instructions.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,46 @@ class PtrToIntInst : public CastInst {
49494949
}
49504950
};
49514951

4952+
/// This class represents a cast from a pointer to an address (non-capturing
4953+
/// ptrtoint).
4954+
class PtrToAddrInst : public CastInst {
4955+
protected:
4956+
// Note: Instruction needs to be a friend here to call cloneImpl.
4957+
friend class Instruction;
4958+
4959+
/// Clone an identical PtrToIntInst.
4960+
PtrToAddrInst *cloneImpl() const;
4961+
4962+
public:
4963+
/// Constructor with insert-before-instruction semantics
4964+
PtrToAddrInst(Value *S, ///< The value to be converted
4965+
Type *Ty, ///< The type to convert to
4966+
const Twine &NameStr = "", ///< A name for the new instruction
4967+
InsertPosition InsertBefore =
4968+
nullptr ///< Where to insert the new instruction
4969+
);
4970+
4971+
/// Gets the pointer operand.
4972+
Value *getPointerOperand() { return getOperand(0); }
4973+
/// Gets the pointer operand.
4974+
const Value *getPointerOperand() const { return getOperand(0); }
4975+
/// Gets the operand index of the pointer operand.
4976+
static unsigned getPointerOperandIndex() { return 0U; }
4977+
4978+
/// Returns the address space of the pointer operand.
4979+
unsigned getPointerAddressSpace() const {
4980+
return getPointerOperand()->getType()->getPointerAddressSpace();
4981+
}
4982+
4983+
// Methods for support type inquiry through isa, cast, and dyn_cast:
4984+
static bool classof(const Instruction *I) {
4985+
return I->getOpcode() == PtrToAddr;
4986+
}
4987+
static bool classof(const Value *V) {
4988+
return isa<Instruction>(V) && classof(cast<Instruction>(V));
4989+
}
4990+
};
4991+
49524992
//===----------------------------------------------------------------------===//
49534993
// BitCastInst Class
49544994
//===----------------------------------------------------------------------===//

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,7 @@ class CastInst : public UnaryInstruction {
22782278
return Opcode::FPToSI;
22792279
case llvm::Instruction::FPExt:
22802280
return Opcode::FPExt;
2281+
case llvm::Instruction::PtrToAddr:
22812282
case llvm::Instruction::PtrToInt:
22822283
return Opcode::PtrToInt;
22832284
case llvm::Instruction::IntToPtr:

0 commit comments

Comments
 (0)