MachineOutliner.cpp 43 KB

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