LLVM 22.0.0git
MachineOutliner.cpp
Go to the documentation of this file.
1//===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Replaces repeated sequences of instructions with function calls.
11///
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.
16///
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
21///
22/// * Create an outlined function
23/// * Call that outlined function
24///
25/// Targets must implement
26/// * getOutliningCandidateInfo
27/// * buildOutlinedFrame
28/// * insertOutlinedCall
29/// * isFunctionSafeToOutlineFrom
30///
31/// in order to make use of the MachineOutliner.
32///
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
36///
37/// https://www.youtube.com/watch?v=yorld-WSOeU
38///
39/// The slides for the talk are available at
40///
41/// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf
42///
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.
48///
49/// For the original RFC for this pass, please see
50///
51/// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html
52///
53/// For more information on the suffix tree data structure, please see
54/// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
55///
56//===----------------------------------------------------------------------===//
58#include "llvm/ADT/DenseMap.h"
59#include "llvm/ADT/SmallSet.h"
60#include "llvm/ADT/Statistic.h"
61#include "llvm/ADT/Twine.h"
70#include "llvm/CodeGen/Passes.h"
74#include "llvm/IR/DIBuilder.h"
75#include "llvm/IR/IRBuilder.h"
76#include "llvm/IR/Mangler.h"
77#include "llvm/IR/Module.h"
80#include "llvm/Support/Debug.h"
85#include <tuple>
86#include <vector>
87
88#define DEBUG_TYPE "machine-outliner"
89
90using namespace llvm;
91using namespace ore;
92using namespace outliner;
93
94// Statistics for outlined functions.
95STATISTIC(NumOutlined, "Number of candidates outlined");
96STATISTIC(FunctionsCreated, "Number of functions created");
97
98// Statistics for instruction mapping.
99STATISTIC(NumLegalInUnsignedVec, "Outlinable instructions mapped");
100STATISTIC(NumIllegalInUnsignedVec,
101 "Unoutlinable instructions mapped + number of sentinel values");
102STATISTIC(NumSentinels, "Sentinel values inserted during mapping");
103STATISTIC(NumInvisible,
104 "Invisible instructions skipped during mapping");
105STATISTIC(UnsignedVecSize,
106 "Total number of instructions mapped and saved to mapping vector");
107STATISTIC(StableHashAttempts,
108 "Count of hashing attempts made for outlined functions");
109STATISTIC(StableHashDropped,
110 "Count of unsuccessful hashing attempts for outlined functions");
111STATISTIC(NumRemovedLOHs, "Total number of Linker Optimization Hints removed");
112STATISTIC(NumPGOBlockedOutlined,
113 "Number of times outlining was blocked by PGO");
114STATISTIC(NumPGOAllowedCold,
115 "Number of times outlining was allowed from cold functions");
116STATISTIC(NumPGOConservativeBlockedOutlined,
117 "Number of times outlining was blocked conservatively when profile "
118 "counts were missing");
119STATISTIC(NumPGOOptimisticOutlined,
120 "Number of times outlining was allowed optimistically when profile "
121 "counts were missing");
122
123// Set to true if the user wants the outliner to run on linkonceodr linkage
124// functions. This is false by default because the linker can dedupe linkonceodr
125// functions. Since the outliner is confined to a single module (modulo LTO),
126// this is off by default. It should, however, be the default behaviour in
127// LTO.
129 "enable-linkonceodr-outlining", cl::Hidden,
130 cl::desc("Enable the machine outliner on linkonceodr functions"),
131 cl::init(false));
132
133/// Number of times to re-run the outliner. This is not the total number of runs
134/// as the outliner will run at least one time. The default value is set to 0,
135/// meaning the outliner will run one time and rerun zero times after that.
137 "machine-outliner-reruns", cl::init(0), cl::Hidden,
138 cl::desc(
139 "Number of times to rerun the outliner after the initial outline"));
140
142 "outliner-benefit-threshold", cl::init(1), cl::Hidden,
143 cl::desc(
144 "The minimum size in bytes before an outlining candidate is accepted"));
145
147 "outliner-leaf-descendants", cl::init(true), cl::Hidden,
148 cl::desc("Consider all leaf descendants of internal nodes of the suffix "
149 "tree as candidates for outlining (if false, only leaf children "
150 "are considered)"));
151
152static cl::opt<bool>
153 DisableGlobalOutlining("disable-global-outlining", cl::Hidden,
154 cl::desc("Disable global outlining only by ignoring "
155 "the codegen data generation or use"),
156 cl::init(false));
157
159 "append-content-hash-outlined-name", cl::Hidden,
160 cl::desc("This appends the content hash to the globally outlined function "
161 "name. It's beneficial for enhancing the precision of the stable "
162 "hash and for ordering the outlined functions."),
163 cl::init(true));
164
165namespace {
166
167/// Maps \p MachineInstrs to unsigned integers and stores the mappings.
168struct InstructionMapper {
169 const MachineModuleInfo &MMI;
170
171 /// The next available integer to assign to a \p MachineInstr that
172 /// cannot be outlined.
173 ///
174 /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
175 unsigned IllegalInstrNumber = -3;
176
177 /// The next available integer to assign to a \p MachineInstr that can
178 /// be outlined.
179 unsigned LegalInstrNumber = 0;
180
181 /// Correspondence from \p MachineInstrs to unsigned integers.
183 InstructionIntegerMap;
184
185 /// Correspondence between \p MachineBasicBlocks and target-defined flags.
187
188 /// The vector of unsigned integers that the module is mapped to.
189 SmallVector<unsigned> UnsignedVec;
190
191 /// Stores the location of the instruction associated with the integer
192 /// at index i in \p UnsignedVec for each index i.
194
195 // Set if we added an illegal number in the previous step.
196 // Since each illegal number is unique, we only need one of them between
197 // each range of legal numbers. This lets us make sure we don't add more
198 // than one illegal number per range.
199 bool AddedIllegalLastTime = false;
200
201 /// Maps \p *It to a legal integer.
202 ///
203 /// Updates \p CanOutlineWithPrevInstr, \p HaveLegalRange, \p InstrListForMBB,
204 /// \p UnsignedVecForMBB, \p InstructionIntegerMap, and \p LegalInstrNumber.
205 ///
206 /// \returns The integer that \p *It was mapped to.
207 unsigned mapToLegalUnsigned(
208 MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
209 bool &HaveLegalRange, unsigned &NumLegalInBlock,
210 SmallVector<unsigned> &UnsignedVecForMBB,
212 // We added something legal, so we should unset the AddedLegalLastTime
213 // flag.
214 AddedIllegalLastTime = false;
215
216 // If we have at least two adjacent legal instructions (which may have
217 // invisible instructions in between), remember that.
218 if (CanOutlineWithPrevInstr)
219 HaveLegalRange = true;
220 CanOutlineWithPrevInstr = true;
221
222 // Keep track of the number of legal instructions we insert.
223 NumLegalInBlock++;
224
225 // Get the integer for this instruction or give it the current
226 // LegalInstrNumber.
227 InstrListForMBB.push_back(It);
228 MachineInstr &MI = *It;
229 bool WasInserted;
231 ResultIt;
232 std::tie(ResultIt, WasInserted) =
233 InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber));
234 unsigned MINumber = ResultIt->second;
235
236 // There was an insertion.
237 if (WasInserted)
238 LegalInstrNumber++;
239
240 UnsignedVecForMBB.push_back(MINumber);
241
242 // Make sure we don't overflow or use any integers reserved by the DenseMap.
243 if (LegalInstrNumber >= IllegalInstrNumber)
244 report_fatal_error("Instruction mapping overflow!");
245
246 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
247 "Tried to assign DenseMap tombstone or empty key to instruction.");
248 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
249 "Tried to assign DenseMap tombstone or empty key to instruction.");
250
251 // Statistics.
252 ++NumLegalInUnsignedVec;
253 return MINumber;
254 }
255
256 /// Maps \p *It to an illegal integer.
257 ///
258 /// Updates \p InstrListForMBB, \p UnsignedVecForMBB, and \p
259 /// IllegalInstrNumber.
260 ///
261 /// \returns The integer that \p *It was mapped to.
262 unsigned mapToIllegalUnsigned(
263 MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
264 SmallVector<unsigned> &UnsignedVecForMBB,
266 // Can't outline an illegal instruction. Set the flag.
267 CanOutlineWithPrevInstr = false;
268
269 // Only add one illegal number per range of legal numbers.
270 if (AddedIllegalLastTime)
271 return IllegalInstrNumber;
272
273 // Remember that we added an illegal number last time.
274 AddedIllegalLastTime = true;
275 unsigned MINumber = IllegalInstrNumber;
276
277 InstrListForMBB.push_back(It);
278 UnsignedVecForMBB.push_back(IllegalInstrNumber);
279 IllegalInstrNumber--;
280 // Statistics.
281 ++NumIllegalInUnsignedVec;
282
283 assert(LegalInstrNumber < IllegalInstrNumber &&
284 "Instruction mapping overflow!");
285
286 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
287 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
288
289 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
290 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
291
292 return MINumber;
293 }
294
295 /// Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
296 /// and appends it to \p UnsignedVec and \p InstrList.
297 ///
298 /// Two instructions are assigned the same integer if they are identical.
299 /// If an instruction is deemed unsafe to outline, then it will be assigned an
300 /// unique integer. The resulting mapping is placed into a suffix tree and
301 /// queried for candidates.
302 ///
303 /// \param MBB The \p MachineBasicBlock to be translated into integers.
304 /// \param TII \p TargetInstrInfo for the function.
305 void convertToUnsignedVec(MachineBasicBlock &MBB,
306 const TargetInstrInfo &TII) {
307 LLVM_DEBUG(dbgs() << "*** Converting MBB '" << MBB.getName()
308 << "' to unsigned vector ***\n");
309 unsigned Flags = 0;
310
311 // Don't even map in this case.
312 if (!TII.isMBBSafeToOutlineFrom(MBB, Flags))
313 return;
314
315 auto OutlinableRanges = TII.getOutlinableRanges(MBB, Flags);
316 LLVM_DEBUG(dbgs() << MBB.getName() << ": " << OutlinableRanges.size()
317 << " outlinable range(s)\n");
318 if (OutlinableRanges.empty())
319 return;
320
321 // Store info for the MBB for later outlining.
322 MBBFlagsMap[&MBB] = Flags;
323
325
326 // The number of instructions in this block that will be considered for
327 // outlining.
328 unsigned NumLegalInBlock = 0;
329
330 // True if we have at least two legal instructions which aren't separated
331 // by an illegal instruction.
332 bool HaveLegalRange = false;
333
334 // True if we can perform outlining given the last mapped (non-invisible)
335 // instruction. This lets us know if we have a legal range.
336 bool CanOutlineWithPrevInstr = false;
337
338 // FIXME: Should this all just be handled in the target, rather than using
339 // repeated calls to getOutliningType?
340 SmallVector<unsigned> UnsignedVecForMBB;
342
343 LLVM_DEBUG(dbgs() << "*** Mapping outlinable ranges ***\n");
344 for (auto &OutlinableRange : OutlinableRanges) {
345 auto OutlinableRangeBegin = OutlinableRange.first;
346 auto OutlinableRangeEnd = OutlinableRange.second;
347#ifndef NDEBUG
349 dbgs() << "Mapping "
350 << std::distance(OutlinableRangeBegin, OutlinableRangeEnd)
351 << " instruction range\n");
352 // Everything outside of an outlinable range is illegal.
353 unsigned NumSkippedInRange = 0;
354#endif
355 for (; It != OutlinableRangeBegin; ++It) {
356#ifndef NDEBUG
357 ++NumSkippedInRange;
358#endif
359 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
360 InstrListForMBB);
361 }
362#ifndef NDEBUG
363 LLVM_DEBUG(dbgs() << "Skipped " << NumSkippedInRange
364 << " instructions outside outlinable range\n");
365#endif
366 assert(It != MBB.end() && "Should still have instructions?");
367 // `It` is now positioned at the beginning of a range of instructions
368 // which may be outlinable. Check if each instruction is known to be safe.
369 for (; It != OutlinableRangeEnd; ++It) {
370 // Keep track of where this instruction is in the module.
371 switch (TII.getOutliningType(MMI, It, Flags)) {
372 case InstrType::Illegal:
373 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
374 InstrListForMBB);
375 break;
376
377 case InstrType::Legal:
378 mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
379 NumLegalInBlock, UnsignedVecForMBB,
380 InstrListForMBB);
381 break;
382
383 case InstrType::LegalTerminator:
384 mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
385 NumLegalInBlock, UnsignedVecForMBB,
386 InstrListForMBB);
387 // The instruction also acts as a terminator, so we have to record
388 // that in the string.
389 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
390 InstrListForMBB);
391 break;
392
393 case InstrType::Invisible:
394 // Normally this is set by mapTo(Blah)Unsigned, but we just want to
395 // skip this instruction. So, unset the flag here.
396 ++NumInvisible;
397 AddedIllegalLastTime = false;
398 break;
399 }
400 }
401 }
402
403 LLVM_DEBUG(dbgs() << "HaveLegalRange = " << HaveLegalRange << "\n");
404
405 // Are there enough legal instructions in the block for outlining to be
406 // possible?
407 if (HaveLegalRange) {
408 // After we're done every insertion, uniquely terminate this part of the
409 // "string". This makes sure we won't match across basic block or function
410 // boundaries since the "end" is encoded uniquely and thus appears in no
411 // repeated substring.
412 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
413 InstrListForMBB);
414 ++NumSentinels;
415 append_range(InstrList, InstrListForMBB);
416 append_range(UnsignedVec, UnsignedVecForMBB);
417 }
418 }
419
420 InstructionMapper(const MachineModuleInfo &MMI_) : MMI(MMI_) {
421 // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
422 // changed.
423 assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
424 "DenseMapInfo<unsigned>'s empty key isn't -1!");
425 assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 &&
426 "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
427 }
428};
429
430/// An interprocedural pass which finds repeated sequences of
431/// instructions and replaces them with calls to functions.
432///
433/// Each instruction is mapped to an unsigned integer and placed in a string.
434/// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
435/// is then repeatedly queried for repeated sequences of instructions. Each
436/// non-overlapping repeated sequence is then placed in its own
437/// \p MachineFunction and each instance is then replaced with a call to that
438/// function.
439struct MachineOutliner : public ModulePass {
440
441 static char ID;
442
443 MachineModuleInfo *MMI = nullptr;
444 const TargetMachine *TM = nullptr;
445
446 /// Set to true if the outliner should consider functions with
447 /// linkonceodr linkage.
448 bool OutlineFromLinkOnceODRs = false;
449
450 /// The current repeat number of machine outlining.
451 unsigned OutlineRepeatedNum = 0;
452
453 /// The mode for whether to run the outliner
454 /// Set to always-outline by default for compatibility with llc's -run-pass
455 /// option.
456 RunOutliner RunOutlinerMode = RunOutliner::AlwaysOutline;
457
458 /// This is a compact representation of hash sequences of outlined functions.
459 /// It is used when OutlinerMode = CGDataMode::Write.
460 /// The resulting hash tree will be emitted into __llvm_outlined section
461 /// which will be dead-stripped not going to the final binary.
462 /// A post-process using llvm-cgdata, lld, or ThinLTO can merge them into
463 /// a global oulined hash tree for the subsequent codegen.
464 std::unique_ptr<OutlinedHashTree> LocalHashTree;
465
466 /// The mode of the outliner.
467 /// When is's CGDataMode::None, candidates are populated with the suffix tree
468 /// within a module and outlined.
469 /// When it's CGDataMode::Write, in addition to CGDataMode::None, the hash
470 /// sequences of outlined functions are published into LocalHashTree.
471 /// When it's CGDataMode::Read, candidates are populated with the global
472 /// outlined hash tree that has been built by the previous codegen.
473 CGDataMode OutlinerMode = CGDataMode::None;
474
475 StringRef getPassName() const override { return "Machine Outliner"; }
476
477 void getAnalysisUsage(AnalysisUsage &AU) const override {
478 AU.addRequired<MachineModuleInfoWrapperPass>();
479 AU.addRequired<TargetPassConfig>();
480 AU.addPreserved<MachineModuleInfoWrapperPass>();
481 AU.addUsedIfAvailable<ImmutableModuleSummaryIndexWrapperPass>();
482 if (RunOutlinerMode == RunOutliner::OptimisticPGO ||
483 RunOutlinerMode == RunOutliner::ConservativePGO) {
484 AU.addRequired<BlockFrequencyInfoWrapperPass>();
485 AU.addRequired<ProfileSummaryInfoWrapperPass>();
486 }
487 AU.setPreservesAll();
488 ModulePass::getAnalysisUsage(AU);
489 }
490
491 MachineOutliner() : ModulePass(ID) {
493 }
494
495 /// Remark output explaining that not outlining a set of candidates would be
496 /// better than outlining that set.
497 void emitNotOutliningCheaperRemark(
498 unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
499 OutlinedFunction &OF);
500
501 /// Remark output explaining that a function was outlined.
502 void emitOutlinedFunctionRemark(OutlinedFunction &OF);
503
504 /// Find all repeated substrings that satisfy the outlining cost model by
505 /// constructing a suffix tree.
506 ///
507 /// If a substring appears at least twice, then it must be represented by
508 /// an internal node which appears in at least two suffixes. Each suffix
509 /// is represented by a leaf node. To do this, we visit each internal node
510 /// in the tree, using the leaf children of each internal node. If an
511 /// internal node represents a beneficial substring, then we use each of
512 /// its leaf children to find the locations of its substring.
513 ///
514 /// \param Mapper Contains outlining mapping information.
515 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
516 /// each type of candidate.
517 void
518 findCandidates(InstructionMapper &Mapper,
519 std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList);
520
521 /// Find all repeated substrings that match in the global outlined hash
522 /// tree built from the previous codegen.
523 ///
524 /// \param Mapper Contains outlining mapping information.
525 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
526 /// each type of candidate.
527 void findGlobalCandidates(
528 InstructionMapper &Mapper,
529 std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList);
530
531 /// Replace the sequences of instructions represented by \p OutlinedFunctions
532 /// with calls to functions.
533 ///
534 /// \param M The module we are outlining from.
535 /// \param FunctionList A list of functions to be inserted into the module.
536 /// \param Mapper Contains the instruction mappings for the module.
537 /// \param[out] OutlinedFunctionNum The outlined function number.
538 bool outline(Module &M,
539 std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
540 InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
541
542 /// Creates a function for \p OF and inserts it into the module.
543 MachineFunction *createOutlinedFunction(Module &M, OutlinedFunction &OF,
544 InstructionMapper &Mapper,
545 unsigned Name);
546
547 /// Compute and publish the stable hash sequence of instructions in the
548 /// outlined function, \p MF. The parameter \p CandSize represents the number
549 /// of candidates that have identical instruction sequences to \p MF.
550 void computeAndPublishHashSequence(MachineFunction &MF, unsigned CandSize);
551
552 /// Initialize the outliner mode.
553 void initializeOutlinerMode(const Module &M);
554
555 /// Emit the outlined hash tree into __llvm_outline section.
556 void emitOutlinedHashTree(Module &M);
557
558 /// Calls 'doOutline()' 1 + OutlinerReruns times.
559 bool runOnModule(Module &M) override;
560
561 /// Construct a suffix tree on the instructions in \p M and outline repeated
562 /// strings from that tree.
563 bool doOutline(Module &M, unsigned &OutlinedFunctionNum);
564
565 /// Return a DISubprogram for OF if one exists, and null otherwise. Helper
566 /// function for remark emission.
567 DISubprogram *getSubprogramOrNull(const OutlinedFunction &OF) {
568 for (const Candidate &C : OF.Candidates)
569 if (MachineFunction *MF = C.getMF())
570 if (DISubprogram *SP = MF->getFunction().getSubprogram())
571 return SP;
572 return nullptr;
573 }
574
575 /// Populate and \p InstructionMapper with instruction-to-integer mappings.
576 /// These are used to construct a suffix tree.
577 void populateMapper(InstructionMapper &Mapper, Module &M);
578
579 /// Initialize information necessary to output a size remark.
580 /// FIXME: This should be handled by the pass manager, not the outliner.
581 /// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
582 /// pass manager.
583 void initSizeRemarkInfo(const Module &M,
584 StringMap<unsigned> &FunctionToInstrCount);
585
586 /// Emit the remark.
587 // FIXME: This should be handled by the pass manager, not the outliner.
588 void
589 emitInstrCountChangedRemark(const Module &M,
590 const StringMap<unsigned> &FunctionToInstrCount);
591};
592} // Anonymous namespace.
593
594char MachineOutliner::ID = 0;
595
596namespace llvm {
598 MachineOutliner *OL = new MachineOutliner();
599 OL->RunOutlinerMode = RunOutlinerMode;
600 return OL;
601}
602
603} // namespace llvm
604
605INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false,
606 false)
607
608void MachineOutliner::emitNotOutliningCheaperRemark(
609 unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
610 OutlinedFunction &OF) {
611 // FIXME: Right now, we arbitrarily choose some Candidate from the
612 // OutlinedFunction. This isn't necessarily fixed, nor does it have to be.
613 // We should probably sort these by function name or something to make sure
614 // the remarks are stable.
615 Candidate &C = CandidatesForRepeatedSeq.front();
616 MachineOptimizationRemarkEmitter MORE(*(C.getMF()), nullptr);
617 MORE.emit([&]() {
618 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
619 C.front().getDebugLoc(), C.getMBB());
620 R << "Did not outline " << NV("Length", StringLen) << " instructions"
621 << " from " << NV("NumOccurrences", CandidatesForRepeatedSeq.size())
622 << " locations."
623 << " Bytes from outlining all occurrences ("
624 << NV("OutliningCost", OF.getOutliningCost()) << ")"
625 << " >= Unoutlined instruction bytes ("
626 << NV("NotOutliningCost", OF.getNotOutlinedCost()) << ")"
627 << " (Also found at: ";
628
629 // Tell the user the other places the candidate was found.
630 for (unsigned i = 1, e = CandidatesForRepeatedSeq.size(); i < e; i++) {
631 R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
632 CandidatesForRepeatedSeq[i].front().getDebugLoc());
633 if (i != e - 1)
634 R << ", ";
635 }
636
637 R << ")";
638 return R;
639 });
640}
641
642void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
643 MachineBasicBlock *MBB = &*OF.MF->begin();
644 MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr);
645 MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction",
647 R << "Saved " << NV("OutliningBenefit", OF.getBenefit()) << " bytes by "
648 << "outlining " << NV("Length", OF.getNumInstrs()) << " instructions "
649 << "from " << NV("NumOccurrences", OF.getOccurrenceCount())
650 << " locations. "
651 << "(Found at: ";
652
653 // Tell the user the other places the candidate was found.
654 for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) {
655
656 R << NV((Twine("StartLoc") + Twine(i)).str(),
657 OF.Candidates[i].front().getDebugLoc());
658 if (i != e - 1)
659 R << ", ";
660 }
661
662 R << ")";
663
664 MORE.emit(R);
665}
666
668 unsigned StartIdx;
669 unsigned EndIdx;
670 unsigned Count;
671 MatchedEntry(unsigned StartIdx, unsigned EndIdx, unsigned Count)
673 MatchedEntry() = delete;
674};
675
676// Find all matches in the global outlined hash tree.
677// It's quadratic complexity in theory, but it's nearly linear in practice
678// since the length of outlined sequences are small within a block.
679static SmallVector<MatchedEntry> getMatchedEntries(InstructionMapper &Mapper) {
680 auto &InstrList = Mapper.InstrList;
681 auto &UnsignedVec = Mapper.UnsignedVec;
682
683 SmallVector<MatchedEntry> MatchedEntries;
684 auto Size = UnsignedVec.size();
685
686 // Get the global outlined hash tree built from the previous run.
688 const auto *RootNode = cgdata::getOutlinedHashTree()->getRoot();
689
690 auto getValidInstr = [&](unsigned Index) -> const MachineInstr * {
691 if (UnsignedVec[Index] >= Mapper.LegalInstrNumber)
692 return nullptr;
693 return &(*InstrList[Index]);
694 };
695
696 auto getStableHashAndFollow =
697 [](const MachineInstr &MI, const HashNode *CurrNode) -> const HashNode * {
698 stable_hash StableHash = stableHashValue(MI);
699 if (!StableHash)
700 return nullptr;
701 auto It = CurrNode->Successors.find(StableHash);
702 return (It == CurrNode->Successors.end()) ? nullptr : It->second.get();
703 };
704
705 for (unsigned I = 0; I < Size; ++I) {
706 const MachineInstr *MI = getValidInstr(I);
707 if (!MI || MI->isDebugInstr())
708 continue;
709 const HashNode *CurrNode = getStableHashAndFollow(*MI, RootNode);
710 if (!CurrNode)
711 continue;
712
713 for (unsigned J = I + 1; J < Size; ++J) {
714 const MachineInstr *MJ = getValidInstr(J);
715 if (!MJ)
716 break;
717 // Skip debug instructions as we did for the outlined function.
718 if (MJ->isDebugInstr())
719 continue;
720 CurrNode = getStableHashAndFollow(*MJ, CurrNode);
721 if (!CurrNode)
722 break;
723 // Even with a match ending with a terminal, we continue finding
724 // matches to populate all candidates.
725 if (auto Count = CurrNode->Terminals)
726 MatchedEntries.emplace_back(I, J, *Count);
727 }
728 }
729
730 return MatchedEntries;
731}
732
733void MachineOutliner::findGlobalCandidates(
734 InstructionMapper &Mapper,
735 std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
736 FunctionList.clear();
737 auto &InstrList = Mapper.InstrList;
738 auto &MBBFlagsMap = Mapper.MBBFlagsMap;
739
740 std::vector<Candidate> CandidatesForRepeatedSeq;
741 for (auto &ME : getMatchedEntries(Mapper)) {
742 CandidatesForRepeatedSeq.clear();
743 MachineBasicBlock::iterator StartIt = InstrList[ME.StartIdx];
744 MachineBasicBlock::iterator EndIt = InstrList[ME.EndIdx];
745 auto Length = ME.EndIdx - ME.StartIdx + 1;
746 MachineBasicBlock *MBB = StartIt->getParent();
747 CandidatesForRepeatedSeq.emplace_back(ME.StartIdx, Length, StartIt, EndIt,
748 MBB, FunctionList.size(),
749 MBBFlagsMap[MBB]);
750 const TargetInstrInfo *TII =
752 unsigned MinRepeats = 1;
753 std::optional<std::unique_ptr<OutlinedFunction>> OF =
754 TII->getOutliningCandidateInfo(*MMI, CandidatesForRepeatedSeq,
755 MinRepeats);
756 if (!OF.has_value() || OF.value()->Candidates.empty())
757 continue;
758 // We create a global candidate for each match.
759 assert(OF.value()->Candidates.size() == MinRepeats);
760 FunctionList.emplace_back(std::make_unique<GlobalOutlinedFunction>(
761 std::move(OF.value()), ME.Count));
762 }
763}
764
765void MachineOutliner::findCandidates(
766 InstructionMapper &Mapper,
767 std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
768 FunctionList.clear();
769 SuffixTree ST(Mapper.UnsignedVec, OutlinerLeafDescendants);
770
771 // First, find all of the repeated substrings in the tree of minimum length
772 // 2.
773 std::vector<Candidate> CandidatesForRepeatedSeq;
774 LLVM_DEBUG(dbgs() << "*** Discarding overlapping candidates *** \n");
776 dbgs() << "Searching for overlaps in all repeated sequences...\n");
777 for (SuffixTree::RepeatedSubstring &RS : ST) {
778 CandidatesForRepeatedSeq.clear();
779 unsigned StringLen = RS.Length;
780 LLVM_DEBUG(dbgs() << " Sequence length: " << StringLen << "\n");
781 // Debug code to keep track of how many candidates we removed.
782#ifndef NDEBUG
783 unsigned NumDiscarded = 0;
784 unsigned NumKept = 0;
785#endif
786 // Sort the start indices so that we can efficiently check if candidates
787 // overlap with the ones we've already found for this sequence.
788 llvm::sort(RS.StartIndices);
789 for (const unsigned &StartIdx : RS.StartIndices) {
790 // Trick: Discard some candidates that would be incompatible with the
791 // ones we've already found for this sequence. This will save us some
792 // work in candidate selection.
793 //
794 // If two candidates overlap, then we can't outline them both. This
795 // happens when we have candidates that look like, say
796 //
797 // AA (where each "A" is an instruction).
798 //
799 // We might have some portion of the module that looks like this:
800 // AAAAAA (6 A's)
801 //
802 // In this case, there are 5 different copies of "AA" in this range, but
803 // at most 3 can be outlined. If only outlining 3 of these is going to
804 // be unbeneficial, then we ought to not bother.
805 //
806 // Note that two things DON'T overlap when they look like this:
807 // start1...end1 .... start2...end2
808 // That is, one must either
809 // * End before the other starts
810 // * Start after the other ends
811 unsigned EndIdx = StartIdx + StringLen - 1;
812 if (!CandidatesForRepeatedSeq.empty() &&
813 StartIdx <= CandidatesForRepeatedSeq.back().getEndIdx()) {
814#ifndef NDEBUG
815 ++NumDiscarded;
816 LLVM_DEBUG(dbgs() << " .. DISCARD candidate @ [" << StartIdx << ", "
817 << EndIdx << "]; overlaps with candidate @ ["
818 << CandidatesForRepeatedSeq.back().getStartIdx()
819 << ", " << CandidatesForRepeatedSeq.back().getEndIdx()
820 << "]\n");
821#endif
822 continue;
823 }
824 // It doesn't overlap with anything, so we can outline it.
825 // Each sequence is over [StartIt, EndIt].
826 // Save the candidate and its location.
827#ifndef NDEBUG
828 ++NumKept;
829#endif
830 MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx];
831 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
832 MachineBasicBlock *MBB = StartIt->getParent();
833 CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen, StartIt, EndIt,
834 MBB, FunctionList.size(),
835 Mapper.MBBFlagsMap[MBB]);
836 }
837#ifndef NDEBUG
838 LLVM_DEBUG(dbgs() << " Candidates discarded: " << NumDiscarded
839 << "\n");
840 LLVM_DEBUG(dbgs() << " Candidates kept: " << NumKept << "\n\n");
841#endif
842 unsigned MinRepeats = 2;
843
844 // We've found something we might want to outline.
845 // Create an OutlinedFunction to store it and check if it'd be beneficial
846 // to outline.
847 if (CandidatesForRepeatedSeq.size() < MinRepeats)
848 continue;
849
850 // Arbitrarily choose a TII from the first candidate.
851 // FIXME: Should getOutliningCandidateInfo move to TargetMachine?
852 const TargetInstrInfo *TII =
853 CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo();
854
855 std::optional<std::unique_ptr<OutlinedFunction>> OF =
856 TII->getOutliningCandidateInfo(*MMI, CandidatesForRepeatedSeq,
857 MinRepeats);
858
859 // If we deleted too many candidates, then there's nothing worth outlining.
860 // FIXME: This should take target-specified instruction sizes into account.
861 if (!OF.has_value() || OF.value()->Candidates.size() < MinRepeats)
862 continue;
863
864 // Is it better to outline this candidate than not?
865 if (OF.value()->getBenefit() < OutlinerBenefitThreshold) {
866 emitNotOutliningCheaperRemark(StringLen, CandidatesForRepeatedSeq,
867 *OF.value());
868 continue;
869 }
870
871 FunctionList.emplace_back(std::move(OF.value()));
872 }
873}
874
875void MachineOutliner::computeAndPublishHashSequence(MachineFunction &MF,
876 unsigned CandSize) {
877 // Compute the hash sequence for the outlined function.
878 SmallVector<stable_hash> OutlinedHashSequence;
879 for (auto &MBB : MF) {
880 for (auto &NewMI : MBB) {
881 stable_hash Hash = stableHashValue(NewMI);
882 if (!Hash) {
883 OutlinedHashSequence.clear();
884 break;
885 }
886 OutlinedHashSequence.push_back(Hash);
887 }
888 }
889
890 // Append a unique name based on the non-empty hash sequence.
891 if (AppendContentHashToOutlinedName && !OutlinedHashSequence.empty()) {
892 auto CombinedHash = stable_hash_combine(OutlinedHashSequence);
893 auto NewName =
894 MF.getName().str() + ".content." + std::to_string(CombinedHash);
895 MF.getFunction().setName(NewName);
896 }
897
898 // Publish the non-empty hash sequence to the local hash tree.
899 if (OutlinerMode == CGDataMode::Write) {
900 StableHashAttempts++;
901 if (!OutlinedHashSequence.empty())
902 LocalHashTree->insert({OutlinedHashSequence, CandSize});
903 else
904 StableHashDropped++;
905 }
906}
907
908MachineFunction *MachineOutliner::createOutlinedFunction(
909 Module &M, OutlinedFunction &OF, InstructionMapper &Mapper, unsigned Name) {
910
911 // Create the function name. This should be unique.
912 // FIXME: We should have a better naming scheme. This should be stable,
913 // regardless of changes to the outliner's cost model/traversal order.
914 std::string FunctionName = "OUTLINED_FUNCTION_";
915 if (OutlineRepeatedNum > 0)
916 FunctionName += std::to_string(OutlineRepeatedNum + 1) + "_";
917 FunctionName += std::to_string(Name);
918 LLVM_DEBUG(dbgs() << "NEW FUNCTION: " << FunctionName << "\n");
919
920 // Create the function using an IR-level function.
921 LLVMContext &C = M.getContext();
922 Function *F = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
923 Function::ExternalLinkage, FunctionName, M);
924
925 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
926 // which gives us better results when we outline from linkonceodr functions.
927 F->setLinkage(GlobalValue::InternalLinkage);
928 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
929
930 // Set optsize/minsize, so we don't insert padding between outlined
931 // functions.
932 F->addFnAttr(Attribute::OptimizeForSize);
933 F->addFnAttr(Attribute::MinSize);
934
935 Candidate &FirstCand = OF.Candidates.front();
936 const TargetInstrInfo &TII =
937 *FirstCand.getMF()->getSubtarget().getInstrInfo();
938
939 TII.mergeOutliningCandidateAttributes(*F, OF.Candidates);
940
941 // Set uwtable, so we generate eh_frame.
942 UWTableKind UW = std::accumulate(
943 OF.Candidates.cbegin(), OF.Candidates.cend(), UWTableKind::None,
944 [](UWTableKind K, const outliner::Candidate &C) {
945 return std::max(K, C.getMF()->getFunction().getUWTableKind());
946 });
947 F->setUWTableKind(UW);
948
949 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
950 IRBuilder<> Builder(EntryBB);
951 Builder.CreateRetVoid();
952
953 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
954 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
955 MF.setIsOutlined(true);
956 MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock();
957
958 // Insert the new function into the module.
959 MF.insert(MF.begin(), &MBB);
960
961 MachineFunction *OriginalMF = FirstCand.front().getMF();
962 const std::vector<MCCFIInstruction> &Instrs =
963 OriginalMF->getFrameInstructions();
964 for (auto &MI : FirstCand) {
965 if (MI.isDebugInstr())
966 continue;
967
968 // Don't keep debug information for outlined instructions.
969 auto DL = DebugLoc();
970 if (MI.isCFIInstruction()) {
971 unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
972 MCCFIInstruction CFI = Instrs[CFIIndex];
973 BuildMI(MBB, MBB.end(), DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
974 .addCFIIndex(MF.addFrameInst(CFI));
975 } else {
976 MachineInstr &NewMI = TII.duplicate(MBB, MBB.end(), MI);
977 NewMI.dropMemRefs(MF);
978 NewMI.setDebugLoc(DL);
979 }
980 }
981
982 if (OutlinerMode != CGDataMode::None)
983 computeAndPublishHashSequence(MF, OF.Candidates.size());
984
985 // Set normal properties for a late MachineFunction.
986 MF.getProperties().resetIsSSA();
987 MF.getProperties().setNoPHIs();
988 MF.getProperties().setNoVRegs();
989 MF.getProperties().setTracksLiveness();
991
992 // Compute live-in set for outlined fn
993 const MachineRegisterInfo &MRI = MF.getRegInfo();
994 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
995 LivePhysRegs LiveIns(TRI);
996 for (auto &Cand : OF.Candidates) {
997 // Figure out live-ins at the first instruction.
998 MachineBasicBlock &OutlineBB = *Cand.front().getParent();
999 LivePhysRegs CandLiveIns(TRI);
1000 CandLiveIns.addLiveOuts(OutlineBB);
1001 for (const MachineInstr &MI :
1002 reverse(make_range(Cand.begin(), OutlineBB.end())))
1003 CandLiveIns.stepBackward(MI);
1004
1005 // The live-in set for the outlined function is the union of the live-ins
1006 // from all the outlining points.
1007 for (MCPhysReg Reg : CandLiveIns)
1008 LiveIns.addReg(Reg);
1009 }
1010 addLiveIns(MBB, LiveIns);
1011
1012 TII.buildOutlinedFrame(MBB, MF, OF);
1013
1014 // If there's a DISubprogram associated with this outlined function, then
1015 // emit debug info for the outlined function.
1016 if (DISubprogram *SP = getSubprogramOrNull(OF)) {
1017 // We have a DISubprogram. Get its DICompileUnit.
1018 DICompileUnit *CU = SP->getUnit();
1019 DIBuilder DB(M, true, CU);
1020 DIFile *Unit = SP->getFile();
1021 Mangler Mg;
1022 // Get the mangled name of the function for the linkage name.
1023 std::string Dummy;
1024 raw_string_ostream MangledNameStream(Dummy);
1025 Mg.getNameWithPrefix(MangledNameStream, F, false);
1026
1027 DISubprogram *OutlinedSP = DB.createFunction(
1028 Unit /* Context */, F->getName(), StringRef(Dummy), Unit /* File */,
1029 0 /* Line 0 is reserved for compiler-generated code. */,
1030 DB.createSubroutineType(DB.getOrCreateTypeArray({})), /* void type */
1031 0, /* Line 0 is reserved for compiler-generated code. */
1032 DINode::DIFlags::FlagArtificial /* Compiler-generated code. */,
1033 /* Outlined code is optimized code by definition. */
1034 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
1035
1036 // Attach subprogram to the function.
1037 F->setSubprogram(OutlinedSP);
1038 // We're done with the DIBuilder.
1039 DB.finalize();
1040 }
1041
1042 return &MF;
1043}
1044
1045bool MachineOutliner::outline(
1046 Module &M, std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
1047 InstructionMapper &Mapper, unsigned &OutlinedFunctionNum) {
1048 LLVM_DEBUG(dbgs() << "*** Outlining ***\n");
1049 LLVM_DEBUG(dbgs() << "NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size()
1050 << "\n");
1051 bool OutlinedSomething = false;
1052
1053 // Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
1054 // The function with highest priority should be outlined first.
1055 stable_sort(FunctionList, [](const std::unique_ptr<OutlinedFunction> &LHS,
1056 const std::unique_ptr<OutlinedFunction> &RHS) {
1057 return LHS->getNotOutlinedCost() * RHS->getOutliningCost() >
1058 RHS->getNotOutlinedCost() * LHS->getOutliningCost();
1059 });
1060
1061 // Walk over each function, outlining them as we go along. Functions are
1062 // outlined greedily, based off the sort above.
1063 auto *UnsignedVecBegin = Mapper.UnsignedVec.begin();
1064 LLVM_DEBUG(dbgs() << "WALKING FUNCTION LIST\n");
1065 for (auto &OF : FunctionList) {
1066#ifndef NDEBUG
1067 auto NumCandidatesBefore = OF->Candidates.size();
1068#endif
1069 // If we outlined something that overlapped with a candidate in a previous
1070 // step, then we can't outline from it.
1071 erase_if(OF->Candidates, [&UnsignedVecBegin](Candidate &C) {
1072 return std::any_of(UnsignedVecBegin + C.getStartIdx(),
1073 UnsignedVecBegin + C.getEndIdx() + 1, [](unsigned I) {
1074 return I == static_cast<unsigned>(-1);
1075 });
1076 });
1077
1078#ifndef NDEBUG
1079 auto NumCandidatesAfter = OF->Candidates.size();
1080 LLVM_DEBUG(dbgs() << "PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
1081 << "/" << NumCandidatesBefore << " candidates\n");
1082#endif
1083
1084 // If we made it unbeneficial to outline this function, skip it.
1085 if (OF->getBenefit() < OutlinerBenefitThreshold) {
1086 LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF->getBenefit()
1087 << " B) < threshold (" << OutlinerBenefitThreshold
1088 << " B)\n");
1089 continue;
1090 }
1091
1092 LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF->getBenefit()
1093 << " B) > threshold (" << OutlinerBenefitThreshold
1094 << " B)\n");
1095
1096 // Remove all Linker Optimization Hints from the candidates.
1097 // TODO: The intersection of the LOHs from all candidates should be legal in
1098 // the outlined function.
1099 SmallPtrSet<MachineInstr *, 2> MIs;
1100 for (Candidate &C : OF->Candidates) {
1101 for (MachineInstr &MI : C)
1102 MIs.insert(&MI);
1103 NumRemovedLOHs += TM->clearLinkerOptimizationHints(MIs);
1104 MIs.clear();
1105 }
1106
1107 // It's beneficial. Create the function and outline its sequence's
1108 // occurrences.
1109 OF->MF = createOutlinedFunction(M, *OF, Mapper, OutlinedFunctionNum);
1110 emitOutlinedFunctionRemark(*OF);
1111 FunctionsCreated++;
1112 OutlinedFunctionNum++; // Created a function, move to the next name.
1113 MachineFunction *MF = OF->MF;
1114 const TargetSubtargetInfo &STI = MF->getSubtarget();
1115 const TargetInstrInfo &TII = *STI.getInstrInfo();
1116
1117 // Replace occurrences of the sequence with calls to the new function.
1118 LLVM_DEBUG(dbgs() << "CREATE OUTLINED CALLS\n");
1119 for (Candidate &C : OF->Candidates) {
1120 MachineBasicBlock &MBB = *C.getMBB();
1121 MachineBasicBlock::iterator StartIt = C.begin();
1122 MachineBasicBlock::iterator EndIt = std::prev(C.end());
1123
1124 // Insert the call.
1125 auto CallInst = TII.insertOutlinedCall(M, MBB, StartIt, *MF, C);
1126// Insert the call.
1127#ifndef NDEBUG
1128 auto MBBBeingOutlinedFromName =
1129 MBB.getName().empty() ? "<unknown>" : MBB.getName().str();
1130 auto MFBeingOutlinedFromName = MBB.getParent()->getName().empty()
1131 ? "<unknown>"
1132 : MBB.getParent()->getName().str();
1133 LLVM_DEBUG(dbgs() << " CALL: " << MF->getName() << " in "
1134 << MFBeingOutlinedFromName << ":"
1135 << MBBBeingOutlinedFromName << "\n");
1136 LLVM_DEBUG(dbgs() << " .. " << *CallInst);
1137#endif
1138
1139 // If the caller tracks liveness, then we need to make sure that
1140 // anything we outline doesn't break liveness assumptions. The outlined
1141 // functions themselves currently don't track liveness, but we should
1142 // make sure that the ranges we yank things out of aren't wrong.
1143 if (MBB.getParent()->getProperties().hasTracksLiveness()) {
1144 // The following code is to add implicit def operands to the call
1145 // instruction. It also updates call site information for moved
1146 // code.
1147 SmallSet<Register, 2> UseRegs, DefRegs;
1148 // Copy over the defs in the outlined range.
1149 // First inst in outlined range <-- Anything that's defined in this
1150 // ... .. range has to be added as an
1151 // implicit Last inst in outlined range <-- def to the call
1152 // instruction. Also remove call site information for outlined block
1153 // of code. The exposed uses need to be copied in the outlined range.
1155 Iter = EndIt.getReverse(),
1156 Last = std::next(CallInst.getReverse());
1157 Iter != Last; Iter++) {
1158 MachineInstr *MI = &*Iter;
1159 SmallSet<Register, 2> InstrUseRegs;
1160 for (MachineOperand &MOP : MI->operands()) {
1161 // Skip over anything that isn't a register.
1162 if (!MOP.isReg())
1163 continue;
1164
1165 if (MOP.isDef()) {
1166 // Introduce DefRegs set to skip the redundant register.
1167 DefRegs.insert(MOP.getReg());
1168 if (UseRegs.count(MOP.getReg()) &&
1169 !InstrUseRegs.count(MOP.getReg()))
1170 // Since the regiester is modeled as defined,
1171 // it is not necessary to be put in use register set.
1172 UseRegs.erase(MOP.getReg());
1173 } else if (!MOP.isUndef()) {
1174 // Any register which is not undefined should
1175 // be put in the use register set.
1176 UseRegs.insert(MOP.getReg());
1177 InstrUseRegs.insert(MOP.getReg());
1178 }
1179 }
1180 if (MI->isCandidateForAdditionalCallInfo())
1181 MI->getMF()->eraseAdditionalCallInfo(MI);
1182 }
1183
1184 for (const Register &I : DefRegs)
1185 // If it's a def, add it to the call instruction.
1186 CallInst->addOperand(
1187 MachineOperand::CreateReg(I, true, /* isDef = true */
1188 true /* isImp = true */));
1189
1190 for (const Register &I : UseRegs)
1191 // If it's a exposed use, add it to the call instruction.
1192 CallInst->addOperand(
1193 MachineOperand::CreateReg(I, false, /* isDef = false */
1194 true /* isImp = true */));
1195 }
1196
1197 // Erase from the point after where the call was inserted up to, and
1198 // including, the final instruction in the sequence.
1199 // Erase needs one past the end, so we need std::next there too.
1200 MBB.erase(std::next(StartIt), std::next(EndIt));
1201
1202 // Keep track of what we removed by marking them all as -1.
1203 for (unsigned &I : make_range(UnsignedVecBegin + C.getStartIdx(),
1204 UnsignedVecBegin + C.getEndIdx() + 1))
1205 I = static_cast<unsigned>(-1);
1206 OutlinedSomething = true;
1207
1208 // Statistics.
1209 NumOutlined++;
1210 }
1211 }
1212
1213 LLVM_DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n");
1214 return OutlinedSomething;
1215}
1216
1217static bool allowPGOOutlining(RunOutliner RunOutlinerMode,
1218 const ProfileSummaryInfo *PSI,
1219 const BlockFrequencyInfo *BFI,
1221 if (RunOutlinerMode != RunOutliner::OptimisticPGO &&
1222 RunOutlinerMode != RunOutliner::ConservativePGO)
1223 return true;
1224 auto *MF = MBB.getParent();
1225 if (MF->getFunction().hasFnAttribute(Attribute::Cold)) {
1226 ++NumPGOAllowedCold;
1227 return true;
1228 }
1229
1230 auto *BB = MBB.getBasicBlock();
1231 if (BB && PSI && BFI)
1232 if (auto Count = BFI->getBlockProfileCount(BB))
1233 return *Count <= PSI->getOrCompColdCountThreshold();
1234
1235 if (RunOutlinerMode == RunOutliner::OptimisticPGO) {
1236 auto *TII = MF->getSubtarget().getInstrInfo();
1237 if (TII->shouldOutlineFromFunctionByDefault(*MF)) {
1238 // Profile data is unavailable, but we optimistically allow outlining
1239 ++NumPGOOptimisticOutlined;
1240 return true;
1241 }
1242 return false;
1243 }
1244 assert(RunOutlinerMode == RunOutliner::ConservativePGO);
1245 // Profile data is unavailable, so we conservatively block outlining
1246 ++NumPGOConservativeBlockedOutlined;
1247 return false;
1248}
1249
1250void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M) {
1251 // Build instruction mappings for each function in the module. Start by
1252 // iterating over each Function in M.
1253 LLVM_DEBUG(dbgs() << "*** Populating mapper ***\n");
1254 bool EnableProfileGuidedOutlining =
1255 RunOutlinerMode == RunOutliner::OptimisticPGO ||
1256 RunOutlinerMode == RunOutliner::ConservativePGO;
1257 ProfileSummaryInfo *PSI = nullptr;
1258 if (EnableProfileGuidedOutlining)
1259 PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
1260 for (Function &F : M) {
1261 LLVM_DEBUG(dbgs() << "MAPPING FUNCTION: " << F.getName() << "\n");
1262
1263 if (F.hasFnAttribute("nooutline")) {
1264 LLVM_DEBUG(dbgs() << "SKIP: Function has nooutline attribute\n");
1265 continue;
1266 }
1267
1268 // There's something in F. Check if it has a MachineFunction associated with
1269 // it.
1270 MachineFunction *MF = MMI->getMachineFunction(F);
1271
1272 // If it doesn't, then there's nothing to outline from. Move to the next
1273 // Function.
1274 if (!MF) {
1275 LLVM_DEBUG(dbgs() << "SKIP: Function does not have a MachineFunction\n");
1276 continue;
1277 }
1278
1279 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1280 BlockFrequencyInfo *BFI = nullptr;
1281 if (EnableProfileGuidedOutlining && F.hasProfileData())
1282 BFI = &getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
1283 if (RunOutlinerMode == RunOutliner::TargetDefault &&
1284 !TII->shouldOutlineFromFunctionByDefault(*MF)) {
1285 LLVM_DEBUG(dbgs() << "SKIP: Target does not want to outline from "
1286 "function by default\n");
1287 continue;
1288 }
1289
1290 // We have a MachineFunction. Ask the target if it's suitable for outlining.
1291 // If it isn't, then move on to the next Function in the module.
1292 if (!TII->isFunctionSafeToOutlineFrom(*MF, OutlineFromLinkOnceODRs)) {
1293 LLVM_DEBUG(dbgs() << "SKIP: " << MF->getName()
1294 << ": unsafe to outline from\n");
1295 continue;
1296 }
1297
1298 // We have a function suitable for outlining. Iterate over every
1299 // MachineBasicBlock in MF and try to map its instructions to a list of
1300 // unsigned integers.
1301 const unsigned MinMBBSize = 2;
1302
1303 for (MachineBasicBlock &MBB : *MF) {
1304 LLVM_DEBUG(dbgs() << " MAPPING MBB: '" << MBB.getName() << "'\n");
1305 // If there isn't anything in MBB, then there's no point in outlining from
1306 // it.
1307 // If there are fewer than 2 instructions in the MBB, then it can't ever
1308 // contain something worth outlining.
1309 // FIXME: This should be based off of the maximum size in B of an outlined
1310 // call versus the size in B of the MBB.
1311 if (MBB.size() < MinMBBSize) {
1312 LLVM_DEBUG(dbgs() << " SKIP: MBB size less than minimum size of "
1313 << MinMBBSize << "\n");
1314 continue;
1315 }
1316
1317 // Check if MBB could be the target of an indirect branch. If it is, then
1318 // we don't want to outline from it.
1319 if (MBB.hasAddressTaken()) {
1320 LLVM_DEBUG(dbgs() << " SKIP: MBB's address is taken\n");
1321 continue;
1322 }
1323
1324 if (!allowPGOOutlining(RunOutlinerMode, PSI, BFI, MBB)) {
1325 ++NumPGOBlockedOutlined;
1326 continue;
1327 }
1328
1329 // MBB is suitable for outlining. Map it to a list of unsigneds.
1330 Mapper.convertToUnsignedVec(MBB, *TII);
1331 }
1332 }
1333 // Statistics.
1334 UnsignedVecSize = Mapper.UnsignedVec.size();
1335}
1336
1337void MachineOutliner::initSizeRemarkInfo(
1338 const Module &M, StringMap<unsigned> &FunctionToInstrCount) {
1339 // Collect instruction counts for every function. We'll use this to emit
1340 // per-function size remarks later.
1341 for (const Function &F : M) {
1342 MachineFunction *MF = MMI->getMachineFunction(F);
1343
1344 // We only care about MI counts here. If there's no MachineFunction at this
1345 // point, then there won't be after the outliner runs, so let's move on.
1346 if (!MF)
1347 continue;
1348 FunctionToInstrCount[F.getName().str()] = MF->getInstructionCount();
1349 }
1350}
1351
1352void MachineOutliner::emitInstrCountChangedRemark(
1353 const Module &M, const StringMap<unsigned> &FunctionToInstrCount) {
1354 // Iterate over each function in the module and emit remarks.
1355 // Note that we won't miss anything by doing this, because the outliner never
1356 // deletes functions.
1357 for (const Function &F : M) {
1358 MachineFunction *MF = MMI->getMachineFunction(F);
1359
1360 // The outliner never deletes functions. If we don't have a MF here, then we
1361 // didn't have one prior to outlining either.
1362 if (!MF)
1363 continue;
1364
1365 std::string Fname = std::string(F.getName());
1366 unsigned FnCountAfter = MF->getInstructionCount();
1367 unsigned FnCountBefore = 0;
1368
1369 // Check if the function was recorded before.
1370 auto It = FunctionToInstrCount.find(Fname);
1371
1372 // Did we have a previously-recorded size? If yes, then set FnCountBefore
1373 // to that.
1374 if (It != FunctionToInstrCount.end())
1375 FnCountBefore = It->second;
1376
1377 // Compute the delta and emit a remark if there was a change.
1378 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
1379 static_cast<int64_t>(FnCountBefore);
1380 if (FnDelta == 0)
1381 continue;
1382
1383 MachineOptimizationRemarkEmitter MORE(*MF, nullptr);
1384 MORE.emit([&]() {
1385 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
1386 DiagnosticLocation(), &MF->front());
1387 R << DiagnosticInfoOptimizationBase::Argument("Pass", "Machine Outliner")
1388 << ": Function: "
1389 << DiagnosticInfoOptimizationBase::Argument("Function", F.getName())
1390 << ": MI instruction count changed from "
1391 << DiagnosticInfoOptimizationBase::Argument("MIInstrsBefore",
1392 FnCountBefore)
1393 << " to "
1394 << DiagnosticInfoOptimizationBase::Argument("MIInstrsAfter",
1395 FnCountAfter)
1396 << "; Delta: "
1397 << DiagnosticInfoOptimizationBase::Argument("Delta", FnDelta);
1398 return R;
1399 });
1400 }
1401}
1402
1403void MachineOutliner::initializeOutlinerMode(const Module &M) {
1405 return;
1406
1407 if (auto *IndexWrapperPass =
1408 getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>()) {
1409 auto *TheIndex = IndexWrapperPass->getIndex();
1410 // (Full)LTO module does not have functions added to the index.
1411 // In this case, we run the outliner without using codegen data as usual.
1412 if (TheIndex && !TheIndex->hasExportedFunctions(M))
1413 return;
1414 }
1415
1416 // When codegen data write is enabled, we want to write the local outlined
1417 // hash tree to the custom section, `__llvm_outline`.
1418 // When the outlined hash tree is available from the previous codegen data,
1419 // we want to read it to optimistically create global outlining candidates.
1420 if (cgdata::emitCGData()) {
1421 OutlinerMode = CGDataMode::Write;
1422 // Create a local outlined hash tree to be published.
1423 LocalHashTree = std::make_unique<OutlinedHashTree>();
1424 // We don't need to read the outlined hash tree from the previous codegen
1425 } else if (cgdata::hasOutlinedHashTree())
1426 OutlinerMode = CGDataMode::Read;
1427}
1428
1429void MachineOutliner::emitOutlinedHashTree(Module &M) {
1430 assert(LocalHashTree);
1431 if (!LocalHashTree->empty()) {
1432 LLVM_DEBUG({
1433 dbgs() << "Emit outlined hash tree. Size: " << LocalHashTree->size()
1434 << "\n";
1435 });
1436 SmallVector<char> Buf;
1437 raw_svector_ostream OS(Buf);
1438
1439 OutlinedHashTreeRecord HTR(std::move(LocalHashTree));
1440 HTR.serialize(OS);
1441
1442 llvm::StringRef Data(Buf.data(), Buf.size());
1443 std::unique_ptr<MemoryBuffer> Buffer =
1444 MemoryBuffer::getMemBuffer(Data, "in-memory outlined hash tree", false);
1445
1446 Triple TT(M.getTargetTriple());
1448 M, *Buffer,
1449 getCodeGenDataSectionName(CG_outline, TT.getObjectFormat()));
1450 }
1451}
1452
1453bool MachineOutliner::runOnModule(Module &M) {
1454 if (skipModule(M))
1455 return false;
1456
1457 // Check if there's anything in the module. If it's empty, then there's
1458 // nothing to outline.
1459 if (M.empty())
1460 return false;
1461
1462 // Initialize the outliner mode.
1463 initializeOutlinerMode(M);
1464
1465 MMI = &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
1466 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
1467
1468 // Number to append to the current outlined function.
1469 unsigned OutlinedFunctionNum = 0;
1470
1471 OutlineRepeatedNum = 0;
1472 if (!doOutline(M, OutlinedFunctionNum))
1473 return false;
1474
1475 for (unsigned I = 0; I < OutlinerReruns; ++I) {
1476 OutlinedFunctionNum = 0;
1477 OutlineRepeatedNum++;
1478 if (!doOutline(M, OutlinedFunctionNum)) {
1479 LLVM_DEBUG({
1480 dbgs() << "Did not outline on iteration " << I + 2 << " out of "
1481 << OutlinerReruns + 1 << "\n";
1482 });
1483 break;
1484 }
1485 }
1486
1487 if (OutlinerMode == CGDataMode::Write)
1488 emitOutlinedHashTree(M);
1489
1490 return true;
1491}
1492
1493bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
1494 // If the user passed -enable-machine-outliner=always or
1495 // -enable-machine-outliner, the pass will run on all functions in the module.
1496 // Otherwise, if the target supports default outlining, it will run on all
1497 // functions deemed by the target to be worth outlining from by default. Tell
1498 // the user how the outliner is running.
1499 LLVM_DEBUG({
1500 dbgs() << "Machine Outliner: Running on ";
1501 switch (RunOutlinerMode) {
1502 case RunOutliner::AlwaysOutline:
1503 dbgs() << "all functions";
1504 break;
1505 case RunOutliner::OptimisticPGO:
1506 dbgs() << "optimistically cold functions";
1507 break;
1508 case RunOutliner::ConservativePGO:
1509 dbgs() << "conservatively cold functions";
1510 break;
1511 case RunOutliner::TargetDefault:
1512 dbgs() << "target-default functions";
1513 break;
1514 case RunOutliner::NeverOutline:
1515 llvm_unreachable("should not outline");
1516 }
1517 dbgs() << "\n";
1518 });
1519
1520 // If the user specifies that they want to outline from linkonceodrs, set
1521 // it here.
1522 OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining;
1523 InstructionMapper Mapper(*MMI);
1524
1525 // Prepare instruction mappings for the suffix tree.
1526 populateMapper(Mapper, M);
1527 std::vector<std::unique_ptr<OutlinedFunction>> FunctionList;
1528
1529 // Find all of the outlining candidates.
1530 if (OutlinerMode == CGDataMode::Read)
1531 findGlobalCandidates(Mapper, FunctionList);
1532 else
1533 findCandidates(Mapper, FunctionList);
1534
1535 // If we've requested size remarks, then collect the MI counts of every
1536 // function before outlining, and the MI counts after outlining.
1537 // FIXME: This shouldn't be in the outliner at all; it should ultimately be
1538 // the pass manager's responsibility.
1539 // This could pretty easily be placed in outline instead, but because we
1540 // really ultimately *don't* want this here, it's done like this for now
1541 // instead.
1542
1543 // Check if we want size remarks.
1544 bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark();
1545 StringMap<unsigned> FunctionToInstrCount;
1546 if (ShouldEmitSizeRemarks)
1547 initSizeRemarkInfo(M, FunctionToInstrCount);
1548
1549 // Outline each of the candidates and return true if something was outlined.
1550 bool OutlinedSomething =
1551 outline(M, FunctionList, Mapper, OutlinedFunctionNum);
1552
1553 // If we outlined something, we definitely changed the MI count of the
1554 // module. If we've asked for size remarks, then output them.
1555 // FIXME: This should be in the pass manager.
1556 if (ShouldEmitSizeRemarks && OutlinedSomething)
1557 emitInstrCountChangedRemark(M, FunctionToInstrCount);
1558
1559 LLVM_DEBUG({
1560 if (!OutlinedSomething)
1561 dbgs() << "Stopped outlining at iteration " << OutlineRepeatedNum
1562 << " because no changes were found.\n";
1563 });
1564
1565 return OutlinedSomething;
1566}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the DenseMap class.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
static DISubprogram * getSubprogramOrNull(OutlinableGroup &Group)
Get the subprogram if it exists for one of the outlined regions.
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
static cl::opt< bool > DisableGlobalOutlining("disable-global-outlining", cl::Hidden, cl::desc("Disable global outlining only by ignoring " "the codegen data generation or use"), cl::init(false))
static bool allowPGOOutlining(RunOutliner RunOutlinerMode, const ProfileSummaryInfo *PSI, const BlockFrequencyInfo *BFI, MachineBasicBlock &MBB)
static cl::opt< unsigned > OutlinerBenefitThreshold("outliner-benefit-threshold", cl::init(1), cl::Hidden, cl::desc("The minimum size in bytes before an outlining candidate is accepted"))
static cl::opt< bool > OutlinerLeafDescendants("outliner-leaf-descendants", cl::init(true), cl::Hidden, cl::desc("Consider all leaf descendants of internal nodes of the suffix " "tree as candidates for outlining (if false, only leaf children " "are considered)"))
static cl::opt< bool > AppendContentHashToOutlinedName("append-content-hash-outlined-name", cl::Hidden, cl::desc("This appends the content hash to the globally outlined function " "name. It's beneficial for enhancing the precision of the stable " "hash and for ordering the outlined functions."), cl::init(true))
static cl::opt< unsigned > OutlinerReruns("machine-outliner-reruns", cl::init(0), cl::Hidden, cl::desc("Number of times to rerun the outliner after the initial outline"))
Number of times to re-run the outliner.
static cl::opt< bool > EnableLinkOnceODROutlining("enable-linkonceodr-outlining", cl::Hidden, cl::desc("Enable the machine outliner on linkonceodr functions"), cl::init(false))
static SmallVector< MatchedEntry > getMatchedEntries(InstructionMapper &Mapper)
Contains all data structures shared between the outliner implemented in MachineOutliner....
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
This is the interface to build a ModuleSummaryIndex for a module.
static Expected< Function * > createOutlinedFunction(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, const OpenMPIRBuilder::TargetKernelDefaultAttrs &DefaultAttrs, StringRef FuncName, SmallVectorImpl< Value * > &Inputs, OpenMPIRBuilder::TargetBodyGenCallbackTy &CBFunc, OpenMPIRBuilder::TargetGenArgAccessorsCallbackTy &ArgAccessorFuncCB)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:167
#define LLVM_DEBUG(...)
Definition Debug.h:114
Target-Independent Code Generator Pass Configuration Options pass.
Value * RHS
Value * LHS
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:214
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:166
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
unsigned getInstructionCount() const
Return the number of MachineInstrs in this MachineFunction.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const std::vector< MCCFIInstruction > & getFrameInstructions() const
Returns a reference to a list of cfi instructions in the function's prologue.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
Representation of each machine instruction.
bool isDebugInstr() const
LLVM_ABI void dropMemRefs(MachineFunction &MF)
Clear this MachineInstr's memory reference descriptor list.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
This class contains meta information specific to a module.
LLVM_ABI MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
LLVM_ABI MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
Diagnostic information for missed-optimization remarks.
LLVM_ABI void freezeReservedRegs()
freezeReservedRegs - Called by the register allocator to freeze the set of reserved registers before ...
LLVM_ABI void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition Mangler.cpp:121
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
const HashNode * getRoot() const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Analysis providing profile information.
LLVM_ABI uint64_t getOrCompColdCountThreshold() const
Returns ColdCountThreshold if set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:175
bool erase(const T &V)
Definition SmallSet.h:197
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:181
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
iterator end()
Definition StringMap.h:224
iterator find(StringRef Key)
Definition StringMap.h:235
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:233
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:154
virtual const TargetInstrInfo * getInstrInfo() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
SmallVector< const MachineInstr * > InstrList
bool hasOutlinedHashTree()
const OutlinedHashTree * getOutlinedHashTree()
bool emitCGData()
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:477
void stable_sort(R &&Range)
Definition STLExtras.h:2040
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2118
uint64_t stable_hash
An opaque object representing a stable hash code.
LLVM_ABI void initializeMachineOutlinerPass(PassRegistry &)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:400
UWTableKind
Definition CodeGen.h:148
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1632
LLVM_ABI stable_hash stableHashValue(const MachineOperand &MO)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI ModulePass * createMachineOutlinerPass(RunOutliner RunOutlinerMode)
This pass performs outlining on machine instructions directly before printing assembly.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2102
stable_hash stable_hash_combine(ArrayRef< stable_hash > Buffer)
LLVM_ABI void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName, Align Alignment=Align(1))
Embed the memory buffer Buf into the module M as a global using the specified section name.
void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.
LLVM_ABI std::string getCodeGenDataSectionName(CGDataSectKind CGSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
#define MORE()
Definition regcomp.c:246
MatchedEntry()=delete
MatchedEntry(unsigned StartIdx, unsigned EndIdx, unsigned Count)
An information struct used to provide DenseMap with the various necessary components for a given valu...
A HashNode is an entry in an OutlinedHashTree, holding a hash value and a collection of Successors (o...
std::optional< unsigned > Terminals
The number of terminals in the sequence ending at this node.
An individual sequence of instructions to be replaced with a call to an outlined function.
MachineFunction * getMF() const
The information necessary to create an outlined function for some class of candidate.