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