Skip to content

Commit d049ea5

Browse files
committed
Adapt FEAT_CPA SDAG/GISel to shouldPreservePtrArith in targetLowering
1 parent 1a7982e commit d049ea5

File tree

6 files changed

+69
-53
lines changed

6 files changed

+69
-53
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30416,3 +30416,8 @@ bool AArch64TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
3041630416

3041730417
return TargetLowering::isTypeDesirableForOp(Opc, VT);
3041830418
}
30419+
30420+
bool AArch64TargetLowering::shouldPreservePtrArith(const Function &F,
30421+
EVT VT) const {
30422+
return Subtarget->hasCPA() && Subtarget->hasCPACodegen();
30423+
}

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ class AArch64TargetLowering : public TargetLowering {
539539
/// True if stack clash protection is enabled for this functions.
540540
bool hasInlineStackProbe(const MachineFunction &MF) const override;
541541

542+
/// In AArch64, true if FEAT_CPA is present. Allows pointer arithmetic
543+
/// semantics to be preserved for instruction selection.
544+
bool shouldPreservePtrArith(const Function &F, EVT PtrVT) const override;
545+
542546
private:
543547
/// Keep a pointer to the AArch64Subtarget around so that we can
544548
/// make the right decision when generating code for different targets.

llvm/lib/Target/AArch64/AArch64TargetMachine.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,3 @@ bool AArch64TargetMachine::parseMachineFunctionInfo(
932932
MF.getInfo<AArch64FunctionInfo>()->initializeBaseYamlFields(YamlMFI);
933933
return false;
934934
}
935-
936-
bool AArch64TargetMachine::shouldPreservePtrArith(const Function &F) const {
937-
return getSubtargetImpl(F)->hasCPA() && getSubtargetImpl(F)->hasCPACodegen();
938-
}

llvm/lib/Target/AArch64/AArch64TargetMachine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class AArch64TargetMachine : public CodeGenTargetMachineImpl {
7676
ScheduleDAGInstrs *
7777
createPostMachineScheduler(MachineSchedContext *C) const override;
7878

79-
/// In AArch64, true if FEAT_CPA is present. Allows pointer arithmetic
80-
/// semantics to be preserved for instruction selection.
81-
bool shouldPreservePtrArith(const Function &F) const override;
82-
8379
private:
8480
bool isLittle;
8581
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,12 +2197,14 @@ bool AArch64InstructionSelector::preISelLower(MachineInstr &I) {
21972197
}
21982198
return Changed;
21992199
}
2200-
case TargetOpcode::G_PTR_ADD:
2200+
case TargetOpcode::G_PTR_ADD: {
22012201
// If Checked Pointer Arithmetic (FEAT_CPA) is present, preserve the pointer
22022202
// arithmetic semantics instead of falling back to regular arithmetic.
2203-
if (TM.shouldPreservePtrArith(MF.getFunction()))
2203+
const auto &TL = STI.getTargetLowering();
2204+
if (TL->shouldPreservePtrArith(MF.getFunction(), EVT()))
22042205
return false;
22052206
return convertPtrAddToAdd(I, MRI);
2207+
}
22062208
case TargetOpcode::G_LOAD: {
22072209
// For scalar loads of pointers, we try to convert the dest type from p0
22082210
// to s64 so that our imported patterns can match. Like with the G_PTR_ADD

llvm/test/CodeGen/AArch64/cpa-selectiondag.ll

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,17 @@ define void @msubpt1(i32 %index, i32 %elem) {
119119
; CHECK-CPA-O0: // %bb.0: // %entry
120120
; CHECK-CPA-O0-NEXT: // implicit-def: $x8
121121
; CHECK-CPA-O0-NEXT: mov w8, w0
122-
; CHECK-CPA-O0-NEXT: sxtw x8, w8
123-
; CHECK-CPA-O0-NEXT: mov w9, #48 // =0x30
124-
; CHECK-CPA-O0-NEXT: // kill: def $x9 killed $w9
125-
; CHECK-CPA-O0-NEXT: mneg x8, x8, x9
126-
; CHECK-CPA-O0-NEXT: add x8, x8, #288
122+
; CHECK-CPA-O0-NEXT: sxtw x9, w8
123+
; CHECK-CPA-O0-NEXT: mov x8, xzr
124+
; CHECK-CPA-O0-NEXT: subs x8, x8, x9
125+
; CHECK-CPA-O0-NEXT: lsl x8, x8, #1
126+
; CHECK-CPA-O0-NEXT: subs x10, x8, x9
127127
; CHECK-CPA-O0-NEXT: adrp x9, array2
128128
; CHECK-CPA-O0-NEXT: add x9, x9, :lo12:array2
129+
; CHECK-CPA-O0-NEXT: mov w8, #288 // =0x120
130+
; CHECK-CPA-O0-NEXT: // kill: def $x8 killed $w8
129131
; CHECK-CPA-O0-NEXT: addpt x8, x9, x8
132+
; CHECK-CPA-O0-NEXT: addpt x8, x8, x10, lsl #4
130133
; CHECK-CPA-O0-NEXT: mov w10, #96 // =0x60
131134
; CHECK-CPA-O0-NEXT: // kill: def $x10 killed $w10
132135
; CHECK-CPA-O0-NEXT: addpt x10, x9, x10
@@ -141,19 +144,21 @@ define void @msubpt1(i32 %index, i32 %elem) {
141144
; CHECK-CPA-O3-LABEL: msubpt1:
142145
; CHECK-CPA-O3: // %bb.0: // %entry
143146
; CHECK-CPA-O3-NEXT: // kill: def $w0 killed $w0 def $x0
144-
; CHECK-CPA-O3-NEXT: sxtw x8, w0
145-
; CHECK-CPA-O3-NEXT: mov w9, #48 // =0x30
146-
; CHECK-CPA-O3-NEXT: adrp x10, array2
147-
; CHECK-CPA-O3-NEXT: add x10, x10, :lo12:array2
148-
; CHECK-CPA-O3-NEXT: mneg x8, x8, x9
149-
; CHECK-CPA-O3-NEXT: mov w9, #96 // =0x60
150-
; CHECK-CPA-O3-NEXT: ldr q2, [x10, #96]
151-
; CHECK-CPA-O3-NEXT: addpt x9, x10, x9
152-
; CHECK-CPA-O3-NEXT: ldp q1, q0, [x9, #16]
153-
; CHECK-CPA-O3-NEXT: add x8, x8, #288
154-
; CHECK-CPA-O3-NEXT: addpt x8, x10, x8
155-
; CHECK-CPA-O3-NEXT: stp q1, q0, [x8, #16]
156-
; CHECK-CPA-O3-NEXT: str q2, [x8]
147+
; CHECK-CPA-O3-NEXT: sxtw x9, w0
148+
; CHECK-CPA-O3-NEXT: adrp x8, array2
149+
; CHECK-CPA-O3-NEXT: add x8, x8, :lo12:array2
150+
; CHECK-CPA-O3-NEXT: mov w11, #96 // =0x60
151+
; CHECK-CPA-O3-NEXT: mov w12, #288 // =0x120
152+
; CHECK-CPA-O3-NEXT: ldr q2, [x8, #96]
153+
; CHECK-CPA-O3-NEXT: neg x10, x9
154+
; CHECK-CPA-O3-NEXT: addpt x11, x8, x11
155+
; CHECK-CPA-O3-NEXT: lsl x10, x10, #1
156+
; CHECK-CPA-O3-NEXT: ldp q1, q0, [x11, #16]
157+
; CHECK-CPA-O3-NEXT: sub x9, x10, x9
158+
; CHECK-CPA-O3-NEXT: addpt x10, x8, x12
159+
; CHECK-CPA-O3-NEXT: addpt x9, x10, x9, lsl #4
160+
; CHECK-CPA-O3-NEXT: stp q1, q0, [x9, #16]
161+
; CHECK-CPA-O3-NEXT: str q2, [x9]
157162
; CHECK-CPA-O3-NEXT: ret
158163
;
159164
; CHECK-NOCPA-O0-LABEL: msubpt1:
@@ -200,29 +205,29 @@ entry:
200205
define void @subpt1(i32 %index, i32 %elem) {
201206
; CHECK-CPA-O0-LABEL: subpt1:
202207
; CHECK-CPA-O0: // %bb.0: // %entry
203-
; CHECK-CPA-O0-NEXT: // implicit-def: $x8
204-
; CHECK-CPA-O0-NEXT: mov w8, w0
205-
; CHECK-CPA-O0-NEXT: sxtw x9, w8
206-
; CHECK-CPA-O0-NEXT: mov w8, #96 // =0x60
207-
; CHECK-CPA-O0-NEXT: // kill: def $x8 killed $w8
208-
; CHECK-CPA-O0-NEXT: subs x8, x8, x9, lsl #8
209208
; CHECK-CPA-O0-NEXT: adrp x9, array
210209
; CHECK-CPA-O0-NEXT: add x9, x9, :lo12:array
210+
; CHECK-CPA-O0-NEXT: mov w8, #96 // =0x60
211+
; CHECK-CPA-O0-NEXT: // kill: def $x8 killed $w8
211212
; CHECK-CPA-O0-NEXT: addpt x8, x9, x8
213+
; CHECK-CPA-O0-NEXT: // implicit-def: $x10
214+
; CHECK-CPA-O0-NEXT: mov w10, w0
215+
; CHECK-CPA-O0-NEXT: sbfiz x10, x10, #8, #32
216+
; CHECK-CPA-O0-NEXT: subpt x8, x8, x10
212217
; CHECK-CPA-O0-NEXT: ldr q0, [x9, #32]
213218
; CHECK-CPA-O0-NEXT: str q0, [x8]
214219
; CHECK-CPA-O0-NEXT: ret
215220
;
216221
; CHECK-CPA-O3-LABEL: subpt1:
217222
; CHECK-CPA-O3: // %bb.0: // %entry
218223
; CHECK-CPA-O3-NEXT: // kill: def $w0 killed $w0 def $x0
219-
; CHECK-CPA-O3-NEXT: sxtw x8, w0
224+
; CHECK-CPA-O3-NEXT: adrp x8, array
225+
; CHECK-CPA-O3-NEXT: add x8, x8, :lo12:array
220226
; CHECK-CPA-O3-NEXT: mov w9, #96 // =0x60
221-
; CHECK-CPA-O3-NEXT: sub x8, x9, x8, lsl #8
222-
; CHECK-CPA-O3-NEXT: adrp x9, array
223-
; CHECK-CPA-O3-NEXT: add x9, x9, :lo12:array
224-
; CHECK-CPA-O3-NEXT: ldr q0, [x9, #32]
225-
; CHECK-CPA-O3-NEXT: addpt x8, x9, x8
227+
; CHECK-CPA-O3-NEXT: sbfiz x10, x0, #8, #32
228+
; CHECK-CPA-O3-NEXT: addpt x9, x8, x9
229+
; CHECK-CPA-O3-NEXT: ldr q0, [x8, #32]
230+
; CHECK-CPA-O3-NEXT: subpt x8, x9, x10
226231
; CHECK-CPA-O3-NEXT: str q0, [x8]
227232
; CHECK-CPA-O3-NEXT: ret
228233
;
@@ -259,24 +264,28 @@ entry:
259264
define void @subpt2(i32 %index, i32 %elem) {
260265
; CHECK-CPA-O0-LABEL: subpt2:
261266
; CHECK-CPA-O0: // %bb.0: // %entry
262-
; CHECK-CPA-O0-NEXT: mov w8, #96 // =0x60
263-
; CHECK-CPA-O0-NEXT: // kill: def $x8 killed $w8
264-
; CHECK-CPA-O0-NEXT: subs x8, x8, w0, sxtw #4
267+
; CHECK-CPA-O0-NEXT: mov x8, xzr
268+
; CHECK-CPA-O0-NEXT: subs x10, x8, w0, sxtw
265269
; CHECK-CPA-O0-NEXT: adrp x9, array
266270
; CHECK-CPA-O0-NEXT: add x9, x9, :lo12:array
271+
; CHECK-CPA-O0-NEXT: mov w8, #96 // =0x60
272+
; CHECK-CPA-O0-NEXT: // kill: def $x8 killed $w8
267273
; CHECK-CPA-O0-NEXT: addpt x8, x9, x8
274+
; CHECK-CPA-O0-NEXT: addpt x8, x8, x10, lsl #4
268275
; CHECK-CPA-O0-NEXT: ldr q0, [x9, #32]
269276
; CHECK-CPA-O0-NEXT: str q0, [x8]
270277
; CHECK-CPA-O0-NEXT: ret
271278
;
272279
; CHECK-CPA-O3-LABEL: subpt2:
273280
; CHECK-CPA-O3: // %bb.0: // %entry
274-
; CHECK-CPA-O3-NEXT: mov w8, #96 // =0x60
275-
; CHECK-CPA-O3-NEXT: adrp x9, array
276-
; CHECK-CPA-O3-NEXT: add x9, x9, :lo12:array
277-
; CHECK-CPA-O3-NEXT: sub x8, x8, w0, sxtw #4
278-
; CHECK-CPA-O3-NEXT: ldr q0, [x9, #32]
279-
; CHECK-CPA-O3-NEXT: addpt x8, x9, x8
281+
; CHECK-CPA-O3-NEXT: mov x8, xzr
282+
; CHECK-CPA-O3-NEXT: mov w9, #96 // =0x60
283+
; CHECK-CPA-O3-NEXT: adrp x10, array
284+
; CHECK-CPA-O3-NEXT: add x10, x10, :lo12:array
285+
; CHECK-CPA-O3-NEXT: sub x8, x8, w0, sxtw
286+
; CHECK-CPA-O3-NEXT: addpt x9, x10, x9
287+
; CHECK-CPA-O3-NEXT: ldr q0, [x10, #32]
288+
; CHECK-CPA-O3-NEXT: addpt x8, x9, x8, lsl #4
280289
; CHECK-CPA-O3-NEXT: str q0, [x8]
281290
; CHECK-CPA-O3-NEXT: ret
282291
;
@@ -656,14 +665,18 @@ define hidden void @multidim() {
656665
; CHECK-CPA-O0-LABEL: multidim:
657666
; CHECK-CPA-O0: // %bb.0: // %entry
658667
; CHECK-CPA-O0-NEXT: adrp x8, b
659-
; CHECK-CPA-O0-NEXT: ldrh w10, [x8, :lo12:b]
668+
; CHECK-CPA-O0-NEXT: ldrh w9, [x8, :lo12:b]
669+
; CHECK-CPA-O0-NEXT: mov w8, w9
670+
; CHECK-CPA-O0-NEXT: mov w10, w8
660671
; CHECK-CPA-O0-NEXT: adrp x8, a
661672
; CHECK-CPA-O0-NEXT: add x8, x8, :lo12:a
662-
; CHECK-CPA-O0-NEXT: add w9, w10, #1
673+
; CHECK-CPA-O0-NEXT: mov w11, #2 // =0x2
674+
; CHECK-CPA-O0-NEXT: // kill: def $x11 killed $w11
675+
; CHECK-CPA-O0-NEXT: addpt x8, x8, x11
676+
; CHECK-CPA-O0-NEXT: addpt x8, x8, x10, lsl #1
677+
; CHECK-CPA-O0-NEXT: add w9, w9, #1
663678
; CHECK-CPA-O0-NEXT: mov w9, w9
664679
; CHECK-CPA-O0-NEXT: // kill: def $x9 killed $w9
665-
; CHECK-CPA-O0-NEXT: add x9, x9, w10, uxtw #1
666-
; CHECK-CPA-O0-NEXT: add x9, x9, #2
667680
; CHECK-CPA-O0-NEXT: addpt x8, x8, x9
668681
; CHECK-CPA-O0-NEXT: ldrb w8, [x8]
669682
; CHECK-CPA-O0-NEXT: cbz w8, .LBB14_2

0 commit comments

Comments
 (0)