blob: a5dbbdb4fdcd22091fb4cf9286fa7af6e5350560 [file] [log] [blame]
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001//===---- MachineOutliner.h - Outliner data structures ------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scullcdfcccc2018-10-05 20:58:37 +01006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Contains all data structures shared between the outliner implemented in
11/// MachineOutliner.cpp and target implementations of the outliner.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_MACHINEOUTLINER_H
16#define LLVM_MACHINEOUTLINER_H
17
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020018#include "llvm/CodeGen/LivePhysRegs.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010019#include "llvm/CodeGen/LiveRegUnits.h"
20#include "llvm/CodeGen/MachineFunction.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020021#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010022#include "llvm/CodeGen/TargetRegisterInfo.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010023
24namespace llvm {
25namespace outliner {
26
27/// Represents how an instruction should be mapped by the outliner.
28/// \p Legal instructions are those which are safe to outline.
29/// \p LegalTerminator instructions are safe to outline, but only as the
30/// last instruction in a sequence.
31/// \p Illegal instructions are those which cannot be outlined.
32/// \p Invisible instructions are instructions which can be outlined, but
33/// shouldn't actually impact the outlining result.
34enum InstrType { Legal, LegalTerminator, Illegal, Invisible };
35
36/// An individual sequence of instructions to be replaced with a call to
37/// an outlined function.
38struct Candidate {
39private:
40 /// The start index of this \p Candidate in the instruction list.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 unsigned StartIdx = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010042
43 /// The number of instructions in this \p Candidate.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020044 unsigned Len = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010045
46 // The first instruction in this \p Candidate.
47 MachineBasicBlock::iterator FirstInst;
48
49 // The last instruction in this \p Candidate.
50 MachineBasicBlock::iterator LastInst;
51
52 // The basic block that contains this Candidate.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020053 MachineBasicBlock *MBB = nullptr;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010054
55 /// Cost of calling an outlined function from this point as defined by the
56 /// target.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020057 unsigned CallOverhead = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010058
59public:
60 /// The index of this \p Candidate's \p OutlinedFunction in the list of
61 /// \p OutlinedFunctions.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020062 unsigned FunctionIdx = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063
Andrew Scullcdfcccc2018-10-05 20:58:37 +010064 /// Identifier denoting the instructions to emit to call an outlined function
65 /// from this point. Defined by the target.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020066 unsigned CallConstructionID = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067
68 /// Contains physical register liveness information for the MBB containing
69 /// this \p Candidate.
70 ///
71 /// This is optionally used by the target to calculate more fine-grained
72 /// cost model information.
73 LiveRegUnits LRU;
74
75 /// Contains the accumulated register liveness information for the
76 /// instructions in this \p Candidate.
77 ///
78 /// This is optionally used by the target to determine which registers have
79 /// been used across the sequence.
80 LiveRegUnits UsedInSequence;
81
Andrew Walbran16937d02019-10-22 13:54:20 +010082 /// Target-specific flags for this Candidate's MBB.
83 unsigned Flags = 0x0;
84
85 /// True if initLRU has been called on this Candidate.
86 bool LRUWasSet = false;
87
Andrew Scullcdfcccc2018-10-05 20:58:37 +010088 /// Return the number of instructions in this Candidate.
89 unsigned getLength() const { return Len; }
90
91 /// Return the start index of this candidate.
92 unsigned getStartIdx() const { return StartIdx; }
93
94 /// Return the end index of this candidate.
95 unsigned getEndIdx() const { return StartIdx + Len - 1; }
96
97 /// Set the CallConstructionID and CallOverhead of this candidate to CID and
98 /// CO respectively.
99 void setCallInfo(unsigned CID, unsigned CO) {
100 CallConstructionID = CID;
101 CallOverhead = CO;
102 }
103
104 /// Returns the call overhead of this candidate if it is in the list.
Andrew Walbran16937d02019-10-22 13:54:20 +0100105 unsigned getCallOverhead() const { return CallOverhead; }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106
107 MachineBasicBlock::iterator &front() { return FirstInst; }
108 MachineBasicBlock::iterator &back() { return LastInst; }
109 MachineFunction *getMF() const { return MBB->getParent(); }
110 MachineBasicBlock *getMBB() const { return MBB; }
111
112 /// The number of instructions that would be saved by outlining every
113 /// candidate of this type.
114 ///
115 /// This is a fixed value which is not updated during the candidate pruning
116 /// process. It is only used for deciding which candidate to keep if two
117 /// candidates overlap. The true benefit is stored in the OutlinedFunction
118 /// for some given candidate.
119 unsigned Benefit = 0;
120
121 Candidate(unsigned StartIdx, unsigned Len,
122 MachineBasicBlock::iterator &FirstInst,
123 MachineBasicBlock::iterator &LastInst, MachineBasicBlock *MBB,
Andrew Walbran16937d02019-10-22 13:54:20 +0100124 unsigned FunctionIdx, unsigned Flags)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100125 : StartIdx(StartIdx), Len(Len), FirstInst(FirstInst), LastInst(LastInst),
Andrew Walbran16937d02019-10-22 13:54:20 +0100126 MBB(MBB), FunctionIdx(FunctionIdx), Flags(Flags) {}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100127 Candidate() {}
128
129 /// Used to ensure that \p Candidates are outlined in an order that
130 /// preserves the start and end indices of other \p Candidates.
131 bool operator<(const Candidate &RHS) const {
132 return getStartIdx() > RHS.getStartIdx();
133 }
134
135 /// Compute the registers that are live across this Candidate.
136 /// Used by targets that need this information for cost model calculation.
137 /// If a target does not need this information, then this should not be
138 /// called.
139 void initLRU(const TargetRegisterInfo &TRI) {
140 assert(MBB->getParent()->getRegInfo().tracksLiveness() &&
141 "Candidate's Machine Function must track liveness");
Andrew Walbran16937d02019-10-22 13:54:20 +0100142 // Only initialize once.
143 if (LRUWasSet)
144 return;
145 LRUWasSet = true;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146 LRU.init(TRI);
147 LRU.addLiveOuts(*MBB);
148
149 // Compute liveness from the end of the block up to the beginning of the
150 // outlining candidate.
151 std::for_each(MBB->rbegin(), (MachineBasicBlock::reverse_iterator)front(),
152 [this](MachineInstr &MI) { LRU.stepBackward(MI); });
153
154 // Walk over the sequence itself and figure out which registers were used
155 // in the sequence.
156 UsedInSequence.init(TRI);
157 std::for_each(front(), std::next(back()),
158 [this](MachineInstr &MI) { UsedInSequence.accumulate(MI); });
159 }
160};
161
162/// The information necessary to create an outlined function for some
163/// class of candidate.
164struct OutlinedFunction {
165
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100166public:
Andrew Walbran16937d02019-10-22 13:54:20 +0100167 std::vector<Candidate> Candidates;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100168
169 /// The actual outlined function created.
170 /// This is initialized after we go through and create the actual function.
171 MachineFunction *MF = nullptr;
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Represents the size of a sequence in bytes. (Some instructions vary
174 /// widely in size, so just counting the instructions isn't very useful.)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200175 unsigned SequenceSize = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100176
177 /// Target-defined overhead of constructing a frame for this function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200178 unsigned FrameOverhead = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179
180 /// Target-defined identifier for constructing a frame for this function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200181 unsigned FrameConstructionID = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100182
183 /// Return the number of candidates for this \p OutlinedFunction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100184 unsigned getOccurrenceCount() const { return Candidates.size(); }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100185
186 /// Return the number of bytes it would take to outline this
187 /// function.
Andrew Walbran16937d02019-10-22 13:54:20 +0100188 unsigned getOutliningCost() const {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100189 unsigned CallOverhead = 0;
Andrew Walbran16937d02019-10-22 13:54:20 +0100190 for (const Candidate &C : Candidates)
191 CallOverhead += C.getCallOverhead();
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100192 return CallOverhead + SequenceSize + FrameOverhead;
193 }
194
195 /// Return the size in bytes of the unoutlined sequences.
Andrew Walbran16937d02019-10-22 13:54:20 +0100196 unsigned getNotOutlinedCost() const {
197 return getOccurrenceCount() * SequenceSize;
198 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100199
200 /// Return the number of instructions that would be saved by outlining
201 /// this function.
Andrew Walbran16937d02019-10-22 13:54:20 +0100202 unsigned getBenefit() const {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100203 unsigned NotOutlinedCost = getNotOutlinedCost();
204 unsigned OutlinedCost = getOutliningCost();
205 return (NotOutlinedCost < OutlinedCost) ? 0
206 : NotOutlinedCost - OutlinedCost;
207 }
208
Andrew Walbran16937d02019-10-22 13:54:20 +0100209 /// Return the number of instructions in this sequence.
210 unsigned getNumInstrs() const { return Candidates[0].getLength(); }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100211
Andrew Walbran16937d02019-10-22 13:54:20 +0100212 OutlinedFunction(std::vector<Candidate> &Candidates, unsigned SequenceSize,
213 unsigned FrameOverhead, unsigned FrameConstructionID)
214 : Candidates(Candidates), SequenceSize(SequenceSize),
215 FrameOverhead(FrameOverhead), FrameConstructionID(FrameConstructionID) {
216 const unsigned B = getBenefit();
217 for (Candidate &C : Candidates)
218 C.Benefit = B;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100219 }
220
221 OutlinedFunction() {}
222};
223} // namespace outliner
224} // namespace llvm
225
226#endif