Skip to content

Commit 79e207b

Browse files
committed
[llvm][aarch64] Add support for the MS qualifiers __ptr32, __ptr64, __sptr, __uptr
1 parent 956c070 commit 79e207b

File tree

5 files changed

+278
-13
lines changed

5 files changed

+278
-13
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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();
@@ -23558,6 +23594,26 @@ static SDValue performLOADCombine(SDNode *N,
2355823594
performTBISimplification(N->getOperand(1), DCI, DAG);
2355923595

2356023596
LoadSDNode *LD = cast<LoadSDNode>(N);
23597+
EVT RegVT = LD->getValueType(0);
23598+
EVT MemVT = LD->getMemoryVT();
23599+
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23600+
SDLoc DL(LD);
23601+
23602+
// Cast ptr32 and ptr64 pointers to the default address space before a load.
23603+
unsigned AddrSpace = LD->getAddressSpace();
23604+
if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23605+
AddrSpace == ARM64AS::PTR32_UPTR) {
23606+
MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23607+
if (PtrVT != LD->getBasePtr().getSimpleValueType()) {
23608+
SDValue Cast =
23609+
DAG.getAddrSpaceCast(DL, PtrVT, LD->getBasePtr(), AddrSpace, 0);
23610+
return DAG.getExtLoad(LD->getExtensionType(), DL, RegVT, LD->getChain(),
23611+
Cast, LD->getPointerInfo(), MemVT,
23612+
LD->getOriginalAlign(),
23613+
LD->getMemOperand()->getFlags());
23614+
}
23615+
}
23616+
2356123617
if (LD->isVolatile() || !Subtarget->isLittleEndian())
2356223618
return SDValue(N, 0);
2356323619

@@ -23567,13 +23623,11 @@ static SDValue performLOADCombine(SDNode *N,
2356723623
if (!LD->isNonTemporal())
2356823624
return SDValue(N, 0);
2356923625

23570-
EVT MemVT = LD->getMemoryVT();
2357123626
if (MemVT.isScalableVector() || MemVT.getSizeInBits() <= 256 ||
2357223627
MemVT.getSizeInBits() % 256 == 0 ||
2357323628
256 % MemVT.getScalarSizeInBits() != 0)
2357423629
return SDValue(N, 0);
2357523630

23576-
SDLoc DL(LD);
2357723631
SDValue Chain = LD->getChain();
2357823632
SDValue BasePtr = LD->getBasePtr();
2357923633
SDNodeFlags Flags = LD->getFlags();
@@ -23833,12 +23887,28 @@ static SDValue performSTORECombine(SDNode *N,
2383323887
SDValue Value = ST->getValue();
2383423888
SDValue Ptr = ST->getBasePtr();
2383523889
EVT ValueVT = Value.getValueType();
23890+
EVT MemVT = ST->getMemoryVT();
23891+
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23892+
SDLoc DL(ST);
2383623893

2383723894
auto hasValidElementTypeForFPTruncStore = [](EVT VT) {
2383823895
EVT EltVT = VT.getVectorElementType();
2383923896
return EltVT == MVT::f32 || EltVT == MVT::f64;
2384023897
};
2384123898

23899+
// Cast ptr32 and ptr64 pointers to the default address space before a store.
23900+
unsigned AddrSpace = ST->getAddressSpace();
23901+
if (AddrSpace == ARM64AS::PTR64 || AddrSpace == ARM64AS::PTR32_SPTR ||
23902+
AddrSpace == ARM64AS::PTR32_UPTR) {
23903+
MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
23904+
if (PtrVT != Ptr.getSimpleValueType()) {
23905+
SDValue Cast = DAG.getAddrSpaceCast(DL, PtrVT, Ptr, AddrSpace, 0);
23906+
return DAG.getStore(Chain, DL, Value, Cast, ST->getPointerInfo(),
23907+
ST->getOriginalAlign(),
23908+
ST->getMemOperand()->getFlags(), ST->getAAInfo());
23909+
}
23910+
}
23911+
2384223912
if (SDValue Res = combineI8TruncStore(ST, DAG, Subtarget))
2384323913
return Res;
2384423914

@@ -23852,8 +23922,8 @@ static SDValue performSTORECombine(SDNode *N,
2385223922
ValueVT.isFixedLengthVector() &&
2385323923
ValueVT.getFixedSizeInBits() >= Subtarget->getMinSVEVectorSizeInBits() &&
2385423924
hasValidElementTypeForFPTruncStore(Value.getOperand(0).getValueType()))
23855-
return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0), Ptr,
23856-
ST->getMemoryVT(), ST->getMemOperand());
23925+
return DAG.getTruncStore(Chain, DL, Value.getOperand(0), Ptr, MemVT,
23926+
ST->getMemOperand());
2385723927

