[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / CodeGen / MachineOutliner.cpp
blob7783b5e0d3cc4b003d031ef63e5d2bb74bd61274
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 //===----------------------------------------------------------------------===//
57 #include "llvm/CodeGen/MachineOutliner.h"
58 #include "llvm/ADT/DenseMap.h"
59 #include "llvm/ADT/SmallSet.h"
60 #include "llvm/ADT/Statistic.h"
61 #include "llvm/ADT/Twine.h"
62 #include "llvm/CodeGen/MachineModuleInfo.h"
63 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
64 #include "llvm/CodeGen/Passes.h"
65 #include "llvm/CodeGen/TargetInstrInfo.h"
66 #include "llvm/CodeGen/TargetSubtargetInfo.h"
67 #include "llvm/IR/DIBuilder.h"
68 #include "llvm/IR/IRBuilder.h"
69 #include "llvm/IR/Mangler.h"
70 #include "llvm/InitializePasses.h"
71 #include "llvm/Support/CommandLine.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/SuffixTree.h"
74 #include "llvm/Support/raw_ostream.h"
75 #include <functional>
76 #include <tuple>
77 #include <vector>
79 #define DEBUG_TYPE "machine-outliner"
81 using namespace llvm;
82 using namespace ore;
83 using namespace outliner;
85 STATISTIC(NumOutlined, "Number of candidates outlined");
86 STATISTIC(FunctionsCreated, "Number of functions created");
88 // Set to true if the user wants the outliner to run on linkonceodr linkage
89 // functions. This is false by default because the linker can dedupe linkonceodr
90 // functions. Since the outliner is confined to a single module (modulo LTO),
91 // this is off by default. It should, however, be the default behaviour in
92 // LTO.
93 static cl::opt<bool> EnableLinkOnceODROutlining(
94 "enable-linkonceodr-outlining", cl::Hidden,
95 cl::desc("Enable the machine outliner on linkonceodr functions"),
96 cl::init(false));
98 /// Number of times to re-run the outliner. This is not the total number of runs
99 /// as the outliner will run at least one time. The default value is set to 0,
100 /// meaning the outliner will run one time and rerun zero times after that.
101 static cl::opt<unsigned> OutlinerReruns(
102 "machine-outliner-reruns", cl::init(0), cl::Hidden,
103 cl::desc(
104 "Number of times to rerun the outliner after the initial outline"));
106 namespace {
108 /// Maps \p MachineInstrs to unsigned integers and stores the mappings.
109 struct InstructionMapper {
111 /// The next available integer to assign to a \p MachineInstr that
112 /// cannot be outlined.
114 /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
115 unsigned IllegalInstrNumber = -3;
117 /// The next available integer to assign to a \p MachineInstr that can
118 /// be outlined.
119 unsigned LegalInstrNumber = 0;
121 /// Correspondence from \p MachineInstrs to unsigned integers.
122 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>
123 InstructionIntegerMap;
125 /// Correspondence between \p MachineBasicBlocks and target-defined flags.
126 DenseMap<MachineBasicBlock *, unsigned> MBBFlagsMap;
128 /// The vector of unsigned integers that the module is mapped to.
129 std::vector<unsigned> UnsignedVec;
131 /// Stores the location of the instruction associated with the integer
132 /// at index i in \p UnsignedVec for each index i.
133 std::vector<MachineBasicBlock::iterator> InstrList;
135 // Set if we added an illegal number in the previous step.
136 // Since each illegal number is unique, we only need one of them between
137 // each range of legal numbers. This lets us make sure we don't add more
138 // than one illegal number per range.
139 bool AddedIllegalLastTime = false;
141 /// Maps \p *It to a legal integer.
143 /// Updates \p CanOutlineWithPrevInstr, \p HaveLegalRange, \p InstrListForMBB,
144 /// \p UnsignedVecForMBB, \p InstructionIntegerMap, and \p LegalInstrNumber.
146 /// \returns The integer that \p *It was mapped to.
147 unsigned mapToLegalUnsigned(
148 MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
149 bool &HaveLegalRange, unsigned &NumLegalInBlock,
150 std::vector<unsigned> &UnsignedVecForMBB,
151 std::vector<MachineBasicBlock::iterator> &InstrListForMBB) {
152 // We added something legal, so we should unset the AddedLegalLastTime
153 // flag.
154 AddedIllegalLastTime = false;
156 // If we have at least two adjacent legal instructions (which may have
157 // invisible instructions in between), remember that.
158 if (CanOutlineWithPrevInstr)
159 HaveLegalRange = true;
160 CanOutlineWithPrevInstr = true;
162 // Keep track of the number of legal instructions we insert.
163 NumLegalInBlock++;
165 // Get the integer for this instruction or give it the current
166 // LegalInstrNumber.
167 InstrListForMBB.push_back(It);
168 MachineInstr &MI = *It;
169 bool WasInserted;
170 DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>::iterator
171 ResultIt;
172 std::tie(ResultIt, WasInserted) =
173 InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber));
174 unsigned MINumber = ResultIt->second;
176 // There was an insertion.
177 if (WasInserted)
178 LegalInstrNumber++;
180 UnsignedVecForMBB.push_back(MINumber);
182 // Make sure we don't overflow or use any integers reserved by the DenseMap.
183 if (LegalInstrNumber >= IllegalInstrNumber)
184 report_fatal_error("Instruction mapping overflow!");
186 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
187 "Tried to assign DenseMap tombstone or empty key to instruction.");
188 assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
189 "Tried to assign DenseMap tombstone or empty key to instruction.");
191 return MINumber;
194 /// Maps \p *It to an illegal integer.
196 /// Updates \p InstrListForMBB, \p UnsignedVecForMBB, and \p
197 /// IllegalInstrNumber.
199 /// \returns The integer that \p *It was mapped to.
200 unsigned mapToIllegalUnsigned(
201 MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
202 std::vector<unsigned> &UnsignedVecForMBB,
203 std::vector<MachineBasicBlock::iterator> &InstrListForMBB) {
204 // Can't outline an illegal instruction. Set the flag.
205 CanOutlineWithPrevInstr = false;
207 // Only add one illegal number per range of legal numbers.
208 if (AddedIllegalLastTime)
209 return IllegalInstrNumber;
211 // Remember that we added an illegal number last time.
212 AddedIllegalLastTime = true;
213 unsigned MINumber = IllegalInstrNumber;
215 InstrListForMBB.push_back(It);
216 UnsignedVecForMBB.push_back(IllegalInstrNumber);
217 IllegalInstrNumber--;
219 assert(LegalInstrNumber < IllegalInstrNumber &&
220 "Instruction mapping overflow!");
222 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
223 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
225 assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
226 "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
228 return MINumber;
231 /// Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
232 /// and appends it to \p UnsignedVec and \p InstrList.
234 /// Two instructions are assigned the same integer if they are identical.
235 /// If an instruction is deemed unsafe to outline, then it will be assigned an
236 /// unique integer. The resulting mapping is placed into a suffix tree and
237 /// queried for candidates.
239 /// \param MBB The \p MachineBasicBlock to be translated into integers.
240 /// \param TII \p TargetInstrInfo for the function.
241 void convertToUnsignedVec(MachineBasicBlock &MBB,
242 const TargetInstrInfo &TII) {
243 unsigned Flags = 0;
245 // Don't even map in this case.
246 if (!TII.isMBBSafeToOutlineFrom(MBB, Flags))
247 return;
249 // Store info for the MBB for later outlining.
250 MBBFlagsMap[&MBB] = Flags;
252 MachineBasicBlock::iterator It = MBB.begin();
254 // The number of instructions in this block that will be considered for
255 // outlining.
256 unsigned NumLegalInBlock = 0;
258 // True if we have at least two legal instructions which aren't separated
259 // by an illegal instruction.
260 bool HaveLegalRange = false;
262 // True if we can perform outlining given the last mapped (non-invisible)
263 // instruction. This lets us know if we have a legal range.
264 bool CanOutlineWithPrevInstr = false;
266 // FIXME: Should this all just be handled in the target, rather than using
267 // repeated calls to getOutliningType?
268 std::vector<unsigned> UnsignedVecForMBB;
269 std::vector<MachineBasicBlock::iterator> InstrListForMBB;
271 for (MachineBasicBlock::iterator Et = MBB.end(); It != Et; ++It) {
272 // Keep track of where this instruction is in the module.
273 switch (TII.getOutliningType(It, Flags)) {
274 case InstrType::Illegal:
275 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
276 InstrListForMBB);
277 break;
279 case InstrType::Legal:
280 mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
281 NumLegalInBlock, UnsignedVecForMBB, InstrListForMBB);
282 break;
284 case InstrType::LegalTerminator:
285 mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
286 NumLegalInBlock, UnsignedVecForMBB, InstrListForMBB);
287 // The instruction also acts as a terminator, so we have to record that
288 // in the string.
289 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
290 InstrListForMBB);
291 break;
293 case InstrType::Invisible:
294 // Normally this is set by mapTo(Blah)Unsigned, but we just want to
295 // skip this instruction. So, unset the flag here.
296 AddedIllegalLastTime = false;
297 break;
301 // Are there enough legal instructions in the block for outlining to be
302 // possible?
303 if (HaveLegalRange) {
304 // After we're done every insertion, uniquely terminate this part of the
305 // "string". This makes sure we won't match across basic block or function
306 // boundaries since the "end" is encoded uniquely and thus appears in no
307 // repeated substring.
308 mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
309 InstrListForMBB);
310 llvm::append_range(InstrList, InstrListForMBB);
311 llvm::append_range(UnsignedVec, UnsignedVecForMBB);
315 InstructionMapper() {
316 // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
317 // changed.
318 assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
319 "DenseMapInfo<unsigned>'s empty key isn't -1!");
320 assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 &&
321 "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
325 /// An interprocedural pass which finds repeated sequences of
326 /// instructions and replaces them with calls to functions.
328 /// Each instruction is mapped to an unsigned integer and placed in a string.
329 /// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
330 /// is then repeatedly queried for repeated sequences of instructions. Each
331 /// non-overlapping repeated sequence is then placed in its own
332 /// \p MachineFunction and each instance is then replaced with a call to that
333 /// function.
334 struct MachineOutliner : public ModulePass {
336 static char ID;
338 /// Set to true if the outliner should consider functions with
339 /// linkonceodr linkage.
340 bool OutlineFromLinkOnceODRs = false;
342 /// The current repeat number of machine outlining.
343 unsigned OutlineRepeatedNum = 0;
345 /// Set to true if the outliner should run on all functions in the module
346 /// considered safe for outlining.
347 /// Set to true by default for compatibility with llc's -run-pass option.
348 /// Set when the pass is constructed in TargetPassConfig.
349 bool RunOnAllFunctions = true;
351 StringRef getPassName() const override { return "Machine Outliner"; }
353 void getAnalysisUsage(AnalysisUsage &AU) const override {
354 AU.addRequired<MachineModuleInfoWrapperPass>();
355 AU.addPreserved<MachineModuleInfoWrapperPass>();
356 AU.setPreservesAll();
357 ModulePass::getAnalysisUsage(AU);
360 MachineOutliner() : ModulePass(ID) {
361 initializeMachineOutlinerPass(*PassRegistry::getPassRegistry());
364 /// Remark output explaining that not outlining a set of candidates would be
365 /// better than outlining that set.
366 void emitNotOutliningCheaperRemark(
367 unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
368 OutlinedFunction &OF);
370 /// Remark output explaining that a function was outlined.
371 void emitOutlinedFunctionRemark(OutlinedFunction &OF);
373 /// Find all repeated substrings that satisfy the outlining cost model by
374 /// constructing a suffix tree.
376 /// If a substring appears at least twice, then it must be represented by
377 /// an internal node which appears in at least two suffixes. Each suffix
378 /// is represented by a leaf node. To do this, we visit each internal node
379 /// in the tree, using the leaf children of each internal node. If an
380 /// internal node represents a beneficial substring, then we use each of
381 /// its leaf children to find the locations of its substring.
383 /// \param Mapper Contains outlining mapping information.
384 /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
385 /// each type of candidate.
386 void findCandidates(InstructionMapper &Mapper,
387 std::vector<OutlinedFunction> &FunctionList);
389 /// Replace the sequences of instructions represented by \p OutlinedFunctions
390 /// with calls to functions.
392 /// \param M The module we are outlining from.
393 /// \param FunctionList A list of functions to be inserted into the module.
394 /// \param Mapper Contains the instruction mappings for the module.
395 bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList,
396 InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
398 /// Creates a function for \p OF and inserts it into the module.
399 MachineFunction *createOutlinedFunction(Module &M, OutlinedFunction &OF,
400 InstructionMapper &Mapper,
401 unsigned Name);
403 /// Calls 'doOutline()' 1 + OutlinerReruns times.
404 bool runOnModule(Module &M) override;
406 /// Construct a suffix tree on the instructions in \p M and outline repeated
407 /// strings from that tree.
408 bool doOutline(Module &M, unsigned &OutlinedFunctionNum);
410 /// Return a DISubprogram for OF if one exists, and null otherwise. Helper
411 /// function for remark emission.
412 DISubprogram *getSubprogramOrNull(const OutlinedFunction &OF) {
413 for (const Candidate &C : OF.Candidates)
414 if (MachineFunction *MF = C.getMF())
415 if (DISubprogram *SP = MF->getFunction().getSubprogram())
416 return SP;
417 return nullptr;
420 /// Populate and \p InstructionMapper with instruction-to-integer mappings.
421 /// These are used to construct a suffix tree.
422 void populateMapper(InstructionMapper &Mapper, Module &M,
423 MachineModuleInfo &MMI);
425 /// Initialize information necessary to output a size remark.
426 /// FIXME: This should be handled by the pass manager, not the outliner.
427 /// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
428 /// pass manager.
429 void initSizeRemarkInfo(const Module &M, const MachineModuleInfo &MMI,
430 StringMap<unsigned> &FunctionToInstrCount);
432 /// Emit the remark.
433 // FIXME: This should be handled by the pass manager, not the outliner.
434 void
435 emitInstrCountChangedRemark(const Module &M, const MachineModuleInfo &MMI,
436 const StringMap<unsigned> &FunctionToInstrCount);
438 } // Anonymous namespace.
440 char MachineOutliner::ID = 0;
442 namespace llvm {
443 ModulePass *createMachineOutlinerPass(bool RunOnAllFunctions) {
444 MachineOutliner *OL = new MachineOutliner();
445 OL->RunOnAllFunctions = RunOnAllFunctions;
446 return OL;
449 } // namespace llvm
451 INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false,
452 false)
454 void MachineOutliner::emitNotOutliningCheaperRemark(
455 unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
456 OutlinedFunction &OF) {
457 // FIXME: Right now, we arbitrarily choose some Candidate from the
458 // OutlinedFunction. This isn't necessarily fixed, nor does it have to be.
459 // We should probably sort these by function name or something to make sure
460 // the remarks are stable.
461 Candidate &C = CandidatesForRepeatedSeq.front();
462 MachineOptimizationRemarkEmitter MORE(*(C.getMF()), nullptr);
463 MORE.emit([&]() {
464 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
465 C.front()->getDebugLoc(), C.getMBB());
466 R << "Did not outline " << NV("Length", StringLen) << " instructions"
467 << " from " << NV("NumOccurrences", CandidatesForRepeatedSeq.size())
468 << " locations."
469 << " Bytes from outlining all occurrences ("
470 << NV("OutliningCost", OF.getOutliningCost()) << ")"
471 << " >= Unoutlined instruction bytes ("
472 << NV("NotOutliningCost", OF.getNotOutlinedCost()) << ")"
473 << " (Also found at: ";
475 // Tell the user the other places the candidate was found.
476 for (unsigned i = 1, e = CandidatesForRepeatedSeq.size(); i < e; i++) {
477 R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
478 CandidatesForRepeatedSeq[i].front()->getDebugLoc());
479 if (i != e - 1)
480 R << ", ";
483 R << ")";
484 return R;
488 void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
489 MachineBasicBlock *MBB = &*OF.MF->begin();
490 MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr);
491 MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction",
492 MBB->findDebugLoc(MBB->begin()), MBB);
493 R << "Saved " << NV("OutliningBenefit", OF.getBenefit()) << " bytes by "
494 << "outlining " << NV("Length", OF.getNumInstrs()) << " instructions "
495 << "from " << NV("NumOccurrences", OF.getOccurrenceCount())
496 << " locations. "
497 << "(Found at: ";
499 // Tell the user the other places the candidate was found.
500 for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) {
502 R << NV((Twine("StartLoc") + Twine(i)).str(),
503 OF.Candidates[i].front()->getDebugLoc());
504 if (i != e - 1)
505 R << ", ";
508 R << ")";
510 MORE.emit(R);
513 void MachineOutliner::findCandidates(
514 InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
515 FunctionList.clear();
516 SuffixTree ST(Mapper.UnsignedVec);
518 // First, find all of the repeated substrings in the tree of minimum length
519 // 2.
520 std::vector<Candidate> CandidatesForRepeatedSeq;
521 for (const SuffixTree::RepeatedSubstring &RS : ST) {
522 CandidatesForRepeatedSeq.clear();
523 unsigned StringLen = RS.Length;
524 for (const unsigned &StartIdx : RS.StartIndices) {
525 unsigned EndIdx = StartIdx + StringLen - 1;
526 // Trick: Discard some candidates that would be incompatible with the
527 // ones we've already found for this sequence. This will save us some
528 // work in candidate selection.
530 // If two candidates overlap, then we can't outline them both. This
531 // happens when we have candidates that look like, say
533 // AA (where each "A" is an instruction).
535 // We might have some portion of the module that looks like this:
536 // AAAAAA (6 A's)
538 // In this case, there are 5 different copies of "AA" in this range, but
539 // at most 3 can be outlined. If only outlining 3 of these is going to
540 // be unbeneficial, then we ought to not bother.
542 // Note that two things DON'T overlap when they look like this:
543 // start1...end1 .... start2...end2
544 // That is, one must either
545 // * End before the other starts
546 // * Start after the other ends
547 if (llvm::all_of(CandidatesForRepeatedSeq, [&StartIdx,
548 &EndIdx](const Candidate &C) {
549 return (EndIdx < C.getStartIdx() || StartIdx > C.getEndIdx());
550 })) {
551 // It doesn't overlap with anything, so we can outline it.
552 // Each sequence is over [StartIt, EndIt].
553 // Save the candidate and its location.
555 MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx];
556 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
557 MachineBasicBlock *MBB = StartIt->getParent();
559 CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen, StartIt,
560 EndIt, MBB, FunctionList.size(),
561 Mapper.MBBFlagsMap[MBB]);
565 // We've found something we might want to outline.
566 // Create an OutlinedFunction to store it and check if it'd be beneficial
567 // to outline.
568 if (CandidatesForRepeatedSeq.size() < 2)
569 continue;
571 // Arbitrarily choose a TII from the first candidate.
572 // FIXME: Should getOutliningCandidateInfo move to TargetMachine?
573 const TargetInstrInfo *TII =
574 CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo();
576 OutlinedFunction OF =
577 TII->getOutliningCandidateInfo(CandidatesForRepeatedSeq);
579 // If we deleted too many candidates, then there's nothing worth outlining.
580 // FIXME: This should take target-specified instruction sizes into account.
581 if (OF.Candidates.size() < 2)
582 continue;
584 // Is it better to outline this candidate than not?
585 if (OF.getBenefit() < 1) {
586 emitNotOutliningCheaperRemark(StringLen, CandidatesForRepeatedSeq, OF);
587 continue;
590 FunctionList.push_back(OF);
594 MachineFunction *MachineOutliner::createOutlinedFunction(
595 Module &M, OutlinedFunction &OF, InstructionMapper &Mapper, unsigned Name) {
597 // Create the function name. This should be unique.
598 // FIXME: We should have a better naming scheme. This should be stable,
599 // regardless of changes to the outliner's cost model/traversal order.
600 std::string FunctionName = "OUTLINED_FUNCTION_";
601 if (OutlineRepeatedNum > 0)
602 FunctionName += std::to_string(OutlineRepeatedNum + 1) + "_";
603 FunctionName += std::to_string(Name);
605 // Create the function using an IR-level function.
606 LLVMContext &C = M.getContext();
607 Function *F = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
608 Function::ExternalLinkage, FunctionName, M);
610 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
611 // which gives us better results when we outline from linkonceodr functions.
612 F->setLinkage(GlobalValue::InternalLinkage);
613 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
615 // Set optsize/minsize, so we don't insert padding between outlined
616 // functions.
617 F->addFnAttr(Attribute::OptimizeForSize);
618 F->addFnAttr(Attribute::MinSize);
620 Candidate &FirstCand = OF.Candidates.front();
621 const TargetInstrInfo &TII =
622 *FirstCand.getMF()->getSubtarget().getInstrInfo();
624 TII.mergeOutliningCandidateAttributes(*F, OF.Candidates);
626 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
627 IRBuilder<> Builder(EntryBB);
628 Builder.CreateRetVoid();
630 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
631 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
632 MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock();
634 // Insert the new function into the module.
635 MF.insert(MF.begin(), &MBB);
637 MachineFunction *OriginalMF = FirstCand.front()->getMF();
638 const std::vector<MCCFIInstruction> &Instrs =
639 OriginalMF->getFrameInstructions();
640 for (auto I = FirstCand.front(), E = std::next(FirstCand.back()); I != E;
641 ++I) {
642 if (I->isDebugInstr())
643 continue;
644 MachineInstr *NewMI = MF.CloneMachineInstr(&*I);
645 if (I->isCFIInstruction()) {
646 unsigned CFIIndex = NewMI->getOperand(0).getCFIIndex();
647 MCCFIInstruction CFI = Instrs[CFIIndex];
648 (void)MF.addFrameInst(CFI);
650 NewMI->dropMemRefs(MF);
652 // Don't keep debug information for outlined instructions.
653 NewMI->setDebugLoc(DebugLoc());
654 MBB.insert(MBB.end(), NewMI);
657 // Set normal properties for a late MachineFunction.
658 MF.getProperties().reset(MachineFunctionProperties::Property::IsSSA);
659 MF.getProperties().set(MachineFunctionProperties::Property::NoPHIs);
660 MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
661 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
662 MF.getRegInfo().freezeReservedRegs(MF);
664 // Compute live-in set for outlined fn
665 const MachineRegisterInfo &MRI = MF.getRegInfo();
666 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
667 LivePhysRegs LiveIns(TRI);
668 for (auto &Cand : OF.Candidates) {
669 // Figure out live-ins at the first instruction.
670 MachineBasicBlock &OutlineBB = *Cand.front()->getParent();
671 LivePhysRegs CandLiveIns(TRI);
672 CandLiveIns.addLiveOuts(OutlineBB);
673 for (const MachineInstr &MI :
674 reverse(make_range(Cand.front(), OutlineBB.end())))
675 CandLiveIns.stepBackward(MI);
677 // The live-in set for the outlined function is the union of the live-ins
678 // from all the outlining points.
679 for (MCPhysReg Reg : CandLiveIns)
680 LiveIns.addReg(Reg);
682 addLiveIns(MBB, LiveIns);
684 TII.buildOutlinedFrame(MBB, MF, OF);
686 // If there's a DISubprogram associated with this outlined function, then
687 // emit debug info for the outlined function.
688 if (DISubprogram *SP = getSubprogramOrNull(OF)) {
689 // We have a DISubprogram. Get its DICompileUnit.
690 DICompileUnit *CU = SP->getUnit();
691 DIBuilder DB(M, true, CU);
692 DIFile *Unit = SP->getFile();
693 Mangler Mg;
694 // Get the mangled name of the function for the linkage name.
695 std::string Dummy;
696 llvm::raw_string_ostream MangledNameStream(Dummy);
697 Mg.getNameWithPrefix(MangledNameStream, F, false);
699 DISubprogram *OutlinedSP = DB.createFunction(
700 Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()),
701 Unit /* File */,
702 0 /* Line 0 is reserved for compiler-generated code. */,
703 DB.createSubroutineType(DB.getOrCreateTypeArray(None)), /* void type */
704 0, /* Line 0 is reserved for compiler-generated code. */
705 DINode::DIFlags::FlagArtificial /* Compiler-generated code. */,
706 /* Outlined code is optimized code by definition. */
707 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
709 // Don't add any new variables to the subprogram.
710 DB.finalizeSubprogram(OutlinedSP);
712 // Attach subprogram to the function.
713 F->setSubprogram(OutlinedSP);
714 // We're done with the DIBuilder.
715 DB.finalize();
718 return &MF;
721 bool MachineOutliner::outline(Module &M,
722 std::vector<OutlinedFunction> &FunctionList,
723 InstructionMapper &Mapper,
724 unsigned &OutlinedFunctionNum) {
726 bool OutlinedSomething = false;
728 // Sort by benefit. The most beneficial functions should be outlined first.
729 llvm::stable_sort(FunctionList, [](const OutlinedFunction &LHS,
730 const OutlinedFunction &RHS) {
731 return LHS.getBenefit() > RHS.getBenefit();
734 // Walk over each function, outlining them as we go along. Functions are
735 // outlined greedily, based off the sort above.
736 for (OutlinedFunction &OF : FunctionList) {
737 // If we outlined something that overlapped with a candidate in a previous
738 // step, then we can't outline from it.
739 erase_if(OF.Candidates, [&Mapper](Candidate &C) {
740 return std::any_of(
741 Mapper.UnsignedVec.begin() + C.getStartIdx(),
742 Mapper.UnsignedVec.begin() + C.getEndIdx() + 1,
743 [](unsigned I) { return (I == static_cast<unsigned>(-1)); });
746 // If we made it unbeneficial to outline this function, skip it.
747 if (OF.getBenefit() < 1)
748 continue;
750 // It's beneficial. Create the function and outline its sequence's
751 // occurrences.
752 OF.MF = createOutlinedFunction(M, OF, Mapper, OutlinedFunctionNum);
753 emitOutlinedFunctionRemark(OF);
754 FunctionsCreated++;
755 OutlinedFunctionNum++; // Created a function, move to the next name.
756 MachineFunction *MF = OF.MF;
757 const TargetSubtargetInfo &STI = MF->getSubtarget();
758 const TargetInstrInfo &TII = *STI.getInstrInfo();
760 // Replace occurrences of the sequence with calls to the new function.
761 for (Candidate &C : OF.Candidates) {
762 MachineBasicBlock &MBB = *C.getMBB();
763 MachineBasicBlock::iterator StartIt = C.front();
764 MachineBasicBlock::iterator EndIt = C.back();
766 // Insert the call.
767 auto CallInst = TII.insertOutlinedCall(M, MBB, StartIt, *MF, C);
769 // If the caller tracks liveness, then we need to make sure that
770 // anything we outline doesn't break liveness assumptions. The outlined
771 // functions themselves currently don't track liveness, but we should
772 // make sure that the ranges we yank things out of aren't wrong.
773 if (MBB.getParent()->getProperties().hasProperty(
774 MachineFunctionProperties::Property::TracksLiveness)) {
775 // The following code is to add implicit def operands to the call
776 // instruction. It also updates call site information for moved
777 // code.
778 SmallSet<Register, 2> UseRegs, DefRegs;
779 // Copy over the defs in the outlined range.
780 // First inst in outlined range <-- Anything that's defined in this
781 // ... .. range has to be added as an
782 // implicit Last inst in outlined range <-- def to the call
783 // instruction. Also remove call site information for outlined block
784 // of code. The exposed uses need to be copied in the outlined range.
785 for (MachineBasicBlock::reverse_iterator
786 Iter = EndIt.getReverse(),
787 Last = std::next(CallInst.getReverse());
788 Iter != Last; Iter++) {
789 MachineInstr *MI = &*Iter;
790 SmallSet<Register, 2> InstrUseRegs;
791 for (MachineOperand &MOP : MI->operands()) {
792 // Skip over anything that isn't a register.
793 if (!MOP.isReg())
794 continue;
796 if (MOP.isDef()) {
797 // Introduce DefRegs set to skip the redundant register.
798 DefRegs.insert(MOP.getReg());
799 if (UseRegs.count(MOP.getReg()) &&
800 !InstrUseRegs.count(MOP.getReg()))
801 // Since the regiester is modeled as defined,
802 // it is not necessary to be put in use register set.
803 UseRegs.erase(MOP.getReg());
804 } else if (!MOP.isUndef()) {
805 // Any register which is not undefined should
806 // be put in the use register set.
807 UseRegs.insert(MOP.getReg());
808 InstrUseRegs.insert(MOP.getReg());
811 if (MI->isCandidateForCallSiteEntry())
812 MI->getMF()->eraseCallSiteInfo(MI);
815 for (const Register &I : DefRegs)
816 // If it's a def, add it to the call instruction.
817 CallInst->addOperand(
818 MachineOperand::CreateReg(I, true, /* isDef = true */
819 true /* isImp = true */));
821 for (const Register &I : UseRegs)
822 // If it's a exposed use, add it to the call instruction.
823 CallInst->addOperand(
824 MachineOperand::CreateReg(I, false, /* isDef = false */
825 true /* isImp = true */));
828 // Erase from the point after where the call was inserted up to, and
829 // including, the final instruction in the sequence.
830 // Erase needs one past the end, so we need std::next there too.
831 MBB.erase(std::next(StartIt), std::next(EndIt));
833 // Keep track of what we removed by marking them all as -1.
834 std::for_each(Mapper.UnsignedVec.begin() + C.getStartIdx(),
835 Mapper.UnsignedVec.begin() + C.getEndIdx() + 1,
836 [](unsigned &I) { I = static_cast<unsigned>(-1); });
837 OutlinedSomething = true;
839 // Statistics.
840 NumOutlined++;
844 LLVM_DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n";);
845 return OutlinedSomething;
848 void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
849 MachineModuleInfo &MMI) {
850 // Build instruction mappings for each function in the module. Start by
851 // iterating over each Function in M.
852 for (Function &F : M) {
854 // If there's nothing in F, then there's no reason to try and outline from
855 // it.
856 if (F.empty())
857 continue;
859 // There's something in F. Check if it has a MachineFunction associated with
860 // it.
861 MachineFunction *MF = MMI.getMachineFunction(F);
863 // If it doesn't, then there's nothing to outline from. Move to the next
864 // Function.
865 if (!MF)
866 continue;
868 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
870 if (!RunOnAllFunctions && !TII->shouldOutlineFromFunctionByDefault(*MF))
871 continue;
873 // We have a MachineFunction. Ask the target if it's suitable for outlining.
874 // If it isn't, then move on to the next Function in the module.
875 if (!TII->isFunctionSafeToOutlineFrom(*MF, OutlineFromLinkOnceODRs))
876 continue;
878 // We have a function suitable for outlining. Iterate over every
879 // MachineBasicBlock in MF and try to map its instructions to a list of
880 // unsigned integers.
881 for (MachineBasicBlock &MBB : *MF) {
882 // If there isn't anything in MBB, then there's no point in outlining from
883 // it.
884 // If there are fewer than 2 instructions in the MBB, then it can't ever
885 // contain something worth outlining.
886 // FIXME: This should be based off of the maximum size in B of an outlined
887 // call versus the size in B of the MBB.
888 if (MBB.empty() || MBB.size() < 2)
889 continue;
891 // Check if MBB could be the target of an indirect branch. If it is, then
892 // we don't want to outline from it.
893 if (MBB.hasAddressTaken())
894 continue;
896 // MBB is suitable for outlining. Map it to a list of unsigneds.
897 Mapper.convertToUnsignedVec(MBB, *TII);
902 void MachineOutliner::initSizeRemarkInfo(
903 const Module &M, const MachineModuleInfo &MMI,
904 StringMap<unsigned> &FunctionToInstrCount) {
905 // Collect instruction counts for every function. We'll use this to emit
906 // per-function size remarks later.
907 for (const Function &F : M) {
908 MachineFunction *MF = MMI.getMachineFunction(F);
910 // We only care about MI counts here. If there's no MachineFunction at this
911 // point, then there won't be after the outliner runs, so let's move on.
912 if (!MF)
913 continue;
914 FunctionToInstrCount[F.getName().str()] = MF->getInstructionCount();
918 void MachineOutliner::emitInstrCountChangedRemark(
919 const Module &M, const MachineModuleInfo &MMI,
920 const StringMap<unsigned> &FunctionToInstrCount) {
921 // Iterate over each function in the module and emit remarks.
922 // Note that we won't miss anything by doing this, because the outliner never
923 // deletes functions.
924 for (const Function &F : M) {
925 MachineFunction *MF = MMI.getMachineFunction(F);
927 // The outliner never deletes functions. If we don't have a MF here, then we
928 // didn't have one prior to outlining either.
929 if (!MF)
930 continue;
932 std::string Fname = std::string(F.getName());
933 unsigned FnCountAfter = MF->getInstructionCount();
934 unsigned FnCountBefore = 0;
936 // Check if the function was recorded before.
937 auto It = FunctionToInstrCount.find(Fname);
939 // Did we have a previously-recorded size? If yes, then set FnCountBefore
940 // to that.
941 if (It != FunctionToInstrCount.end())
942 FnCountBefore = It->second;
944 // Compute the delta and emit a remark if there was a change.
945 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
946 static_cast<int64_t>(FnCountBefore);
947 if (FnDelta == 0)
948 continue;
950 MachineOptimizationRemarkEmitter MORE(*MF, nullptr);
951 MORE.emit([&]() {
952 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
953 DiagnosticLocation(), &MF->front());
954 R << DiagnosticInfoOptimizationBase::Argument("Pass", "Machine Outliner")
955 << ": Function: "
956 << DiagnosticInfoOptimizationBase::Argument("Function", F.getName())
957 << ": MI instruction count changed from "
958 << DiagnosticInfoOptimizationBase::Argument("MIInstrsBefore",
959 FnCountBefore)
960 << " to "
961 << DiagnosticInfoOptimizationBase::Argument("MIInstrsAfter",
962 FnCountAfter)
963 << "; Delta: "
964 << DiagnosticInfoOptimizationBase::Argument("Delta", FnDelta);
965 return R;
970 bool MachineOutliner::runOnModule(Module &M) {
971 // Check if there's anything in the module. If it's empty, then there's
972 // nothing to outline.
973 if (M.empty())
974 return false;
976 // Number to append to the current outlined function.
977 unsigned OutlinedFunctionNum = 0;
979 OutlineRepeatedNum = 0;
980 if (!doOutline(M, OutlinedFunctionNum))
981 return false;
983 for (unsigned I = 0; I < OutlinerReruns; ++I) {
984 OutlinedFunctionNum = 0;
985 OutlineRepeatedNum++;
986 if (!doOutline(M, OutlinedFunctionNum)) {
987 LLVM_DEBUG({
988 dbgs() << "Did not outline on iteration " << I + 2 << " out of "
989 << OutlinerReruns + 1 << "\n";
991 break;
995 return true;
998 bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
999 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
1001 // If the user passed -enable-machine-outliner=always or
1002 // -enable-machine-outliner, the pass will run on all functions in the module.
1003 // Otherwise, if the target supports default outlining, it will run on all
1004 // functions deemed by the target to be worth outlining from by default. Tell
1005 // the user how the outliner is running.
1006 LLVM_DEBUG({
1007 dbgs() << "Machine Outliner: Running on ";
1008 if (RunOnAllFunctions)
1009 dbgs() << "all functions";
1010 else
1011 dbgs() << "target-default functions";
1012 dbgs() << "\n";
1015 // If the user specifies that they want to outline from linkonceodrs, set
1016 // it here.
1017 OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining;
1018 InstructionMapper Mapper;
1020 // Prepare instruction mappings for the suffix tree.
1021 populateMapper(Mapper, M, MMI);
1022 std::vector<OutlinedFunction> FunctionList;
1024 // Find all of the outlining candidates.
1025 findCandidates(Mapper, FunctionList);
1027 // If we've requested size remarks, then collect the MI counts of every
1028 // function before outlining, and the MI counts after outlining.
1029 // FIXME: This shouldn't be in the outliner at all; it should ultimately be
1030 // the pass manager's responsibility.
1031 // This could pretty easily be placed in outline instead, but because we
1032 // really ultimately *don't* want this here, it's done like this for now
1033 // instead.
1035 // Check if we want size remarks.
1036 bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark();
1037 StringMap<unsigned> FunctionToInstrCount;
1038 if (ShouldEmitSizeRemarks)
1039 initSizeRemarkInfo(M, MMI, FunctionToInstrCount);
1041 // Outline each of the candidates and return true if something was outlined.
1042 bool OutlinedSomething =
1043 outline(M, FunctionList, Mapper, OutlinedFunctionNum);
1045 // If we outlined something, we definitely changed the MI count of the
1046 // module. If we've asked for size remarks, then output them.
1047 // FIXME: This should be in the pass manager.
1048 if (ShouldEmitSizeRemarks && OutlinedSomething)
1049 emitInstrCountChangedRemark(M, MMI, FunctionToInstrCount);
1051 LLVM_DEBUG({
1052 if (!OutlinedSomething)
1053 dbgs() << "Stopped outlining at iteration " << OutlineRepeatedNum
1054 << " because no changes were found.\n";
1057 return OutlinedSomething;