@@ -533,6 +533,9 @@ AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
533533 setOperationAction(ISD::XOR, MVT::i32, Custom);
534534 setOperationAction(ISD::XOR, MVT::i64, Custom);
535535
536+ setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
537+ setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
538+
536539 // Virtually no operation on f128 is legal, but LLVM can't expand them when
537540 // there's a valid register class, so we need custom operations in most cases.
538541 setOperationAction(ISD::FABS, MVT::f128, Expand);
@@ -6722,6 +6725,37 @@ static SDValue LowerTruncateVectorStore(SDLoc DL, StoreSDNode *ST,
67226725 ST->getBasePtr(), ST->getMemOperand());
67236726}
67246727
6728+ static SDValue LowerADDRSPACECAST(SDValue Op, SelectionDAG &DAG) {
6729+ SDLoc dl(Op);
6730+ SDValue Src = Op.getOperand(0);
6731+ MVT DestVT = Op.getSimpleValueType();
6732+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6733+ AddrSpaceCastSDNode *N = cast<AddrSpaceCastSDNode>(Op.getNode());
6734+
6735+ unsigned SrcAS = N->getSrcAddressSpace();
6736+ unsigned DestAS = N->getDestAddressSpace();
6737+ assert(SrcAS != DestAS &&
6738+ "addrspacecast must be between different address spaces");
6739+ assert(TLI.getTargetMachine().getPointerSize(SrcAS) !=
6740+ TLI.getTargetMachine().getPointerSize(DestAS) &&
6741+ "addrspacecast must be between different ptr sizes");
6742+
6743+ if (SrcAS == ARM64AS::PTR32_SPTR) {
6744+ return DAG.getNode(ISD::SIGN_EXTEND, dl, DestVT, Src,
6745+ DAG.getTargetConstant(0, dl, DestVT));
6746+ } else if (SrcAS == ARM64AS::PTR32_UPTR) {
6747+ return DAG.getNode(ISD::ZERO_EXTEND, dl, DestVT, Src,
6748+ DAG.getTargetConstant(0, dl, DestVT));
6749+ } else if ((DestAS == ARM64AS::PTR32_SPTR) ||
6750+ (DestAS == ARM64AS::PTR32_UPTR)) {
6751+ SDValue Ext = DAG.getAnyExtOrTrunc(Src, dl, DestVT);
6752+ SDValue Trunc = DAG.getZeroExtendInReg(Ext, dl, DestVT);
6753+ return Trunc;
6754+ } else {
6755+ return Src;
6756+ }
6757+ }
6758+
67256759// Custom lowering for any store, vector or scalar and/or default or with
67266760// a truncate operations. Currently only custom lower truncate operation
67276761// from vector v4i16 to v4i8 or volatile stores of i128.
@@ -7375,6 +7409,8 @@ SDValue AArch64TargetLowering::LowerOperation(SDValue Op,
73757409 case ISD::SIGN_EXTEND:
73767410 case ISD::ZERO_EXTEND:
73777411 return LowerFixedLengthVectorIntExtendToSVE(Op, DAG);
7412+ case ISD::ADDRSPACECAST:
7413+ return LowerADDRSPACECAST(Op, DAG);
73787414 case ISD::SIGN_EXTEND_INREG: {
73797415 // Only custom lower when ExtraVT has a legal byte based element type.
73807416 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
@@ -23366,6 +23402,26 @@ static SDValue performLOADCombine(SDNode *N,
2336623402 performTBISimplification(N->getOperand(1), DCI, DAG);
2336723403
2336823404 LoadSDNode *LD = cast<LoadSDNode>(N);
23405+ EVT RegVT = LD->getValueType(0);
23406+ EVT MemVT = LD->getMemoryVT();
23407+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23408+ SDLoc DL(LD);
23409+
23410+ // Cast ptr32 and ptr64 pointers to the default address space before a load.
23411+ unsigned AddrSpace = LD->getAddressSpace();
23412+ if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23413+ AddrSpace == ARM64AS::PTR32_UPTR) {
23414+ MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23415+ if (PtrVT != LD->getBasePtr().getSimpleValueType()) {
23416+ SDValue Cast =
23417+ DAG.getAddrSpaceCast(DL, PtrVT, LD->getBasePtr(), AddrSpace, 0);
23418+ return DAG.getExtLoad(LD->getExtensionType(), DL, RegVT, LD->getChain(),
23419+ Cast, LD->getPointerInfo(), MemVT,
23420+ LD->getOriginalAlign(),
23421+ LD->getMemOperand()->getFlags());
23422+ }
23423+ }
23424+
2336923425 if (LD->isVolatile() || !Subtarget->isLittleEndian())
2337023426 return SDValue(N, 0);
2337123427
@@ -23375,13 +23431,11 @@ static SDValue performLOADCombine(SDNode *N,
2337523431 if (!LD->isNonTemporal())
2337623432 return SDValue(N, 0);
2337723433
23378- EVT MemVT = LD->getMemoryVT();
2337923434 if (MemVT.isScalableVector() || MemVT.getSizeInBits() <= 256 ||
2338023435 MemVT.getSizeInBits() % 256 == 0 ||
2338123436 256 % MemVT.getScalarSizeInBits() != 0)
2338223437 return SDValue(N, 0);
2338323438
23384- SDLoc DL(LD);
2338523439 SDValue Chain = LD->getChain();
2338623440 SDValue BasePtr = LD->getBasePtr();
2338723441 SDNodeFlags Flags = LD->getFlags();
@@ -23641,12 +23695,28 @@ static SDValue performSTORECombine(SDNode *N,
2364123695 SDValue Value = ST->getValue();
2364223696 SDValue Ptr = ST->getBasePtr();
2364323697 EVT ValueVT = Value.getValueType();
23698+ EVT MemVT = ST->getMemoryVT();
23699+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23700+ SDLoc DL(ST);
2364423701
2364523702 auto hasValidElementTypeForFPTruncStore = [](EVT VT) {
2364623703 EVT EltVT = VT.getVectorElementType();
2364723704 return EltVT == MVT::f32 || EltVT == MVT::f64;
2364823705 };
2364923706
23707+ // Cast ptr32 and ptr64 pointers to the default address space before a store.
23708+ unsigned AddrSpace = ST->getAddressSpace();
23709+ if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23710+ AddrSpace == ARM64AS::PTR32_UPTR) {
23711+ MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23712+ if (PtrVT != Ptr.getSimpleValueType()) {
23713+ SDValue Cast = DAG.getAddrSpaceCast(DL, PtrVT, Ptr, AddrSpace, 0);
23714+ return DAG.getStore(Chain, DL, Value, Cast, ST->getPointerInfo(),
23715+ ST->getOriginalAlign(),
23716+ ST->getMemOperand()->getFlags(), ST->getAAInfo());
23717+ }
23718+ }
23719+
2365023720 if (SDValue Res = combineI8TruncStore(ST, DAG, Subtarget))
2365123721 return Res;
2365223722
@@ -23660,8 +23730,8 @@ static SDValue performSTORECombine(SDNode *N,
2366023730 ValueVT.isFixedLengthVector() &&
2366123731 ValueVT.getFixedSizeInBits() >= Subtarget->getMinSVEVectorSizeInBits() &&
2366223732 hasValidElementTypeForFPTruncStore(Value.getOperand(0).getValueType()))
23663- return DAG.getTruncStore(Chain, SDLoc(N) , Value.getOperand(0), Ptr,
23664- ST->getMemoryVT(), ST-> getMemOperand());
23733+ return DAG.getTruncStore(Chain, DL , Value.getOperand(0), Ptr, MemVT ,
23734+ ST->getMemOperand());
2366523735
2366623736 if (SDValue Split = splitStores(N, DCI, DAG, Subtarget))
2366723737 return Split;
@@ -26988,6 +27058,11 @@ void AArch64TargetLowering::ReplaceNodeResults(
2698827058 ReplaceATOMIC_LOAD_128Results(N, Results, DAG, Subtarget);
2698927059 return;
2699027060 }
27061+ case ISD::ADDRSPACECAST: {
27062+ SDValue V = LowerADDRSPACECAST(SDValue(N, 0), DAG);
27063+ Results.push_back(V);
27064+ return;
27065+ }
2699127066 case ISD::ATOMIC_LOAD:
2699227067 case ISD::LOAD: {
2699327068 MemSDNode *LoadNode = cast<MemSDNode>(N);
0 commit comments