10b57cec5SDimitry Andric //===---- MachineOutliner.cpp - Outline instructions -----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// Replaces repeated sequences of instructions with function calls.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// This works by placing every instruction from every basic block in a
130b57cec5SDimitry Andric /// suffix tree, and repeatedly querying that tree for repeated sequences of
140b57cec5SDimitry Andric /// instructions. If a sequence of instructions appears often, then it ought
150b57cec5SDimitry Andric /// to be beneficial to pull out into a function.
160b57cec5SDimitry Andric ///
170b57cec5SDimitry Andric /// The MachineOutliner communicates with a given target using hooks defined in
180b57cec5SDimitry Andric /// TargetInstrInfo.h. The target supplies the outliner with information on how
190b57cec5SDimitry Andric /// a specific sequence of instructions should be outlined. This information
200b57cec5SDimitry Andric /// is used to deduce the number of instructions necessary to
210b57cec5SDimitry Andric ///
220b57cec5SDimitry Andric /// * Create an outlined function
230b57cec5SDimitry Andric /// * Call that outlined function
240b57cec5SDimitry Andric ///
250b57cec5SDimitry Andric /// Targets must implement
260b57cec5SDimitry Andric /// * getOutliningCandidateInfo
270b57cec5SDimitry Andric /// * buildOutlinedFrame
280b57cec5SDimitry Andric /// * insertOutlinedCall
290b57cec5SDimitry Andric /// * isFunctionSafeToOutlineFrom
300b57cec5SDimitry Andric ///
310b57cec5SDimitry Andric /// in order to make use of the MachineOutliner.
320b57cec5SDimitry Andric ///
330b57cec5SDimitry Andric /// This was originally presented at the 2016 LLVM Developers' Meeting in the
340b57cec5SDimitry Andric /// talk "Reducing Code Size Using Outlining". For a high-level overview of
350b57cec5SDimitry Andric /// how this pass works, the talk is available on YouTube at
360b57cec5SDimitry Andric ///
370b57cec5SDimitry Andric /// https://www.youtube.com/watch?v=yorld-WSOeU
380b57cec5SDimitry Andric ///
390b57cec5SDimitry Andric /// The slides for the talk are available at
400b57cec5SDimitry Andric ///
410b57cec5SDimitry Andric /// http://www.llvm.org/devmtg/2016-11/Slides/Paquette-Outliner.pdf
420b57cec5SDimitry Andric ///
430b57cec5SDimitry Andric /// The talk provides an overview of how the outliner finds candidates and
440b57cec5SDimitry Andric /// ultimately outlines them. It describes how the main data structure for this
450b57cec5SDimitry Andric /// pass, the suffix tree, is queried and purged for candidates. It also gives
460b57cec5SDimitry Andric /// a simplified suffix tree construction algorithm for suffix trees based off
470b57cec5SDimitry Andric /// of the algorithm actually used here, Ukkonen's algorithm.
480b57cec5SDimitry Andric ///
490b57cec5SDimitry Andric /// For the original RFC for this pass, please see
500b57cec5SDimitry Andric ///
510b57cec5SDimitry Andric /// http://lists.llvm.org/pipermail/llvm-dev/2016-August/104170.html
520b57cec5SDimitry Andric ///
530b57cec5SDimitry Andric /// For more information on the suffix tree data structure, please see
540b57cec5SDimitry Andric /// https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf
550b57cec5SDimitry Andric ///
560b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
570b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOutliner.h"
580b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
595ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h"
600b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
610b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
6281ad6265SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
6381ad6265SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
640b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
650b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
660b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
670b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
680b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
690b57cec5SDimitry Andric #include "llvm/IR/DIBuilder.h"
700b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
710b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
72480093f4SDimitry Andric #include "llvm/InitializePasses.h"
730b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
740b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
755ffd83dbSDimitry Andric #include "llvm/Support/SuffixTree.h"
760b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
770b57cec5SDimitry Andric #include <functional>
780b57cec5SDimitry Andric #include <tuple>
790b57cec5SDimitry Andric #include <vector>
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric #define DEBUG_TYPE "machine-outliner"
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric using namespace llvm;
840b57cec5SDimitry Andric using namespace ore;
850b57cec5SDimitry Andric using namespace outliner;
860b57cec5SDimitry Andric
8781ad6265SDimitry Andric // Statistics for outlined functions.
880b57cec5SDimitry Andric STATISTIC(NumOutlined, "Number of candidates outlined");
890b57cec5SDimitry Andric STATISTIC(FunctionsCreated, "Number of functions created");
900b57cec5SDimitry Andric
9181ad6265SDimitry Andric // Statistics for instruction mapping.
92fe013be4SDimitry Andric STATISTIC(NumLegalInUnsignedVec, "Outlinable instructions mapped");
9381ad6265SDimitry Andric STATISTIC(NumIllegalInUnsignedVec,
94fe013be4SDimitry Andric "Unoutlinable instructions mapped + number of sentinel values");
95fe013be4SDimitry Andric STATISTIC(NumSentinels, "Sentinel values inserted during mapping");
96fe013be4SDimitry Andric STATISTIC(NumInvisible,
97fe013be4SDimitry Andric "Invisible instructions skipped during mapping");
98fe013be4SDimitry Andric STATISTIC(UnsignedVecSize,
99fe013be4SDimitry Andric "Total number of instructions mapped and saved to mapping vector");
10081ad6265SDimitry Andric
1010b57cec5SDimitry Andric // Set to true if the user wants the outliner to run on linkonceodr linkage
1020b57cec5SDimitry Andric // functions. This is false by default because the linker can dedupe linkonceodr
1030b57cec5SDimitry Andric // functions. Since the outliner is confined to a single module (modulo LTO),
1040b57cec5SDimitry Andric // this is off by default. It should, however, be the default behaviour in
1050b57cec5SDimitry Andric // LTO.
1060b57cec5SDimitry Andric static cl::opt<bool> EnableLinkOnceODROutlining(
107480093f4SDimitry Andric "enable-linkonceodr-outlining", cl::Hidden,
1080b57cec5SDimitry Andric cl::desc("Enable the machine outliner on linkonceodr functions"),
1090b57cec5SDimitry Andric cl::init(false));
1100b57cec5SDimitry Andric
1115ffd83dbSDimitry Andric /// Number of times to re-run the outliner. This is not the total number of runs
1125ffd83dbSDimitry Andric /// as the outliner will run at least one time. The default value is set to 0,
1135ffd83dbSDimitry Andric /// meaning the outliner will run one time and rerun zero times after that.
1145ffd83dbSDimitry Andric static cl::opt<unsigned> OutlinerReruns(
1155ffd83dbSDimitry Andric "machine-outliner-reruns", cl::init(0), cl::Hidden,
1165ffd83dbSDimitry Andric cl::desc(
1175ffd83dbSDimitry Andric "Number of times to rerun the outliner after the initial outline"));
1185ffd83dbSDimitry Andric
119fe013be4SDimitry Andric static cl::opt<unsigned> OutlinerBenefitThreshold(
120fe013be4SDimitry Andric "outliner-benefit-threshold", cl::init(1), cl::Hidden,
121fe013be4SDimitry Andric cl::desc(
122fe013be4SDimitry Andric "The minimum size in bytes before an outlining candidate is accepted"));
123fe013be4SDimitry Andric
1240b57cec5SDimitry Andric namespace {
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric /// Maps \p MachineInstrs to unsigned integers and stores the mappings.
1270b57cec5SDimitry Andric struct InstructionMapper {
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric /// The next available integer to assign to a \p MachineInstr that
1300b57cec5SDimitry Andric /// cannot be outlined.
1310b57cec5SDimitry Andric ///
1320b57cec5SDimitry Andric /// Set to -3 for compatability with \p DenseMapInfo<unsigned>.
1330b57cec5SDimitry Andric unsigned IllegalInstrNumber = -3;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric /// The next available integer to assign to a \p MachineInstr that can
1360b57cec5SDimitry Andric /// be outlined.
1370b57cec5SDimitry Andric unsigned LegalInstrNumber = 0;
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric /// Correspondence from \p MachineInstrs to unsigned integers.
1400b57cec5SDimitry Andric DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>
1410b57cec5SDimitry Andric InstructionIntegerMap;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric /// Correspondence between \p MachineBasicBlocks and target-defined flags.
1440b57cec5SDimitry Andric DenseMap<MachineBasicBlock *, unsigned> MBBFlagsMap;
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric /// The vector of unsigned integers that the module is mapped to.
147fe013be4SDimitry Andric SmallVector<unsigned> UnsignedVec;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric /// Stores the location of the instruction associated with the integer
1500b57cec5SDimitry Andric /// at index i in \p UnsignedVec for each index i.
151fe013be4SDimitry Andric SmallVector<MachineBasicBlock::iterator> InstrList;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric // Set if we added an illegal number in the previous step.
1540b57cec5SDimitry Andric // Since each illegal number is unique, we only need one of them between
1550b57cec5SDimitry Andric // each range of legal numbers. This lets us make sure we don't add more
1560b57cec5SDimitry Andric // than one illegal number per range.
1570b57cec5SDimitry Andric bool AddedIllegalLastTime = false;
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric /// Maps \p *It to a legal integer.
1600b57cec5SDimitry Andric ///
1610b57cec5SDimitry Andric /// Updates \p CanOutlineWithPrevInstr, \p HaveLegalRange, \p InstrListForMBB,
1620b57cec5SDimitry Andric /// \p UnsignedVecForMBB, \p InstructionIntegerMap, and \p LegalInstrNumber.
1630b57cec5SDimitry Andric ///
1640b57cec5SDimitry Andric /// \returns The integer that \p *It was mapped to.
mapToLegalUnsigned__anona9cd00110111::InstructionMapper1650b57cec5SDimitry Andric unsigned mapToLegalUnsigned(
1660b57cec5SDimitry Andric MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
1670b57cec5SDimitry Andric bool &HaveLegalRange, unsigned &NumLegalInBlock,
168fe013be4SDimitry Andric SmallVector<unsigned> &UnsignedVecForMBB,
169fe013be4SDimitry Andric SmallVector<MachineBasicBlock::iterator> &InstrListForMBB) {
1700b57cec5SDimitry Andric // We added something legal, so we should unset the AddedLegalLastTime
1710b57cec5SDimitry Andric // flag.
1720b57cec5SDimitry Andric AddedIllegalLastTime = false;
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric // If we have at least two adjacent legal instructions (which may have
1750b57cec5SDimitry Andric // invisible instructions in between), remember that.
1760b57cec5SDimitry Andric if (CanOutlineWithPrevInstr)
1770b57cec5SDimitry Andric HaveLegalRange = true;
1780b57cec5SDimitry Andric CanOutlineWithPrevInstr = true;
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric // Keep track of the number of legal instructions we insert.
1810b57cec5SDimitry Andric NumLegalInBlock++;
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric // Get the integer for this instruction or give it the current
1840b57cec5SDimitry Andric // LegalInstrNumber.
1850b57cec5SDimitry Andric InstrListForMBB.push_back(It);
1860b57cec5SDimitry Andric MachineInstr &MI = *It;
1870b57cec5SDimitry Andric bool WasInserted;
1880b57cec5SDimitry Andric DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>::iterator
1890b57cec5SDimitry Andric ResultIt;
1900b57cec5SDimitry Andric std::tie(ResultIt, WasInserted) =
1910b57cec5SDimitry Andric InstructionIntegerMap.insert(std::make_pair(&MI, LegalInstrNumber));
1920b57cec5SDimitry Andric unsigned MINumber = ResultIt->second;
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric // There was an insertion.
1950b57cec5SDimitry Andric if (WasInserted)
1960b57cec5SDimitry Andric LegalInstrNumber++;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric UnsignedVecForMBB.push_back(MINumber);
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric // Make sure we don't overflow or use any integers reserved by the DenseMap.
2010b57cec5SDimitry Andric if (LegalInstrNumber >= IllegalInstrNumber)
2020b57cec5SDimitry Andric report_fatal_error("Instruction mapping overflow!");
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric assert(LegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
2050b57cec5SDimitry Andric "Tried to assign DenseMap tombstone or empty key to instruction.");
2060b57cec5SDimitry Andric assert(LegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
2070b57cec5SDimitry Andric "Tried to assign DenseMap tombstone or empty key to instruction.");
2080b57cec5SDimitry Andric
20981ad6265SDimitry Andric // Statistics.
21081ad6265SDimitry Andric ++NumLegalInUnsignedVec;
2110b57cec5SDimitry Andric return MINumber;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric /// Maps \p *It to an illegal integer.
2150b57cec5SDimitry Andric ///
2160b57cec5SDimitry Andric /// Updates \p InstrListForMBB, \p UnsignedVecForMBB, and \p
2170b57cec5SDimitry Andric /// IllegalInstrNumber.
2180b57cec5SDimitry Andric ///
2190b57cec5SDimitry Andric /// \returns The integer that \p *It was mapped to.
mapToIllegalUnsigned__anona9cd00110111::InstructionMapper220480093f4SDimitry Andric unsigned mapToIllegalUnsigned(
221480093f4SDimitry Andric MachineBasicBlock::iterator &It, bool &CanOutlineWithPrevInstr,
222fe013be4SDimitry Andric SmallVector<unsigned> &UnsignedVecForMBB,
223fe013be4SDimitry Andric SmallVector<MachineBasicBlock::iterator> &InstrListForMBB) {
2240b57cec5SDimitry Andric // Can't outline an illegal instruction. Set the flag.
2250b57cec5SDimitry Andric CanOutlineWithPrevInstr = false;
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric // Only add one illegal number per range of legal numbers.
2280b57cec5SDimitry Andric if (AddedIllegalLastTime)
2290b57cec5SDimitry Andric return IllegalInstrNumber;
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric // Remember that we added an illegal number last time.
2320b57cec5SDimitry Andric AddedIllegalLastTime = true;
2330b57cec5SDimitry Andric unsigned MINumber = IllegalInstrNumber;
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric InstrListForMBB.push_back(It);
2360b57cec5SDimitry Andric UnsignedVecForMBB.push_back(IllegalInstrNumber);
2370b57cec5SDimitry Andric IllegalInstrNumber--;
23881ad6265SDimitry Andric // Statistics.
23981ad6265SDimitry Andric ++NumIllegalInUnsignedVec;
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric assert(LegalInstrNumber < IllegalInstrNumber &&
2420b57cec5SDimitry Andric "Instruction mapping overflow!");
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andric assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getEmptyKey() &&
2450b57cec5SDimitry Andric "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric assert(IllegalInstrNumber != DenseMapInfo<unsigned>::getTombstoneKey() &&
2480b57cec5SDimitry Andric "IllegalInstrNumber cannot be DenseMap tombstone or empty key!");
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andric return MINumber;
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric /// Transforms a \p MachineBasicBlock into a \p vector of \p unsigneds
2540b57cec5SDimitry Andric /// and appends it to \p UnsignedVec and \p InstrList.
2550b57cec5SDimitry Andric ///
2560b57cec5SDimitry Andric /// Two instructions are assigned the same integer if they are identical.
2570b57cec5SDimitry Andric /// If an instruction is deemed unsafe to outline, then it will be assigned an
2580b57cec5SDimitry Andric /// unique integer. The resulting mapping is placed into a suffix tree and
2590b57cec5SDimitry Andric /// queried for candidates.
2600b57cec5SDimitry Andric ///
2610b57cec5SDimitry Andric /// \param MBB The \p MachineBasicBlock to be translated into integers.
2620b57cec5SDimitry Andric /// \param TII \p TargetInstrInfo for the function.
convertToUnsignedVec__anona9cd00110111::InstructionMapper2630b57cec5SDimitry Andric void convertToUnsignedVec(MachineBasicBlock &MBB,
2640b57cec5SDimitry Andric const TargetInstrInfo &TII) {
265fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "*** Converting MBB '" << MBB.getName()
266fe013be4SDimitry Andric << "' to unsigned vector ***\n");
2670b57cec5SDimitry Andric unsigned Flags = 0;
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric // Don't even map in this case.
2700b57cec5SDimitry Andric if (!TII.isMBBSafeToOutlineFrom(MBB, Flags))
2710b57cec5SDimitry Andric return;
2720b57cec5SDimitry Andric
273fe013be4SDimitry Andric auto OutlinableRanges = TII.getOutlinableRanges(MBB, Flags);
274fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << MBB.getName() << ": " << OutlinableRanges.size()
275fe013be4SDimitry Andric << " outlinable range(s)\n");
276fe013be4SDimitry Andric if (OutlinableRanges.empty())
277fe013be4SDimitry Andric return;
278fe013be4SDimitry Andric
2790b57cec5SDimitry Andric // Store info for the MBB for later outlining.
2800b57cec5SDimitry Andric MBBFlagsMap[&MBB] = Flags;
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andric MachineBasicBlock::iterator It = MBB.begin();
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // The number of instructions in this block that will be considered for
2850b57cec5SDimitry Andric // outlining.
2860b57cec5SDimitry Andric unsigned NumLegalInBlock = 0;
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric // True if we have at least two legal instructions which aren't separated
2890b57cec5SDimitry Andric // by an illegal instruction.
2900b57cec5SDimitry Andric bool HaveLegalRange = false;
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andric // True if we can perform outlining given the last mapped (non-invisible)
2930b57cec5SDimitry Andric // instruction. This lets us know if we have a legal range.
2940b57cec5SDimitry Andric bool CanOutlineWithPrevInstr = false;
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric // FIXME: Should this all just be handled in the target, rather than using
2970b57cec5SDimitry Andric // repeated calls to getOutliningType?
298fe013be4SDimitry Andric SmallVector<unsigned> UnsignedVecForMBB;
299fe013be4SDimitry Andric SmallVector<MachineBasicBlock::iterator> InstrListForMBB;
3000b57cec5SDimitry Andric
301fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "*** Mapping outlinable ranges ***\n");
302fe013be4SDimitry Andric for (auto &OutlinableRange : OutlinableRanges) {
303fe013be4SDimitry Andric auto OutlinableRangeBegin = OutlinableRange.first;
304fe013be4SDimitry Andric auto OutlinableRangeEnd = OutlinableRange.second;
305fe013be4SDimitry Andric #ifndef NDEBUG
306fe013be4SDimitry Andric LLVM_DEBUG(
307fe013be4SDimitry Andric dbgs() << "Mapping "
308fe013be4SDimitry Andric << std::distance(OutlinableRangeBegin, OutlinableRangeEnd)
309fe013be4SDimitry Andric << " instruction range\n");
310fe013be4SDimitry Andric // Everything outside of an outlinable range is illegal.
311fe013be4SDimitry Andric unsigned NumSkippedInRange = 0;
312fe013be4SDimitry Andric #endif
313fe013be4SDimitry Andric for (; It != OutlinableRangeBegin; ++It) {
314fe013be4SDimitry Andric #ifndef NDEBUG
315fe013be4SDimitry Andric ++NumSkippedInRange;
316fe013be4SDimitry Andric #endif
317fe013be4SDimitry Andric mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
318fe013be4SDimitry Andric InstrListForMBB);
319fe013be4SDimitry Andric }
320fe013be4SDimitry Andric #ifndef NDEBUG
321fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "Skipped " << NumSkippedInRange
322fe013be4SDimitry Andric << " instructions outside outlinable range\n");
323fe013be4SDimitry Andric #endif
324fe013be4SDimitry Andric assert(It != MBB.end() && "Should still have instructions?");
325fe013be4SDimitry Andric // `It` is now positioned at the beginning of a range of instructions
326fe013be4SDimitry Andric // which may be outlinable. Check if each instruction is known to be safe.
327fe013be4SDimitry Andric for (; It != OutlinableRangeEnd; ++It) {
3280b57cec5SDimitry Andric // Keep track of where this instruction is in the module.
3290b57cec5SDimitry Andric switch (TII.getOutliningType(It, Flags)) {
3300b57cec5SDimitry Andric case InstrType::Illegal:
331480093f4SDimitry Andric mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
332480093f4SDimitry Andric InstrListForMBB);
3330b57cec5SDimitry Andric break;
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric case InstrType::Legal:
3360b57cec5SDimitry Andric mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
337fe013be4SDimitry Andric NumLegalInBlock, UnsignedVecForMBB,
338fe013be4SDimitry Andric InstrListForMBB);
3390b57cec5SDimitry Andric break;
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric case InstrType::LegalTerminator:
3420b57cec5SDimitry Andric mapToLegalUnsigned(It, CanOutlineWithPrevInstr, HaveLegalRange,
343fe013be4SDimitry Andric NumLegalInBlock, UnsignedVecForMBB,
344fe013be4SDimitry Andric InstrListForMBB);
345fe013be4SDimitry Andric // The instruction also acts as a terminator, so we have to record
346fe013be4SDimitry Andric // that in the string.
3470b57cec5SDimitry Andric mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
3480b57cec5SDimitry Andric InstrListForMBB);
3490b57cec5SDimitry Andric break;
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric case InstrType::Invisible:
3520b57cec5SDimitry Andric // Normally this is set by mapTo(Blah)Unsigned, but we just want to
3530b57cec5SDimitry Andric // skip this instruction. So, unset the flag here.
35481ad6265SDimitry Andric ++NumInvisible;
3550b57cec5SDimitry Andric AddedIllegalLastTime = false;
3560b57cec5SDimitry Andric break;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric }
359fe013be4SDimitry Andric }
360fe013be4SDimitry Andric
361fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "HaveLegalRange = " << HaveLegalRange << "\n");
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric // Are there enough legal instructions in the block for outlining to be
3640b57cec5SDimitry Andric // possible?
3650b57cec5SDimitry Andric if (HaveLegalRange) {
3660b57cec5SDimitry Andric // After we're done every insertion, uniquely terminate this part of the
3670b57cec5SDimitry Andric // "string". This makes sure we won't match across basic block or function
3680b57cec5SDimitry Andric // boundaries since the "end" is encoded uniquely and thus appears in no
3690b57cec5SDimitry Andric // repeated substring.
3700b57cec5SDimitry Andric mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
3710b57cec5SDimitry Andric InstrListForMBB);
372fe013be4SDimitry Andric ++NumSentinels;
373fe013be4SDimitry Andric append_range(InstrList, InstrListForMBB);
374fe013be4SDimitry Andric append_range(UnsignedVec, UnsignedVecForMBB);
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric
InstructionMapper__anona9cd00110111::InstructionMapper3780b57cec5SDimitry Andric InstructionMapper() {
3790b57cec5SDimitry Andric // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
3800b57cec5SDimitry Andric // changed.
3810b57cec5SDimitry Andric assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
3820b57cec5SDimitry Andric "DenseMapInfo<unsigned>'s empty key isn't -1!");
3830b57cec5SDimitry Andric assert(DenseMapInfo<unsigned>::getTombstoneKey() == (unsigned)-2 &&
3840b57cec5SDimitry Andric "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric };
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric /// An interprocedural pass which finds repeated sequences of
3890b57cec5SDimitry Andric /// instructions and replaces them with calls to functions.
3900b57cec5SDimitry Andric ///
3910b57cec5SDimitry Andric /// Each instruction is mapped to an unsigned integer and placed in a string.
3920b57cec5SDimitry Andric /// The resulting mapping is then placed in a \p SuffixTree. The \p SuffixTree
3930b57cec5SDimitry Andric /// is then repeatedly queried for repeated sequences of instructions. Each
3940b57cec5SDimitry Andric /// non-overlapping repeated sequence is then placed in its own
3950b57cec5SDimitry Andric /// \p MachineFunction and each instance is then replaced with a call to that
3960b57cec5SDimitry Andric /// function.
3970b57cec5SDimitry Andric struct MachineOutliner : public ModulePass {
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andric static char ID;
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric /// Set to true if the outliner should consider functions with
4020b57cec5SDimitry Andric /// linkonceodr linkage.
4030b57cec5SDimitry Andric bool OutlineFromLinkOnceODRs = false;
4040b57cec5SDimitry Andric
4055ffd83dbSDimitry Andric /// The current repeat number of machine outlining.
4065ffd83dbSDimitry Andric unsigned OutlineRepeatedNum = 0;
4075ffd83dbSDimitry Andric
4080b57cec5SDimitry Andric /// Set to true if the outliner should run on all functions in the module
4090b57cec5SDimitry Andric /// considered safe for outlining.
4100b57cec5SDimitry Andric /// Set to true by default for compatibility with llc's -run-pass option.
4110b57cec5SDimitry Andric /// Set when the pass is constructed in TargetPassConfig.
4120b57cec5SDimitry Andric bool RunOnAllFunctions = true;
4130b57cec5SDimitry Andric
getPassName__anona9cd00110111::MachineOutliner4140b57cec5SDimitry Andric StringRef getPassName() const override { return "Machine Outliner"; }
4150b57cec5SDimitry Andric
getAnalysisUsage__anona9cd00110111::MachineOutliner4160b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
4178bcb0991SDimitry Andric AU.addRequired<MachineModuleInfoWrapperPass>();
4188bcb0991SDimitry Andric AU.addPreserved<MachineModuleInfoWrapperPass>();
4190b57cec5SDimitry Andric AU.setPreservesAll();
4200b57cec5SDimitry Andric ModulePass::getAnalysisUsage(AU);
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
MachineOutliner__anona9cd00110111::MachineOutliner4230b57cec5SDimitry Andric MachineOutliner() : ModulePass(ID) {
4240b57cec5SDimitry Andric initializeMachineOutlinerPass(*PassRegistry::getPassRegistry());
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric /// Remark output explaining that not outlining a set of candidates would be
4280b57cec5SDimitry Andric /// better than outlining that set.
4290b57cec5SDimitry Andric void emitNotOutliningCheaperRemark(
4300b57cec5SDimitry Andric unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
4310b57cec5SDimitry Andric OutlinedFunction &OF);
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andric /// Remark output explaining that a function was outlined.
4340b57cec5SDimitry Andric void emitOutlinedFunctionRemark(OutlinedFunction &OF);
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andric /// Find all repeated substrings that satisfy the outlining cost model by
4370b57cec5SDimitry Andric /// constructing a suffix tree.
4380b57cec5SDimitry Andric ///
4390b57cec5SDimitry Andric /// If a substring appears at least twice, then it must be represented by
4400b57cec5SDimitry Andric /// an internal node which appears in at least two suffixes. Each suffix
4410b57cec5SDimitry Andric /// is represented by a leaf node. To do this, we visit each internal node
4420b57cec5SDimitry Andric /// in the tree, using the leaf children of each internal node. If an
4430b57cec5SDimitry Andric /// internal node represents a beneficial substring, then we use each of
4440b57cec5SDimitry Andric /// its leaf children to find the locations of its substring.
4450b57cec5SDimitry Andric ///
4460b57cec5SDimitry Andric /// \param Mapper Contains outlining mapping information.
4470b57cec5SDimitry Andric /// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
4480b57cec5SDimitry Andric /// each type of candidate.
4490b57cec5SDimitry Andric void findCandidates(InstructionMapper &Mapper,
4500b57cec5SDimitry Andric std::vector<OutlinedFunction> &FunctionList);
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andric /// Replace the sequences of instructions represented by \p OutlinedFunctions
4530b57cec5SDimitry Andric /// with calls to functions.
4540b57cec5SDimitry Andric ///
4550b57cec5SDimitry Andric /// \param M The module we are outlining from.
4560b57cec5SDimitry Andric /// \param FunctionList A list of functions to be inserted into the module.
4570b57cec5SDimitry Andric /// \param Mapper Contains the instruction mappings for the module.
4580b57cec5SDimitry Andric bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList,
459480093f4SDimitry Andric InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
4600b57cec5SDimitry Andric
4610b57cec5SDimitry Andric /// Creates a function for \p OF and inserts it into the module.
4620b57cec5SDimitry Andric MachineFunction *createOutlinedFunction(Module &M, OutlinedFunction &OF,
4630b57cec5SDimitry Andric InstructionMapper &Mapper,
4640b57cec5SDimitry Andric unsigned Name);
4650b57cec5SDimitry Andric
4665ffd83dbSDimitry Andric /// Calls 'doOutline()' 1 + OutlinerReruns times.
467480093f4SDimitry Andric bool runOnModule(Module &M) override;
468480093f4SDimitry Andric
4690b57cec5SDimitry Andric /// Construct a suffix tree on the instructions in \p M and outline repeated
4700b57cec5SDimitry Andric /// strings from that tree.
471480093f4SDimitry Andric bool doOutline(Module &M, unsigned &OutlinedFunctionNum);
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric /// Return a DISubprogram for OF if one exists, and null otherwise. Helper
4740b57cec5SDimitry Andric /// function for remark emission.
getSubprogramOrNull__anona9cd00110111::MachineOutliner4750b57cec5SDimitry Andric DISubprogram *getSubprogramOrNull(const OutlinedFunction &OF) {
4760b57cec5SDimitry Andric for (const Candidate &C : OF.Candidates)
477480093f4SDimitry Andric if (MachineFunction *MF = C.getMF())
478480093f4SDimitry Andric if (DISubprogram *SP = MF->getFunction().getSubprogram())
4790b57cec5SDimitry Andric return SP;
4800b57cec5SDimitry Andric return nullptr;
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric /// Populate and \p InstructionMapper with instruction-to-integer mappings.
4840b57cec5SDimitry Andric /// These are used to construct a suffix tree.
4850b57cec5SDimitry Andric void populateMapper(InstructionMapper &Mapper, Module &M,
4860b57cec5SDimitry Andric MachineModuleInfo &MMI);
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric /// Initialize information necessary to output a size remark.
4890b57cec5SDimitry Andric /// FIXME: This should be handled by the pass manager, not the outliner.
4900b57cec5SDimitry Andric /// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
4910b57cec5SDimitry Andric /// pass manager.
492480093f4SDimitry Andric void initSizeRemarkInfo(const Module &M, const MachineModuleInfo &MMI,
4930b57cec5SDimitry Andric StringMap<unsigned> &FunctionToInstrCount);
4940b57cec5SDimitry Andric
4950b57cec5SDimitry Andric /// Emit the remark.
4960b57cec5SDimitry Andric // FIXME: This should be handled by the pass manager, not the outliner.
497480093f4SDimitry Andric void
498480093f4SDimitry Andric emitInstrCountChangedRemark(const Module &M, const MachineModuleInfo &MMI,
4990b57cec5SDimitry Andric const StringMap<unsigned> &FunctionToInstrCount);
5000b57cec5SDimitry Andric };
5010b57cec5SDimitry Andric } // Anonymous namespace.
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric char MachineOutliner::ID = 0;
5040b57cec5SDimitry Andric
5050b57cec5SDimitry Andric namespace llvm {
createMachineOutlinerPass(bool RunOnAllFunctions)5060b57cec5SDimitry Andric ModulePass *createMachineOutlinerPass(bool RunOnAllFunctions) {
5070b57cec5SDimitry Andric MachineOutliner *OL = new MachineOutliner();
5080b57cec5SDimitry Andric OL->RunOnAllFunctions = RunOnAllFunctions;
5090b57cec5SDimitry Andric return OL;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric
5120b57cec5SDimitry Andric } // namespace llvm
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric INITIALIZE_PASS(MachineOutliner, DEBUG_TYPE, "Machine Function Outliner", false,
5150b57cec5SDimitry Andric false)
5160b57cec5SDimitry Andric
emitNotOutliningCheaperRemark(unsigned StringLen,std::vector<Candidate> & CandidatesForRepeatedSeq,OutlinedFunction & OF)5170b57cec5SDimitry Andric void MachineOutliner::emitNotOutliningCheaperRemark(
5180b57cec5SDimitry Andric unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
5190b57cec5SDimitry Andric OutlinedFunction &OF) {
5200b57cec5SDimitry Andric // FIXME: Right now, we arbitrarily choose some Candidate from the
5210b57cec5SDimitry Andric // OutlinedFunction. This isn't necessarily fixed, nor does it have to be.
5220b57cec5SDimitry Andric // We should probably sort these by function name or something to make sure
5230b57cec5SDimitry Andric // the remarks are stable.
5240b57cec5SDimitry Andric Candidate &C = CandidatesForRepeatedSeq.front();
5250b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(*(C.getMF()), nullptr);
5260b57cec5SDimitry Andric MORE.emit([&]() {
5270b57cec5SDimitry Andric MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
528*a58f00eaSDimitry Andric C.front().getDebugLoc(), C.getMBB());
5290b57cec5SDimitry Andric R << "Did not outline " << NV("Length", StringLen) << " instructions"
5300b57cec5SDimitry Andric << " from " << NV("NumOccurrences", CandidatesForRepeatedSeq.size())
5310b57cec5SDimitry Andric << " locations."
5320b57cec5SDimitry Andric << " Bytes from outlining all occurrences ("
5330b57cec5SDimitry Andric << NV("OutliningCost", OF.getOutliningCost()) << ")"
5340b57cec5SDimitry Andric << " >= Unoutlined instruction bytes ("
5350b57cec5SDimitry Andric << NV("NotOutliningCost", OF.getNotOutlinedCost()) << ")"
5360b57cec5SDimitry Andric << " (Also found at: ";
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric // Tell the user the other places the candidate was found.
5390b57cec5SDimitry Andric for (unsigned i = 1, e = CandidatesForRepeatedSeq.size(); i < e; i++) {
5400b57cec5SDimitry Andric R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
541*a58f00eaSDimitry Andric CandidatesForRepeatedSeq[i].front().getDebugLoc());
5420b57cec5SDimitry Andric if (i != e - 1)
5430b57cec5SDimitry Andric R << ", ";
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric R << ")";
5470b57cec5SDimitry Andric return R;
5480b57cec5SDimitry Andric });
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric
emitOutlinedFunctionRemark(OutlinedFunction & OF)5510b57cec5SDimitry Andric void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
5520b57cec5SDimitry Andric MachineBasicBlock *MBB = &*OF.MF->begin();
5530b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(*OF.MF, nullptr);
5540b57cec5SDimitry Andric MachineOptimizationRemark R(DEBUG_TYPE, "OutlinedFunction",
5550b57cec5SDimitry Andric MBB->findDebugLoc(MBB->begin()), MBB);
5560b57cec5SDimitry Andric R << "Saved " << NV("OutliningBenefit", OF.getBenefit()) << " bytes by "
5570b57cec5SDimitry Andric << "outlining " << NV("Length", OF.getNumInstrs()) << " instructions "
5580b57cec5SDimitry Andric << "from " << NV("NumOccurrences", OF.getOccurrenceCount())
5590b57cec5SDimitry Andric << " locations. "
5600b57cec5SDimitry Andric << "(Found at: ";
5610b57cec5SDimitry Andric
5620b57cec5SDimitry Andric // Tell the user the other places the candidate was found.
5630b57cec5SDimitry Andric for (size_t i = 0, e = OF.Candidates.size(); i < e; i++) {
5640b57cec5SDimitry Andric
5650b57cec5SDimitry Andric R << NV((Twine("StartLoc") + Twine(i)).str(),
566*a58f00eaSDimitry Andric OF.Candidates[i].front().getDebugLoc());
5670b57cec5SDimitry Andric if (i != e - 1)
5680b57cec5SDimitry Andric R << ", ";
5690b57cec5SDimitry Andric }
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric R << ")";
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric MORE.emit(R);
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric
findCandidates(InstructionMapper & Mapper,std::vector<OutlinedFunction> & FunctionList)576480093f4SDimitry Andric void MachineOutliner::findCandidates(
577480093f4SDimitry Andric InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
5780b57cec5SDimitry Andric FunctionList.clear();
5790b57cec5SDimitry Andric SuffixTree ST(Mapper.UnsignedVec);
5800b57cec5SDimitry Andric
581480093f4SDimitry Andric // First, find all of the repeated substrings in the tree of minimum length
5820b57cec5SDimitry Andric // 2.
5830b57cec5SDimitry Andric std::vector<Candidate> CandidatesForRepeatedSeq;
584fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "*** Discarding overlapping candidates *** \n");
585fe013be4SDimitry Andric LLVM_DEBUG(
586fe013be4SDimitry Andric dbgs() << "Searching for overlaps in all repeated sequences...\n");
587fe6060f1SDimitry Andric for (const SuffixTree::RepeatedSubstring &RS : ST) {
5880b57cec5SDimitry Andric CandidatesForRepeatedSeq.clear();
5890b57cec5SDimitry Andric unsigned StringLen = RS.Length;
590fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " Sequence length: " << StringLen << "\n");
591fe013be4SDimitry Andric // Debug code to keep track of how many candidates we removed.
592fe013be4SDimitry Andric #ifndef NDEBUG
593fe013be4SDimitry Andric unsigned NumDiscarded = 0;
594fe013be4SDimitry Andric unsigned NumKept = 0;
595fe013be4SDimitry Andric #endif
5960b57cec5SDimitry Andric for (const unsigned &StartIdx : RS.StartIndices) {
5970b57cec5SDimitry Andric // Trick: Discard some candidates that would be incompatible with the
5980b57cec5SDimitry Andric // ones we've already found for this sequence. This will save us some
5990b57cec5SDimitry Andric // work in candidate selection.
6000b57cec5SDimitry Andric //
6010b57cec5SDimitry Andric // If two candidates overlap, then we can't outline them both. This
6020b57cec5SDimitry Andric // happens when we have candidates that look like, say
6030b57cec5SDimitry Andric //
6040b57cec5SDimitry Andric // AA (where each "A" is an instruction).
6050b57cec5SDimitry Andric //
6060b57cec5SDimitry Andric // We might have some portion of the module that looks like this:
6070b57cec5SDimitry Andric // AAAAAA (6 A's)
6080b57cec5SDimitry Andric //
6090b57cec5SDimitry Andric // In this case, there are 5 different copies of "AA" in this range, but
6100b57cec5SDimitry Andric // at most 3 can be outlined. If only outlining 3 of these is going to
6110b57cec5SDimitry Andric // be unbeneficial, then we ought to not bother.
6120b57cec5SDimitry Andric //
6130b57cec5SDimitry Andric // Note that two things DON'T overlap when they look like this:
6140b57cec5SDimitry Andric // start1...end1 .... start2...end2
6150b57cec5SDimitry Andric // That is, one must either
6160b57cec5SDimitry Andric // * End before the other starts
6170b57cec5SDimitry Andric // * Start after the other ends
618fe013be4SDimitry Andric unsigned EndIdx = StartIdx + StringLen - 1;
619fe013be4SDimitry Andric auto FirstOverlap = find_if(
620fe013be4SDimitry Andric CandidatesForRepeatedSeq, [StartIdx, EndIdx](const Candidate &C) {
621fe013be4SDimitry Andric return EndIdx >= C.getStartIdx() && StartIdx <= C.getEndIdx();
622fe013be4SDimitry Andric });
623fe013be4SDimitry Andric if (FirstOverlap != CandidatesForRepeatedSeq.end()) {
624fe013be4SDimitry Andric #ifndef NDEBUG
625fe013be4SDimitry Andric ++NumDiscarded;
626fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " .. DISCARD candidate @ [" << StartIdx
627fe013be4SDimitry Andric << ", " << EndIdx << "]; overlaps with candidate @ ["
628fe013be4SDimitry Andric << FirstOverlap->getStartIdx() << ", "
629fe013be4SDimitry Andric << FirstOverlap->getEndIdx() << "]\n");
630fe013be4SDimitry Andric #endif
631fe013be4SDimitry Andric continue;
632fe013be4SDimitry Andric }
6330b57cec5SDimitry Andric // It doesn't overlap with anything, so we can outline it.
6340b57cec5SDimitry Andric // Each sequence is over [StartIt, EndIt].
6350b57cec5SDimitry Andric // Save the candidate and its location.
636fe013be4SDimitry Andric #ifndef NDEBUG
637fe013be4SDimitry Andric ++NumKept;
638fe013be4SDimitry Andric #endif
6390b57cec5SDimitry Andric MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx];
6400b57cec5SDimitry Andric MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx];
6410b57cec5SDimitry Andric MachineBasicBlock *MBB = StartIt->getParent();
642fe013be4SDimitry Andric CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen, StartIt, EndIt,
643fe013be4SDimitry Andric MBB, FunctionList.size(),
6440b57cec5SDimitry Andric Mapper.MBBFlagsMap[MBB]);
6450b57cec5SDimitry Andric }
646fe013be4SDimitry Andric #ifndef NDEBUG
647fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " Candidates discarded: " << NumDiscarded
648fe013be4SDimitry Andric << "\n");
649fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " Candidates kept: " << NumKept << "\n\n");
650fe013be4SDimitry Andric #endif
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric // We've found something we might want to outline.
6530b57cec5SDimitry Andric // Create an OutlinedFunction to store it and check if it'd be beneficial
6540b57cec5SDimitry Andric // to outline.
6550b57cec5SDimitry Andric if (CandidatesForRepeatedSeq.size() < 2)
6560b57cec5SDimitry Andric continue;
6570b57cec5SDimitry Andric
6580b57cec5SDimitry Andric // Arbitrarily choose a TII from the first candidate.
6590b57cec5SDimitry Andric // FIXME: Should getOutliningCandidateInfo move to TargetMachine?
6600b57cec5SDimitry Andric const TargetInstrInfo *TII =
6610b57cec5SDimitry Andric CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo();
6620b57cec5SDimitry Andric
663fe013be4SDimitry Andric std::optional<OutlinedFunction> OF =
6640b57cec5SDimitry Andric TII->getOutliningCandidateInfo(CandidatesForRepeatedSeq);
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andric // If we deleted too many candidates, then there's nothing worth outlining.
6670b57cec5SDimitry Andric // FIXME: This should take target-specified instruction sizes into account.
668fe013be4SDimitry Andric if (!OF || OF->Candidates.size() < 2)
6690b57cec5SDimitry Andric continue;
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric // Is it better to outline this candidate than not?
672fe013be4SDimitry Andric if (OF->getBenefit() < OutlinerBenefitThreshold) {
673fe013be4SDimitry Andric emitNotOutliningCheaperRemark(StringLen, CandidatesForRepeatedSeq, *OF);
6740b57cec5SDimitry Andric continue;
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric
677fe013be4SDimitry Andric FunctionList.push_back(*OF);
6780b57cec5SDimitry Andric }
6790b57cec5SDimitry Andric }
6800b57cec5SDimitry Andric
createOutlinedFunction(Module & M,OutlinedFunction & OF,InstructionMapper & Mapper,unsigned Name)681480093f4SDimitry Andric MachineFunction *MachineOutliner::createOutlinedFunction(
682480093f4SDimitry Andric Module &M, OutlinedFunction &OF, InstructionMapper &Mapper, unsigned Name) {
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andric // Create the function name. This should be unique.
6850b57cec5SDimitry Andric // FIXME: We should have a better naming scheme. This should be stable,
6860b57cec5SDimitry Andric // regardless of changes to the outliner's cost model/traversal order.
6875ffd83dbSDimitry Andric std::string FunctionName = "OUTLINED_FUNCTION_";
6885ffd83dbSDimitry Andric if (OutlineRepeatedNum > 0)
6895ffd83dbSDimitry Andric FunctionName += std::to_string(OutlineRepeatedNum + 1) + "_";
6905ffd83dbSDimitry Andric FunctionName += std::to_string(Name);
691fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "NEW FUNCTION: " << FunctionName << "\n");
6920b57cec5SDimitry Andric
6930b57cec5SDimitry Andric // Create the function using an IR-level function.
6940b57cec5SDimitry Andric LLVMContext &C = M.getContext();
6950b57cec5SDimitry Andric Function *F = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
6960b57cec5SDimitry Andric Function::ExternalLinkage, FunctionName, M);
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
6990b57cec5SDimitry Andric // which gives us better results when we outline from linkonceodr functions.
7000b57cec5SDimitry Andric F->setLinkage(GlobalValue::InternalLinkage);
7010b57cec5SDimitry Andric F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
7020b57cec5SDimitry Andric
7030b57cec5SDimitry Andric // Set optsize/minsize, so we don't insert padding between outlined
7040b57cec5SDimitry Andric // functions.
7050b57cec5SDimitry Andric F->addFnAttr(Attribute::OptimizeForSize);
7060b57cec5SDimitry Andric F->addFnAttr(Attribute::MinSize);
7070b57cec5SDimitry Andric
7080b57cec5SDimitry Andric Candidate &FirstCand = OF.Candidates.front();
7094824e7fdSDimitry Andric const TargetInstrInfo &TII =
7104824e7fdSDimitry Andric *FirstCand.getMF()->getSubtarget().getInstrInfo();
7110b57cec5SDimitry Andric
7124824e7fdSDimitry Andric TII.mergeOutliningCandidateAttributes(*F, OF.Candidates);
7135ffd83dbSDimitry Andric
71481ad6265SDimitry Andric // Set uwtable, so we generate eh_frame.
71581ad6265SDimitry Andric UWTableKind UW = std::accumulate(
71681ad6265SDimitry Andric OF.Candidates.cbegin(), OF.Candidates.cend(), UWTableKind::None,
71781ad6265SDimitry Andric [](UWTableKind K, const outliner::Candidate &C) {
71881ad6265SDimitry Andric return std::max(K, C.getMF()->getFunction().getUWTableKind());
71981ad6265SDimitry Andric });
72081ad6265SDimitry Andric if (UW != UWTableKind::None)
72181ad6265SDimitry Andric F->setUWTableKind(UW);
72281ad6265SDimitry Andric
7230b57cec5SDimitry Andric BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
7240b57cec5SDimitry Andric IRBuilder<> Builder(EntryBB);
7250b57cec5SDimitry Andric Builder.CreateRetVoid();
7260b57cec5SDimitry Andric
7278bcb0991SDimitry Andric MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
7280b57cec5SDimitry Andric MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
729fe013be4SDimitry Andric MF.setIsOutlined(true);
7300b57cec5SDimitry Andric MachineBasicBlock &MBB = *MF.CreateMachineBasicBlock();
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric // Insert the new function into the module.
7330b57cec5SDimitry Andric MF.insert(MF.begin(), &MBB);
7340b57cec5SDimitry Andric
735*a58f00eaSDimitry Andric MachineFunction *OriginalMF = FirstCand.front().getMF();
7365ffd83dbSDimitry Andric const std::vector<MCCFIInstruction> &Instrs =
7375ffd83dbSDimitry Andric OriginalMF->getFrameInstructions();
738*a58f00eaSDimitry Andric for (auto &MI : FirstCand) {
739*a58f00eaSDimitry Andric if (MI.isDebugInstr())
740e8d8bef9SDimitry Andric continue;
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric // Don't keep debug information for outlined instructions.
74381ad6265SDimitry Andric auto DL = DebugLoc();
744*a58f00eaSDimitry Andric if (MI.isCFIInstruction()) {
745*a58f00eaSDimitry Andric unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
74681ad6265SDimitry Andric MCCFIInstruction CFI = Instrs[CFIIndex];
74781ad6265SDimitry Andric BuildMI(MBB, MBB.end(), DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
74881ad6265SDimitry Andric .addCFIIndex(MF.addFrameInst(CFI));
74981ad6265SDimitry Andric } else {
750*a58f00eaSDimitry Andric MachineInstr *NewMI = MF.CloneMachineInstr(&MI);
75181ad6265SDimitry Andric NewMI->dropMemRefs(MF);
75281ad6265SDimitry Andric NewMI->setDebugLoc(DL);
7530b57cec5SDimitry Andric MBB.insert(MBB.end(), NewMI);
7540b57cec5SDimitry Andric }
75581ad6265SDimitry Andric }
7560b57cec5SDimitry Andric
7575ffd83dbSDimitry Andric // Set normal properties for a late MachineFunction.
7585ffd83dbSDimitry Andric MF.getProperties().reset(MachineFunctionProperties::Property::IsSSA);
7595ffd83dbSDimitry Andric MF.getProperties().set(MachineFunctionProperties::Property::NoPHIs);
7605ffd83dbSDimitry Andric MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
7615ffd83dbSDimitry Andric MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
7620b57cec5SDimitry Andric MF.getRegInfo().freezeReservedRegs(MF);
7630b57cec5SDimitry Andric
7645ffd83dbSDimitry Andric // Compute live-in set for outlined fn
7655ffd83dbSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
7665ffd83dbSDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
7675ffd83dbSDimitry Andric LivePhysRegs LiveIns(TRI);
7685ffd83dbSDimitry Andric for (auto &Cand : OF.Candidates) {
7695ffd83dbSDimitry Andric // Figure out live-ins at the first instruction.
770*a58f00eaSDimitry Andric MachineBasicBlock &OutlineBB = *Cand.front().getParent();
7715ffd83dbSDimitry Andric LivePhysRegs CandLiveIns(TRI);
7725ffd83dbSDimitry Andric CandLiveIns.addLiveOuts(OutlineBB);
7735ffd83dbSDimitry Andric for (const MachineInstr &MI :
774*a58f00eaSDimitry Andric reverse(make_range(Cand.begin(), OutlineBB.end())))
7755ffd83dbSDimitry Andric CandLiveIns.stepBackward(MI);
7765ffd83dbSDimitry Andric
7775ffd83dbSDimitry Andric // The live-in set for the outlined function is the union of the live-ins
7785ffd83dbSDimitry Andric // from all the outlining points.
779e8d8bef9SDimitry Andric for (MCPhysReg Reg : CandLiveIns)
7805ffd83dbSDimitry Andric LiveIns.addReg(Reg);
7815ffd83dbSDimitry Andric }
7825ffd83dbSDimitry Andric addLiveIns(MBB, LiveIns);
7835ffd83dbSDimitry Andric
7845ffd83dbSDimitry Andric TII.buildOutlinedFrame(MBB, MF, OF);
7855ffd83dbSDimitry Andric
7860b57cec5SDimitry Andric // If there's a DISubprogram associated with this outlined function, then
7870b57cec5SDimitry Andric // emit debug info for the outlined function.
7880b57cec5SDimitry Andric if (DISubprogram *SP = getSubprogramOrNull(OF)) {
7890b57cec5SDimitry Andric // We have a DISubprogram. Get its DICompileUnit.
7900b57cec5SDimitry Andric DICompileUnit *CU = SP->getUnit();
7910b57cec5SDimitry Andric DIBuilder DB(M, true, CU);
7920b57cec5SDimitry Andric DIFile *Unit = SP->getFile();
7930b57cec5SDimitry Andric Mangler Mg;
7940b57cec5SDimitry Andric // Get the mangled name of the function for the linkage name.
7950b57cec5SDimitry Andric std::string Dummy;
796fe013be4SDimitry Andric raw_string_ostream MangledNameStream(Dummy);
7970b57cec5SDimitry Andric Mg.getNameWithPrefix(MangledNameStream, F, false);
7980b57cec5SDimitry Andric
7990b57cec5SDimitry Andric DISubprogram *OutlinedSP = DB.createFunction(
8000b57cec5SDimitry Andric Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()),
8010b57cec5SDimitry Andric Unit /* File */,
8020b57cec5SDimitry Andric 0 /* Line 0 is reserved for compiler-generated code. */,
803bdd1243dSDimitry Andric DB.createSubroutineType(
804bdd1243dSDimitry Andric DB.getOrCreateTypeArray(std::nullopt)), /* void type */
8050b57cec5SDimitry Andric 0, /* Line 0 is reserved for compiler-generated code. */
8060b57cec5SDimitry Andric DINode::DIFlags::FlagArtificial /* Compiler-generated code. */,
8070b57cec5SDimitry Andric /* Outlined code is optimized code by definition. */
8080b57cec5SDimitry Andric DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
8090b57cec5SDimitry Andric
8100b57cec5SDimitry Andric // Don't add any new variables to the subprogram.
8110b57cec5SDimitry Andric DB.finalizeSubprogram(OutlinedSP);
8120b57cec5SDimitry Andric
8130b57cec5SDimitry Andric // Attach subprogram to the function.
8140b57cec5SDimitry Andric F->setSubprogram(OutlinedSP);
8150b57cec5SDimitry Andric // We're done with the DIBuilder.
8160b57cec5SDimitry Andric DB.finalize();
8170b57cec5SDimitry Andric }
8180b57cec5SDimitry Andric
8190b57cec5SDimitry Andric return &MF;
8200b57cec5SDimitry Andric }
8210b57cec5SDimitry Andric
outline(Module & M,std::vector<OutlinedFunction> & FunctionList,InstructionMapper & Mapper,unsigned & OutlinedFunctionNum)8220b57cec5SDimitry Andric bool MachineOutliner::outline(Module &M,
8230b57cec5SDimitry Andric std::vector<OutlinedFunction> &FunctionList,
824480093f4SDimitry Andric InstructionMapper &Mapper,
825480093f4SDimitry Andric unsigned &OutlinedFunctionNum) {
826fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "*** Outlining ***\n");
827fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size()
828fe013be4SDimitry Andric << "\n");
8290b57cec5SDimitry Andric bool OutlinedSomething = false;
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andric // Sort by benefit. The most beneficial functions should be outlined first.
832fe013be4SDimitry Andric stable_sort(FunctionList,
833fe013be4SDimitry Andric [](const OutlinedFunction &LHS, const OutlinedFunction &RHS) {
8340b57cec5SDimitry Andric return LHS.getBenefit() > RHS.getBenefit();
8350b57cec5SDimitry Andric });
8360b57cec5SDimitry Andric
8370b57cec5SDimitry Andric // Walk over each function, outlining them as we go along. Functions are
8380b57cec5SDimitry Andric // outlined greedily, based off the sort above.
839fe013be4SDimitry Andric auto *UnsignedVecBegin = Mapper.UnsignedVec.begin();
840fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "WALKING FUNCTION LIST\n");
8410b57cec5SDimitry Andric for (OutlinedFunction &OF : FunctionList) {
842fe013be4SDimitry Andric #ifndef NDEBUG
843fe013be4SDimitry Andric auto NumCandidatesBefore = OF.Candidates.size();
844fe013be4SDimitry Andric #endif
8450b57cec5SDimitry Andric // If we outlined something that overlapped with a candidate in a previous
8460b57cec5SDimitry Andric // step, then we can't outline from it.
847fe013be4SDimitry Andric erase_if(OF.Candidates, [&UnsignedVecBegin](Candidate &C) {
848fe013be4SDimitry Andric return std::any_of(UnsignedVecBegin + C.getStartIdx(),
849fe013be4SDimitry Andric UnsignedVecBegin + C.getEndIdx() + 1, [](unsigned I) {
850fe013be4SDimitry Andric return I == static_cast<unsigned>(-1);
851fe013be4SDimitry Andric });
8520b57cec5SDimitry Andric });
8530b57cec5SDimitry Andric
854fe013be4SDimitry Andric #ifndef NDEBUG
855fe013be4SDimitry Andric auto NumCandidatesAfter = OF.Candidates.size();
856fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
857fe013be4SDimitry Andric << "/" << NumCandidatesBefore << " candidates\n");
858fe013be4SDimitry Andric #endif
859fe013be4SDimitry Andric
8600b57cec5SDimitry Andric // If we made it unbeneficial to outline this function, skip it.
861fe013be4SDimitry Andric if (OF.getBenefit() < OutlinerBenefitThreshold) {
862fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF.getBenefit()
863fe013be4SDimitry Andric << " B) < threshold (" << OutlinerBenefitThreshold
864fe013be4SDimitry Andric << " B)\n");
8650b57cec5SDimitry Andric continue;
866fe013be4SDimitry Andric }
867fe013be4SDimitry Andric
868fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF.getBenefit()
869fe013be4SDimitry Andric << " B) > threshold (" << OutlinerBenefitThreshold
870fe013be4SDimitry Andric << " B)\n");
8710b57cec5SDimitry Andric
8720b57cec5SDimitry Andric // It's beneficial. Create the function and outline its sequence's
8730b57cec5SDimitry Andric // occurrences.
8740b57cec5SDimitry Andric OF.MF = createOutlinedFunction(M, OF, Mapper, OutlinedFunctionNum);
8750b57cec5SDimitry Andric emitOutlinedFunctionRemark(OF);
8760b57cec5SDimitry Andric FunctionsCreated++;
8770b57cec5SDimitry Andric OutlinedFunctionNum++; // Created a function, move to the next name.
8780b57cec5SDimitry Andric MachineFunction *MF = OF.MF;
8790b57cec5SDimitry Andric const TargetSubtargetInfo &STI = MF->getSubtarget();
8800b57cec5SDimitry Andric const TargetInstrInfo &TII = *STI.getInstrInfo();
8810b57cec5SDimitry Andric
8820b57cec5SDimitry Andric // Replace occurrences of the sequence with calls to the new function.
883fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "CREATE OUTLINED CALLS\n");
8840b57cec5SDimitry Andric for (Candidate &C : OF.Candidates) {
8850b57cec5SDimitry Andric MachineBasicBlock &MBB = *C.getMBB();
886*a58f00eaSDimitry Andric MachineBasicBlock::iterator StartIt = C.begin();
887*a58f00eaSDimitry Andric MachineBasicBlock::iterator EndIt = std::prev(C.end());
8880b57cec5SDimitry Andric
8890b57cec5SDimitry Andric // Insert the call.
8900b57cec5SDimitry Andric auto CallInst = TII.insertOutlinedCall(M, MBB, StartIt, *MF, C);
891fe013be4SDimitry Andric // Insert the call.
892fe013be4SDimitry Andric #ifndef NDEBUG
893fe013be4SDimitry Andric auto MBBBeingOutlinedFromName =
894fe013be4SDimitry Andric MBB.getName().empty() ? "<unknown>" : MBB.getName().str();
895fe013be4SDimitry Andric auto MFBeingOutlinedFromName = MBB.getParent()->getName().empty()
896fe013be4SDimitry Andric ? "<unknown>"
897fe013be4SDimitry Andric : MBB.getParent()->getName().str();
898fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " CALL: " << MF->getName() << " in "
899fe013be4SDimitry Andric << MFBeingOutlinedFromName << ":"
900fe013be4SDimitry Andric << MBBBeingOutlinedFromName << "\n");
901fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " .. " << *CallInst);
902fe013be4SDimitry Andric #endif
9030b57cec5SDimitry Andric
9040b57cec5SDimitry Andric // If the caller tracks liveness, then we need to make sure that
9050b57cec5SDimitry Andric // anything we outline doesn't break liveness assumptions. The outlined
9060b57cec5SDimitry Andric // functions themselves currently don't track liveness, but we should
9070b57cec5SDimitry Andric // make sure that the ranges we yank things out of aren't wrong.
9080b57cec5SDimitry Andric if (MBB.getParent()->getProperties().hasProperty(
9090b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness)) {
9105ffd83dbSDimitry Andric // The following code is to add implicit def operands to the call
9110b57cec5SDimitry Andric // instruction. It also updates call site information for moved
9120b57cec5SDimitry Andric // code.
9135ffd83dbSDimitry Andric SmallSet<Register, 2> UseRegs, DefRegs;
9140b57cec5SDimitry Andric // Copy over the defs in the outlined range.
9150b57cec5SDimitry Andric // First inst in outlined range <-- Anything that's defined in this
9160b57cec5SDimitry Andric // ... .. range has to be added as an
9170b57cec5SDimitry Andric // implicit Last inst in outlined range <-- def to the call
9180b57cec5SDimitry Andric // instruction. Also remove call site information for outlined block
9195ffd83dbSDimitry Andric // of code. The exposed uses need to be copied in the outlined range.
9205ffd83dbSDimitry Andric for (MachineBasicBlock::reverse_iterator
9215ffd83dbSDimitry Andric Iter = EndIt.getReverse(),
9225ffd83dbSDimitry Andric Last = std::next(CallInst.getReverse());
9235ffd83dbSDimitry Andric Iter != Last; Iter++) {
9245ffd83dbSDimitry Andric MachineInstr *MI = &*Iter;
925349cc55cSDimitry Andric SmallSet<Register, 2> InstrUseRegs;
9265ffd83dbSDimitry Andric for (MachineOperand &MOP : MI->operands()) {
9275ffd83dbSDimitry Andric // Skip over anything that isn't a register.
9285ffd83dbSDimitry Andric if (!MOP.isReg())
9295ffd83dbSDimitry Andric continue;
9305ffd83dbSDimitry Andric
9315ffd83dbSDimitry Andric if (MOP.isDef()) {
9325ffd83dbSDimitry Andric // Introduce DefRegs set to skip the redundant register.
9335ffd83dbSDimitry Andric DefRegs.insert(MOP.getReg());
934349cc55cSDimitry Andric if (UseRegs.count(MOP.getReg()) &&
935349cc55cSDimitry Andric !InstrUseRegs.count(MOP.getReg()))
9365ffd83dbSDimitry Andric // Since the regiester is modeled as defined,
9375ffd83dbSDimitry Andric // it is not necessary to be put in use register set.
9385ffd83dbSDimitry Andric UseRegs.erase(MOP.getReg());
9395ffd83dbSDimitry Andric } else if (!MOP.isUndef()) {
9405ffd83dbSDimitry Andric // Any register which is not undefined should
9415ffd83dbSDimitry Andric // be put in the use register set.
9425ffd83dbSDimitry Andric UseRegs.insert(MOP.getReg());
943349cc55cSDimitry Andric InstrUseRegs.insert(MOP.getReg());
9445ffd83dbSDimitry Andric }
9455ffd83dbSDimitry Andric }
9465ffd83dbSDimitry Andric if (MI->isCandidateForCallSiteEntry())
9475ffd83dbSDimitry Andric MI->getMF()->eraseCallSiteInfo(MI);
9485ffd83dbSDimitry Andric }
9495ffd83dbSDimitry Andric
9505ffd83dbSDimitry Andric for (const Register &I : DefRegs)
9515ffd83dbSDimitry Andric // If it's a def, add it to the call instruction.
9525ffd83dbSDimitry Andric CallInst->addOperand(
9535ffd83dbSDimitry Andric MachineOperand::CreateReg(I, true, /* isDef = true */
9545ffd83dbSDimitry Andric true /* isImp = true */));
9555ffd83dbSDimitry Andric
9565ffd83dbSDimitry Andric for (const Register &I : UseRegs)
9575ffd83dbSDimitry Andric // If it's a exposed use, add it to the call instruction.
9585ffd83dbSDimitry Andric CallInst->addOperand(
9595ffd83dbSDimitry Andric MachineOperand::CreateReg(I, false, /* isDef = false */
9605ffd83dbSDimitry Andric true /* isImp = true */));
9610b57cec5SDimitry Andric }
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric // Erase from the point after where the call was inserted up to, and
9640b57cec5SDimitry Andric // including, the final instruction in the sequence.
9650b57cec5SDimitry Andric // Erase needs one past the end, so we need std::next there too.
9660b57cec5SDimitry Andric MBB.erase(std::next(StartIt), std::next(EndIt));
9670b57cec5SDimitry Andric
9680b57cec5SDimitry Andric // Keep track of what we removed by marking them all as -1.
969fe013be4SDimitry Andric for (unsigned &I : make_range(UnsignedVecBegin + C.getStartIdx(),
970fe013be4SDimitry Andric UnsignedVecBegin + C.getEndIdx() + 1))
97181ad6265SDimitry Andric I = static_cast<unsigned>(-1);
9720b57cec5SDimitry Andric OutlinedSomething = true;
9730b57cec5SDimitry Andric
9740b57cec5SDimitry Andric // Statistics.
9750b57cec5SDimitry Andric NumOutlined++;
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric }
9780b57cec5SDimitry Andric
9790b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "OutlinedSomething = " << OutlinedSomething << "\n";);
9800b57cec5SDimitry Andric return OutlinedSomething;
9810b57cec5SDimitry Andric }
9820b57cec5SDimitry Andric
populateMapper(InstructionMapper & Mapper,Module & M,MachineModuleInfo & MMI)9830b57cec5SDimitry Andric void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
9840b57cec5SDimitry Andric MachineModuleInfo &MMI) {
9850b57cec5SDimitry Andric // Build instruction mappings for each function in the module. Start by
9860b57cec5SDimitry Andric // iterating over each Function in M.
987fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "*** Populating mapper ***\n");
9880b57cec5SDimitry Andric for (Function &F : M) {
989fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "MAPPING FUNCTION: " << F.getName() << "\n");
9900b57cec5SDimitry Andric
991bdd1243dSDimitry Andric if (F.hasFnAttribute("nooutline")) {
992fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "SKIP: Function has nooutline attribute\n");
9930b57cec5SDimitry Andric continue;
994bdd1243dSDimitry Andric }
9950b57cec5SDimitry Andric
9960b57cec5SDimitry Andric // There's something in F. Check if it has a MachineFunction associated with
9970b57cec5SDimitry Andric // it.
9980b57cec5SDimitry Andric MachineFunction *MF = MMI.getMachineFunction(F);
9990b57cec5SDimitry Andric
10000b57cec5SDimitry Andric // If it doesn't, then there's nothing to outline from. Move to the next
10010b57cec5SDimitry Andric // Function.
1002fe013be4SDimitry Andric if (!MF) {
1003fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "SKIP: Function does not have a MachineFunction\n");
10040b57cec5SDimitry Andric continue;
1005fe013be4SDimitry Andric }
10060b57cec5SDimitry Andric
10070b57cec5SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1008fe013be4SDimitry Andric if (!RunOnAllFunctions && !TII->shouldOutlineFromFunctionByDefault(*MF)) {
1009fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "SKIP: Target does not want to outline from "
1010fe013be4SDimitry Andric "function by default\n");
10110b57cec5SDimitry Andric continue;
1012fe013be4SDimitry Andric }
10130b57cec5SDimitry Andric
10140b57cec5SDimitry Andric // We have a MachineFunction. Ask the target if it's suitable for outlining.
10150b57cec5SDimitry Andric // If it isn't, then move on to the next Function in the module.
1016fe013be4SDimitry Andric if (!TII->isFunctionSafeToOutlineFrom(*MF, OutlineFromLinkOnceODRs)) {
1017fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << "SKIP: " << MF->getName()
1018fe013be4SDimitry Andric << ": unsafe to outline from\n");
10190b57cec5SDimitry Andric continue;
1020fe013be4SDimitry Andric }
10210b57cec5SDimitry Andric
10220b57cec5SDimitry Andric // We have a function suitable for outlining. Iterate over every
10230b57cec5SDimitry Andric // MachineBasicBlock in MF and try to map its instructions to a list of
10240b57cec5SDimitry Andric // unsigned integers.
1025fe013be4SDimitry Andric const unsigned MinMBBSize = 2;
1026fe013be4SDimitry Andric
10270b57cec5SDimitry Andric for (MachineBasicBlock &MBB : *MF) {
1028fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " MAPPING MBB: '" << MBB.getName() << "'\n");
10290b57cec5SDimitry Andric // If there isn't anything in MBB, then there's no point in outlining from
10300b57cec5SDimitry Andric // it.
10310b57cec5SDimitry Andric // If there are fewer than 2 instructions in the MBB, then it can't ever
10320b57cec5SDimitry Andric // contain something worth outlining.
10330b57cec5SDimitry Andric // FIXME: This should be based off of the maximum size in B of an outlined
10340b57cec5SDimitry Andric // call versus the size in B of the MBB.
1035fe013be4SDimitry Andric if (MBB.size() < MinMBBSize) {
1036fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " SKIP: MBB size less than minimum size of "
1037fe013be4SDimitry Andric << MinMBBSize << "\n");
10380b57cec5SDimitry Andric continue;
1039fe013be4SDimitry Andric }
10400b57cec5SDimitry Andric
10410b57cec5SDimitry Andric // Check if MBB could be the target of an indirect branch. If it is, then
10420b57cec5SDimitry Andric // we don't want to outline from it.
1043fe013be4SDimitry Andric if (MBB.hasAddressTaken()) {
1044fe013be4SDimitry Andric LLVM_DEBUG(dbgs() << " SKIP: MBB's address is taken\n");
10450b57cec5SDimitry Andric continue;
1046fe013be4SDimitry Andric }
10470b57cec5SDimitry Andric
10480b57cec5SDimitry Andric // MBB is suitable for outlining. Map it to a list of unsigneds.
10490b57cec5SDimitry Andric Mapper.convertToUnsignedVec(MBB, *TII);
10500b57cec5SDimitry Andric }
1051fe013be4SDimitry Andric }
105281ad6265SDimitry Andric // Statistics.
105381ad6265SDimitry Andric UnsignedVecSize = Mapper.UnsignedVec.size();
10540b57cec5SDimitry Andric }
10550b57cec5SDimitry Andric
initSizeRemarkInfo(const Module & M,const MachineModuleInfo & MMI,StringMap<unsigned> & FunctionToInstrCount)10560b57cec5SDimitry Andric void MachineOutliner::initSizeRemarkInfo(
10570b57cec5SDimitry Andric const Module &M, const MachineModuleInfo &MMI,
10580b57cec5SDimitry Andric StringMap<unsigned> &FunctionToInstrCount) {
10590b57cec5SDimitry Andric // Collect instruction counts for every function. We'll use this to emit
10600b57cec5SDimitry Andric // per-function size remarks later.
10610b57cec5SDimitry Andric for (const Function &F : M) {
10620b57cec5SDimitry Andric MachineFunction *MF = MMI.getMachineFunction(F);
10630b57cec5SDimitry Andric
10640b57cec5SDimitry Andric // We only care about MI counts here. If there's no MachineFunction at this
10650b57cec5SDimitry Andric // point, then there won't be after the outliner runs, so let's move on.
10660b57cec5SDimitry Andric if (!MF)
10670b57cec5SDimitry Andric continue;
10680b57cec5SDimitry Andric FunctionToInstrCount[F.getName().str()] = MF->getInstructionCount();
10690b57cec5SDimitry Andric }
10700b57cec5SDimitry Andric }
10710b57cec5SDimitry Andric
emitInstrCountChangedRemark(const Module & M,const MachineModuleInfo & MMI,const StringMap<unsigned> & FunctionToInstrCount)10720b57cec5SDimitry Andric void MachineOutliner::emitInstrCountChangedRemark(
10730b57cec5SDimitry Andric const Module &M, const MachineModuleInfo &MMI,
10740b57cec5SDimitry Andric const StringMap<unsigned> &FunctionToInstrCount) {
10750b57cec5SDimitry Andric // Iterate over each function in the module and emit remarks.
10760b57cec5SDimitry Andric // Note that we won't miss anything by doing this, because the outliner never
10770b57cec5SDimitry Andric // deletes functions.
10780b57cec5SDimitry Andric for (const Function &F : M) {
10790b57cec5SDimitry Andric MachineFunction *MF = MMI.getMachineFunction(F);
10800b57cec5SDimitry Andric
10810b57cec5SDimitry Andric // The outliner never deletes functions. If we don't have a MF here, then we
10820b57cec5SDimitry Andric // didn't have one prior to outlining either.
10830b57cec5SDimitry Andric if (!MF)
10840b57cec5SDimitry Andric continue;
10850b57cec5SDimitry Andric
10865ffd83dbSDimitry Andric std::string Fname = std::string(F.getName());
10870b57cec5SDimitry Andric unsigned FnCountAfter = MF->getInstructionCount();
10880b57cec5SDimitry Andric unsigned FnCountBefore = 0;
10890b57cec5SDimitry Andric
10900b57cec5SDimitry Andric // Check if the function was recorded before.
10910b57cec5SDimitry Andric auto It = FunctionToInstrCount.find(Fname);
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andric // Did we have a previously-recorded size? If yes, then set FnCountBefore
10940b57cec5SDimitry Andric // to that.
10950b57cec5SDimitry Andric if (It != FunctionToInstrCount.end())
10960b57cec5SDimitry Andric FnCountBefore = It->second;
10970b57cec5SDimitry Andric
10980b57cec5SDimitry Andric // Compute the delta and emit a remark if there was a change.
10990b57cec5SDimitry Andric int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
11000b57cec5SDimitry Andric static_cast<int64_t>(FnCountBefore);
11010b57cec5SDimitry Andric if (FnDelta == 0)
11020b57cec5SDimitry Andric continue;
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(*MF, nullptr);
11050b57cec5SDimitry Andric MORE.emit([&]() {
11060b57cec5SDimitry Andric MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
1107480093f4SDimitry Andric DiagnosticLocation(), &MF->front());
11080b57cec5SDimitry Andric R << DiagnosticInfoOptimizationBase::Argument("Pass", "Machine Outliner")
11090b57cec5SDimitry Andric << ": Function: "
11100b57cec5SDimitry Andric << DiagnosticInfoOptimizationBase::Argument("Function", F.getName())
11110b57cec5SDimitry Andric << ": MI instruction count changed from "
11120b57cec5SDimitry Andric << DiagnosticInfoOptimizationBase::Argument("MIInstrsBefore",
11130b57cec5SDimitry Andric FnCountBefore)
11140b57cec5SDimitry Andric << " to "
11150b57cec5SDimitry Andric << DiagnosticInfoOptimizationBase::Argument("MIInstrsAfter",
11160b57cec5SDimitry Andric FnCountAfter)
11170b57cec5SDimitry Andric << "; Delta: "
11180b57cec5SDimitry Andric << DiagnosticInfoOptimizationBase::Argument("Delta", FnDelta);
11190b57cec5SDimitry Andric return R;
11200b57cec5SDimitry Andric });
11210b57cec5SDimitry Andric }
11220b57cec5SDimitry Andric }
11230b57cec5SDimitry Andric
runOnModule(Module & M)11240b57cec5SDimitry Andric bool MachineOutliner::runOnModule(Module &M) {
11250b57cec5SDimitry Andric // Check if there's anything in the module. If it's empty, then there's
11260b57cec5SDimitry Andric // nothing to outline.
11270b57cec5SDimitry Andric if (M.empty())
11280b57cec5SDimitry Andric return false;
11290b57cec5SDimitry Andric
1130480093f4SDimitry Andric // Number to append to the current outlined function.
1131480093f4SDimitry Andric unsigned OutlinedFunctionNum = 0;
1132480093f4SDimitry Andric
11335ffd83dbSDimitry Andric OutlineRepeatedNum = 0;
1134480093f4SDimitry Andric if (!doOutline(M, OutlinedFunctionNum))
1135480093f4SDimitry Andric return false;
11365ffd83dbSDimitry Andric
11375ffd83dbSDimitry Andric for (unsigned I = 0; I < OutlinerReruns; ++I) {
11385ffd83dbSDimitry Andric OutlinedFunctionNum = 0;
11395ffd83dbSDimitry Andric OutlineRepeatedNum++;
11405ffd83dbSDimitry Andric if (!doOutline(M, OutlinedFunctionNum)) {
11415ffd83dbSDimitry Andric LLVM_DEBUG({
11425ffd83dbSDimitry Andric dbgs() << "Did not outline on iteration " << I + 2 << " out of "
11435ffd83dbSDimitry Andric << OutlinerReruns + 1 << "\n";
11445ffd83dbSDimitry Andric });
11455ffd83dbSDimitry Andric break;
11465ffd83dbSDimitry Andric }
11475ffd83dbSDimitry Andric }
11485ffd83dbSDimitry Andric
1149480093f4SDimitry Andric return true;
1150480093f4SDimitry Andric }
1151480093f4SDimitry Andric
doOutline(Module & M,unsigned & OutlinedFunctionNum)1152480093f4SDimitry Andric bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11538bcb0991SDimitry Andric MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
11540b57cec5SDimitry Andric
11550b57cec5SDimitry Andric // If the user passed -enable-machine-outliner=always or
11560b57cec5SDimitry Andric // -enable-machine-outliner, the pass will run on all functions in the module.
11570b57cec5SDimitry Andric // Otherwise, if the target supports default outlining, it will run on all
11580b57cec5SDimitry Andric // functions deemed by the target to be worth outlining from by default. Tell
11590b57cec5SDimitry Andric // the user how the outliner is running.
1160480093f4SDimitry Andric LLVM_DEBUG({
11610b57cec5SDimitry Andric dbgs() << "Machine Outliner: Running on ";
11620b57cec5SDimitry Andric if (RunOnAllFunctions)
11630b57cec5SDimitry Andric dbgs() << "all functions";
11640b57cec5SDimitry Andric else
11650b57cec5SDimitry Andric dbgs() << "target-default functions";
1166480093f4SDimitry Andric dbgs() << "\n";
1167480093f4SDimitry Andric });
11680b57cec5SDimitry Andric
11690b57cec5SDimitry Andric // If the user specifies that they want to outline from linkonceodrs, set
11700b57cec5SDimitry Andric // it here.
11710b57cec5SDimitry Andric OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining;
11720b57cec5SDimitry Andric InstructionMapper Mapper;
11730b57cec5SDimitry Andric
11740b57cec5SDimitry Andric // Prepare instruction mappings for the suffix tree.
11750b57cec5SDimitry Andric populateMapper(Mapper, M, MMI);
11760b57cec5SDimitry Andric std::vector<OutlinedFunction> FunctionList;
11770b57cec5SDimitry Andric
11780b57cec5SDimitry Andric // Find all of the outlining candidates.
11790b57cec5SDimitry Andric findCandidates(Mapper, FunctionList);
11800b57cec5SDimitry Andric
11810b57cec5SDimitry Andric // If we've requested size remarks, then collect the MI counts of every
11820b57cec5SDimitry Andric // function before outlining, and the MI counts after outlining.
11830b57cec5SDimitry Andric // FIXME: This shouldn't be in the outliner at all; it should ultimately be
11840b57cec5SDimitry Andric // the pass manager's responsibility.
11850b57cec5SDimitry Andric // This could pretty easily be placed in outline instead, but because we
11860b57cec5SDimitry Andric // really ultimately *don't* want this here, it's done like this for now
11870b57cec5SDimitry Andric // instead.
11880b57cec5SDimitry Andric
11890b57cec5SDimitry Andric // Check if we want size remarks.
11900b57cec5SDimitry Andric bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark();
11910b57cec5SDimitry Andric StringMap<unsigned> FunctionToInstrCount;
11920b57cec5SDimitry Andric if (ShouldEmitSizeRemarks)
11930b57cec5SDimitry Andric initSizeRemarkInfo(M, MMI, FunctionToInstrCount);
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andric // Outline each of the candidates and return true if something was outlined.
1196480093f4SDimitry Andric bool OutlinedSomething =
1197480093f4SDimitry Andric outline(M, FunctionList, Mapper, OutlinedFunctionNum);
11980b57cec5SDimitry Andric
11990b57cec5SDimitry Andric // If we outlined something, we definitely changed the MI count of the
12000b57cec5SDimitry Andric // module. If we've asked for size remarks, then output them.
12010b57cec5SDimitry Andric // FIXME: This should be in the pass manager.
12020b57cec5SDimitry Andric if (ShouldEmitSizeRemarks && OutlinedSomething)
12030b57cec5SDimitry Andric emitInstrCountChangedRemark(M, MMI, FunctionToInstrCount);
12040b57cec5SDimitry Andric
12055ffd83dbSDimitry Andric LLVM_DEBUG({
12065ffd83dbSDimitry Andric if (!OutlinedSomething)
12075ffd83dbSDimitry Andric dbgs() << "Stopped outlining at iteration " << OutlineRepeatedNum
12085ffd83dbSDimitry Andric << " because no changes were found.\n";
12095ffd83dbSDimitry Andric });
12105ffd83dbSDimitry Andric
12110b57cec5SDimitry Andric return OutlinedSomething;
12120b57cec5SDimitry Andric }
1213