@@ -530,6 +530,9 @@ AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
530530 setOperationAction(ISD::XOR, MVT::i32, Custom);
531531 setOperationAction(ISD::XOR, MVT::i64, Custom);
532532
533+ setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
534+ setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
535+
533536 // Virtually no operation on f128 is legal, but LLVM can't expand them when
534537 // there's a valid register class, so we need custom operations in most cases.
535538 setOperationAction(ISD::FABS, MVT::f128, Expand);
@@ -6880,6 +6883,37 @@ static SDValue LowerTruncateVectorStore(SDLoc DL, StoreSDNode *ST,
68806883 ST->getBasePtr(), ST->getMemOperand());
68816884}
68826885
6886+ static SDValue LowerADDRSPACECAST(SDValue Op, SelectionDAG &DAG) {
6887+ SDLoc dl(Op);
6888+ SDValue Src = Op.getOperand(0);
6889+ MVT DestVT = Op.getSimpleValueType();
6890+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6891+ AddrSpaceCastSDNode *N = cast<AddrSpaceCastSDNode>(Op.getNode());
6892+
6893+ unsigned SrcAS = N->getSrcAddressSpace();
6894+ unsigned DestAS = N->getDestAddressSpace();
6895+ assert(SrcAS != DestAS &&
6896+ "addrspacecast must be between different address spaces");
6897+ assert(TLI.getTargetMachine().getPointerSize(SrcAS) !=
6898+ TLI.getTargetMachine().getPointerSize(DestAS) &&
6899+ "addrspacecast must be between different ptr sizes");
6900+
6901+ if (SrcAS == ARM64AS::PTR32_SPTR) {
6902+ return DAG.getNode(ISD::SIGN_EXTEND, dl, DestVT, Src,
6903+ DAG.getTargetConstant(0, dl, DestVT));
6904+ } else if (SrcAS == ARM64AS::PTR32_UPTR) {
6905+ return DAG.getNode(ISD::ZERO_EXTEND, dl, DestVT, Src,
6906+ DAG.getTargetConstant(0, dl, DestVT));
6907+ } else if ((DestAS == ARM64AS::PTR32_SPTR) ||
6908+ (DestAS == ARM64AS::PTR32_UPTR)) {
6909+ SDValue Ext = DAG.getAnyExtOrTrunc(Src, dl, DestVT);
6910+ SDValue Trunc = DAG.getZeroExtendInReg(Ext, dl, DestVT);
6911+ return Trunc;
6912+ } else {
6913+ return Src;
6914+ }
6915+ }
6916+
68836917// Custom lowering for any store, vector or scalar and/or default or with
68846918// a truncate operations. Currently only custom lower truncate operation
68856919// from vector v4i16 to v4i8 or volatile stores of i128.
@@ -7541,6 +7575,8 @@ SDValue AArch64TargetLowering::LowerOperation(SDValue Op,
75417575 case ISD::SIGN_EXTEND:
75427576 case ISD::ZERO_EXTEND:
75437577 return LowerFixedLengthVectorIntExtendToSVE(Op, DAG);
7578+ case ISD::ADDRSPACECAST:
7579+ return LowerADDRSPACECAST(Op, DAG);
75447580 case ISD::SIGN_EXTEND_INREG: {
75457581 // Only custom lower when ExtraVT has a legal byte based element type.
75467582 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
@@ -23555,6 +23591,26 @@ static SDValue performLOADCombine(SDNode *N,
2355523591 performTBISimplification(N->getOperand(1), DCI, DAG);
2355623592
2355723593 LoadSDNode *LD = cast<LoadSDNode>(N);
23594+ EVT RegVT = LD->getValueType(0);
23595+ EVT MemVT = LD->getMemoryVT();
23596+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23597+ SDLoc DL(LD);
23598+
23599+ // Cast ptr32 and ptr64 pointers to the default address space before a load.
23600+ unsigned AddrSpace = LD->getAddressSpace();
23601+ if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23602+ AddrSpace == ARM64AS::PTR32_UPTR) {
23603+ MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23604+ if (PtrVT != LD->getBasePtr().getSimpleValueType()) {
23605+ SDValue Cast =
23606+ DAG.getAddrSpaceCast(DL, PtrVT, LD->getBasePtr(), AddrSpace, 0);
23607+ return DAG.getExtLoad(LD->getExtensionType(), DL, RegVT, LD->getChain(),
23608+ Cast, LD->getPointerInfo(), MemVT,
23609+ LD->getOriginalAlign(),
23610+ LD->getMemOperand()->getFlags());
23611+ }
23612+ }
23613+
2355823614 if (LD->isVolatile() || !Subtarget->isLittleEndian())
2355923615 return SDValue(N, 0);
2356023616
@@ -23564,13 +23620,11 @@ static SDValue performLOADCombine(SDNode *N,
2356423620 if (!LD->isNonTemporal())
2356523621 return SDValue(N, 0);
2356623622
23567- EVT MemVT = LD->getMemoryVT();
2356823623 if (MemVT.isScalableVector() || MemVT.getSizeInBits() <= 256 ||
2356923624 MemVT.getSizeInBits() % 256 == 0 ||
2357023625 256 % MemVT.getScalarSizeInBits() != 0)
2357123626 return SDValue(N, 0);
2357223627
23573- SDLoc DL(LD);
2357423628 SDValue Chain = LD->getChain();
2357523629 SDValue BasePtr = LD->getBasePtr();
2357623630 SDNodeFlags Flags = LD->getFlags();
@@ -23830,12 +23884,28 @@ static SDValue performSTORECombine(SDNode *N,
2383023884 SDValue Value = ST->getValue();
2383123885 SDValue Ptr = ST->getBasePtr();
2383223886 EVT ValueVT = Value.getValueType();
23887+ EVT MemVT = ST->getMemoryVT();
23888+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23889+ SDLoc DL(ST);
2383323890
2383423891 auto hasValidElementTypeForFPTruncStore = [](EVT VT) {
2383523892 EVT EltVT = VT.getVectorElementType();
2383623893 return EltVT == MVT::f32 || EltVT == MVT::f64;
2383723894 };
2383823895
23896+ // Cast ptr32 and ptr64 pointers to the default address space before a store.
23897+ unsigned AddrSpace = ST->getAddressSpace();
23898+ if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23899+ AddrSpace == ARM64AS::PTR32_UPTR) {
23900+ MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23901+ if (PtrVT != Ptr.getSimpleValueType()) {
23902+ SDValue Cast = DAG.getAddrSpaceCast(DL, PtrVT, Ptr, AddrSpace, 0);
23903+ return DAG.getStore(Chain, DL, Value, Cast, ST->getPointerInfo(),
23904+ ST->getOriginalAlign(),
23905+ ST->getMemOperand()->getFlags(), ST->getAAInfo());
23906+ }
23907+ }
23908+
2383923909 if (SDValue Res = combineI8TruncStore(ST, DAG, Subtarget))
2384023910 return Res;
2384123911
@@ -23849,8 +23919,8 @@ static SDValue performSTORECombine(SDNode *N,
2384923919 ValueVT.isFixedLengthVector() &&
2385023920 ValueVT.getFixedSizeInBits() >= Subtarget->getMinSVEVectorSizeInBits() &&
2385123921 hasValidElementTypeForFPTruncStore(Value.getOperand(0).getValueType()))
23852- return DAG.getTruncStore(Chain, SDLoc(N) , Value.getOperand(0), Ptr,
23853- ST->getMemoryVT(), ST-> getMemOperand());
23922+ return DAG.getTruncStore(Chain, DL , Value.getOperand(0), Ptr, MemVT ,
23923+ ST->getMemOperand());
2385423924
2385523925 if (SDValue Split = splitStores(N, DCI, DAG, Subtarget))
2385623926 return Split;
@@ -27391,6 +27461,11 @@ void AArch64TargetLowering::ReplaceNodeResults(
2739127461 ReplaceATOMIC_LOAD_128Results(N, Results, DAG, Subtarget);
2739227462 return;
2739327463 }
27464+ case ISD::ADDRSPACECAST: {
27465+ SDValue V = LowerADDRSPACECAST(SDValue(N, 0), DAG);
27466+ Results.push_back(V);
27467+ return;
27468+ }
2739427469 case ISD::ATOMIC_LOAD:
2739527470 case ISD::LOAD: {
2739627471 MemSDNode *LoadNode = cast<MemSDNode>(N);
0 commit comments