@@ -456,16 +456,19 @@ struct MachineOutliner : public ModulePass {
456
456
// / \param Mapper Contains outlining mapping information.
457
457
// / \param[out] FunctionList Filled with a list of \p OutlinedFunctions
458
458
// / 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);
461
462
462
463
// / Replace the sequences of instructions represented by \p OutlinedFunctions
463
464
// / with calls to functions.
464
465
// /
465
466
// / \param M The module we are outlining from.
466
467
// / \param FunctionList A list of functions to be inserted into the module.
467
468
// / \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,
469
472
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
470
473
471
474
// / Creates a function for \p OF and inserts it into the module.
@@ -583,7 +586,8 @@ void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
583
586
}
584
587
585
588
void MachineOutliner::findCandidates (
586
- InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
589
+ InstructionMapper &Mapper,
590
+ std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
587
591
FunctionList.clear ();
588
592
SuffixTree ST (Mapper.UnsignedVec , OutlinerLeafDescendants);
589
593
@@ -658,33 +662,36 @@ void MachineOutliner::findCandidates(
658
662
<< " \n " );
659
663
LLVM_DEBUG (dbgs () << " Candidates kept: " << NumKept << " \n\n " );
660
664
#endif
665
+ unsigned MinRepeats = 2 ;
661
666
662
667
// We've found something we might want to outline.
663
668
// Create an OutlinedFunction to store it and check if it'd be beneficial
664
669
// to outline.
665
- if (CandidatesForRepeatedSeq.size () < 2 )
670
+ if (CandidatesForRepeatedSeq.size () < MinRepeats )
666
671
continue ;
667
672
668
673
// Arbitrarily choose a TII from the first candidate.
669
674
// FIXME: Should getOutliningCandidateInfo move to TargetMachine?
670
675
const TargetInstrInfo *TII =
671
676
CandidatesForRepeatedSeq[0 ].getMF ()->getSubtarget ().getInstrInfo ();
672
677
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);
675
681
676
682
// If we deleted too many candidates, then there's nothing worth outlining.
677
683
// 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 )
679
685
continue ;
680
686
681
687
// 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 ());
684
691
continue ;
685
692
}
686
693
687
- FunctionList.push_back (*OF );
694
+ FunctionList.emplace_back ( std::move (OF. value ()) );
688
695
}
689
696
}
690
697
@@ -827,71 +834,70 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
827
834
return &MF;
828
835
}
829
836
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) {
834
840
LLVM_DEBUG (dbgs () << " *** Outlining ***\n " );
835
841
LLVM_DEBUG (dbgs () << " NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size ()
836
842
<< " \n " );
837
843
bool OutlinedSomething = false ;
838
844
839
845
// Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
840
846
// 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
+ });
846
852
847
853
// Walk over each function, outlining them as we go along. Functions are
848
854
// outlined greedily, based off the sort above.
849
855
auto *UnsignedVecBegin = Mapper.UnsignedVec .begin ();
850
856
LLVM_DEBUG (dbgs () << " WALKING FUNCTION LIST\n " );
851
- for (OutlinedFunction &OF : FunctionList) {
857
+ for (auto &OF : FunctionList) {
852
858
#ifndef NDEBUG
853
- auto NumCandidatesBefore = OF. Candidates .size ();
859
+ auto NumCandidatesBefore = OF-> Candidates .size ();
854
860
#endif
855
861
// If we outlined something that overlapped with a candidate in a previous
856
862
// step, then we can't outline from it.
857
- erase_if (OF. Candidates , [&UnsignedVecBegin](Candidate &C) {
863
+ erase_if (OF-> Candidates , [&UnsignedVecBegin](Candidate &C) {
858
864
return std::any_of (UnsignedVecBegin + C.getStartIdx (),
859
865
UnsignedVecBegin + C.getEndIdx () + 1 , [](unsigned I) {
860
866
return I == static_cast <unsigned >(-1 );
861
867
});
862
868
});
863
869
864
870
#ifndef NDEBUG
865
- auto NumCandidatesAfter = OF. Candidates .size ();
871
+ auto NumCandidatesAfter = OF-> Candidates .size ();
866
872
LLVM_DEBUG (dbgs () << " PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
867
873
<< " /" << NumCandidatesBefore << " candidates\n " );
868
874
#endif
869
875
870
876
// 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 ()
873
879
<< " B) < threshold (" << OutlinerBenefitThreshold
874
880
<< " B)\n " );
875
881
continue ;
876
882
}
877
883
878
- LLVM_DEBUG (dbgs () << " OUTLINE: Expected benefit (" << OF. getBenefit ()
884
+ LLVM_DEBUG (dbgs () << " OUTLINE: Expected benefit (" << OF-> getBenefit ()
879
885
<< " B) > threshold (" << OutlinerBenefitThreshold
880
886
<< " B)\n " );
881
887
882
888
// It's beneficial. Create the function and outline its sequence's
883
889
// occurrences.
884
- OF. MF = createOutlinedFunction (M, OF, Mapper, OutlinedFunctionNum);
885
- emitOutlinedFunctionRemark (OF);
890
+ OF-> MF = createOutlinedFunction (M, * OF, Mapper, OutlinedFunctionNum);
891
+ emitOutlinedFunctionRemark (* OF);
886
892
FunctionsCreated++;
887
893
OutlinedFunctionNum++; // Created a function, move to the next name.
888
- MachineFunction *MF = OF. MF ;
894
+ MachineFunction *MF = OF-> MF ;
889
895
const TargetSubtargetInfo &STI = MF->getSubtarget ();
890
896
const TargetInstrInfo &TII = *STI.getInstrInfo ();
891
897
892
898
// Replace occurrences of the sequence with calls to the new function.
893
899
LLVM_DEBUG (dbgs () << " CREATE OUTLINED CALLS\n " );
894
- for (Candidate &C : OF. Candidates ) {
900
+ for (Candidate &C : OF-> Candidates ) {
895
901
MachineBasicBlock &MBB = *C.getMBB ();
896
902
MachineBasicBlock::iterator StartIt = C.begin ();
897
903
MachineBasicBlock::iterator EndIt = std::prev (C.end ());
@@ -1180,7 +1186,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
1180
1186
1181
1187
// Prepare instruction mappings for the suffix tree.
1182
1188
populateMapper (Mapper, M);
1183
- std::vector<OutlinedFunction> FunctionList;
1189
+ std::vector<std::unique_ptr< OutlinedFunction> > FunctionList;
1184
1190
1185
1191
// Find all of the outlining candidates.
1186
1192
findCandidates (Mapper, FunctionList);
0 commit comments