@@ -456,16 +456,19 @@ struct MachineOutliner : public ModulePass {
456456 // / \param Mapper Contains outlining mapping information.
457457 // / \param[out] FunctionList Filled with a list of \p OutlinedFunctions
458458 // / each type of candidate.
459- void findCandidates (InstructionMapper &Mapper,
460- std::vector<OutlinedFunction> &FunctionList);
459+ void
460+ findCandidates (InstructionMapper &Mapper,
461+ std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList);
461462
462463 // / Replace the sequences of instructions represented by \p OutlinedFunctions
463464 // / with calls to functions.
464465 // /
465466 // / \param M The module we are outlining from.
466467 // / \param FunctionList A list of functions to be inserted into the module.
467468 // / \param Mapper Contains the instruction mappings for the module.
468- bool outline (Module &M, std::vector<OutlinedFunction> &FunctionList,
469+ // / \param[out] OutlinedFunctionNum The outlined function number.
470+ bool outline (Module &M,
471+ std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
469472 InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
470473
471474 // / Creates a function for \p OF and inserts it into the module.
@@ -583,7 +586,8 @@ void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
583586}
584587
585588void MachineOutliner::findCandidates (
586- InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
589+ InstructionMapper &Mapper,
590+ std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
587591 FunctionList.clear ();
588592 SuffixTree ST (Mapper.UnsignedVec , OutlinerLeafDescendants);
589593
@@ -658,33 +662,36 @@ void MachineOutliner::findCandidates(
658662 << " \n " );
659663 LLVM_DEBUG (dbgs () << " Candidates kept: " << NumKept << " \n\n " );
660664#endif
665+ unsigned MinRepeats = 2 ;
661666
662667 // We've found something we might want to outline.
663668 // Create an OutlinedFunction to store it and check if it'd be beneficial
664669 // to outline.
665- if (CandidatesForRepeatedSeq.size () < 2 )
670+ if (CandidatesForRepeatedSeq.size () < MinRepeats )
666671 continue ;
667672
668673 // Arbitrarily choose a TII from the first candidate.
669674 // FIXME: Should getOutliningCandidateInfo move to TargetMachine?
670675 const TargetInstrInfo *TII =
671676 CandidatesForRepeatedSeq[0 ].getMF ()->getSubtarget ().getInstrInfo ();
672677
673- std::optional<OutlinedFunction> OF =
674- TII->getOutliningCandidateInfo (*MMI, CandidatesForRepeatedSeq);
678+ std::optional<std::unique_ptr<OutlinedFunction>> OF =
679+ TII->getOutliningCandidateInfo (*MMI, CandidatesForRepeatedSeq,
680+ MinRepeats);
675681
676682 // If we deleted too many candidates, then there's nothing worth outlining.
677683 // FIXME: This should take target-specified instruction sizes into account.
678- if (!OF || OF->Candidates .size () < 2 )
684+ if (!OF. has_value () || OF. value () ->Candidates .size () < MinRepeats )
679685 continue ;
680686
681687 // Is it better to outline this candidate than not?
682- if (OF->getBenefit () < OutlinerBenefitThreshold) {
683- emitNotOutliningCheaperRemark (StringLen, CandidatesForRepeatedSeq, *OF);
688+ if (OF.value ()->getBenefit () < OutlinerBenefitThreshold) {
689+ emitNotOutliningCheaperRemark (StringLen, CandidatesForRepeatedSeq,
690+ *OF.value ());
684691 continue ;
685692 }
686693
687- FunctionList.push_back (*OF );
694+ FunctionList.emplace_back ( std::move (OF. value ()) );
688695 }
689696}
690697
@@ -827,71 +834,70 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
827834 return &MF;
828835}
829836
830- bool MachineOutliner::outline (Module &M,
831- std::vector<OutlinedFunction> &FunctionList,
832- InstructionMapper &Mapper,
833- unsigned &OutlinedFunctionNum) {
837+ bool MachineOutliner::outline (
838+ Module &M, std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
839+ InstructionMapper &Mapper, unsigned &OutlinedFunctionNum) {
834840 LLVM_DEBUG (dbgs () << " *** Outlining ***\n " );
835841 LLVM_DEBUG (dbgs () << " NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size ()
836842 << " \n " );
837843 bool OutlinedSomething = false ;
838844
839845 // Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
840846 // The function with highest priority should be outlined first.
841- stable_sort (FunctionList,
842- []( const OutlinedFunction &LHS, const OutlinedFunction &RHS) {
843- return LHS. getNotOutlinedCost () * RHS. getOutliningCost () >
844- RHS. getNotOutlinedCost () * LHS. getOutliningCost ();
845- });
847+ stable_sort (FunctionList, []( const std::unique_ptr<OutlinedFunction> &LHS,
848+ const std::unique_ptr< OutlinedFunction> &RHS) {
849+ return LHS-> getNotOutlinedCost () * RHS-> getOutliningCost () >
850+ RHS-> getNotOutlinedCost () * LHS-> getOutliningCost ();
851+ });
846852
847853 // Walk over each function, outlining them as we go along. Functions are
848854 // outlined greedily, based off the sort above.
849855 auto *UnsignedVecBegin = Mapper.UnsignedVec .begin ();
850856 LLVM_DEBUG (dbgs () << " WALKING FUNCTION LIST\n " );
851- for (OutlinedFunction &OF : FunctionList) {
857+ for (auto &OF : FunctionList) {
852858#ifndef NDEBUG
853- auto NumCandidatesBefore = OF. Candidates .size ();
859+ auto NumCandidatesBefore = OF-> Candidates .size ();
854860#endif
855861 // If we outlined something that overlapped with a candidate in a previous
856862 // step, then we can't outline from it.
857- erase_if (OF. Candidates , [&UnsignedVecBegin](Candidate &C) {
863+ erase_if (OF-> Candidates , [&UnsignedVecBegin](Candidate &C) {
858864 return std::any_of (UnsignedVecBegin + C.getStartIdx (),
859865 UnsignedVecBegin + C.getEndIdx () + 1 , [](unsigned I) {
860866 return I == static_cast <unsigned >(-1 );
861867 });
862868 });
863869
864870#ifndef NDEBUG
865- auto NumCandidatesAfter = OF. Candidates .size ();
871+ auto NumCandidatesAfter = OF-> Candidates .size ();
866872 LLVM_DEBUG (dbgs () << " PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
867873 << " /" << NumCandidatesBefore << " candidates\n " );
868874#endif
869875
870876 // If we made it unbeneficial to outline this function, skip it.
871- if (OF. getBenefit () < OutlinerBenefitThreshold) {
872- LLVM_DEBUG (dbgs () << " SKIP: Expected benefit (" << OF. getBenefit ()
877+ if (OF-> getBenefit () < OutlinerBenefitThreshold) {
878+ LLVM_DEBUG (dbgs () << " SKIP: Expected benefit (" << OF-> getBenefit ()
873879 << " B) < threshold (" << OutlinerBenefitThreshold
874880 << " B)\n " );
875881 continue ;
876882 }
877883
878- LLVM_DEBUG (dbgs () << " OUTLINE: Expected benefit (" << OF. getBenefit ()
884+ LLVM_DEBUG (dbgs () << " OUTLINE: Expected benefit (" << OF-> getBenefit ()
879885 << " B) > threshold (" << OutlinerBenefitThreshold
880886 << " B)\n " );
881887
882888 // It's beneficial. Create the function and outline its sequence's
883889 // occurrences.
884- OF. MF = createOutlinedFunction (M, OF, Mapper, OutlinedFunctionNum);
885- emitOutlinedFunctionRemark (OF);
890+ OF-> MF = createOutlinedFunction (M, * OF, Mapper, OutlinedFunctionNum);
891+ emitOutlinedFunctionRemark (* OF);
886892 FunctionsCreated++;
887893 OutlinedFunctionNum++; // Created a function, move to the next name.
888- MachineFunction *MF = OF. MF ;
894+ MachineFunction *MF = OF-> MF ;
889895 const TargetSubtargetInfo &STI = MF->getSubtarget ();
890896 const TargetInstrInfo &TII = *STI.getInstrInfo ();
891897
892898 // Replace occurrences of the sequence with calls to the new function.
893899 LLVM_DEBUG (dbgs () << " CREATE OUTLINED CALLS\n " );
894- for (Candidate &C : OF. Candidates ) {
900+ for (Candidate &C : OF-> Candidates ) {
895901 MachineBasicBlock &MBB = *C.getMBB ();
896902 MachineBasicBlock::iterator StartIt = C.begin ();
897903 MachineBasicBlock::iterator EndIt = std::prev (C.end ());
@@ -1180,7 +1186,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11801186
11811187 // Prepare instruction mappings for the suffix tree.
11821188 populateMapper (Mapper, M);
1183- std::vector<OutlinedFunction> FunctionList;
1189+ std::vector<std::unique_ptr< OutlinedFunction> > FunctionList;
11841190
11851191 // Find all of the outlining candidates.
11861192 findCandidates (Mapper, FunctionList);
0 commit comments