Skip to content

Commit 58d1eaa

Browse files
committed
[CodeGen] Store SP adjustment in MachineBasicBlock. NFCI.
Record the SP adjustment on entry to each basic block. This is almost always zero except on targets like ARM which can split a basic block in the middle of a call sequence. This simplifies PEI::replaceFrameIndices which previously had to visit basic blocks in a specific order and had special handling for unreachable blocks. More importantly it paves the way for an equally simple implementation of a backwards version of replaceFrameIndices, which is required to fully convert PrologEpilogInserter to backwards register scavenging, which is preferred because it does not rely on accurate kill flags. Differential Revision: https://reviews.llvm.org/D154281
1 parent fb9a741 commit 58d1eaa

File tree

11 files changed

+86
-28
lines changed

11 files changed

+86
-28
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class MachineBasicBlock
111111

112112
const BasicBlock *BB;
113113
int Number;
114+
115+
/// The SP adjustment on entry to this basic block due to call frame setup
116+
/// instructions in a predecessor. This is almost always zero, unless basic
117+
/// blocks are split in the middle of a call sequence.
118+
int SPAdjustment = 0;
119+
114120
MachineFunction *xParent;
115121
Instructions Insts;
116122

@@ -1145,6 +1151,11 @@ class MachineBasicBlock
11451151
int getNumber() const { return Number; }
11461152
void setNumber(int N) { Number = N; }
11471153

1154+
/// Return the SP adjustment on entry to this basic block.
1155+
int getSPAdjustment() const { return SPAdjustment; }
1156+
/// Set the SP adjustment on entry to this basic block.
1157+
void setSPAdjustment(int N) { SPAdjustment = N; }
1158+
11481159
/// Return the MCSymbol for this basic block.
11491160
MCSymbol *getSymbol() const;
11501161

llvm/lib/CodeGen/MIRParser/MILexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
281281
.Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
282282
.Case("machine-block-address-taken",
283283
MIToken::kw_machine_block_address_taken)
284+
.Case("sp-adjustment", MIToken::kw_sp_adjustment)
284285
.Default(MIToken::Identifier);
285286
}
286287

