Index: llvm/lib/Target/RISCV/RISCVFrameLowering.cpp =================================================================== --- llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -690,39 +690,49 @@ return Offset; } + + // From here, we first select a legal base register to index off, and then + // compute the offset expression. The index expression can't cross a unknown + // variable sized region. We have at two most such regions: 1) dynamic stack + // realignment, and 2) variable sized objects. Both are optional, and may or + // may not exist. Scalable vectors are variable, but not unknown. We can + // compute offset expressions which cross the RVV region. + // + // Here's what the stack layout looks like: + // |--------------------------| -- <-- FP (if present) + // | callee-allocated save | | <----| + // | area for register varargs| | | + // |--------------------------| | | + // | callee-saved registers | | | + // |--------------------------| -- | + // | realignment (the size of | | | + // | this area is not counted | | | + // | in MFI.getStackSize()) | | | + // |--------------------------| -- |-- MFI.getStackSize() + // | RVV alignment padding | | | + // | (not counted in | | | + // | MFI.getStackSize() but | | | + // | counted in | | | + // | RVFI.getRVVStackSize()) | | | + // |--------------------------| -- | + // | RVV objects | | | + // | (not counted in | | | + // | MFI.getStackSize()) | | | + // |--------------------------| -- | + // | padding before RVV | | | + // | (not counted in | | | + // | MFI.getStackSize() or in | | | + // | RVFI.getRVVStackSize()) | | | + // |--------------------------| -- | + // | scalar local variables | | <----' + // |--------------------------| -- <-- BP (if var sized objects present) + // | VarSize objects | | + // |--------------------------| -- <-- SP + if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) { // If the stack was realigned, the frame pointer is set in order to allow // SP to be restored, so we need another base register to record the stack // after realignment. - // |--------------------------| -- <-- FP - // | callee-allocated save | | <----| - // | area for register varargs| | | - // |--------------------------| | | - // | callee-saved registers | | | - // |--------------------------| -- | - // | realignment (the size of | | | - // | this area is not counted | | | - // | in MFI.getStackSize()) | | | - // |--------------------------| -- |-- MFI.getStackSize() - // | RVV alignment padding | | | - // | (not counted in | | | - // | MFI.getStackSize() but | | | - // | counted in | | | - // | RVFI.getRVVStackSize()) | | | - // |--------------------------| -- | - // | RVV objects | | | - // | (not counted in | | | - // | MFI.getStackSize()) | | | - // |--------------------------| -- | - // | padding before RVV | | | - // | (not counted in | | | - // | MFI.getStackSize() or in | | | - // | RVFI.getRVVStackSize()) | | | - // |--------------------------| -- | - // | scalar local variables | | <----' - // |--------------------------| -- <-- BP (if var sized objects present) - // | VarSize objects | | - // |--------------------------| -- <-- SP if (hasBP(MF)) { FrameReg = RISCVABI::getBPReg(); } else { @@ -740,19 +750,6 @@ Offset -= StackOffset::getFixed(RVFI->getLibCallStackSize()); // When using FP to access scalable vector objects, we need to minus // the frame size. - // - // |--------------------------| -- <-- FP - // | callee-allocated save | | - // | area for register varargs| | - // |--------------------------| | - // | callee-saved registers | | - // |--------------------------| | MFI.getStackSize() - // | scalar local variables | | - // |--------------------------| -- (Offset of RVV objects is from here.) - // | RVV objects | - // |--------------------------| - // | VarSize objects | - // |--------------------------| <-- SP if (MFI.getStackID(FI) == TargetStackID::ScalableVector) { assert(!RI->hasStackRealignment(MF) && "Can't index across variable sized realign"); @@ -771,32 +768,6 @@ assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects()); // When using SP to access frame objects, we need to add RVV stack size. - // - // |--------------------------| -- <-- FP - // | callee-allocated save | | <----| - // | area for register varargs| | | - // |--------------------------| | | - // | callee-saved registers | | | - // |--------------------------| -- | - // | RVV alignment padding | | | - // | (not counted in | | | - // | MFI.getStackSize() but | | | - // | counted in | | | - // | RVFI.getRVVStackSize()) | | | - // |--------------------------| -- | - // | RVV objects | | |-- MFI.getStackSize() - // | (not counted in | | | - // | MFI.getStackSize()) | | | - // |--------------------------| -- | - // | padding before RVV | | | - // | (not counted in | | | - // | MFI.getStackSize()) | | | - // |--------------------------| -- | - // | scalar local variables | | <----' - // |--------------------------| -- <-- BP (if var sized objects present) - // | VarSize objects | | - // |--------------------------| -- <-- SP - // // The total amount of padding surrounding RVV objects is described by // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV // objects to the required alignment.