diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h --- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h +++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h @@ -111,6 +111,12 @@ const BasicBlock *BB; int Number; + + /// The SP adjustment on entry to this basic block due to call frame setup + /// instructions in a predecessor. This is almost always zero, unless basic + /// blocks are split in the middle of a call sequence. + int SPAdjustment = 0; + MachineFunction *xParent; Instructions Insts; @@ -1145,6 +1151,11 @@ int getNumber() const { return Number; } void setNumber(int N) { Number = N; } + /// Return the SP adjustment on entry to this basic block. + int getSPAdjustment() const { return SPAdjustment; } + /// Set the SP adjustment on entry to this basic block. + void setSPAdjustment(int N) { SPAdjustment = N; } + /// Return the MCSymbol for this basic block. MCSymbol *getSymbol() const; diff --git a/llvm/lib/CodeGen/PrologEpilogInserter.cpp b/llvm/lib/CodeGen/PrologEpilogInserter.cpp --- a/llvm/lib/CodeGen/PrologEpilogInserter.cpp +++ b/llvm/lib/CodeGen/PrologEpilogInserter.cpp @@ -17,7 +17,6 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitVector.h" -#include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/SmallPtrSet.h" @@ -1341,34 +1340,17 @@ FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) || TRI->requiresFrameIndexReplacementScavenging(MF); - // Store SPAdj at exit of a basic block. - SmallVector SPState; - SPState.resize(MF.getNumBlockIDs()); - df_iterator_default_set Reachable; - - // Iterate over the reachable blocks in DFS order. - for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable); - DFI != DFE; ++DFI) { - int SPAdj = 0; - // Check the exit state of the DFS stack predecessor. - if (DFI.getPathLength() >= 2) { - MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); - assert(Reachable.count(StackPred) && - "DFS stack predecessor is already visited.\n"); - SPAdj = SPState[StackPred->getNumber()]; - } - MachineBasicBlock *BB = *DFI; - replaceFrameIndices(BB, MF, SPAdj); - SPState[BB->getNumber()] = SPAdj; - } + for (auto &MBB : MF) { + int SPAdj = MBB.getSPAdjustment(); + replaceFrameIndices(&MBB, MF, SPAdj); - // Handle the unreachable blocks. - for (auto &BB : MF) { - if (Reachable.count(&BB)) - // Already handled in DFS traversal. - continue; - int SPAdj = 0; - replaceFrameIndices(&BB, MF, SPAdj); + // If call frame pseudo ops have already been simplified then we can no + // longer track the SP adjustment (but that's OK because in that case, frame + // index elimination does not care about the SP adjustment). + if (!TFI.canSimplifyCallFramePseudos(MF)) { + for (auto *Succ : MBB.successors()) + assert(Succ->getSPAdjustment() == SPAdj); + } } } diff --git a/llvm/lib/Target/ARM/ARMISelLowering.h b/llvm/lib/Target/ARM/ARMISelLowering.h --- a/llvm/lib/Target/ARM/ARMISelLowering.h +++ b/llvm/lib/Target/ARM/ARMISelLowering.h @@ -970,6 +970,8 @@ void EmitSjLjDispatchBlock(MachineInstr &MI, MachineBasicBlock *MBB) const; + int getSPAdjustment(MachineInstr &MI) const; + MachineBasicBlock *EmitStructByval(MachineInstr &MI, MachineBasicBlock *MBB) const; diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -11365,6 +11365,21 @@ } } +// Get the SP adjustment just before MI. +int ARMTargetLowering::getSPAdjustment(MachineInstr &MI) const { + const TargetInstrInfo *TII = Subtarget->getInstrInfo(); + + // Search backwards from MI for the most recent call frame setup instruction. + MachineBasicBlock *MBB = MI.getParent(); + for (auto &AdjI : reverse(make_range(MBB->instr_begin(), MI.getIterator()))) { + if (AdjI.getOpcode() == ARM::ADJCALLSTACKDOWN) + return TII->getSPAdjust(AdjI); + } + + // If none was found, use the SP adjustment from the start of the basic block. + return MBB->getSPAdjustment(); +} + MachineBasicBlock * ARMTargetLowering::EmitStructByval(MachineInstr &MI, MachineBasicBlock *BB) const { @@ -11481,6 +11496,11 @@ MF->insert(It, loopMBB); MF->insert(It, exitMBB); + // Set the SP adjustment on entry to the new basic blocks. + int SPAdj = getSPAdjustment(MI); + loopMBB->setSPAdjustment(SPAdj); + exitMBB->setSPAdjustment(SPAdj); + // Transfer the remainder of BB and its successor edges to exitMBB. exitMBB->splice(exitMBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)), BB->end());