1 //===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
10 /// Replaces repeated sequences of instructions with function calls.
12 /// This works by placing every instruction from every basic block in a
13 /// suffix tree, and repeatedly querying that tree for repeated sequences of
14 /// instructions. If a sequence of instructions appears often, then it ought
15 /// to be beneficial to pull out into a function.
17 /// The MachineOutliner communicates with a given target using hooks defined in
18 /// TargetInstrInfo.h. The target supplies the outliner with information on how
19 /// a specific sequence of instructions should be outlined. This information
20 /// is used to deduce the number of instructions necessary to
22 /// * Create an outlined function
23 /// * Call that outlined function
25 /// Targets must implement
26 /// * getOutliningCandidateInfo
27 /// * buildOutlinedFrame
28 /// * insertOutlinedCall
29 /// * isFunctionSafeToOutlineFrom
31 /// in order to make use of the MachineOutliner.
33 /// This was originally presented at the 2016 LLVM Developers' Meeting in the
34 /// talk "Reducing Code Size Using Outlining". For a high-level overview of
35 /// how this pass works, the talk is available on YouTube at
37 /// https://www.youtube.com/watch?v=yorld-WSOeU
39 /// The slides for the talk are available at
41 /// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf
43 /// The talk provides an overview of how the outliner finds candidates and
44 /// ultimately outlines them. It describes how the main data structure for this
45 /// pass, the suffix tree, is queried and purged for candidates. It also gives
46 /// a simplified suffix tree construction algorithm for suffix trees based off
47 /// of the algorithm actually used here, Ukkonen's algorithm.
49 /// For the original RFC for this pass, please see
51 /// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html
53 /// For more information on the suffix tree data structure, please see
54 /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
56 //===----------------------------------------------------------------------===//
57 #include "llvm/CodeGen/MachineOutliner.h"
58 #include "llvm/ADT/DenseMap.h"
59 #include "llvm/ADT/SmallSet.h"
60 #include "llvm/ADT/Statistic.h"
61 #include "llvm/ADT/Twine.h"
62 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
63 #include "llvm/CodeGen/LivePhysRegs.h"
64 #include "llvm/CodeGen/MachineModuleInfo.h"
65 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
66 #include "llvm/CodeGen/Passes.h"
67 #include "llvm/CodeGen/TargetInstrInfo.h"
68 #include "llvm/CodeGen/TargetSubtargetInfo.h"
69 #include "llvm/IR/DIBuilder.h"
70 #include "llvm/IR/IRBuilder.h"
71 #include "llvm/IR/Mangler.h"
72 #include "llvm/IR/Module.h"
73 #include "llvm/InitializePasses.h"
74 #include "llvm/Support/CommandLine.h"
75 #include "llvm/Support/Debug.h"
76 #include "llvm/Support/SuffixTree.h"
77 #include "llvm/Support/raw_ostream.h"
82 #define DEBUG_TYPE "machine-outliner"
86 using namespace outliner
;
88 // Statistics for outlined functions.
89 STATISTIC(NumOutlined
, "Number of candidates outlined");
90 STATISTIC(FunctionsCreated
, "Number of functions created");
92 // Statistics for instruction mapping.
93 STATISTIC(NumLegalInUnsignedVec
, "Outlinable instructions mapped");
94 STATISTIC(NumIllegalInUnsignedVec
,
95 "Unoutlinable instructions mapped + number of sentinel values");
96 STATISTIC(NumSentinels
, "Sentinel values inserted during mapping");
97 STATISTIC(NumInvisible
,
98 "Invisible instructions skipped during mapping");
99 STATISTIC(UnsignedVecSize
,
100 "Total number of instructions mapped and saved to mapping vector");
102 // Set to true if the user wants the outliner to run on linkonceodr linkage
103 // functions. This is false by default because the linker can dedupe linkonceodr
104 // functions. Since the outliner is confined to a single module (modulo LTO),
105 // this is off by default. It should, however, be the default behaviour in
107 static cl::opt
<bool> EnableLinkOnceODROutlining(
108 "enable-linkonceodr-outlining", cl::Hidden
,
109 cl::desc("Enable the machine outliner on linkonceodr functions"),
112 /// Number of times to re-run the outliner. This is not the total number of runs
113 /// as the outliner will run at least one time. The default value is set to 0,
114 /// meaning the outliner will run one time and rerun zero times after that.
115 static cl::opt
<unsigned> OutlinerReruns(
116 "machine-outliner-reruns", cl::init(0), cl::Hidden
,
118 "Number of times to rerun the outliner after the initial outline"));
120 static cl::opt
<unsigned> OutlinerBenefitThreshold(
121 "outliner-benefit-threshold", cl::init(1), cl::Hidden
,
123 "The minimum size in bytes before an outlining candidate is accepted"));
125 static cl::opt
<bool> OutlinerLeafDescendants(
126 "outliner-leaf-descendants", cl::init(true), cl::Hidden
,
127 cl::desc("Consider all leaf descendants of internal nodes of the suffix "
128 "tree as candidates for outlining (if false, only leaf children "
133 /// Maps \p MachineInstrs to unsigned integers and stores the mappings.
134 struct InstructionMapper
{
136 /// The next available integer to assign to a \p MachineInstr that
137 /// cannot be outlined.
139 /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
140 unsigned IllegalInstrNumber
= -3;
142 /// The next available integer to assign to a \p MachineInstr that can
144 unsigned LegalInstrNumber
= 0;
146 /// Correspondence from \p MachineInstrs to unsigned integers.
147 DenseMap
<MachineInstr
*, unsigned, MachineInstrExpressionTrait
>
148 InstructionIntegerMap
;
150 /// Correspondence between \p MachineBasicBlocks and target-defined flags.
151 DenseMap
<MachineBasicBlock
*, unsigned> MBBFlagsMap
;
153 /// The vector of unsigned integers that the module is mapped to.
154 SmallVector
<unsigned> UnsignedVec
;
156 /// Stores the location of the instruction associated with the integer
157 /// at index i in \p UnsignedVec for each index i.
158 SmallVector
<MachineBasicBlock::iterator
> InstrList
;
160 // Set if we added an illegal number in the previous step.
161 // Since each illegal number is unique, we only need one of them between
162 // each range of legal numbers. This lets us make sure we don't add more
163 // than one illegal number per range.
164 bool AddedIllegalLastTime
= false;
166 /// Maps \p *It to a legal integer.
168 /// Updates \p CanOutlineWithPrevInstr, \p HaveLegalRange, \p InstrListForMBB,
169 /// \p UnsignedVecForMBB, \p InstructionIntegerMap, and \p LegalInstrNumber.
171 /// \returns The integer that \p *It was mapped to.
172 unsigned mapToLegalUnsigned(
173 MachineBasicBlock::iterator
&It
, bool &CanOutlineWithPrevInstr
,
174 bool &HaveLegalRange
, unsigned &NumLegalInBlock
,
175 SmallVector
<unsigned> &UnsignedVecForMBB
,
176 SmallVector
<MachineBasicBlock::iterator
> &InstrListForMBB
) {
177 // We added something legal, so we should unset the AddedLegalLastTime
179 AddedIllegalLastTime
= false;
181 // If we have at least two adjacent legal instructions (which may have
182 // invisible instructions in between), remember that.
183 if (CanOutlineWithPrevInstr
)
184 HaveLegalRange
= true;
185 CanOutlineWithPrevInstr
= true;
187 // Keep track of the number of legal instructions we insert.
190 // Get the integer for this instruction or give it the current
192 InstrListForMBB
.push_back(It
);
193 MachineInstr
&MI
= *It
;
195 DenseMap
<MachineInstr
*, unsigned, MachineInstrExpressionTrait
>::iterator
197 std::tie(ResultIt
, WasInserted
) =
198 InstructionIntegerMap
.insert(std::make_pair(&MI
, LegalInstrNumber
));
199 unsigned MINumber
= ResultIt
->second
;
201 // There was an insertion.
205 UnsignedVecForMBB
.push_back(MINumber
);
207 // Make sure we don't overflow or use any integers reserved by the DenseMap.
208 if (LegalInstrNumber
>= IllegalInstrNumber
)
209 report_fatal_error("Instruction mapping overflow!");
211 assert(LegalInstrNumber
!= DenseMapInfo
<unsigned>::getEmptyKey() &&
212 "Tried to assign DenseMap tombstone or empty key to instruction.");
213 assert(LegalInstrNumber
!= DenseMapInfo
<unsigned>::getTombstoneKey() &&
214 "Tried to assign DenseMap tombstone or empty key to instruction.");
217 ++NumLegalInUnsignedVec
;
221 /// Maps \p *It to an illegal integer.
223 /// Updates \p InstrListForMBB, \p UnsignedVecForMBB, and \p
224 /// IllegalInstrNumber.
226 /// \returns The integer that \p *It was mapped to.
227 unsigned mapToIllegalUnsigned(
228 MachineBasicBlock::iterator
&It
, bool &CanOutlineWithPrevInstr
,
229 SmallVector
<unsigned> &UnsignedVecForMBB
,
230 SmallVector
<MachineBasicBlock::iterator
> &InstrListForMBB
) {
231 // Can't outline an illegal instruction. Set the flag.
232 CanOutlineWithPrevInstr
= false;
234 // Only add one illegal number per range of legal numbers.
235 if (AddedIllegalLastTime
)
236 return IllegalInstrNumber
;
238 // Remember that we added an illegal number last time.
239 AddedIllegalLastTime
= true;
240 unsigned MINumber
= IllegalInstrNumber
;
242 InstrListForMBB
.push_back(It
);
243 UnsignedVecForMBB
.push_back(IllegalInstrNumber
);
244 IllegalInstrNumber
--;
246 ++NumIllegalInUnsignedVec
;
248 assert(LegalInstrNumber
< IllegalInstrNumber
&&
249 "Instruction mapping overflow!");
251 assert(IllegalInstrNumber
!= DenseMapInfo
<unsigned>::getEmptyKey() &&
252 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
254 assert(IllegalInstrNumber
!= DenseMapInfo
<unsigned>::getTombstoneKey() &&
255 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
260 /// Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
261 /// and appends it to \p UnsignedVec and \p InstrList.
263 /// Two instructions are assigned the same integer if they are identical.
264 /// If an instruction is deemed unsafe to outline, then it will be assigned an
265 /// unique integer. The resulting mapping is placed into a suffix tree and
266 /// queried for candidates.
268 /// \param MBB The \p MachineBasicBlock to be translated into integers.
269 /// \param TII \p TargetInstrInfo for the function.
270 void convertToUnsignedVec(MachineBasicBlock
&MBB
,
271 const TargetInstrInfo
&TII
) {
272 LLVM_DEBUG(dbgs() << "*** Converting MBB '" << MBB
.getName()
273 << "' to unsigned vector ***\n");
276 // Don't even map in this case.
277 if (!TII
.isMBBSafeToOutlineFrom(MBB
, Flags
))
280 auto OutlinableRanges
= TII
.getOutlinableRanges(MBB
, Flags
);
281 LLVM_DEBUG(dbgs() << MBB
.getName() << ": " << OutlinableRanges
.size()
282 << " outlinable range(s)\n");
283 if (OutlinableRanges
.empty())
286 // Store info for the MBB for later outlining.
287 MBBFlagsMap
[&MBB
] = Flags
;
289 MachineBasicBlock::iterator It
= MBB
.begin();
291 // The number of instructions in this block that will be considered for
293 unsigned NumLegalInBlock
= 0;
295 // True if we have at least two legal instructions which aren't separated
296 // by an illegal instruction.
297 bool HaveLegalRange
= false;
299 // True if we can perform outlining given the last mapped (non-invisible)
300 // instruction. This lets us know if we have a legal range.
301 bool CanOutlineWithPrevInstr
= false;
303 // FIXME: Should this all just be handled in the target, rather than using
304 // repeated calls to getOutliningType?
305 SmallVector
<unsigned> UnsignedVecForMBB
;
306 SmallVector
<MachineBasicBlock::iterator
> InstrListForMBB
;
308 LLVM_DEBUG(dbgs() << "*** Mapping outlinable ranges ***\n");
309 for (auto &OutlinableRange
: OutlinableRanges
) {
310 auto OutlinableRangeBegin
= OutlinableRange
.first
;
311 auto OutlinableRangeEnd
= OutlinableRange
.second
;
315 << std::distance(OutlinableRangeBegin
, OutlinableRangeEnd
)
316 << " instruction range\n");
317 // Everything outside of an outlinable range is illegal.
318 unsigned NumSkippedInRange
= 0;
320 for (; It
!= OutlinableRangeBegin
; ++It
) {
324 mapToIllegalUnsigned(It
, CanOutlineWithPrevInstr
, UnsignedVecForMBB
,
328 LLVM_DEBUG(dbgs() << "Skipped " << NumSkippedInRange
329 << " instructions outside outlinable range\n");
331 assert(It
!= MBB
.end() && "Should still have instructions?");
332 // `It` is now positioned at the beginning of a range of instructions
333 // which may be outlinable. Check if each instruction is known to be safe.
334 for (; It
!= OutlinableRangeEnd
; ++It
) {
335 // Keep track of where this instruction is in the module.
336 switch (TII
.getOutliningType(It
, Flags
)) {
337 case InstrType::Illegal
:
338 mapToIllegalUnsigned(It
, CanOutlineWithPrevInstr
, UnsignedVecForMBB
,
342 case InstrType::Legal
:
343 mapToLegalUnsigned(It
, CanOutlineWithPrevInstr
, HaveLegalRange
,
344 NumLegalInBlock
, UnsignedVecForMBB
,
348 case InstrType::LegalTerminator
:
349 mapToLegalUnsigned(It
, CanOutlineWithPrevInstr
, HaveLegalRange
,
350 NumLegalInBlock
, UnsignedVecForMBB
,
352 // The instruction also acts as a terminator, so we have to record
353 // that in the string.
354 mapToIllegalUnsigned(It
, CanOutlineWithPrevInstr
, UnsignedVecForMBB
,
358 case InstrType::Invisible
:
359 // Normally this is set by mapTo(Blah)Unsigned, but we just want to
360 // skip this instruction. So, unset the flag here.
362 AddedIllegalLastTime
= false;
368 LLVM_DEBUG(dbgs() << "HaveLegalRange = " << HaveLegalRange
<< "\n");
370 // Are there enough legal instructions in the block for outlining to be
372 if (HaveLegalRange
) {
373 // After we're done every insertion, uniquely terminate this part of the
374 // "string". This makes sure we won't match across basic block or function
375 // boundaries since the "end" is encoded uniquely and thus appears in no
376 // repeated substring.
377 mapToIllegalUnsigned(It
, CanOutlineWithPrevInstr
, UnsignedVecForMBB
,
380 append_range(InstrList
, InstrListForMBB
);
381 append_range(UnsignedVec
, UnsignedVecForMBB
);
385 InstructionMapper() {
386 // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
388 assert(DenseMapInfo
<unsigned>::getEmptyKey() == (unsigned)-1 &&
389 "DenseMapInfo<unsigned>'s empty key isn't -1!");
390 assert(DenseMapInfo
<unsigned>::getTombstoneKey() == (unsigned)-2 &&
391 "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
395 /// An interprocedural pass which finds repeated sequences of
396 /// instructions and replaces them with calls to functions.
398 /// Each instruction is mapped to an unsigned integer and placed in a string.
399 /// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
400 /// is then repeatedly queried for repeated sequences of instructions. Each
401 /// non-overlapping repeated sequence is then placed in its own
402 /// \p MachineFunction and each instance is then replaced with a call to that
404 struct MachineOutliner
: public ModulePass
{
408 /// Set to true if the outliner should consider functions with
409 /// linkonceodr linkage.
410 bool OutlineFromLinkOnceODRs
= false;
412 /// The current repeat number of machine outlining.
413 unsigned OutlineRepeatedNum
= 0;
415 /// Set to true if the outliner should run on all functions in the module
416 /// considered safe for outlining.
417 /// Set to true by default for compatibility with llc's -run-pass option.
418 /// Set when the pass is constructed in TargetPassConfig.
419 bool RunOnAllFunctions
= true;
421 StringRef
getPassName() const override
{ return "Machine Outliner"; }
423 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
424 AU
.addRequired
<MachineModuleInfoWrapperPass
>();
425 AU
.addPreserved
<MachineModuleInfoWrapperPass
>();
426 AU
.setPreservesAll();
427 ModulePass::getAnalysisUsage(AU
);
430 MachineOutliner() : ModulePass(ID
) {
431 initializeMachineOutlinerPass(*PassRegistry::getPassRegistry());
434 /// Remark output explaining that not outlining a set of candidates would be
435 /// better than outlining that set.
436 void emitNotOutliningCheaperRemark(
437 unsigned StringLen
, std::vector
<Candidate
> &CandidatesForRepeatedSeq
,
438 OutlinedFunction
&OF
);
440 /// Remark output explaining that a function was outlined.
441 void emitOutlinedFunctionRemark(OutlinedFunction
&OF
);
443 /// Find all repeated substrings that satisfy the outlining cost model by
444 /// constructing a suffix tree.
446 /// If a substring appears at least twice, then it must be represented by
447 /// an internal node which appears in at least two suffixes. Each suffix
448 /// is represented by a leaf node. To do this, we visit each internal node
449 /// in the tree, using the leaf children of each internal node. If an
450 /// internal node represents a beneficial substring, then we use each of
451 /// its leaf children to find the locations of its substring.
453 /// \param Mapper Contains outlining mapping information.
454 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
455 /// each type of candidate.
456 void findCandidates(InstructionMapper
&Mapper
,
457 std::vector
<OutlinedFunction
> &FunctionList
);
459 /// Replace the sequences of instructions represented by \p OutlinedFunctions
460 /// with calls to functions.
462 /// \param M The module we are outlining from.
463 /// \param FunctionList A list of functions to be inserted into the module.
464 /// \param Mapper Contains the instruction mappings for the module.
465 bool outline(Module
&M
, std::vector
<OutlinedFunction
> &FunctionList
,
466 InstructionMapper
&Mapper
, unsigned &OutlinedFunctionNum
);
468 /// Creates a function for \p OF and inserts it into the module.
469 MachineFunction
*createOutlinedFunction(Module
&M
, OutlinedFunction
&OF
,
470 InstructionMapper
&Mapper
,
473 /// Calls 'doOutline()' 1 + OutlinerReruns times.
474 bool runOnModule(Module
&M
) override
;
476 /// Construct a suffix tree on the instructions in \p M and outline repeated
477 /// strings from that tree.
478 bool doOutline(Module
&M
, unsigned &OutlinedFunctionNum
);
480 /// Return a DISubprogram for OF if one exists, and null otherwise. Helper
481 /// function for remark emission.
482 DISubprogram
*getSubprogramOrNull(const OutlinedFunction
&OF
) {
483 for (const Candidate
&C
: OF
.Candidates
)
484 if (MachineFunction
*MF
= C
.getMF())
485 if (DISubprogram
*SP
= MF
->getFunction().getSubprogram())
490 /// Populate and \p InstructionMapper with instruction-to-integer mappings.
491 /// These are used to construct a suffix tree.
492 void populateMapper(InstructionMapper
&Mapper
, Module
&M
,
493 MachineModuleInfo
&MMI
);
495 /// Initialize information necessary to output a size remark.
496 /// FIXME: This should be handled by the pass manager, not the outliner.
497 /// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
499 void initSizeRemarkInfo(const Module
&M
, const MachineModuleInfo
&MMI
,
500 StringMap
<unsigned> &FunctionToInstrCount
);
503 // FIXME: This should be handled by the pass manager, not the outliner.
505 emitInstrCountChangedRemark(const Module
&M
, const MachineModuleInfo
&MMI
,
506 const StringMap
<unsigned> &FunctionToInstrCount
);
508 } // Anonymous namespace.
510 char MachineOutliner::ID
= 0;
513 ModulePass
*createMachineOutlinerPass(bool RunOnAllFunctions
) {
514 MachineOutliner
*OL
= new MachineOutliner();
515 OL
->RunOnAllFunctions
= RunOnAllFunctions
;
521 INITIALIZE_PASS(MachineOutliner
, DEBUG_TYPE
, "Machine Function Outliner", false,
524 void MachineOutliner::emitNotOutliningCheaperRemark(
525 unsigned StringLen
, std::vector
<Candidate
> &CandidatesForRepeatedSeq
,
526 OutlinedFunction
&OF
) {
527 // FIXME: Right now, we arbitrarily choose some Candidate from the
528 // OutlinedFunction. This isn't necessarily fixed, nor does it have to be.
529 // We should probably sort these by function name or something to make sure
530 // the remarks are stable.
531 Candidate
&C
= CandidatesForRepeatedSeq
.front();
532 MachineOptimizationRemarkEmitter
MORE(*(C
.getMF()), nullptr);
534 MachineOptimizationRemarkMissed
R(DEBUG_TYPE
, "NotOutliningCheaper",
535 C
.front().getDebugLoc(), C
.getMBB());
536 R
<< "Did not outline " << NV("Length", StringLen
) << " instructions"
537 << " from " << NV("NumOccurrences", CandidatesForRepeatedSeq
.size())
539 << " Bytes from outlining all occurrences ("
540 << NV("OutliningCost", OF
.getOutliningCost()) << ")"
541 << " >= Unoutlined instruction bytes ("
542 << NV("NotOutliningCost", OF
.getNotOutlinedCost()) << ")"
543 << " (Also found at: ";
545 // Tell the user the other places the candidate was found.
546 for (unsigned i
= 1, e
= CandidatesForRepeatedSeq
.size(); i
< e
; i
++) {
547 R
<< NV((Twine("OtherStartLoc") + Twine(i
)).str(),
548 CandidatesForRepeatedSeq
[i
].front().getDebugLoc());
558 void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction
&OF
) {
559 MachineBasicBlock
*MBB
= &*OF
.MF
->begin();
560 MachineOptimizationRemarkEmitter
MORE(*OF
.MF
, nullptr);
561 MachineOptimizationRemark
R(DEBUG_TYPE
, "OutlinedFunction",
562 MBB
->findDebugLoc(MBB
->begin()), MBB
);
563 R
<< "Saved " << NV("OutliningBenefit", OF
.getBenefit()) << " bytes by "
564 << "outlining " << NV("Length", OF
.getNumInstrs()) << " instructions "
565 << "from " << NV("NumOccurrences", OF
.getOccurrenceCount())
569 // Tell the user the other places the candidate was found.
570 for (size_t i
= 0, e
= OF
.Candidates
.size(); i
< e
; i
++) {
572 R
<< NV((Twine("StartLoc") + Twine(i
)).str(),
573 OF
.Candidates
[i
].front().getDebugLoc());
583 void MachineOutliner::findCandidates(
584 InstructionMapper
&Mapper
, std::vector
<OutlinedFunction
> &FunctionList
) {
585 FunctionList
.clear();
586 SuffixTree
ST(Mapper
.UnsignedVec
, OutlinerLeafDescendants
);
588 // First, find all of the repeated substrings in the tree of minimum length
590 std::vector
<Candidate
> CandidatesForRepeatedSeq
;
591 LLVM_DEBUG(dbgs() << "*** Discarding overlapping candidates *** \n");
593 dbgs() << "Searching for overlaps in all repeated sequences...\n");
594 for (SuffixTree::RepeatedSubstring
&RS
: ST
) {
595 CandidatesForRepeatedSeq
.clear();
596 unsigned StringLen
= RS
.Length
;
597 LLVM_DEBUG(dbgs() << " Sequence length: " << StringLen
<< "\n");
598 // Debug code to keep track of how many candidates we removed.
600 unsigned NumDiscarded
= 0;
601 unsigned NumKept
= 0;
603 // Sort the start indices so that we can efficiently check if candidates
604 // overlap with the ones we've already found for this sequence.
605 llvm::sort(RS
.StartIndices
);
606 for (const unsigned &StartIdx
: RS
.StartIndices
) {
607 // Trick: Discard some candidates that would be incompatible with the
608 // ones we've already found for this sequence. This will save us some
609 // work in candidate selection.
611 // If two candidates overlap, then we can't outline them both. This
612 // happens when we have candidates that look like, say
614 // AA (where each "A" is an instruction).
616 // We might have some portion of the module that looks like this:
619 // In this case, there are 5 different copies of "AA" in this range, but
620 // at most 3 can be outlined. If only outlining 3 of these is going to
621 // be unbeneficial, then we ought to not bother.
623 // Note that two things DON'T overlap when they look like this:
624 // start1...end1 .... start2...end2
625 // That is, one must either
626 // * End before the other starts
627 // * Start after the other ends
628 unsigned EndIdx
= StartIdx
+ StringLen
- 1;
629 if (!CandidatesForRepeatedSeq
.empty() &&
630 StartIdx
<= CandidatesForRepeatedSeq
.back().getEndIdx()) {
633 LLVM_DEBUG(dbgs() << " .. DISCARD candidate @ [" << StartIdx
<< ", "
634 << EndIdx
<< "]; overlaps with candidate @ ["
635 << CandidatesForRepeatedSeq
.back().getStartIdx()
636 << ", " << CandidatesForRepeatedSeq
.back().getEndIdx()
641 // It doesn't overlap with anything, so we can outline it.
642 // Each sequence is over [StartIt, EndIt].
643 // Save the candidate and its location.
647 MachineBasicBlock::iterator StartIt
= Mapper
.InstrList
[StartIdx
];
648 MachineBasicBlock::iterator EndIt
= Mapper
.InstrList
[EndIdx
];
649 MachineBasicBlock
*MBB
= StartIt
->getParent();
650 CandidatesForRepeatedSeq
.emplace_back(StartIdx
, StringLen
, StartIt
, EndIt
,
651 MBB
, FunctionList
.size(),
652 Mapper
.MBBFlagsMap
[MBB
]);
655 LLVM_DEBUG(dbgs() << " Candidates discarded: " << NumDiscarded
657 LLVM_DEBUG(dbgs() << " Candidates kept: " << NumKept
<< "\n\n");
660 // We've found something we might want to outline.
661 // Create an OutlinedFunction to store it and check if it'd be beneficial
663 if (CandidatesForRepeatedSeq
.size() < 2)
666 // Arbitrarily choose a TII from the first candidate.
667 // FIXME: Should getOutliningCandidateInfo move to TargetMachine?
668 const TargetInstrInfo
*TII
=
669 CandidatesForRepeatedSeq
[0].getMF()->getSubtarget().getInstrInfo();
671 std::optional
<OutlinedFunction
> OF
=
672 TII
->getOutliningCandidateInfo(CandidatesForRepeatedSeq
);
674 // If we deleted too many candidates, then there's nothing worth outlining.
675 // FIXME: This should take target-specified instruction sizes into account.
676 if (!OF
|| OF
->Candidates
.size() < 2)
679 // Is it better to outline this candidate than not?
680 if (OF
->getBenefit() < OutlinerBenefitThreshold
) {
681 emitNotOutliningCheaperRemark(StringLen
, CandidatesForRepeatedSeq
, *OF
);
685 FunctionList
.push_back(*OF
);
689 MachineFunction
*MachineOutliner::createOutlinedFunction(
690 Module
&M
, OutlinedFunction
&OF
, InstructionMapper
&Mapper
, unsigned Name
) {
692 // Create the function name. This should be unique.
693 // FIXME: We should have a better naming scheme. This should be stable,
694 // regardless of changes to the outliner's cost model/traversal order.
695 std::string FunctionName
= "OUTLINED_FUNCTION_";
696 if (OutlineRepeatedNum
> 0)
697 FunctionName
+= std::to_string(OutlineRepeatedNum
+ 1) + "_";
698 FunctionName
+= std::to_string(Name
);
699 LLVM_DEBUG(dbgs() << "NEW FUNCTION: " << FunctionName
<< "\n");
701 // Create the function using an IR-level function.
702 LLVMContext
&C
= M
.getContext();
703 Function
*F
= Function::Create(FunctionType::get(Type::getVoidTy(C
), false),
704 Function::ExternalLinkage
, FunctionName
, M
);
706 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
707 // which gives us better results when we outline from linkonceodr functions.
708 F
->setLinkage(GlobalValue::InternalLinkage
);
709 F
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
711 // Set optsize/minsize, so we don't insert padding between outlined
713 F
->addFnAttr(Attribute::OptimizeForSize
);
714 F
->addFnAttr(Attribute::MinSize
);
716 Candidate
&FirstCand
= OF
.Candidates
.front();
717 const TargetInstrInfo
&TII
=
718 *FirstCand
.getMF()->getSubtarget().getInstrInfo();
720 TII
.mergeOutliningCandidateAttributes(*F
, OF
.Candidates
);
722 // Set uwtable, so we generate eh_frame.
723 UWTableKind UW
= std::accumulate(
724 OF
.Candidates
.cbegin(), OF
.Candidates
.cend(), UWTableKind::None
,
725 [](UWTableKind K
, const outliner::Candidate
&C
) {
726 return std::max(K
, C
.getMF()->getFunction().getUWTableKind());
728 F
->setUWTableKind(UW
);
730 BasicBlock
*EntryBB
= BasicBlock::Create(C
, "entry", F
);
731 IRBuilder
<> Builder(EntryBB
);
732 Builder
.CreateRetVoid();
734 MachineModuleInfo
&MMI
= getAnalysis
<MachineModuleInfoWrapperPass
>().getMMI();
735 MachineFunction
&MF
= MMI
.getOrCreateMachineFunction(*F
);
736 MF
.setIsOutlined(true);
737 MachineBasicBlock
&MBB
= *MF
.CreateMachineBasicBlock();
739 // Insert the new function into the module.
740 MF
.insert(MF
.begin(), &MBB
);
742 MachineFunction
*OriginalMF
= FirstCand
.front().getMF();
743 const std::vector
<MCCFIInstruction
> &Instrs
=
744 OriginalMF
->getFrameInstructions();
745 for (auto &MI
: FirstCand
) {
746 if (MI
.isDebugInstr())
749 // Don't keep debug information for outlined instructions.
750 auto DL
= DebugLoc();
751 if (MI
.isCFIInstruction()) {
752 unsigned CFIIndex
= MI
.getOperand(0).getCFIIndex();
753 MCCFIInstruction CFI
= Instrs
[CFIIndex
];
754 BuildMI(MBB
, MBB
.end(), DL
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
755 .addCFIIndex(MF
.addFrameInst(CFI
));
757 MachineInstr
*NewMI
= MF
.CloneMachineInstr(&MI
);
758 NewMI
->dropMemRefs(MF
);
759 NewMI
->setDebugLoc(DL
);
760 MBB
.insert(MBB
.end(), NewMI
);
764 // Set normal properties for a late MachineFunction.
765 MF
.getProperties().reset(MachineFunctionProperties::Property::IsSSA
);
766 MF
.getProperties().set(MachineFunctionProperties::Property::NoPHIs
);
767 MF
.getProperties().set(MachineFunctionProperties::Property::NoVRegs
);
768 MF
.getProperties().set(MachineFunctionProperties::Property::TracksLiveness
);
769 MF
.getRegInfo().freezeReservedRegs();
771 // Compute live-in set for outlined fn
772 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
773 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
774 LivePhysRegs
LiveIns(TRI
);
775 for (auto &Cand
: OF
.Candidates
) {
776 // Figure out live-ins at the first instruction.
777 MachineBasicBlock
&OutlineBB
= *Cand
.front().getParent();
778 LivePhysRegs
CandLiveIns(TRI
);
779 CandLiveIns
.addLiveOuts(OutlineBB
);
780 for (const MachineInstr
&MI
:
781 reverse(make_range(Cand
.begin(), OutlineBB
.end())))
782 CandLiveIns
.stepBackward(MI
);
784 // The live-in set for the outlined function is the union of the live-ins
785 // from all the outlining points.
786 for (MCPhysReg Reg
: CandLiveIns
)
789 addLiveIns(MBB
, LiveIns
);
791 TII
.buildOutlinedFrame(MBB
, MF
, OF
);
793 // If there's a DISubprogram associated with this outlined function, then
794 // emit debug info for the outlined function.
795 if (DISubprogram
*SP
= getSubprogramOrNull(OF
)) {
796 // We have a DISubprogram. Get its DICompileUnit.
797 DICompileUnit
*CU
= SP
->getUnit();
798 DIBuilder
DB(M
, true, CU
);
799 DIFile
*Unit
= SP
->getFile();
801 // Get the mangled name of the function for the linkage name.
803 raw_string_ostream
MangledNameStream(Dummy
);
804 Mg
.getNameWithPrefix(MangledNameStream
, F
, false);
806 DISubprogram
*OutlinedSP
= DB
.createFunction(
807 Unit
/* Context */, F
->getName(), StringRef(Dummy
), Unit
/* File */,
808 0 /* Line 0 is reserved for compiler-generated code. */,
809 DB
.createSubroutineType(
810 DB
.getOrCreateTypeArray(std::nullopt
)), /* void type */
811 0, /* Line 0 is reserved for compiler-generated code. */
812 DINode::DIFlags::FlagArtificial
/* Compiler-generated code. */,
813 /* Outlined code is optimized code by definition. */
814 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
816 // Don't add any new variables to the subprogram.
817 DB
.finalizeSubprogram(OutlinedSP
);
819 // Attach subprogram to the function.
820 F
->setSubprogram(OutlinedSP
);
821 // We're done with the DIBuilder.
828 bool MachineOutliner::outline(Module
&M
,
829 std::vector
<OutlinedFunction
> &FunctionList
,
830 InstructionMapper
&Mapper
,
831 unsigned &OutlinedFunctionNum
) {
832 LLVM_DEBUG(dbgs() << "*** Outlining ***\n");
833 LLVM_DEBUG(dbgs() << "NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList
.size()
835 bool OutlinedSomething
= false;
837 // Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
838 // The function with highest priority should be outlined first.
839 stable_sort(FunctionList
,
840 [](const OutlinedFunction
&LHS
, const OutlinedFunction
&RHS
) {
841 return LHS
.getNotOutlinedCost() * RHS
.getOutliningCost() >
842 RHS
.getNotOutlinedCost() * LHS
.getOutliningCost();
845 // Walk over each function, outlining them as we go along. Functions are
846 // outlined greedily, based off the sort above.
847 auto *UnsignedVecBegin
= Mapper
.UnsignedVec
.begin();
848 LLVM_DEBUG(dbgs() << "WALKING FUNCTION LIST\n");
849 for (OutlinedFunction
&OF
: FunctionList
) {
851 auto NumCandidatesBefore
= OF
.Candidates
.size();
853 // If we outlined something that overlapped with a candidate in a previous
854 // step, then we can't outline from it.
855 erase_if(OF
.Candidates
, [&UnsignedVecBegin
](Candidate
&C
) {
856 return std::any_of(UnsignedVecBegin
+ C
.getStartIdx(),
857 UnsignedVecBegin
+ C
.getEndIdx() + 1, [](unsigned I
) {
858 return I
== static_cast<unsigned>(-1);
863 auto NumCandidatesAfter
= OF
.Candidates
.size();
864 LLVM_DEBUG(dbgs() << "PRUNED: " << NumCandidatesBefore
- NumCandidatesAfter
865 << "/" << NumCandidatesBefore
<< " candidates\n");
868 // If we made it unbeneficial to outline this function, skip it.
869 if (OF
.getBenefit() < OutlinerBenefitThreshold
) {
870 LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF
.getBenefit()
871 << " B) < threshold (" << OutlinerBenefitThreshold
876 LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF
.getBenefit()
877 << " B) > threshold (" << OutlinerBenefitThreshold
880 // It's beneficial. Create the function and outline its sequence's
882 OF
.MF
= createOutlinedFunction(M
, OF
, Mapper
, OutlinedFunctionNum
);
883 emitOutlinedFunctionRemark(OF
);
885 OutlinedFunctionNum
++; // Created a function, move to the next name.
886 MachineFunction
*MF
= OF
.MF
;
887 const TargetSubtargetInfo
&STI
= MF
->getSubtarget();
888 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
890 // Replace occurrences of the sequence with calls to the new function.
891 LLVM_DEBUG(dbgs() << "CREATE OUTLINED CALLS\n");
892 for (Candidate
&C
: OF
.Candidates
) {
893 MachineBasicBlock
&MBB
= *C
.getMBB();
894 MachineBasicBlock::iterator StartIt
= C
.begin();
895 MachineBasicBlock::iterator EndIt
= std::prev(C
.end());
898 auto CallInst
= TII
.insertOutlinedCall(M
, MBB
, StartIt
, *MF
, C
);
901 auto MBBBeingOutlinedFromName
=
902 MBB
.getName().empty() ? "<unknown>" : MBB
.getName().str();
903 auto MFBeingOutlinedFromName
= MBB
.getParent()->getName().empty()
905 : MBB
.getParent()->getName().str();
906 LLVM_DEBUG(dbgs() << " CALL: " << MF
->getName() << " in "
907 << MFBeingOutlinedFromName
<< ":"
908 << MBBBeingOutlinedFromName
<< "\n");
909 LLVM_DEBUG(dbgs() << " .. " << *CallInst
);
912 // If the caller tracks liveness, then we need to make sure that
913 // anything we outline doesn't break liveness assumptions. The outlined
914 // functions themselves currently don't track liveness, but we should
915 // make sure that the ranges we yank things out of aren't wrong.
916 if (MBB
.getParent()->getProperties().hasProperty(
917 MachineFunctionProperties::Property::TracksLiveness
)) {
918 // The following code is to add implicit def operands to the call
919 // instruction. It also updates call site information for moved
921 SmallSet
<Register
, 2> UseRegs
, DefRegs
;
922 // Copy over the defs in the outlined range.
923 // First inst in outlined range <-- Anything that's defined in this
924 // ... .. range has to be added as an
925 // implicit Last inst in outlined range <-- def to the call
926 // instruction. Also remove call site information for outlined block
927 // of code. The exposed uses need to be copied in the outlined range.
928 for (MachineBasicBlock::reverse_iterator
929 Iter
= EndIt
.getReverse(),
930 Last
= std::next(CallInst
.getReverse());
931 Iter
!= Last
; Iter
++) {
932 MachineInstr
*MI
= &*Iter
;
933 SmallSet
<Register
, 2> InstrUseRegs
;
934 for (MachineOperand
&MOP
: MI
->operands()) {
935 // Skip over anything that isn't a register.
940 // Introduce DefRegs set to skip the redundant register.
941 DefRegs
.insert(MOP
.getReg());
942 if (UseRegs
.count(MOP
.getReg()) &&
943 !InstrUseRegs
.count(MOP
.getReg()))
944 // Since the regiester is modeled as defined,
945 // it is not necessary to be put in use register set.
946 UseRegs
.erase(MOP
.getReg());
947 } else if (!MOP
.isUndef()) {
948 // Any register which is not undefined should
949 // be put in the use register set.
950 UseRegs
.insert(MOP
.getReg());
951 InstrUseRegs
.insert(MOP
.getReg());
954 if (MI
->isCandidateForCallSiteEntry())
955 MI
->getMF()->eraseCallSiteInfo(MI
);
958 for (const Register
&I
: DefRegs
)
959 // If it's a def, add it to the call instruction.
960 CallInst
->addOperand(
961 MachineOperand::CreateReg(I
, true, /* isDef = true */
962 true /* isImp = true */));
964 for (const Register
&I
: UseRegs
)
965 // If it's a exposed use, add it to the call instruction.
966 CallInst
->addOperand(
967 MachineOperand::CreateReg(I
, false, /* isDef = false */
968 true /* isImp = true */));
971 // Erase from the point after where the call was inserted up to, and
972 // including, the final instruction in the sequence.
973 // Erase needs one past the end, so we need std::next there too.
974 MBB
.erase(std::next(StartIt
), std::next(EndIt
));
976 // Keep track of what we removed by marking them all as -1.
977 for (unsigned &I
: make_range(UnsignedVecBegin
+ C
.getStartIdx(),
978 UnsignedVecBegin
+ C
.getEndIdx() + 1))
979 I
= static_cast<unsigned>(-1);
980 OutlinedSomething
= true;
987 LLVM_DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething
<< "\n";);
988 return OutlinedSomething
;
991 void MachineOutliner::populateMapper(InstructionMapper
&Mapper
, Module
&M
,
992 MachineModuleInfo
&MMI
) {
993 // Build instruction mappings for each function in the module. Start by
994 // iterating over each Function in M.
995 LLVM_DEBUG(dbgs() << "*** Populating mapper ***\n");
996 for (Function
&F
: M
) {
997 LLVM_DEBUG(dbgs() << "MAPPING FUNCTION: " << F
.getName() << "\n");
999 if (F
.hasFnAttribute("nooutline")) {
1000 LLVM_DEBUG(dbgs() << "SKIP: Function has nooutline attribute\n");
1004 // There's something in F. Check if it has a MachineFunction associated with
1006 MachineFunction
*MF
= MMI
.getMachineFunction(F
);
1008 // If it doesn't, then there's nothing to outline from. Move to the next
1011 LLVM_DEBUG(dbgs() << "SKIP: Function does not have a MachineFunction\n");
1015 const TargetInstrInfo
*TII
= MF
->getSubtarget().getInstrInfo();
1016 if (!RunOnAllFunctions
&& !TII
->shouldOutlineFromFunctionByDefault(*MF
)) {
1017 LLVM_DEBUG(dbgs() << "SKIP: Target does not want to outline from "
1018 "function by default\n");
1022 // We have a MachineFunction. Ask the target if it's suitable for outlining.
1023 // If it isn't, then move on to the next Function in the module.
1024 if (!TII
->isFunctionSafeToOutlineFrom(*MF
, OutlineFromLinkOnceODRs
)) {
1025 LLVM_DEBUG(dbgs() << "SKIP: " << MF
->getName()
1026 << ": unsafe to outline from\n");
1030 // We have a function suitable for outlining. Iterate over every
1031 // MachineBasicBlock in MF and try to map its instructions to a list of
1032 // unsigned integers.
1033 const unsigned MinMBBSize
= 2;
1035 for (MachineBasicBlock
&MBB
: *MF
) {
1036 LLVM_DEBUG(dbgs() << " MAPPING MBB: '" << MBB
.getName() << "'\n");
1037 // If there isn't anything in MBB, then there's no point in outlining from
1039 // If there are fewer than 2 instructions in the MBB, then it can't ever
1040 // contain something worth outlining.
1041 // FIXME: This should be based off of the maximum size in B of an outlined
1042 // call versus the size in B of the MBB.
1043 if (MBB
.size() < MinMBBSize
) {
1044 LLVM_DEBUG(dbgs() << " SKIP: MBB size less than minimum size of "
1045 << MinMBBSize
<< "\n");
1049 // Check if MBB could be the target of an indirect branch. If it is, then
1050 // we don't want to outline from it.
1051 if (MBB
.hasAddressTaken()) {
1052 LLVM_DEBUG(dbgs() << " SKIP: MBB's address is taken\n");
1056 // MBB is suitable for outlining. Map it to a list of unsigneds.
1057 Mapper
.convertToUnsignedVec(MBB
, *TII
);
1061 UnsignedVecSize
= Mapper
.UnsignedVec
.size();
1064 void MachineOutliner::initSizeRemarkInfo(
1065 const Module
&M
, const MachineModuleInfo
&MMI
,
1066 StringMap
<unsigned> &FunctionToInstrCount
) {
1067 // Collect instruction counts for every function. We'll use this to emit
1068 // per-function size remarks later.
1069 for (const Function
&F
: M
) {
1070 MachineFunction
*MF
= MMI
.getMachineFunction(F
);
1072 // We only care about MI counts here. If there's no MachineFunction at this
1073 // point, then there won't be after the outliner runs, so let's move on.
1076 FunctionToInstrCount
[F
.getName().str()] = MF
->getInstructionCount();
1080 void MachineOutliner::emitInstrCountChangedRemark(
1081 const Module
&M
, const MachineModuleInfo
&MMI
,
1082 const StringMap
<unsigned> &FunctionToInstrCount
) {
1083 // Iterate over each function in the module and emit remarks.
1084 // Note that we won't miss anything by doing this, because the outliner never
1085 // deletes functions.
1086 for (const Function
&F
: M
) {
1087 MachineFunction
*MF
= MMI
.getMachineFunction(F
);
1089 // The outliner never deletes functions. If we don't have a MF here, then we
1090 // didn't have one prior to outlining either.
1094 std::string Fname
= std::string(F
.getName());
1095 unsigned FnCountAfter
= MF
->getInstructionCount();
1096 unsigned FnCountBefore
= 0;
1098 // Check if the function was recorded before.
1099 auto It
= FunctionToInstrCount
.find(Fname
);
1101 // Did we have a previously-recorded size? If yes, then set FnCountBefore
1103 if (It
!= FunctionToInstrCount
.end())
1104 FnCountBefore
= It
->second
;
1106 // Compute the delta and emit a remark if there was a change.
1107 int64_t FnDelta
= static_cast<int64_t>(FnCountAfter
) -
1108 static_cast<int64_t>(FnCountBefore
);
1112 MachineOptimizationRemarkEmitter
MORE(*MF
, nullptr);
1114 MachineOptimizationRemarkAnalysis
R("size-info", "FunctionMISizeChange",
1115 DiagnosticLocation(), &MF
->front());
1116 R
<< DiagnosticInfoOptimizationBase::Argument("Pass", "Machine Outliner")
1118 << DiagnosticInfoOptimizationBase::Argument("Function", F
.getName())
1119 << ": MI instruction count changed from "
1120 << DiagnosticInfoOptimizationBase::Argument("MIInstrsBefore",
1123 << DiagnosticInfoOptimizationBase::Argument("MIInstrsAfter",
1126 << DiagnosticInfoOptimizationBase::Argument("Delta", FnDelta
);
1132 bool MachineOutliner::runOnModule(Module
&M
) {
1133 // Check if there's anything in the module. If it's empty, then there's
1134 // nothing to outline.
1138 // Number to append to the current outlined function.
1139 unsigned OutlinedFunctionNum
= 0;
1141 OutlineRepeatedNum
= 0;
1142 if (!doOutline(M
, OutlinedFunctionNum
))
1145 for (unsigned I
= 0; I
< OutlinerReruns
; ++I
) {
1146 OutlinedFunctionNum
= 0;
1147 OutlineRepeatedNum
++;
1148 if (!doOutline(M
, OutlinedFunctionNum
)) {
1150 dbgs() << "Did not outline on iteration " << I
+ 2 << " out of "
1151 << OutlinerReruns
+ 1 << "\n";
1160 bool MachineOutliner::doOutline(Module
&M
, unsigned &OutlinedFunctionNum
) {
1161 MachineModuleInfo
&MMI
= getAnalysis
<MachineModuleInfoWrapperPass
>().getMMI();
1163 // If the user passed -enable-machine-outliner=always or
1164 // -enable-machine-outliner, the pass will run on all functions in the module.
1165 // Otherwise, if the target supports default outlining, it will run on all
1166 // functions deemed by the target to be worth outlining from by default. Tell
1167 // the user how the outliner is running.
1169 dbgs() << "Machine Outliner: Running on ";
1170 if (RunOnAllFunctions
)
1171 dbgs() << "all functions";
1173 dbgs() << "target-default functions";
1177 // If the user specifies that they want to outline from linkonceodrs, set
1179 OutlineFromLinkOnceODRs
= EnableLinkOnceODROutlining
;
1180 InstructionMapper Mapper
;
1182 // Prepare instruction mappings for the suffix tree.
1183 populateMapper(Mapper
, M
, MMI
);
1184 std::vector
<OutlinedFunction
> FunctionList
;
1186 // Find all of the outlining candidates.
1187 findCandidates(Mapper
, FunctionList
);
1189 // If we've requested size remarks, then collect the MI counts of every
1190 // function before outlining, and the MI counts after outlining.
1191 // FIXME: This shouldn't be in the outliner at all; it should ultimately be
1192 // the pass manager's responsibility.
1193 // This could pretty easily be placed in outline instead, but because we
1194 // really ultimately *don't* want this here, it's done like this for now
1197 // Check if we want size remarks.
1198 bool ShouldEmitSizeRemarks
= M
.shouldEmitInstrCountChangedRemark();
1199 StringMap
<unsigned> FunctionToInstrCount
;
1200 if (ShouldEmitSizeRemarks
)
1201 initSizeRemarkInfo(M
, MMI
, FunctionToInstrCount
);
1203 // Outline each of the candidates and return true if something was outlined.
1204 bool OutlinedSomething
=
1205 outline(M
, FunctionList
, Mapper
, OutlinedFunctionNum
);
1207 // If we outlined something, we definitely changed the MI count of the
1208 // module. If we've asked for size remarks, then output them.
1209 // FIXME: This should be in the pass manager.
1210 if (ShouldEmitSizeRemarks
&& OutlinedSomething
)
1211 emitInstrCountChangedRemark(M
, MMI
, FunctionToInstrCount
);
1214 if (!OutlinedSomething
)
1215 dbgs() << "Stopped outlining at iteration " << OutlineRepeatedNum
1216 << " because no changes were found.\n";
1219 return OutlinedSomething
;