Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 59d3ae6

Browse files
committed
Add addrspacecast instruction.
Patch by Michele Scandale! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194760 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2b7fef0 commit 59d3ae6

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

+588
-229
lines changed

docs/LangRef.rst

+52-3
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,10 @@ The following is the syntax for constant expressions:
23822382
Convert a constant, CST, to another TYPE. The constraints of the
23832383
operands are the same as those for the :ref:`bitcast
23842384
instruction <i_bitcast>`.
2385+
``addrspacecast (CST to TYPE)``
2386+
Convert a constant pointer or constant vector of pointer, CST, to another
2387+
TYPE in a different address space. The constraints of the operands are the
2388+
same as those for the :ref:`addrspacecast instruction <i_addrspacecast>`.
23852389
``getelementptr (CSTPTR, IDX0, IDX1, ...)``, ``getelementptr inbounds (CSTPTR, IDX0, IDX1, ...)``
23862390
Perform the :ref:`getelementptr operation <i_getelementptr>` on
23872391
constants. As with the :ref:`getelementptr <i_getelementptr>`
@@ -5726,9 +5730,9 @@ is always a *no-op cast* because no bits change with this
57265730
conversion. The conversion is done as if the ``value`` had been stored
57275731
to memory and read back as type ``ty2``. Pointer (or vector of
57285732
pointers) types may only be converted to other pointer (or vector of
5729-
pointers) types with this instruction if the pointer sizes are
5730-
equal. To convert pointers to other types, use the :ref:`inttoptr
5731-
<i_inttoptr>` or :ref:`ptrtoint <i_ptrtoint>` instructions first.
5733+
pointers) types with the same address space through this instruction.
5734+
To convert pointers to other types, use the :ref:`inttoptr <i_inttoptr>`
5735+
or :ref:`ptrtoint <i_ptrtoint>` instructions first.
57325736