2385823928
if (SDValue Split = splitStores(N, DCI, DAG, Subtarget))
2385923929
return Split;
@@ -27394,6 +27464,11 @@ void AArch64TargetLowering::ReplaceNodeResults(
2739427464
ReplaceATOMIC_LOAD_128Results(N, Results, DAG, Subtarget);
2739527465
return;
2739627466
}
27467+
case ISD::ADDRSPACECAST: {
27468+
SDValue V = LowerADDRSPACECAST(SDValue(N, 0), DAG);
27469+
Results.push_back(V);
27470+
return;
27471+
}
2739727472
case ISD::ATOMIC_LOAD:
2739827473
case ISD::LOAD: {
2739927474
MemSDNode *LoadNode = cast<MemSDNode>(N);

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ const unsigned StackProbeMaxLoopUnroll = 4;
563563

564564
} // namespace AArch64
565565

566+
namespace ARM64AS {
567+
enum : unsigned { PTR32_SPTR = 270, PTR32_UPTR = 271, PTR64 = 272 };
568+
}
569+
566570
class AArch64Subtarget;
567571

568572
class AArch64TargetLowering : public TargetLowering {
@@ -594,11 +598,19 @@ class AArch64TargetLowering : public TargetLowering {
594598
unsigned Depth) const override;
595599

596600
MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const override {
597-
// Returning i64 unconditionally here (i.e. even for ILP32) means that the
598-
// *DAG* representation of pointers will always be 64-bits. They will be
599-
// truncated and extended when transferred to memory, but the 64-bit DAG
600-
// allows us to use AArch64's addressing modes much more easily.
601-
return MVT::getIntegerVT(64);
601+
if ((AS == ARM64AS::PTR32_SPTR) || (AS == ARM64AS::PTR32_UPTR)) {
602+
// These are 32-bit pointers created using the `__ptr32` extension or
603+
// similar. They are handled by marking them as being in a different
604+
// address space, and will be extended to 64-bits when used as the target
605+
// of a load or store operation, or cast to a 64-bit pointer type.
606+
return MVT::i32;
607+
} else {
608+
// Returning i64 unconditionally here (i.e. even for ILP32) means that the
609+
// *DAG* representation of pointers will always be 64-bits. They will be
610+
// truncated and extended when transferred to memory, but the 64-bit DAG
611+
// allows us to use AArch64's addressing modes much more easily.
612+
return MVT::i64;
613+
}
602614
}
603615

604616
bool targetShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits,

llvm/lib/Target/AArch64/AArch64TargetMachine.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class AArch64TargetMachine : public CodeGenTargetMachineImpl {
6868

6969
/// Returns true if a cast between SrcAS and DestAS is a noop.
7070
bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {
71-
// Addrspacecasts are always noops.
72-
return true;
71+
return getPointerSize(SrcAS) == getPointerSize(DestAS);
7372
}
7473

7574
private:

llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,9 +2999,8 @@ bool AArch64InstructionSelector::select(MachineInstr &I) {
29992999
bool IsZExtLoad = I.getOpcode() == TargetOpcode::G_ZEXTLOAD;
30003000
LLT PtrTy = MRI.getType(LdSt.getPointerReg());
30013001

3002+
// Can only handle AddressSpace 0, 64-bit pointers.
30023003
if (PtrTy != LLT::pointer(0, 64)) {
3003-
LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
3004-
<< ", expected: " << LLT::pointer(0, 64) << '\n');
30053004
return false;
30063005
}
30073006

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s | FileCheck %s
3+
4+
; Source to regenerate:
5+
; struct Foo {
6+
; int * __ptr32 p32;
7+
; int * __ptr64 p64;
8+
; __attribute__((address_space(9))) int *p_other;
9+
; };
10+
; extern "C" void use_foo(Foo *f);
11+
; extern "C" int use_int(int i);
12+
; extern "C" void test_sign_ext(Foo *f, int * __ptr32 __sptr i) {
13+
; f->p64 = i;
14+
; use_foo(f);
15+
; }
16+
; extern "C" void test_sign_ext_store_load(int * __ptr32 __sptr i) {
17+
; *i = use_int(*i);
18+
; }
19+
; extern "C" void test_zero_ext(Foo *f, int * __ptr32 __uptr i) {
20+
; f->p64 = i;
21+
; use_foo(f);
22+
; }
23+
; extern "C" void test_zero_ext_store_load(int * __ptr32 __uptr i) {
24+
; *i = use_int(*i);
25+
; }
26+
; extern "C" void test_trunc(Foo *f, int * __ptr64 i) {
27+
; f->p32 = i;
28+
; use_foo(f);
29+
; }
30+
; extern "C" void test_noop1(Foo *f, int * __ptr32 i) {
31+
; f->p32 = i;
32+
; use_foo(f);
33+
; }
34+
; extern "C" void test_noop2(Foo *f, int * __ptr64 i) {
35+
; f->p64 = i;
36+
; use_foo(f);
37+
; }
38+
; extern "C" void test_null_arg(Foo *f, int * __ptr32 i) {
39+
; test_noop1(f, 0);
40+
; }
41+
; extern "C" void test_unrecognized(Foo *f, __attribute__((address_space(14))) int *i) {
42+
; f->p32 = (int * __ptr32)i;
43+
; use_foo(f);
44+
; }
45+
;
46+
; $ clang --target=aarch64-windows-msvc -fms-extensions -O2 -S -emit-llvm t.cpp
47+
48+
target datalayout = "e-m:w-p:64:64-i32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
49+
target triple = "aarch64-unknown-windows-msvc"
50+
51+
; Function Attrs: mustprogress uwtable
52+
define dso_local void @test_sign_ext(ptr noundef %f, ptr addrspace(270) noundef %i) local_unnamed_addr #0 {
53+
; CHECK-LABEL: test_sign_ext:
54+
; CHECK: // %bb.0: // %entry
55+
; CHECK-NEXT: // kill: def $w1 killed $w1 def $x1
56+
; CHECK-NEXT: sxtw x8, w1
57+
; CHECK-NEXT: str x8, [x0, #8]
58+
; CHECK-NEXT: b use_foo
59+
entry:
60+
%0 = addrspacecast ptr addrspace(270) %i to ptr
61+
%p64 = getelementptr inbounds nuw i8, ptr %f, i64 8
62+
store ptr %0, ptr %p64, align 8
63+
tail call void @use_foo(ptr noundef %f)
64+
ret void
65+
}
66+
67+
declare dso_local void @use_foo(ptr noundef) local_unnamed_addr #1
68+
69+
; Function Attrs: mustprogress uwtable
70+
define dso_local void @test_sign_ext_store_load(ptr addrspace(270) nocapture noundef %i) local_unnamed_addr #0 {
71+
; CHECK-LABEL: test_sign_ext_store_load:
72+
; CHECK: // %bb.0: // %entry
73+
; CHECK: sxtw x19, w0
74+
; CHECK-NEXT: ldr w0, [x19]
75+
; CHECK-NEXT: bl use_int
76+
; CHECK-NEXT: str w0, [x19]
77+
entry:
78+
%0 = load i32, ptr addrspace(270) %i, align 4
79+
%call = tail call i32 @use_int(i32 noundef %0)
80+
store i32 %call, ptr addrspace(270) %i, align 4
81+
ret void
82+
}
83+
84+
declare dso_local i32 @use_int(i32 noundef) local_unnamed_addr #1
85+
86+
; Function Attrs: mustprogress uwtable
87+
define dso_local void @test_zero_ext(ptr noundef %f, ptr addrspace(271) noundef %i) local_unnamed_addr #0 {
88+
; CHECK-LABEL: test_zero_ext:
89+
; CHECK: // %bb.0: // %entry
90+
; CHECK-NEXT: mov w8, w1
91+
; CHECK-NEXT: str x8, [x0, #8]
92+
; CHECK-NEXT: b use_foo
93+
entry:
94+
%0 = addrspacecast ptr addrspace(271) %i to ptr
95+
%p64 = getelementptr inbounds nuw i8, ptr %f, i64 8
96+
store ptr %0, ptr %p64, align 8
97+
tail call void @use_foo(ptr noundef %f)
98+
ret void
99+
}
100+
101+
; Function Attrs: mustprogress uwtable
102+
define dso_local void @test_zero_ext_store_load(ptr addrspace(271) nocapture noundef %i) local_unnamed_addr #0 {
103+
; CHECK-LABEL: test_zero_ext_store_load:
104+
; CHECK: // %bb.0: // %entry
105+
; CHECK: mov w19, w0
106+
; CHECK-NEXT: ldr w0, [x19]
107+
; CHECK-NEXT: bl use_int
108+
; CHECK-NEXT: str w0, [x19]
109+
entry:
110+
%0 = load i32, ptr addrspace(271) %i, align 4
111+
%call = tail call i32 @use_int(i32 noundef %0)
112+
store i32 %call, ptr addrspace(271) %i, align 4
113+
ret void
114+
}
115+
116+
; Function Attrs: mustprogress uwtable
117+
define dso_local void @test_trunc(ptr noundef %f, ptr noundef %i) local_unnamed_addr #0 {
118+
; CHECK-LABEL: test_trunc:
119+
; CHECK: // %bb.0: // %entry
120+
; CHECK-NEXT: str w1, [x0]
121+
; CHECK-NEXT: b use_foo
122+
entry:
123+
%0 = addrspacecast ptr %i to ptr addrspace(270)
124+
store ptr addrspace(270) %0, ptr %f, align 8
125+
tail call void @use_foo(ptr noundef nonnull %f)
126+
ret void
127+
}
128+
129+
; Function Attrs: mustprogress uwtable
130+
define dso_local void @test_noop1(ptr noundef %f, ptr addrspace(270) noundef %i) local_unnamed_addr #0 {
131+
; CHECK-LABEL: test_noop1:
132+
; CHECK: // %bb.0: // %entry
133+
; CHECK-NEXT: str w1, [x0]
134+
; CHECK-NEXT: b use_foo
135+
entry:
136+
store ptr addrspace(270) %i, ptr %f, align 8
137+
tail call void @use_foo(ptr noundef nonnull %f)
138+
ret void
139+
}
140+
141+
; Function Attrs: mustprogress uwtable
142+
define dso_local void @test_noop2(ptr noundef %f, ptr noundef %i) local_unnamed_addr #0 {
143+
; CHECK-LABEL: test_noop2:
144+
; CHECK: // %bb.0: // %entry
145+
; CHECK-NEXT: str x1, [x0, #8]
146+
; CHECK-NEXT: b use_foo
147+
entry:
148+
%p64 = getelementptr inbounds nuw i8, ptr %f, i64 8
149+
store ptr %i, ptr %p64, align 8
150+
tail call void @use_foo(ptr noundef %f)
151+
ret void
152+
}
153+
154+
; Function Attrs: mustprogress uwtable
155+
define dso_local void @test_null_arg(ptr noundef %f, ptr addrspace(270) nocapture noundef readnone %i) local_unnamed_addr #0 {
156+
; CHECK-LABEL: test_null_arg:
157+
; CHECK: // %bb.0: // %entry
158+
; CHECK-NEXT: str wzr, [x0]
159+
; CHECK-NEXT: b use_foo
160+
entry:
161+
store ptr addrspace(270) null, ptr %f, align 8
162+
tail call void @use_foo(ptr noundef nonnull %f)
163+
ret void
164+
}
165+
166+
; Function Attrs: mustprogress uwtable
167+
define dso_local void @test_unrecognized(ptr noundef %f, ptr addrspace(14) noundef %i) local_unnamed_addr #0 {
168+
; CHECK-LABEL: test_unrecognized:
169+
; CHECK: // %bb.0: // %entry
170+
; CHECK-NEXT: str w1, [x0]
171+
; CHECK-NEXT: b use_foo
172+
entry:
173+
%0 = addrspacecast ptr addrspace(14) %i to ptr addrspace(270)
174+
store ptr addrspace(270) %0, ptr %f, align 8
175+
tail call void @use_foo(ptr noundef nonnull %f)
176+
ret void
177+
}
178+
179+
attributes #0 = { mustprogress uwtable "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }
180+
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }

0 commit comments

Comments
 (0)