[llvm] [Hexagon] Refresh stack aligna after late HVX spill (PR #204660)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 23 06:21:25 PDT 2026
https://github.com/L-roro updated https://github.com/llvm/llvm-project/pull/204660
>From e81b0d42866eb448d0ebc98c8751d99d80e27d10 Mon Sep 17 00:00:00 2001
From: L-roro <rodriguez at roofline.ai>
Date: Mon, 13 Apr 2026 13:10:48 +0000
Subject: [PATCH] Fix Hexagon backend alignment bug
---
.../Target/Hexagon/HexagonFrameLowering.cpp | 177 ++++++++++--------
.../lib/Target/Hexagon/HexagonFrameLowering.h | 8 +-
.../Target/Hexagon/HexagonISelDAGToDAG.cpp | 31 +--
llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.h | 4 -
.../Target/Hexagon/HexagonRegisterInfo.cpp | 46 ++++-
llvm/lib/Target/Hexagon/HexagonRegisterInfo.h | 11 ++
llvm/lib/Target/Hexagon/HexagonVExtract.cpp | 11 --
.../Hexagon/aligna-prologue-expansion.mir | 1 -
.../CodeGen/Hexagon/aligna-save-base-reg.ll | 26 +++
.../Hexagon/spill-vector-alignment.mir | 14 +-
10 files changed, 203 insertions(+), 126 deletions(-)
create mode 100644 llvm/test/CodeGen/Hexagon/aligna-save-base-reg.ll
diff --git a/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp b/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp
index 65922736b325f..dbf1ec504984d 100644
--- a/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp
@@ -360,60 +360,65 @@ static Register getMaxCalleeSavedReg(ArrayRef<CalleeSavedInfo> CSI,
/// frame to be already in place.
static bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR,
const HexagonRegisterInfo &HRI) {
- for (const MachineInstr &MI : MBB) {
- if (MI.isCall())
+ const MachineFunction *MF = MBB.getParent();
+ if (&MBB == &MF->front() && MF->getInfo<HexagonMachineFunctionInfo>()
+ ->getStackAlignBaseReg()
+ .isValid())
+ return true;
+
+ for (const MachineInstr &MI : MBB) {
+ if (MI.isCall())
+ return true;
+ unsigned Opc = MI.getOpcode();
+ switch (Opc) {
+ case Hexagon::PS_alloca:
+ return true;
+ default:
+ break;
+ }
+ // Check individual operands.
+ for (const MachineOperand &MO : MI.operands()) {
+ // While the presence of a frame index does not prove that a stack
+ // frame will be required, all frame indexes should be within alloc-
+ // frame/deallocframe. Otherwise, the code that translates a frame
+ // index into an offset would have to be aware of the placement of
+ // the frame creation/destruction instructions.
+ if (MO.isFI())
return true;
- unsigned Opc = MI.getOpcode();
- switch (Opc) {
- case Hexagon::PS_alloca:
- case Hexagon::PS_aligna:
+ if (MO.isReg()) {
+ Register R = MO.getReg();
+ // Debug instructions may refer to $noreg.
+ if (!R)
+ continue;
+ // Virtual registers will need scavenging, which then may require
+ // a stack slot.
+ if (R.isVirtual())
return true;
- default:
- break;
+ for (MCPhysReg S : HRI.subregs_inclusive(R))
+ if (CSR[S])
+ return true;
+ continue;
}
- // Check individual operands.
- for (const MachineOperand &MO : MI.operands()) {
- // While the presence of a frame index does not prove that a stack
- // frame will be required, all frame indexes should be within alloc-
- // frame/deallocframe. Otherwise, the code that translates a frame
- // index into an offset would have to be aware of the placement of
- // the frame creation/destruction instructions.
- if (MO.isFI())
- return true;
- if (MO.isReg()) {
- Register R = MO.getReg();
- // Debug instructions may refer to $noreg.
- if (!R)
- continue;
- // Virtual registers will need scavenging, which then may require
- // a stack slot.
- if (R.isVirtual())
+ if (MO.isRegMask()) {
+ // A regmask would normally have all callee-saved registers marked
+ // as preserved, so this check would not be needed, but in case of
+ // ever having other regmasks (for other calling conventions),
+ // make sure they would be processed correctly.
+ const uint32_t *BM = MO.getRegMask();
+ for (int x = CSR.find_first(); x >= 0; x = CSR.find_next(x)) {
+ unsigned R = x;
+ // If this regmask does not preserve a CSR, a frame will be needed.
+ if (!(BM[R / 32] & (1u << (R % 32))))
return true;
- for (MCPhysReg S : HRI.subregs_inclusive(R))
- if (CSR[S])
- return true;
- continue;
- }
- if (MO.isRegMask()) {
- // A regmask would normally have all callee-saved registers marked
- // as preserved, so this check would not be needed, but in case of
- // ever having other regmasks (for other calling conventions),
- // make sure they would be processed correctly.
- const uint32_t *BM = MO.getRegMask();
- for (int x = CSR.find_first(); x >= 0; x = CSR.find_next(x)) {
- unsigned R = x;
- // If this regmask does not preserve a CSR, a frame will be needed.
- if (!(BM[R/32] & (1u << (R%32))))
- return true;
- }
}
}
}
- return false;
+ }
+ return false;
}
- /// Returns true if MBB has a machine instructions that indicates a tail call
- /// in the block.
+/// Returns true if MBB has a machine instructions that indicates a tail call
+/// in the block.
static bool hasTailCall(const MachineBasicBlock &MBB) {
MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
if (I == MBB.end())
@@ -595,7 +600,14 @@ void HexagonFrameLowering::emitPrologue(MachineFunction &MF,
findShrunkPrologEpilog(MF, PrologB, EpilogB);
bool PrologueStubs = false;
- insertCSRSpillsInBlock(*PrologB, CSI, HRI, PrologueStubs);
+ MachineBasicBlock::iterator AfterCSR =
+ insertCSRSpillsInBlock(*PrologB, CSI, HRI, PrologueStubs);
+ // Insert PS_aligna after all CSR spills.
+ // PS_aligna initializes the AP register with an aligned
+ // value derived from FP. Since AP is a callee-saved register, its original
+ // value must be saved before it is overwritten, and it must be defined
+ // before any AP-relative stack accesses.
+ insertAlignaInBlock(*PrologB, AfterCSR);
insertPrologueInBlock(*PrologB, PrologueStubs);
// Insert the SCS prologue after all FrameSetup instructions so that it
// follows allocframe and any CSR spills in the instruction stream. The
@@ -1185,6 +1197,24 @@ void HexagonFrameLowering::inlineStackProbe(
}
}
+void HexagonFrameLowering::insertAlignaInBlock(
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertPt) const {
+ MachineFunction &MF = *MBB.getParent();
+ Register AP =
+ MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
+ if (!AP.isValid())
+ return;
+
+ assert(needsAligna(MF) && "Unexpected stack align base register");
+ assert(!getAlignaInstr(MF) && "PS_aligna should not be emitted before PEI");
+
+ auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
+ Align MaxAlign = std::max(MF.getFrameInfo().getMaxAlign(), getStackAlign());
+ DebugLoc DL = MBB.findDebugLoc(InsertPt);
+ BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::PS_aligna), AP)
+ .addImm(MaxAlign.value());
+}
+
void HexagonFrameLowering::updateEntryPaths(MachineFunction &MF,
MachineBasicBlock &SaveB) const {
SetVector<unsigned> Worklist;
@@ -1616,13 +1646,13 @@ HexagonFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
return StackOffset::getFixed(RealOffset);
}
-bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB,
- const CSIVect &CSI, const HexagonRegisterInfo &HRI,
- bool &PrologueStubs) const {
+MachineBasicBlock::iterator HexagonFrameLowering::insertCSRSpillsInBlock(
+ MachineBasicBlock &MBB, const CSIVect &CSI, const HexagonRegisterInfo &HRI,
+ bool &PrologueStubs) const {
+ MachineBasicBlock::iterator MI = MBB.begin();
if (CSI.empty())
- return true;
+ return MI;
- MachineBasicBlock::iterator MI = MBB.begin();
PrologueStubs = false;
MachineFunction &MF = *MBB.getParent();
auto &HST = MF.getSubtarget<HexagonSubtarget>();
@@ -1681,25 +1711,7 @@ bool HexagonFrameLowering::insertCSRSpillsInBlock(MachineBasicBlock &MBB,
}
}
- // Move PS_aligna to after all CSR spills (both inline and spill-function
- // paths). PS_aligna initializes the AP register (e.g. R16) with an aligned
- // value derived from FP. Since AP is a callee-saved register, its original
- // value must be saved before it is overwritten, and it must be defined
- // before any AP-relative stack accesses.
- // MI points to the first non-spill instruction; all spills are before it.
- auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
- if (const MachineInstr *AlignaI = HFI.getAlignaInstr(MF)) {
- MachineInstr *AI = const_cast<MachineInstr *>(AlignaI);
- // PS_aligna is always created in EntryBB during ISEL. Since PS_aligna
- // causes needsStackFrame() to return true, EntryBB will be included in
- // the set of blocks needing a frame. Because EntryBB dominates all blocks,
- // shrink-wrapping will always place PrologB at EntryBB when PS_aligna
- // exists. Therefore, this assertion should always hold.
- assert(AI->getParent() == &MBB && "PS_aligna not in prologue block");
- MBB.splice(MI, AI->getParent(), AI->getIterator());
- }
-
- return true;
+ return MI;
}
bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB,
@@ -1791,11 +1803,12 @@ void HexagonFrameLowering::processFunctionBeforeFrameFinalized(
return;
// Set the physical aligned-stack base address register.
- MCRegister AP;
- if (const MachineInstr *AI = getAlignaInstr(MF))
- AP = AI->getOperand(0).getReg();
+ auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
- assert(!AP.isValid() || AP.isPhysical());
+ Register AP = HMFI.getStackAlignBaseReg();
+ if (!AP.isValid())
+ AP = HRI.computeStackAlignBaseRegister(MF);
+ assert(AP.isPhysical());
HMFI.setStackAlignBaseReg(AP);
}
@@ -1860,9 +1873,13 @@ bool HexagonFrameLowering::assignCalleeSavedSpillSlots(MachineFunction &MF,
BitVector Reserved = TRI->getReservedRegs(MF);
// Unreserve the stack align register: it is reserved for this function
// only, it still needs to be saved/restored.
+ const auto &HRI = *static_cast<const HexagonRegisterInfo *>(TRI);
Register AP =
MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
+ if (!AP.isValid())
+ AP = HRI.computeStackAlignBaseRegister(MF);
if (AP.isValid()) {
+ MF.getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseReg(AP);
Reserved[AP] = false;
// Unreserve super-regs if no other subregisters are reserved.
for (MCPhysReg SP : TRI->superregs(AP)) {
@@ -2361,6 +2378,18 @@ void HexagonFrameLowering::determineCalleeSaves(MachineFunction &MF,
for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R)
SavedRegs.set(*R);
+ // If the function needs dynamic stack realignment, AP is a callee-saved
+ // register that gets overwritten by the PS_aligna emitted in the prologue
+ // but PS_aligna is created during emitPrologue, which runs after this hook.
+ if (needsAligna(MF)) {
+ Register AP =
+ MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
+ if (!AP.isValid())
+ AP = HRI.computeStackAlignBaseRegister(MF);
+ if (AP.isValid())
+ SavedRegs.set(AP);
+ }
+
// Replace predicate register pseudo spill code.
SmallVector<Register,8> NewRegs;
expandSpillMacros(MF, NewRegs);
diff --git a/llvm/lib/Target/Hexagon/HexagonFrameLowering.h b/llvm/lib/Target/Hexagon/HexagonFrameLowering.h
index 0b93795f2b912..06d82a6c89e02 100644
--- a/llvm/lib/Target/Hexagon/HexagonFrameLowering.h
+++ b/llvm/lib/Target/Hexagon/HexagonFrameLowering.h
@@ -127,11 +127,15 @@ class HexagonFrameLowering : public TargetFrameLowering {
const HexagonInstrInfo &TII, Register SP,
unsigned CF) const;
void insertPrologueInBlock(MachineBasicBlock &MBB, bool PrologueStubs) const;
+ void insertAlignaInBlock(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator InsertPt) const;
void insertEpilogueInBlock(MachineBasicBlock &MBB) const;
void insertAllocframe(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertPt, unsigned NumBytes) const;
- bool insertCSRSpillsInBlock(MachineBasicBlock &MBB, const CSIVect &CSI,
- const HexagonRegisterInfo &HRI, bool &PrologueStubs) const;
+ MachineBasicBlock::iterator
+ insertCSRSpillsInBlock(MachineBasicBlock &MBB, const CSIVect &CSI,
+ const HexagonRegisterInfo &HRI,
+ bool &PrologueStubs) const;
bool insertCSRRestoresInBlock(MachineBasicBlock &MBB, const CSIVect &CSI,
const HexagonRegisterInfo &HRI) const;
void updateEntryPaths(MachineFunction &MF, MachineBasicBlock &SaveB) const;
diff --git a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.cpp b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.cpp
index 6380a48d78854..1d89d631433bf 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.cpp
@@ -794,6 +794,7 @@ void HexagonDAGToDAGISel::SelectFrameIndex(SDNode *N) {
} else {
auto &HMFI = *MF->getInfo<HexagonMachineFunctionInfo>();
Register AR = HMFI.getStackAlignBaseReg();
+ assert(AR.isValid() && "Missing stack align base register");
SDValue CH = CurDAG->getEntryNode();
SDValue Ops[] = { CurDAG->getCopyFromReg(CH, DL, AR, MVT::i32), FI, Zero };
R = CurDAG->getMachineNode(Hexagon::PS_fia, DL, MVT::i32, Ops);
@@ -1433,37 +1434,15 @@ void HexagonDAGToDAGISel::emitFunctionEntryCode() {
if (!HFI.needsAligna(*MF))
return;
- MachineFrameInfo &MFI = MF->getFrameInfo();
- MachineBasicBlock *EntryBB = &MF->front();
- Align EntryMaxA = MFI.getMaxAlign();
-
- // Reserve the first non-volatile register.
- Register AP = 0;
auto &HRI = *HST.getRegisterInfo();
- BitVector Reserved = HRI.getReservedRegs(*MF);
- for (const MCPhysReg *R = HRI.getCalleeSavedRegs(MF); *R; ++R) {
- if (Reserved[*R])
- continue;
- AP = *R;
- break;
- }
+ Register AP =
+ MF->getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
+ if (!AP.isValid())
+ AP = HRI.computeStackAlignBaseRegister(*MF);
assert(AP.isValid() && "Couldn't reserve stack align register");
- BuildMI(EntryBB, DebugLoc(), HII->get(Hexagon::PS_aligna), AP)
- .addImm(EntryMaxA.value());
MF->getInfo<HexagonMachineFunctionInfo>()->setStackAlignBaseReg(AP);
}
-void HexagonDAGToDAGISel::updateAligna() {
- auto &HFI = *MF->getSubtarget<HexagonSubtarget>().getFrameLowering();
- if (!HFI.needsAligna(*MF))
- return;
- auto *AlignaI = const_cast<MachineInstr*>(HFI.getAlignaInstr(*MF));
- assert(AlignaI != nullptr);
- unsigned MaxA = MF->getFrameInfo().getMaxAlign().value();
- if (AlignaI->getOperand(1).getImm() < MaxA)
- AlignaI->getOperand(1).setImm(MaxA);
-}
-
// Match a frame index that can be used in an addressing mode.
bool HexagonDAGToDAGISel::SelectAddrFI(SDValue &N, SDValue &R) {
if (N.getOpcode() != ISD::FrameIndex)
diff --git a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.h b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.h
index 518e9b198a059..fa5ac4276b5f7 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.h
+++ b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAG.h
@@ -44,7 +44,6 @@ class HexagonDAGToDAGISel : public SelectionDAGISel {
HII = HST->getInstrInfo();
HRI = HST->getRegisterInfo();
SelectionDAGISel::runOnMachineFunction(MF);
- updateAligna();
return true;
}
@@ -150,9 +149,6 @@ class HexagonDAGToDAGISel : public SelectionDAGISel {
void SelectHvxRor(SDNode *N);
void SelectHvxVAlign(SDNode *N);
- // Function postprocessing.
- void updateAligna();
-
SmallDenseMap<SDNode *,int> RootWeights;
SmallDenseMap<SDNode *,int> RootHeights;
SmallDenseMap<const Value *,int> GAUsesInFunction;
diff --git a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp
index 63d6717ad7b68..10a290ac59bee 100644
--- a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "HexagonRegisterInfo.h"
+#include "HexagonFrameLowering.h"
#include "HexagonMachineFunctionInfo.h"
#include "HexagonSubtarget.h"
#include "llvm/ADT/BitVector.h"
@@ -157,9 +158,45 @@ const uint32_t *HexagonRegisterInfo::getCallPreservedMask(
return HexagonCSR_RegMask;
}
+BitVector
+HexagonRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
+ BitVector Reserved = getBaseReservedRegs(MF);
+ Register AP =
+ MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
+ if (!AP.isValid())
+ AP = computeStackAlignBaseRegister(MF, Reserved);
+ if (AP.isValid()) {
+ Reserved.set(AP);
+ markSuperRegs(Reserved, AP);
+ }
+ return Reserved;
+}
-BitVector HexagonRegisterInfo::getReservedRegs(const MachineFunction &MF)
- const {
+Register HexagonRegisterInfo::computeStackAlignBaseRegister(
+ const MachineFunction &MF) const {
+ return computeStackAlignBaseRegister(MF, getBaseReservedRegs(MF));
+}
+
+Register HexagonRegisterInfo::computeStackAlignBaseRegister(
+ const MachineFunction &MF, const BitVector &BaseReservedRegs) const {
+ auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
+ if (!HFI.needsAligna(MF))
+ return Register();
+
+ // Reserve the first non-volatile register.
+ Register AP;
+ for (const MCPhysReg *R = getCalleeSavedRegs(&MF); *R; ++R) {
+ if (BaseReservedRegs[*R])
+ continue;
+ AP = *R;
+ break;
+ }
+ assert(AP.isValid() && "Couldn't reserve stack align register");
+ return AP;
+}
+
+BitVector
+HexagonRegisterInfo::getBaseReservedRegs(const MachineFunction &MF) const {
BitVector Reserved(getNumRegs());
Reserved.set(Hexagon::R29);
Reserved.set(Hexagon::R30);
@@ -215,11 +252,6 @@ BitVector HexagonRegisterInfo::getReservedRegs(const MachineFunction &MF)
if (MF.getSubtarget().isRegisterReservedByUser(Reg))
Reserved.set(Reg);
- Register AP =
- MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
- if (AP.isValid())
- Reserved.set(AP);
-
for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x))
markSuperRegs(Reserved, x);
diff --git a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.h b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.h
index a6cf0f9cd0074..bf990ad752d36 100644
--- a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.h
+++ b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.h
@@ -38,6 +38,11 @@ class HexagonRegisterInfo : public HexagonGenRegisterInfo {
BitVector getReservedRegs(const MachineFunction &MF) const override;
+ /// Compute the first available callee-saved register to use as the
+ /// aligned-stack base register when this function needs dynamic stack
+ /// realignment. This does not update the machine function info.
+ Register computeStackAlignBaseRegister(const MachineFunction &MF) const;
+
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
unsigned FIOperandNum, RegScavenger *RS = nullptr) const override;
@@ -85,6 +90,12 @@ class HexagonRegisterInfo : public HexagonGenRegisterInfo {
bool isFakeReg(MCPhysReg Reg) const;
bool isEHReturnCalleeSaveReg(Register Reg) const;
+
+private:
+ BitVector getBaseReservedRegs(const MachineFunction &MF) const;
+ Register
+ computeStackAlignBaseRegister(const MachineFunction &MF,
+ const BitVector &BaseReservedRegs) const;
};
} // end namespace llvm
diff --git a/llvm/lib/Target/Hexagon/HexagonVExtract.cpp b/llvm/lib/Target/Hexagon/HexagonVExtract.cpp
index 3b3d25d4c22c5..2be8f27c86402 100644
--- a/llvm/lib/Target/Hexagon/HexagonVExtract.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonVExtract.cpp
@@ -125,7 +125,6 @@ bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) {
return AddrR;
};
- MaybeAlign MaxAlign;
for (auto &P : VExtractMap) {
unsigned VecR = P.first;
if (P.second.size() <= VExtractThreshold)
@@ -133,7 +132,6 @@ bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) {
const auto &VecRC = *MRI.getRegClass(VecR);
Align Alignment = HRI.getSpillAlign(VecRC);
- MaxAlign = std::max(MaxAlign.valueOrOne(), Alignment);
// Make sure this is not a spill slot: spill slots cannot be aligned
// if there are variable-sized objects on the stack. They must be
// accessible via FP (which is not aligned), because SP is unknown,
@@ -172,15 +170,6 @@ bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) {
}
}
- if (AR && MaxAlign) {
- // Update the required stack alignment.
- MachineInstr *AlignaI = MRI.getVRegDef(AR);
- assert(AlignaI->getOpcode() == Hexagon::PS_aligna);
- MachineOperand &Op = AlignaI->getOperand(1);
- if (*MaxAlign > Op.getImm())
- Op.setImm(MaxAlign->value());
- }
-
return Changed;
}
diff --git a/llvm/test/CodeGen/Hexagon/aligna-prologue-expansion.mir b/llvm/test/CodeGen/Hexagon/aligna-prologue-expansion.mir
index 2ab5ba37847dc..a82a9b6c54104 100644
--- a/llvm/test/CodeGen/Hexagon/aligna-prologue-expansion.mir
+++ b/llvm/test/CodeGen/Hexagon/aligna-prologue-expansion.mir
@@ -57,7 +57,6 @@ body: |
renamable $r20 = COPY killed $r3
renamable $r21 = COPY killed $r2
renamable $r22 = COPY killed $r1
- $r16 = PS_aligna 128, implicit killed $r30
renamable $r25 = A2_tfrsi 0
renamable $r2 = A2_tfrsi 0
renamable $r23 = PS_alloca killed renamable $r6, 128, implicit-def dead $r29
diff --git a/llvm/test/CodeGen/Hexagon/aligna-save-base-reg.ll b/llvm/test/CodeGen/Hexagon/aligna-save-base-reg.ll
new file mode 100644
index 0000000000000..690239f07a4b6
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/aligna-save-base-reg.ll
@@ -0,0 +1,26 @@
+; RUN: llc -mtriple=hexagon -mattr=+hvxv68,+hvx-length128b -O2 < %s | FileCheck %s
+
+; When a function needs dynamic stack realignment, AP (here r16) is
+; initialized by PS_aligna in the prologue. AP is a callee-saved register, so it
+; must be saved before PS_aligna clobbers it and restored on the way out.
+; PS_aligna is created during prologue emission (after callee-saved registers
+; are determined), so HexagonFrameLowering::determineCalleeSaves must explicitly
+; add AP to the save set; otherwise the caller's r16 is corrupted.
+
+; CHECK-LABEL: f:
+; CHECK: r16 = and(r30,#-128)
+; CHECK: memd(r30+#-8) = r17:16
+; CHECK: r17:16 = memd(r30+#-8)
+; CHECK: dealloc_return
+
+declare void @use(ptr, ptr)
+
+define void @f(i32 %n) {
+entry:
+ ; Variable-sized alloca -> hasVarSizedObjects -> needsAligna.
+ %vla = alloca i32, i32 %n, align 4
+ ; Over-aligned local -> MaxAlign > stack alignment -> real realignment.
+ %buf = alloca [16 x i32], align 128
+ call void @use(ptr %vla, ptr %buf)
+ ret void
+}
diff --git a/llvm/test/CodeGen/Hexagon/spill-vector-alignment.mir b/llvm/test/CodeGen/Hexagon/spill-vector-alignment.mir
index 380a8f7331b5e..7aed05f7f4fb7 100644
--- a/llvm/test/CodeGen/Hexagon/spill-vector-alignment.mir
+++ b/llvm/test/CodeGen/Hexagon/spill-vector-alignment.mir
@@ -1,17 +1,29 @@
# RUN: llc -mtriple=hexagon -run-pass prolog-epilog %s -o - | FileCheck %s
+# Check that the aligned-stack base register $r16 is
+# saved before it is clobbered by PS_aligna in the prologue.
+# CHECK: S2_storerd_io $r30, -8, killed $d8
+
+# Check that PS_aligna is emitted with the final stack alignment after late HVX
+# spill slots increase the required stack alignment.
+# CHECK: $r16 = PS_aligna 128
+
# Check that the spill of $q0 no longer uses unaligned store instruction.
# CHECK: V6_vS32b_ai $r16, -256, killed $v0
+# Check that the aligned-stack base register is restored on the way out.
+# CHECK: $d8 = L2_loadrd_io $r30, -8
+
---
name: test
tracksRegLiveness: true
+machineFunctionInfo:
+ stackAlignBaseReg: '$r16'
stack:
- { id: 0, type: variable-sized, offset: 0, alignment: 1 }
- { id: 1, type: spill-slot, size: 128, alignment: 128 }
body: |
bb.0:
liveins: $q0
- $r16 = PS_aligna 128, implicit $r30
PS_vstorerq_ai %stack.1, 0, $q0
...
More information about the llvm-commits
mailing list