57335737
Example:
57345738
""""""""
@@ -5740,6 +5744,51 @@ Example:
57405744
%Z = bitcast <2 x int> %V to i64; ; yields i64: %V
57415745
%Z = bitcast <2 x i32*> %V to <2 x i64*> ; yields <2 x i64*>
57425746
5747+
.. _i_addrspacecast:
5748+
5749+
'``addrspacecast .. to``' Instruction
5750+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5751+
5752+
Syntax:
5753+
"""""""
5754+
5755+
::
5756+
5757+
<result> = addrspacecast <pty> <ptrval> to <pty2> ; yields pty2
5758+
5759+
Overview:
5760+
"""""""""
5761+
5762+
The '``addrspacecast``' instruction converts ``ptrval`` from ``pty`` in
5763+
address space ``n`` to type ``pty2`` in address space ``m``.
5764+
5765+
Arguments:
5766+
""""""""""
5767+
5768+
The '``addrspacecast``' instruction takes a pointer or vector of pointer value
5769+
to cast and a pointer type to cast it to, which must have a different
5770+
address space.
5771+
5772+
Semantics:
5773+
""""""""""
5774+
5775+
The '``addrspacecast``' instruction converts the pointer value
5776+
``ptrval`` to type ``pty2``. It can be a *no-op cast* or a complex
5777+
value modification, depending on the target and the address spaces
5778+
pair. Pointers conversion within the same address space must be
5779+
performed with ``bitcast`` instruction. Note that if the address space
5780+
conversion is legal then both result and operand refer to the same memory
5781+
location.
5782+
5783+
Example:
5784+
""""""""
5785+
5786+
.. code-block:: llvm
5787+
5788+
%X = addrspacecast i32* %x to addrspace(1) i32* ; yields addrspace(1) i32*:%x
5789+
%Y = addrspacecast addrspace(1) <2 x i32>* %y to addrspace(2) i64* ; yields addrspace(2) i32*:%y
5790+
%Z = addrspacecast <4 x i32*> %z to <4 x float addrspace(3)*> ; yelds <4 x float addrspace(3)*>:%z
5791+
57435792
.. _otherops:
57445793

57455794
Other Operations

include/llvm-c/Core.h

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ typedef enum {
222222
LLVMPtrToInt = 39,
223223
LLVMIntToPtr = 40,
224224
LLVMBitCast = 41,
225+
LLVMAddrSpaceCast = 60,
225226

226227
/* Other Operators */
227228
LLVMICmp = 42,
@@ -1159,6 +1160,7 @@ LLVMTypeRef LLVMX86MMXType(void);
11591160
macro(UnaryInstruction) \
11601161
macro(AllocaInst) \
11611162
macro(CastInst) \
1163+
macro(AddrSpaceCastInst) \
11621164
macro(BitCastInst) \
11631165
macro(FPExtInst) \
11641166
macro(FPToSIInst) \
@@ -1630,6 +1632,7 @@ LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
16301632
LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
16311633
LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
16321634
LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
1635+
LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
16331636
LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
16341637
LLVMTypeRef ToType);
16351638
LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
@@ -2605,6 +2608,8 @@ LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val,
26052608
LLVMTypeRef DestTy, const char *Name);
26062609
LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val,
26072610
LLVMTypeRef DestTy, const char *Name);
2611+
LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef, LLVMValueRef Val,
2612+
LLVMTypeRef DestTy, const char *Name);
26082613
LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef, LLVMValueRef Val,
26092614
LLVMTypeRef DestTy, const char *Name);
26102615
LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef, LLVMValueRef Val,

include/llvm/AutoUpgrade.h

+13
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
#define LLVM_AUTOUPGRADE_H
1616

1717
namespace llvm {
18+
class Constant;
1819
class Module;
1920
class GlobalVariable;
2021
class Function;
2122
class CallInst;
2223
class Instruction;
24+
class Value;
25+
class Type;
2326

2427
/// This is a more granular function that simply checks an intrinsic function
2528
/// for upgrading, and returns true if it requires upgrading. It may return
@@ -44,6 +47,16 @@ namespace llvm {
4447
/// If the TBAA tag for the given instruction uses the scalar TBAA format,
4548
/// we upgrade it to the struct-path aware TBAA format.
4649
void UpgradeInstWithTBAATag(Instruction *I);
50+
51+
/// This is an auto-upgrade for bitcast between pointers with different
52+
/// address spaces: the instruction is replaced by a pair ptrtoint+inttoptr.
53+
Instruction *UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
54+
Instruction *&Temp);
55+
56+
/// This is an auto-upgrade for bitcast constant expression between pointers
57+
/// with different address spaces: the instruction is replaced by a pair
58+
/// ptrtoint+inttoptr.
59+
Value *UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy);
4760
} // End llvm namespace
4861

4962
#endif

include/llvm/Bitcode/LLVMBitCodes.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ namespace bitc {
194194
CAST_FPEXT = 8,
195195
CAST_PTRTOINT = 9,
196196
CAST_INTTOPTR = 10,
197-
CAST_BITCAST = 11
197+
CAST_BITCAST = 11,
198+
CAST_ADDRSPACECAST = 12
198199
};
199200

200201
/// BinaryOpcodes - These are values used in the bitcode files to encode which

include/llvm/CodeGen/ISDOpcodes.h

+4
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ namespace ISD {
419419
/// getNode().
420420
BITCAST,
421421

422+
/// ADDRSPACECAST - This operator converts between pointers of different
423+
/// address spaces.
424+
ADDRSPACECAST,
425+
422426
/// CONVERT_RNDSAT - This operator is used to support various conversions
423427
/// between various types (float, signed, unsigned and vectors of those
424428
/// types) with rounding and saturation. NOTE: Avoid using this operator as

include/llvm/CodeGen/SelectionDAG.h

+4
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,10 @@ class SelectionDAG {
802802
/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
803803
SDValue getMDNode(const MDNode *MD);
804804

805+
/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
806+
SDValue getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
807+
unsigned SrcAS, unsigned DestAS);
808+
805809
/// getShiftAmountOperand - Return the specified value casted to
806810
/// the target's desired shift amount type.
807811
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);

include/llvm/CodeGen/SelectionDAGNodes.h

+17
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,23 @@ class HandleSDNode : public SDNode {
950950
const SDValue &getValue() const { return Op; }
951951
};
952952

953+
class AddrSpaceCastSDNode : public UnarySDNode {
954+
private:
955+
unsigned SrcAddrSpace;
956+
unsigned DestAddrSpace;
957+
958+
public:
959+
AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
960+
unsigned SrcAS, unsigned DestAS);
961+
962+
unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
963+
unsigned getDestAddressSpace() const { return DestAddrSpace; }
964+
965+
static bool classof(const SDNode *N) {
966+
return N->getOpcode() == ISD::ADDRSPACECAST;
967+
}
968+
};
969+
953970
/// Abstact virtual class for operations for memory operations
954971
class MemSDNode : public SDNode {
955972
private:

include/llvm/IR/Constants.h

+1
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ class ConstantExpr : public Constant {
862862
static Constant *getPtrToInt(Constant *C, Type *Ty);
863863
static Constant *getIntToPtr(Constant *C, Type *Ty);
864864
static Constant *getBitCast (Constant *C, Type *Ty);
865+
static Constant *getAddrSpaceCast (Constant *C, Type *Ty);
865866

866867
static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
867868
static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }

include/llvm/IR/IRBuilder.h

+4
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,10 @@ class IRBuilder : public IRBuilderBase, public Inserter {
11331133
const Twine &Name = "") {
11341134
return CreateCast(Instruction::BitCast, V, DestTy, Name);
11351135
}
1136+
Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1137+
const Twine &Name = "") {
1138+
return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1139+
}
11361140
Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
11371141
const Twine &Name = "") {
11381142
if (V->getType() == DestTy)

include/llvm/IR/Instruction.def

+18-17
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,26 @@ HANDLE_CAST_INST(41, FPExt , FPExtInst ) // Extend floating point
154154
HANDLE_CAST_INST(42, PtrToInt, PtrToIntInst) // Pointer -> Integer
155155
HANDLE_CAST_INST(43, IntToPtr, IntToPtrInst) // Integer -> Pointer
156156
HANDLE_CAST_INST(44, BitCast , BitCastInst ) // Type cast
157-
LAST_CAST_INST(44)
157+
HANDLE_CAST_INST(45, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
158+
LAST_CAST_INST(45)
158159

159160
// Other operators...
160-
FIRST_OTHER_INST(45)
161-
HANDLE_OTHER_INST(45, ICmp , ICmpInst ) // Integer comparison instruction
162-
HANDLE_OTHER_INST(46, FCmp , FCmpInst ) // Floating point comparison instr.
163-
HANDLE_OTHER_INST(47, PHI , PHINode ) // PHI node instruction
164-
HANDLE_OTHER_INST(48, Call , CallInst ) // Call a function
165-
HANDLE_OTHER_INST(49, Select , SelectInst ) // select instruction
166-
HANDLE_OTHER_INST(50, UserOp1, Instruction) // May be used internally in a pass
167-
HANDLE_OTHER_INST(51, UserOp2, Instruction) // Internal to passes only
168-
HANDLE_OTHER_INST(52, VAArg , VAArgInst ) // vaarg instruction
169-
HANDLE_OTHER_INST(53, ExtractElement, ExtractElementInst)// extract from vector
170-
HANDLE_OTHER_INST(54, InsertElement, InsertElementInst) // insert into vector
171-
HANDLE_OTHER_INST(55, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
172-
HANDLE_OTHER_INST(56, ExtractValue, ExtractValueInst)// extract from aggregate
173-
HANDLE_OTHER_INST(57, InsertValue, InsertValueInst) // insert into aggregate
174-
HANDLE_OTHER_INST(58, LandingPad, LandingPadInst) // Landing pad instruction.
175-
LAST_OTHER_INST(58)
161+
FIRST_OTHER_INST(46)
162+
HANDLE_OTHER_INST(46, ICmp , ICmpInst ) // Integer comparison instruction
163+
HANDLE_OTHER_INST(47, FCmp , FCmpInst ) // Floating point comparison instr.
164+
HANDLE_OTHER_INST(48, PHI , PHINode ) // PHI node instruction
165+
HANDLE_OTHER_INST(49, Call , CallInst ) // Call a function
166+
HANDLE_OTHER_INST(50, Select , SelectInst ) // select instruction
167+
HANDLE_OTHER_INST(51, UserOp1, Instruction) // May be used internally in a pass
168+
HANDLE_OTHER_INST(52, UserOp2, Instruction) // Internal to passes only
169+
HANDLE_OTHER_INST(53, VAArg , VAArgInst ) // vaarg instruction
170+
HANDLE_OTHER_INST(54, ExtractElement, ExtractElementInst)// extract from vector
171+
HANDLE_OTHER_INST(55, InsertElement, InsertElementInst) // insert into vector
172+
HANDLE_OTHER_INST(56, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
173+
HANDLE_OTHER_INST(57, ExtractValue, ExtractValueInst)// extract from aggregate
174+
HANDLE_OTHER_INST(58, InsertValue, InsertValueInst) // insert into aggregate
175+
HANDLE_OTHER_INST(59, LandingPad, LandingPadInst) // Landing pad instruction.
176+
LAST_OTHER_INST(59)
176177

177178
#undef FIRST_TERM_INST
178179
#undef HANDLE_TERM_INST

include/llvm/IR/Instructions.h

+37
Original file line numberDiff line numberDiff line change
@@ -3615,6 +3615,43 @@ class BitCastInst : public CastInst {
36153615
}
36163616
};
36173617

3618+
//===----------------------------------------------------------------------===//
3619+
// AddrSpaceCastInst Class
3620+
//===----------------------------------------------------------------------===//
3621+
3622+
/// \brief This class represents a conversion between pointers from
3623+
/// one address space to another.
3624+
class AddrSpaceCastInst : public CastInst {
3625+
protected:
3626+
/// \brief Clone an identical AddrSpaceCastInst
3627+
virtual AddrSpaceCastInst *clone_impl() const;
3628+
3629+
public:
3630+
/// \brief Constructor with insert-before-instruction semantics
3631+
AddrSpaceCastInst(
3632+
Value *S, ///< The value to be casted
3633+
Type *Ty, ///< The type to casted to
3634+
const Twine &NameStr = "", ///< A name for the new instruction
3635+
Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3636+
);
3637+
3638+
/// \brief Constructor with insert-at-end-of-block semantics
3639+
AddrSpaceCastInst(
3640+
Value *S, ///< The value to be casted
3641+
Type *Ty, ///< The type to casted to
3642+
const Twine &NameStr, ///< A name for the new instruction
3643+
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3644+
);
3645+
3646+
// Methods for support type inquiry through isa, cast, and dyn_cast:
3647+
static inline bool classof(const Instruction *I) {
3648+
return I->getOpcode() == AddrSpaceCast;
3649+
}
3650+
static inline bool classof(const Value *V) {
3651+
return isa<Instruction>(V) && classof(cast<Instruction>(V));
3652+
}
3653+
};
3654+
36183655
} // End llvm namespace
36193656

36203657
#endif

include/llvm/InstVisitor.h

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class InstVisitor {
191191
RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);}
192192
RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
193193
RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
194+
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
194195
RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction);}
195196
RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(UnaryInstruction);}
196197
RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}

include/llvm/Target/TargetLowering.h

+5
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,11 @@ class TargetLoweringBase {
827827
return 0;
828828
}
829829

830+
/// Returns true if a cast between SrcAS and DestAS is a noop.
831+
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const {
832+
return false;
833+
}
834+
830835
//===--------------------------------------------------------------------===//
831836
/// \name Helpers for TargetTransformInfo implementations
832837
/// @{

lib/Analysis/ConstantFolding.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ static Constant* StripPtrCastKeepAS(Constant* Ptr) {
663663
if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
664664
NewPtrTy = NewPtrTy->getElementType()->getPointerTo(
665665
OldPtrTy->getAddressSpace());
666-
Ptr = ConstantExpr::getBitCast(Ptr, NewPtrTy);
666+
Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
667667
}
668668
return Ptr;
669669
}
@@ -995,17 +995,18 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
995995
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
996996
case Instruction::IntToPtr:
997997
// If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
998-
// the int size is >= the ptr size. This requires knowing the width of a
999-
// pointer, so it can't be done in ConstantExpr::getCast.
998+
// the int size is >= the ptr size and the address spaces are the same.
999+
// This requires knowing the width of a pointer, so it can't be done in
1000+
// ConstantExpr::getCast.
10001001
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
10011002
if (TD && CE->getOpcode() == Instruction::PtrToInt) {
10021003
Constant *SrcPtr = CE->getOperand(0);
10031004
unsigned SrcPtrSize = TD->getPointerTypeSizeInBits(SrcPtr->getType());
10041005
unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
10051006

10061007
if (MidIntSize >= SrcPtrSize) {
1007-
unsigned DestPtrSize = TD->getPointerTypeSizeInBits(DestTy);
1008-
if (SrcPtrSize == DestPtrSize)
1008+
unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1009+
if (SrcAS == DestTy->getPointerAddressSpace())
10091010
return FoldBitCast(CE->getOperand(0), DestTy, *TD);
10101011
}
10111012
}
@@ -1021,6 +1022,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
10211022
case Instruction::SIToFP:
10221023
case Instruction::FPToUI:
10231024
case Instruction::FPToSI:
1025+
case Instruction::AddrSpaceCast:
10241026
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
10251027
case Instruction::BitCast:
10261028
if (TD)

lib/AsmParser/LLLexer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ lltok::Kind LLLexer::LexIdentifier() {
665665
INSTKEYWORD(inttoptr, IntToPtr);
666666
INSTKEYWORD(ptrtoint, PtrToInt);
667667
INSTKEYWORD(bitcast, BitCast);
668+
INSTKEYWORD(addrspacecast, AddrSpaceCast);
668669
INSTKEYWORD(select, Select);
669670
INSTKEYWORD(va_arg, VAArg);
670671
INSTKEYWORD(ret, Ret);

lib/AsmParser/LLParser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,7 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
24152415
case lltok::kw_fptrunc:
24162416
case lltok::kw_fpext:
24172417
case lltok::kw_bitcast:
2418+
case lltok::kw_addrspacecast:
24182419
case lltok::kw_uitofp:
24192420
case lltok::kw_sitofp:
24202421
case lltok::kw_fptoui:
@@ -3303,6 +3304,7 @@ int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
33033304
case lltok::kw_fptrunc:
33043305
case lltok::kw_fpext:
33053306
case lltok::kw_bitcast:
3307+
case lltok::kw_addrspacecast:
33063308
case lltok::kw_uitofp:
33073309
case lltok::kw_sitofp:
33083310
case lltok::kw_fptoui:

lib/AsmParser/LLToken.h

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ namespace lltok {
150150
kw_phi, kw_call,
151151
kw_trunc, kw_zext, kw_sext, kw_fptrunc, kw_fpext, kw_uitofp, kw_sitofp,
152152
kw_fptoui, kw_fptosi, kw_inttoptr, kw_ptrtoint, kw_bitcast,
153+
kw_addrspacecast,
153154
kw_select, kw_va_arg,
154155

155156
kw_landingpad, kw_personality, kw_cleanup, kw_catch, kw_filter,

0 commit comments

Comments
 (0)