Skip to content

Commit 588fef8

Browse files
committed
Analyze all candidates
1 parent 0e094e1 commit 588fef8

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,30 +3015,25 @@ static bool cannotInsertTailCall(const MachineBasicBlock &MBB) {
30153015
return false;
30163016
}
30173017

3018-
static std::optional<MachineOutlinerConstructionID>
3019-
analyzeCandidate(outliner::Candidate &C) {
3018+
static bool analyzeCandidate(outliner::Candidate &C) {
30203019
// If last instruction is return then we can rely on
30213020
// the verification already performed in the getOutliningTypeImpl.
30223021
if (C.back().isReturn()) {
30233022
assert(!cannotInsertTailCall(*C.getMBB()) &&
30243023
"The candidate who uses return instruction must be outlined "
30253024
"using tail call");
3026-
return MachineOutlinerTailCall;
3025+
return false;
30273026
}
30283027

3029-
auto CandidateUsesX5 = [](outliner::Candidate &C) {
3030-
const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
3031-
if (std::any_of(C.begin(), C.end(), [TRI](const MachineInstr &MI) {
3032-
return isMIModifiesReg(MI, TRI, RISCV::X5);
3033-
}))
3034-
return true;
3035-
return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
3036-
};
3037-
3038-
if (!CandidateUsesX5(C))
3039-
return MachineOutlinerDefault;
3028+
// Filter out candidates where the X5 register (t0) can't be used to setup
3029+
// the function call.
3030+
const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
3031+
if (std::any_of(C.begin(), C.end(), [TRI](const MachineInstr &MI) {
3032+
return isMIModifiesReg(MI, TRI, RISCV::X5);
3033+
}))
3034+
return true;
30403035

3041-
return std::nullopt;
3036+
return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
30423037
}
30433038

30443039
std::optional<std::unique_ptr<outliner::OutlinedFunction>>
@@ -3047,35 +3042,32 @@ RISCVInstrInfo::getOutliningCandidateInfo(
30473042
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
30483043
unsigned MinRepeats) const {
30493044

3050-
// Each RepeatedSequenceLoc is identical.
3051-
outliner::Candidate &Candidate = RepeatedSequenceLocs[0];
3052-
auto CandidateInfo = analyzeCandidate(Candidate);
3053-
if (!CandidateInfo)
3054-
RepeatedSequenceLocs.clear();
3045+
// Analyze each candidate and erase the ones that are not viable.
3046+
llvm::erase_if(RepeatedSequenceLocs, analyzeCandidate);
30553047

30563048
// If the sequence doesn't have enough candidates left, then we're done.
30573049
if (RepeatedSequenceLocs.size() < MinRepeats)
30583050
return std::nullopt;
30593051

3052+
// Each RepeatedSequenceLoc is identical.
3053+
outliner::Candidate &Candidate = RepeatedSequenceLocs[0];
30603054
unsigned InstrSizeCExt =
30613055
Candidate.getMF()->getSubtarget<RISCVSubtarget>().hasStdExtCOrZca() ? 2
30623056
: 4;
30633057
unsigned CallOverhead = 0, FrameOverhead = 0;
30643058

3065-
MachineOutlinerConstructionID MOCI = CandidateInfo.value();
3066-
switch (MOCI) {
3067-
case MachineOutlinerDefault:
3068-
// call t0, function = 8 bytes.
3069-
CallOverhead = 8;
3070-
// jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
3071-
FrameOverhead = InstrSizeCExt;
3072-
break;
3073-
case MachineOutlinerTailCall:
3059+
MachineOutlinerConstructionID MOCI = MachineOutlinerDefault;
3060+
if (Candidate.back().isReturn()) {
3061+
MOCI = MachineOutlinerTailCall;
30743062
// tail call = auipc + jalr in the worst case without linker relaxation.
30753063
CallOverhead = 4 + InstrSizeCExt;
30763064
// Using tail call we move ret instruction from caller to callee.
30773065
FrameOverhead = 0;
3078-
break;
3066+
} else {
3067+
// call t0, function = 8 bytes.
3068+
CallOverhead = 8;
3069+
// jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
3070+
FrameOverhead = InstrSizeCExt;
30793071
}
30803072

30813073
for (auto &C : RepeatedSequenceLocs)

llvm/test/CodeGen/RISCV/machine-outliner-call-x5-liveout.mir

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ body: |
5353
; RV32I-MO: bb.0:
5454
; RV32I-MO-NEXT: liveins: $x10, $x11
5555
; RV32I-MO-NEXT: {{ $}}
56-
; RV32I-MO-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
56+
; RV32I-MO-NEXT: $x11 = ORI $x11, 1023
57+
; RV32I-MO-NEXT: $x12 = ADDI $x10, 17
58+
; RV32I-MO-NEXT: $x11 = AND $x12, $x11
59+
; RV32I-MO-NEXT: $x10 = SUB $x10, $x11
5760
; RV32I-MO-NEXT: BEQ $x10, $x11, %bb.1
5861
; RV32I-MO-NEXT: {{ $}}
5962
; RV32I-MO-NEXT: bb.1:
@@ -65,7 +68,10 @@ body: |
6568
; RV64I-MO: bb.0:
6669
; RV64I-MO-NEXT: liveins: $x10, $x11
6770
; RV64I-MO-NEXT: {{ $}}
68-
; RV64I-MO-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
71+
; RV64I-MO-NEXT: $x11 = ORI $x11, 1023
72+
; RV64I-MO-NEXT: $x12 = ADDI $x10, 17
73+
; RV64I-MO-NEXT: $x11 = AND $x12, $x11
74+
; RV64I-MO-NEXT: $x10 = SUB $x10, $x11
6975
; RV64I-MO-NEXT: BEQ $x10, $x11, %bb.1
7076
; RV64I-MO-NEXT: {{ $}}
7177
; RV64I-MO-NEXT: bb.1:

0 commit comments

Comments
 (0)