llvm/lib/CodeGen/MIRParser/MILexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ struct MIToken {
135135
kw_unknown_address,
136136
kw_ir_block_address_taken,
137137
kw_machine_block_address_taken,
138+
kw_sp_adjustment,
138139

139140
// Metadata types.
140141
kw_distinct,

llvm/lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ class MIParser {
501501
bool parseAddrspace(unsigned &Addrspace);
502502
bool parseSectionID(std::optional<MBBSectionID> &SID);
503503
bool parseBBID(std::optional<unsigned> &BBID);
504+
bool parseSPAdjustment(int &SPAdjustment);
504505
bool parseOperandsOffset(MachineOperand &Op);
505506
bool parseIRValue(const Value *&V);
506507
bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
@@ -676,6 +677,18 @@ bool MIParser::parseBBID(std::optional<unsigned> &BBID) {
676677
return false;
677678
}
678679

680+
// Parse basic block SP adjustment.
681+
bool MIParser::parseSPAdjustment(int &SPAdjustment) {
682+
assert(Token.is(MIToken::kw_sp_adjustment));
683+
lex();
684+
unsigned Value = 0;
685+
if (getUnsigned(Value) || !isInt<32>(Value))
686+
return error("Unknown SP adjustment");
687+
SPAdjustment = (int)Value;
688+
lex();
689+
return false;
690+
}
691+
679692
bool MIParser::parseBasicBlockDefinition(
680693
DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
681694
assert(Token.is(MIToken::MachineBasicBlockLabel));
@@ -693,6 +706,7 @@ bool MIParser::parseBasicBlockDefinition(
693706
std::optional<MBBSectionID> SectionID;
694707
uint64_t Alignment = 0;
695708
std::optional<unsigned> BBID;
709+
int SPAdjustment = 0;
696710
BasicBlock *BB = nullptr;
697711
if (consumeIfPresent(MIToken::lparen)) {
698712
do {
@@ -737,6 +751,10 @@ bool MIParser::parseBasicBlockDefinition(
737751
if (parseBBID(BBID))
738752
return true;
739753
break;
754+
case MIToken::kw_sp_adjustment:
755+
if (parseSPAdjustment(SPAdjustment))
756+
return true;
757+
break;
740758
default:
741759
break;
742760
}
@@ -781,6 +799,7 @@ bool MIParser::parseBasicBlockDefinition(
781799
MF.setBBSectionsType(BasicBlockSection::Labels);
782800
MBB->setBBID(BBID.value());
783801
}
802+
MBB->setSPAdjustment(SPAdjustment);
784803
return false;
785804
}
786805

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ void MachineBasicBlock::printName(raw_ostream &os, unsigned printNameFlags,
570570
os << "bb_id " << *getBBID();
571571
hasAttributes = true;
572572
}
573+
if (SPAdjustment != 0) {
574+
os << (hasAttributes ? ", " : " (");
575+
os << "sp-adjustment " << SPAdjustment;
576+
hasAttributes = true;
577+
}
573578
}
574579

575580
if (hasAttributes)

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/BitVector.h"
20-
#include "llvm/ADT/DepthFirstIterator.h"
2120
#include "llvm/ADT/STLExtras.h"
2221
#include "llvm/ADT/SetVector.h"
2322
#include "llvm/ADT/SmallPtrSet.h"
@@ -1341,34 +1340,17 @@ void PEI::replaceFrameIndices(MachineFunction &MF) {
13411340
FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
13421341
TRI->requiresFrameIndexReplacementScavenging(MF);
13431342

1344-
// Store SPAdj at exit of a basic block.
1345-
SmallVector<int, 8> SPState;
1346-
SPState.resize(MF.getNumBlockIDs());
1347-
df_iterator_default_set<MachineBasicBlock*> Reachable;
1348-
1349-
// Iterate over the reachable blocks in DFS order.
1350-
for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1351-
DFI != DFE; ++DFI) {
1352-
int SPAdj = 0;
1353-
// Check the exit state of the DFS stack predecessor.
1354-
if (DFI.getPathLength() >= 2) {
1355-
MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1356-
assert(Reachable.count(StackPred) &&
1357-
"DFS stack predecessor is already visited.\n");
1358-
SPAdj = SPState[StackPred->getNumber()];
1359-
}
1360-
MachineBasicBlock *BB = *DFI;
1361-
replaceFrameIndices(BB, MF, SPAdj);
1362-
SPState[BB->getNumber()] = SPAdj;
1363-
}
1343+
for (auto &MBB : MF) {
1344+
int SPAdj = MBB.getSPAdjustment();
1345+
replaceFrameIndices(&MBB, MF, SPAdj);
13641346

1365-
// Handle the unreachable blocks.
1366-
for (auto &BB : MF) {
1367-
if (Reachable.count(&BB))
1368-
// Already handled in DFS traversal.
1369-
continue;
1370-
int SPAdj = 0;
1371-
replaceFrameIndices(&BB, MF, SPAdj);
1347+
// If call frame pseudo ops have already been simplified then we can no
1348+
// longer track the SP adjustment (but that's OK because in that case, frame
1349+
// index elimination does not care about the SP adjustment).
1350+
if (!TFI.canSimplifyCallFramePseudos(MF)) {
1351+
for (auto *Succ : MBB.successors())
1352+
assert(Succ->getSPAdjustment() == SPAdj);
1353+
}
13721354
}
13731355
}
13741356

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11369,6 +11369,21 @@ static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
1136911369
}
1137011370
}
1137111371

11372+
// Get the SP adjustment just before MI.
11373+
int ARMTargetLowering::getSPAdjustment(MachineInstr &MI) const {
11374+
const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
11375+
11376+
// Search backwards from MI for the most recent call frame setup instruction.
11377+
MachineBasicBlock *MBB = MI.getParent();
11378+
for (auto &AdjI : reverse(make_range(MBB->instr_begin(), MI.getIterator()))) {
11379+
if (AdjI.getOpcode() == ARM::ADJCALLSTACKDOWN)
11380+
return TII->getSPAdjust(AdjI);
11381+
}
11382+
11383+
// If none was found, use the SP adjustment from the start of the basic block.
11384+
return MBB->getSPAdjustment();
11385+
}
11386+
1137211387
MachineBasicBlock *
1137311388
ARMTargetLowering::EmitStructByval(MachineInstr &MI,
1137411389
MachineBasicBlock *BB) const {
@@ -11485,6 +11500,11 @@ ARMTargetLowering::EmitStructByval(MachineInstr &MI,
1148511500
MF->insert(It, loopMBB);
1148611501
MF->insert(It, exitMBB);
1148711502

11503+
// Set the SP adjustment on entry to the new basic blocks.
11504+
int SPAdj = getSPAdjustment(MI);
11505+
loopMBB->setSPAdjustment(SPAdj);
11506+
exitMBB->setSPAdjustment(SPAdj);
11507+
1148811508
// Transfer the remainder of BB and its successor edges to exitMBB.
1148911509
exitMBB->splice(exitMBB->begin(), BB,
1149011510
std::next(MachineBasicBlock::iterator(MI)), BB->end());

llvm/lib/Target/ARM/ARMISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ class VectorType;
970970

971971
void EmitSjLjDispatchBlock(MachineInstr &MI, MachineBasicBlock *MBB) const;
972972

973+
int getSPAdjustment(MachineInstr &MI) const;
974+
973975
MachineBasicBlock *EmitStructByval(MachineInstr &MI,
974976
MachineBasicBlock *MBB) const;
975977

llvm/test/CodeGen/ARM/2013-06-03-ByVal-2Kbytes.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
; RUN: llc < %s -mcpu=cortex-a15 | FileCheck %s
2+
; RUN: llc < %s -mcpu=cortex-a15 -stop-after=finalize-isel -o %t.mir
3+
; RUN: llc %t.mir -mcpu=cortex-a15 -start-after=finalize-isel -o - | FileCheck %s
24
; ModuleID = 'attri_16.c'
35
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:128-a0:0:64-n32-S64"
46
target triple = "armv4t--linux-gnueabihf"

llvm/test/CodeGen/MIR/Generic/basic-blocks.mir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
ret i32 0
1919
}
2020

21+
define i32 @spadj() {
22+
start:
23+
ret i32 0
24+
}
25+
2126
...
2227
---
2328
# CHECK-LABEL: name: foo
@@ -47,3 +52,11 @@ body: |
4752
bb.0.start (align 4, machine-block-address-taken):
4853
bb.1 (machine-block-address-taken, align 4):
4954
...
55+
---
56+
# CHECK-LABEL: name: spadj
57+
# CHECK: body:
58+
# CHECK-NEXT: bb.0 (sp-adjustment 100):
59+
name: spadj
60+
body: |
61+
bb.0 (sp-adjustment 100):
62+
...

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ static std::unique_ptr<MachineFunction> cloneMF(MachineFunction *SrcMF,
225225
DstMF->CreateMachineBasicBlock(SrcMBB.getBasicBlock());
226226
Src2DstMBB[&SrcMBB] = DstMBB;
227227

228+
DstMBB->setSPAdjustment(SrcMBB.getSPAdjustment());
229+
228230
if (SrcMBB.isIRBlockAddressTaken())
229231
DstMBB->setAddressTakenIRBlock(SrcMBB.getAddressTakenIRBlock());
230232
if (SrcMBB.isMachineBlockAddressTaken())

0 commit comments

Comments
 (0)