Skip to content

Commit 37fd5fa

Browse files
committed
[AArch64] Fix LDR/STR folding causing memtag failures
When generating code with sanitize_memtag, we make use of the fact that the sp+imm forms of many load and store instructions are not tag-checked, so we can use SP directly instead of needing a register holding the tagged pointer. However, this isn't true for the writeback versions of the instructions, so we can't fold ADDs and SUBs into them in AArch64LoadStoreOptimizer. This would be possible in cases where the loads/stores only access untagged stack slots, but that information isn't easily available after frame index elimination.
1 parent 7a9d84e commit 37fd5fa

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ static bool isPromotableLoadFromStore(MachineInstr &MI) {
733733
}
734734
}
735735

736-
static bool isMergeableLdStUpdate(MachineInstr &MI) {
736+
static bool isMergeableLdStUpdate(MachineInstr &MI, AArch64FunctionInfo &AFI) {
737737
unsigned Opc = MI.getOpcode();
738738
switch (Opc) {
739739
default:
@@ -785,6 +785,15 @@ static bool isMergeableLdStUpdate(MachineInstr &MI) {
785785
if (!AArch64InstrInfo::getLdStOffsetOp(MI).isImm())
786786
return false;
787787

788+
// When using stack tagging, simple sp+imm loads and stores are not
789+
// tag-checked, but pre- and post-indexed versions of them are, so we can't
790+
// replace the former with the latter. This transformation would be valid
791+
// if the load/store accesses an untagged stack slot, but we don't have
792+
// that information available after frame indices have been eliminated.
793+
if (AFI.isMTETagged() &&
794+
AArch64InstrInfo::getLdStBaseOp(MI).getReg() == AArch64::SP)
795+
return false;
796+
788797
return true;
789798
}
790799
}
@@ -2772,6 +2781,7 @@ bool AArch64LoadStoreOpt::tryToMergeIndexLdSt(MachineBasicBlock::iterator &MBBI,
27722781

27732782
bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
27742783
bool EnableNarrowZeroStOpt) {
2784+
AArch64FunctionInfo &AFI = *MBB.getParent()->getInfo<AArch64FunctionInfo>();
27752785

27762786
bool Modified = false;
27772787
// Four tranformations to do here:
@@ -2842,7 +2852,7 @@ bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
28422852
// ldr x0, [x2], #4
28432853
for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
28442854
MBBI != E;) {
2845-
if (isMergeableLdStUpdate(*MBBI) && tryToMergeLdStUpdate(MBBI))
2855+
if (isMergeableLdStUpdate(*MBBI, AFI) && tryToMergeLdStUpdate(MBBI))
28462856
Modified = true;
28472857
else
28482858
++MBBI;

llvm/test/CodeGen/AArch64/memtag-merge-writeback.mir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ body: |
6363
; CHECK: liveins: $x0
6464
; CHECK-NEXT: {{ $}}
6565
; CHECK-NEXT: $sp = frame-setup SUBXri $sp, 16, 0
66-
; CHECK-NEXT: early-clobber $sp = STRXpre killed renamable $x0, $sp, 16
66+
; CHECK-NEXT: STRXui killed renamable $x0, $sp, 2
67+
; CHECK-NEXT: $sp = ADDXri $sp, 16, 0
6768
; CHECK-NEXT: RET undef $lr
6869
$sp = frame-setup SUBXri $sp, 16, 0
6970
STRXui killed renamable $x0, $sp, 2
@@ -114,7 +115,8 @@ body: |
114115
; CHECK: liveins: $x0
115116
; CHECK-NEXT: {{ $}}
116117
; CHECK-NEXT: $sp = frame-setup SUBXri $sp, 16, 0
117-
; CHECK-NEXT: early-clobber $sp = STRXpost killed renamable $x0, $sp, 16
118+
; CHECK-NEXT: STRXui killed renamable $x0, $sp, 0
119+
; CHECK-NEXT: $sp = ADDXri $sp, 16, 0
118120
; CHECK-NEXT: RET undef $lr
119121
$sp = frame-setup SUBXri $sp, 16, 0
120122
STRXui killed renamable $x0, $sp, 0

0 commit comments

Comments
 (0)