diff --git a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp --- a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp +++ b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp @@ -696,10 +696,12 @@ return true; } case AArch64::TAGPstack: { - BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDG)) + int64_t Offset = MI.getOperand(2).getImm(); + BuildMI(MBB, MBBI, MI.getDebugLoc(), + TII->get(Offset >= 0 ? AArch64::ADDG : AArch64::SUBG)) .add(MI.getOperand(0)) .add(MI.getOperand(1)) - .add(MI.getOperand(2)) + .addImm(std::abs(Offset)) .add(MI.getOperand(4)); MI.eraseFromParent(); return true; diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -2188,12 +2188,19 @@ MaxOffset = 4095; break; case AArch64::ADDG: - case AArch64::TAGPstack: Scale = 16; Width = 0; MinOffset = 0; MaxOffset = 63; break; + case AArch64::TAGPstack: + Scale = 16; + Width = 0; + // TAGP with a negative offset turns into SUBP, which has a maximum offset + // of 63 (not 64!). + MinOffset = -63; + MaxOffset = 63; + break; case AArch64::LDG: case AArch64::STGOffset: case AArch64::STZGOffset: diff --git a/llvm/test/CodeGen/AArch64/addg_subg.mir b/llvm/test/CodeGen/AArch64/addg_subg.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/addg_subg.mir @@ -0,0 +1,37 @@ +# RUN: llc -mtriple=aarch64 -run-pass=prologepilog,aarch64-expand-pseudo %s -o - | FileCheck %s + +# CHECK: renamable $x8 = IRG $sp, $xzr + +# CHECK: renamable $x0 = ADDG $x8, 0, 0 +# CHECK: renamable $x0 = ADDG $x8, 5, 0 +# CHECK: renamable $x0 = ADDG $x8, 63, 0 +# CHECK: $[[R:x[0-9]+]] = ADDXri $x8, 16, 0 +# CHECK: renamable $x0 = ADDG killed $[[R]], 63, 0 + +# CHECK: renamable $x0 = SUBG $x8, 5, 0 +# CHECK: renamable $x0 = SUBG $x8, 63, 0 +# CHECK: $[[R:x[0-9]+]] = SUBXri $x8, 16, 0 +# CHECK: renamable $x0 = SUBG killed $[[R]], 63, 0 + +--- +name: subg +stack: + - { id: 0, type: default, offset: 0, size: 16, alignment: 16, + stack-id: default, callee-saved-register: '', callee-saved-restored: true, + local-offset: -16, debug-info-variable: '', debug-info-expression: '', + debug-info-location: '' } +body: | + bb.0.entry: + renamable $x8 = IRGstack $sp, $xzr + + renamable $x0 = TAGPstack %stack.0, 0, killed renamable $x8, 0 + renamable $x0 = TAGPstack %stack.0, 5, killed renamable $x8, 0 + renamable $x0 = TAGPstack %stack.0, 63, killed renamable $x8, 0 + renamable $x0 = TAGPstack %stack.0, 64, killed renamable $x8, 0 + + renamable $x0 = TAGPstack %stack.0, -5, killed renamable $x8, 0 + renamable $x0 = TAGPstack %stack.0, -63, killed renamable $x8, 0 + renamable $x0 = TAGPstack %stack.0, -64, killed renamable $x8, 0 + RET_ReallyLR + +...