Mercurial > hg > CbC > CbC_llvm
comparison lib/CodeGen/MachineOutliner.cpp @ 134:3a76565eade5 LLVM5.0.1
update 5.0.1
author | mir3636 |
---|---|
date | Sat, 17 Feb 2018 09:57:20 +0900 |
parents | 803732b1fca8 |
children | c2174574ed3a |
comparison
equal
deleted
inserted
replaced
133:c60214abe0e8 | 134:3a76565eade5 |
---|---|
57 /// | 57 /// |
58 //===----------------------------------------------------------------------===// | 58 //===----------------------------------------------------------------------===// |
59 #include "llvm/ADT/DenseMap.h" | 59 #include "llvm/ADT/DenseMap.h" |
60 #include "llvm/ADT/Statistic.h" | 60 #include "llvm/ADT/Statistic.h" |
61 #include "llvm/ADT/Twine.h" | 61 #include "llvm/ADT/Twine.h" |
62 #include "llvm/CodeGen/MachineFrameInfo.h" | |
63 #include "llvm/CodeGen/MachineFunction.h" | 62 #include "llvm/CodeGen/MachineFunction.h" |
64 #include "llvm/CodeGen/MachineInstrBuilder.h" | |
65 #include "llvm/CodeGen/MachineModuleInfo.h" | 63 #include "llvm/CodeGen/MachineModuleInfo.h" |
66 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" | 64 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
65 #include "llvm/CodeGen/MachineRegisterInfo.h" | |
67 #include "llvm/CodeGen/Passes.h" | 66 #include "llvm/CodeGen/Passes.h" |
67 #include "llvm/CodeGen/TargetInstrInfo.h" | |
68 #include "llvm/CodeGen/TargetRegisterInfo.h" | |
69 #include "llvm/CodeGen/TargetSubtargetInfo.h" | |
70 #include "llvm/IR/DIBuilder.h" | |
68 #include "llvm/IR/IRBuilder.h" | 71 #include "llvm/IR/IRBuilder.h" |
72 #include "llvm/IR/Mangler.h" | |
69 #include "llvm/Support/Allocator.h" | 73 #include "llvm/Support/Allocator.h" |
70 #include "llvm/Support/Debug.h" | 74 #include "llvm/Support/Debug.h" |
71 #include "llvm/Support/raw_ostream.h" | 75 #include "llvm/Support/raw_ostream.h" |
72 #include "llvm/Target/TargetInstrInfo.h" | |
73 #include "llvm/Target/TargetMachine.h" | |
74 #include "llvm/Target/TargetRegisterInfo.h" | |
75 #include "llvm/Target/TargetSubtargetInfo.h" | |
76 #include <functional> | 76 #include <functional> |
77 #include <map> | 77 #include <map> |
78 #include <sstream> | 78 #include <sstream> |
79 #include <tuple> | 79 #include <tuple> |
80 #include <vector> | 80 #include <vector> |
97 unsigned StartIdx; | 97 unsigned StartIdx; |
98 | 98 |
99 /// The number of instructions in this \p Candidate. | 99 /// The number of instructions in this \p Candidate. |
100 unsigned Len; | 100 unsigned Len; |
101 | 101 |
102 /// The MachineFunction containing this \p Candidate. | |
103 MachineFunction *MF = nullptr; | |
104 | |
102 public: | 105 public: |
103 /// Set to false if the candidate overlapped with another candidate. | 106 /// Set to false if the candidate overlapped with another candidate. |
104 bool InCandidateList = true; | 107 bool InCandidateList = true; |
105 | 108 |
106 /// \brief The index of this \p Candidate's \p OutlinedFunction in the list of | 109 /// \brief The index of this \p Candidate's \p OutlinedFunction in the list of |
107 /// \p OutlinedFunctions. | 110 /// \p OutlinedFunctions. |
108 unsigned FunctionIdx; | 111 unsigned FunctionIdx; |
109 | 112 |
110 /// Contains all target-specific information for this \p Candidate. | 113 /// Contains all target-specific information for this \p Candidate. |
111 TargetInstrInfo::MachineOutlinerInfo MInfo; | 114 TargetInstrInfo::MachineOutlinerInfo MInfo; |
115 | |
116 /// If there is a DISubprogram associated with the function that this | |
117 /// Candidate lives in, return it. | |
118 DISubprogram *getSubprogramOrNull() const { | |
119 assert(MF && "Candidate has no MF!"); | |
120 if (DISubprogram *SP = MF->getFunction().getSubprogram()) | |
121 return SP; | |
122 return nullptr; | |
123 } | |
112 | 124 |
113 /// Return the number of instructions in this Candidate. | 125 /// Return the number of instructions in this Candidate. |
114 unsigned getLength() const { return Len; } | 126 unsigned getLength() const { return Len; } |
115 | 127 |
116 /// Return the start index of this candidate. | 128 /// Return the start index of this candidate. |
126 /// process. It is only used for deciding which candidate to keep if two | 138 /// process. It is only used for deciding which candidate to keep if two |
127 /// candidates overlap. The true benefit is stored in the OutlinedFunction | 139 /// candidates overlap. The true benefit is stored in the OutlinedFunction |
128 /// for some given candidate. | 140 /// for some given candidate. |
129 unsigned Benefit = 0; | 141 unsigned Benefit = 0; |
130 | 142 |
131 Candidate(unsigned StartIdx, unsigned Len, unsigned FunctionIdx) | 143 Candidate(unsigned StartIdx, unsigned Len, unsigned FunctionIdx, |
132 : StartIdx(StartIdx), Len(Len), FunctionIdx(FunctionIdx) {} | 144 MachineFunction *MF) |
145 : StartIdx(StartIdx), Len(Len), MF(MF), FunctionIdx(FunctionIdx) {} | |
133 | 146 |
134 Candidate() {} | 147 Candidate() {} |
135 | 148 |
136 /// \brief Used to ensure that \p Candidates are outlined in an order that | 149 /// \brief Used to ensure that \p Candidates are outlined in an order that |
137 /// preserves the start and end indices of other \p Candidates. | 150 /// preserves the start and end indices of other \p Candidates. |
162 /// function. | 175 /// function. |
163 std::vector<unsigned> Sequence; | 176 std::vector<unsigned> Sequence; |
164 | 177 |
165 /// Contains all target-specific information for this \p OutlinedFunction. | 178 /// Contains all target-specific information for this \p OutlinedFunction. |
166 TargetInstrInfo::MachineOutlinerInfo MInfo; | 179 TargetInstrInfo::MachineOutlinerInfo MInfo; |
180 | |
181 /// If there is a DISubprogram for any Candidate for this outlined function, | |
182 /// then return it. Otherwise, return nullptr. | |
183 DISubprogram *getSubprogramOrNull() const { | |
184 for (const auto &C : Candidates) | |
185 if (DISubprogram *SP = C->getSubprogramOrNull()) | |
186 return SP; | |
187 return nullptr; | |
188 } | |
167 | 189 |
168 /// Return the number of candidates for this \p OutlinedFunction. | 190 /// Return the number of candidates for this \p OutlinedFunction. |
169 unsigned getOccurrenceCount() { return OccurrenceCount; } | 191 unsigned getOccurrenceCount() { return OccurrenceCount; } |
170 | 192 |
171 /// Decrement the occurrence count of this OutlinedFunction and return the | 193 /// Decrement the occurrence count of this OutlinedFunction and return the |
721 /// \param TRI \p TargetRegisterInfo for the module. | 743 /// \param TRI \p TargetRegisterInfo for the module. |
722 /// \param TII \p TargetInstrInfo for the module. | 744 /// \param TII \p TargetInstrInfo for the module. |
723 void convertToUnsignedVec(MachineBasicBlock &MBB, | 745 void convertToUnsignedVec(MachineBasicBlock &MBB, |
724 const TargetRegisterInfo &TRI, | 746 const TargetRegisterInfo &TRI, |
725 const TargetInstrInfo &TII) { | 747 const TargetInstrInfo &TII) { |
748 unsigned Flags = TII.getMachineOutlinerMBBFlags(MBB); | |
749 | |
726 for (MachineBasicBlock::iterator It = MBB.begin(), Et = MBB.end(); It != Et; | 750 for (MachineBasicBlock::iterator It = MBB.begin(), Et = MBB.end(); It != Et; |
727 It++) { | 751 It++) { |
728 | 752 |
729 // Keep track of where this instruction is in the module. | 753 // Keep track of where this instruction is in the module. |
730 switch (TII.getOutliningType(*It)) { | 754 switch (TII.getOutliningType(It, Flags)) { |
731 case TargetInstrInfo::MachineOutlinerInstrType::Illegal: | 755 case TargetInstrInfo::MachineOutlinerInstrType::Illegal: |
732 mapToIllegalUnsigned(It); | 756 mapToIllegalUnsigned(It); |
733 break; | 757 break; |
734 | 758 |
735 case TargetInstrInfo::MachineOutlinerInstrType::Legal: | 759 case TargetInstrInfo::MachineOutlinerInstrType::Legal: |
774 static char ID; | 798 static char ID; |
775 | 799 |
776 /// \brief Set to true if the outliner should consider functions with | 800 /// \brief Set to true if the outliner should consider functions with |
777 /// linkonceodr linkage. | 801 /// linkonceodr linkage. |
778 bool OutlineFromLinkOnceODRs = false; | 802 bool OutlineFromLinkOnceODRs = false; |
803 | |
804 // Collection of IR functions created by the outliner. | |
805 std::vector<Function *> CreatedIRFunctions; | |
779 | 806 |
780 StringRef getPassName() const override { return "Machine Outliner"; } | 807 StringRef getPassName() const override { return "Machine Outliner"; } |
781 | 808 |
782 void getAnalysisUsage(AnalysisUsage &AU) const override { | 809 void getAnalysisUsage(AnalysisUsage &AU) const override { |
783 AU.addRequired<MachineModuleInfo>(); | 810 AU.addRequired<MachineModuleInfo>(); |
937 // Figure out the call overhead for each instance of the sequence. | 964 // Figure out the call overhead for each instance of the sequence. |
938 for (auto &ChildPair : Parent.Children) { | 965 for (auto &ChildPair : Parent.Children) { |
939 SuffixTreeNode *M = ChildPair.second; | 966 SuffixTreeNode *M = ChildPair.second; |
940 | 967 |
941 if (M && M->IsInTree && M->isLeaf()) { | 968 if (M && M->IsInTree && M->isLeaf()) { |
942 // Each sequence is over [StartIt, EndIt]. | |
943 MachineBasicBlock::iterator StartIt = Mapper.InstrList[M->SuffixIdx]; | |
944 MachineBasicBlock::iterator EndIt = | |
945 Mapper.InstrList[M->SuffixIdx + StringLen - 1]; | |
946 | |
947 CandidatesForRepeatedSeq.emplace_back(M->SuffixIdx, StringLen, | |
948 FunctionList.size()); | |
949 RepeatedSequenceLocs.emplace_back(std::make_pair(StartIt, EndIt)); | |
950 | |
951 // Never visit this leaf again. | 969 // Never visit this leaf again. |
952 M->IsInTree = false; | 970 M->IsInTree = false; |
971 unsigned StartIdx = M->SuffixIdx; | |
972 unsigned EndIdx = StartIdx + StringLen - 1; | |
973 | |
974 // Trick: Discard some candidates that would be incompatible with the | |
975 // ones we've already found for this sequence. This will save us some | |
976 // work in candidate selection. | |
977 // | |
978 // If two candidates overlap, then we can't outline them both. This | |
979 // happens when we have candidates that look like, say | |
980 // | |
981 // AA (where each "A" is an instruction). | |
982 // | |
983 // We might have some portion of the module that looks like this: | |
984 // AAAAAA (6 A's) | |
985 // | |
986 // In this case, there are 5 different copies of "AA" in this range, but | |
987 // at most 3 can be outlined. If only outlining 3 of these is going to | |
988 // be unbeneficial, then we ought to not bother. | |
989 // | |
990 // Note that two things DON'T overlap when they look like this: | |
991 // start1...end1 .... start2...end2 | |
992 // That is, one must either | |
993 // * End before the other starts | |
994 // * Start after the other ends | |
995 if (std::all_of(CandidatesForRepeatedSeq.begin(), | |
996 CandidatesForRepeatedSeq.end(), | |
997 [&StartIdx, &EndIdx](const Candidate &C) { | |
998 return (EndIdx < C.getStartIdx() || | |
999 StartIdx > C.getEndIdx()); | |
1000 })) { | |
1001 // It doesn't overlap with anything, so we can outline it. | |
1002 // Each sequence is over [StartIt, EndIt]. | |
1003 MachineBasicBlock::iterator StartIt = Mapper.InstrList[StartIdx]; | |
1004 MachineBasicBlock::iterator EndIt = Mapper.InstrList[EndIdx]; | |
1005 | |
1006 // Save the MachineFunction containing the Candidate. | |
1007 MachineFunction *MF = StartIt->getParent()->getParent(); | |
1008 assert(MF && "Candidate doesn't have a MF?"); | |
1009 | |
1010 // Save the candidate and its location. | |
1011 CandidatesForRepeatedSeq.emplace_back(StartIdx, StringLen, | |
1012 FunctionList.size(), MF); | |
1013 RepeatedSequenceLocs.emplace_back(std::make_pair(StartIt, EndIt)); | |
1014 } | |
953 } | 1015 } |
954 } | 1016 } |
955 | 1017 |
956 // We've found something we might want to outline. | 1018 // We've found something we might want to outline. |
957 // Create an OutlinedFunction to store it and check if it'd be beneficial | 1019 // Create an OutlinedFunction to store it and check if it'd be beneficial |
959 TargetInstrInfo::MachineOutlinerInfo MInfo = | 1021 TargetInstrInfo::MachineOutlinerInfo MInfo = |
960 TII.getOutlininingCandidateInfo(RepeatedSequenceLocs); | 1022 TII.getOutlininingCandidateInfo(RepeatedSequenceLocs); |
961 std::vector<unsigned> Seq; | 1023 std::vector<unsigned> Seq; |
962 for (unsigned i = Leaf->SuffixIdx; i < Leaf->SuffixIdx + StringLen; i++) | 1024 for (unsigned i = Leaf->SuffixIdx; i < Leaf->SuffixIdx + StringLen; i++) |
963 Seq.push_back(ST.Str[i]); | 1025 Seq.push_back(ST.Str[i]); |
964 OutlinedFunction OF(FunctionList.size(), Parent.OccurrenceCount, Seq, | 1026 OutlinedFunction OF(FunctionList.size(), CandidatesForRepeatedSeq.size(), |
965 MInfo); | 1027 Seq, MInfo); |
966 unsigned Benefit = OF.getBenefit(); | 1028 unsigned Benefit = OF.getBenefit(); |
967 | 1029 |
968 // Is it better to outline this candidate than not? | 1030 // Is it better to outline this candidate than not? |
969 if (Benefit < 1) { | 1031 if (Benefit < 1) { |
970 // Outlining this candidate would take more instructions than not | 1032 // Outlining this candidate would take more instructions than not |
1089 | 1151 |
1090 // Either the index is 0, or it's at most MaxCandidateLen indices away. | 1152 // Either the index is 0, or it's at most MaxCandidateLen indices away. |
1091 if (C1.getStartIdx() > MaxCandidateLen) | 1153 if (C1.getStartIdx() > MaxCandidateLen) |
1092 FarthestPossibleIdx = C1.getStartIdx() - MaxCandidateLen; | 1154 FarthestPossibleIdx = C1.getStartIdx() - MaxCandidateLen; |
1093 | 1155 |
1094 // Compare against the candidates in the list that start at at most | 1156 // Compare against the candidates in the list that start at most |
1095 // FarthestPossibleIdx indices away from C1. There are at most | 1157 // FarthestPossibleIdx indices away from C1. There are at most |
1096 // MaxCandidateLen of these. | 1158 // MaxCandidateLen of these. |
1097 for (auto Sit = It + 1; Sit != Et; Sit++) { | 1159 for (auto Sit = It + 1; Sit != Et; Sit++) { |
1098 Candidate &C2 = **Sit; | 1160 Candidate &C2 = **Sit; |
1099 | 1161 |
1178 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping | 1240 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping |
1179 // which gives us better results when we outline from linkonceodr functions. | 1241 // which gives us better results when we outline from linkonceodr functions. |
1180 F->setLinkage(GlobalValue::PrivateLinkage); | 1242 F->setLinkage(GlobalValue::PrivateLinkage); |
1181 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); | 1243 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
1182 | 1244 |
1245 // Save F so that we can add debug info later if we need to. | |
1246 CreatedIRFunctions.push_back(F); | |
1247 | |
1183 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F); | 1248 BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F); |
1184 IRBuilder<> Builder(EntryBB); | 1249 IRBuilder<> Builder(EntryBB); |
1185 Builder.CreateRetVoid(); | 1250 Builder.CreateRetVoid(); |
1186 | 1251 |
1187 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); | 1252 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
1201 MachineInstr *NewMI = | 1266 MachineInstr *NewMI = |
1202 MF.CloneMachineInstr(Mapper.IntegerInstructionMap.find(Str)->second); | 1267 MF.CloneMachineInstr(Mapper.IntegerInstructionMap.find(Str)->second); |
1203 NewMI->dropMemRefs(); | 1268 NewMI->dropMemRefs(); |
1204 | 1269 |
1205 // Don't keep debug information for outlined instructions. | 1270 // Don't keep debug information for outlined instructions. |
1206 // FIXME: This means outlined functions are currently undebuggable. | |
1207 NewMI->setDebugLoc(DebugLoc()); | 1271 NewMI->setDebugLoc(DebugLoc()); |
1208 MBB.insert(MBB.end(), NewMI); | 1272 MBB.insert(MBB.end(), NewMI); |
1209 } | 1273 } |
1210 | 1274 |
1211 TII.insertOutlinerEpilogue(MBB, MF, OF.MInfo); | 1275 TII.insertOutlinerEpilogue(MBB, MF, OF.MInfo); |
1212 | 1276 |
1277 // If there's a DISubprogram associated with this outlined function, then | |
1278 // emit debug info for the outlined function. | |
1279 if (DISubprogram *SP = OF.getSubprogramOrNull()) { | |
1280 // We have a DISubprogram. Get its DICompileUnit. | |
1281 DICompileUnit *CU = SP->getUnit(); | |
1282 DIBuilder DB(M, true, CU); | |
1283 DIFile *Unit = SP->getFile(); | |
1284 Mangler Mg; | |
1285 | |
1286 // Walk over each IR function we created in the outliner and create | |
1287 // DISubprograms for each function. | |
1288 for (Function *F : CreatedIRFunctions) { | |
1289 // Get the mangled name of the function for the linkage name. | |
1290 std::string Dummy; | |
1291 llvm::raw_string_ostream MangledNameStream(Dummy); | |
1292 Mg.getNameWithPrefix(MangledNameStream, F, false); | |
1293 | |
1294 DISubprogram *SP = DB.createFunction( | |
1295 Unit /* Context */, F->getName(), StringRef(MangledNameStream.str()), | |
1296 Unit /* File */, | |
1297 0 /* Line 0 is reserved for compiler-generated code. */, | |
1298 DB.createSubroutineType( | |
1299 DB.getOrCreateTypeArray(None)), /* void type */ | |
1300 false, true, 0, /* Line 0 is reserved for compiler-generated code. */ | |
1301 DINode::DIFlags::FlagArtificial /* Compiler-generated code. */, | |
1302 true /* Outlined code is optimized code by definition. */); | |
1303 | |
1304 // Don't add any new variables to the subprogram. | |
1305 DB.finalizeSubprogram(SP); | |
1306 | |
1307 // Attach subprogram to the function. | |
1308 F->setSubprogram(SP); | |
1309 } | |
1310 | |
1311 // We're done with the DIBuilder. | |
1312 DB.finalize(); | |
1313 } | |
1314 | |
1315 MF.getRegInfo().freezeReservedRegs(MF); | |
1213 return &MF; | 1316 return &MF; |
1214 } | 1317 } |
1215 | 1318 |
1216 bool MachineOutliner::outline( | 1319 bool MachineOutliner::outline( |
1217 Module &M, const ArrayRef<std::shared_ptr<Candidate>> &CandidateList, | 1320 Module &M, const ArrayRef<std::shared_ptr<Candidate>> &CandidateList, |
1311 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); | 1414 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
1312 const TargetSubtargetInfo &STI = | 1415 const TargetSubtargetInfo &STI = |
1313 MMI.getOrCreateMachineFunction(*M.begin()).getSubtarget(); | 1416 MMI.getOrCreateMachineFunction(*M.begin()).getSubtarget(); |
1314 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); | 1417 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); |
1315 const TargetInstrInfo *TII = STI.getInstrInfo(); | 1418 const TargetInstrInfo *TII = STI.getInstrInfo(); |
1316 | 1419 |
1317 InstructionMapper Mapper; | 1420 InstructionMapper Mapper; |
1318 | 1421 |
1319 // Build instruction mappings for each function in the module. | 1422 // Build instruction mappings for each function in the module. |
1320 for (Function &F : M) { | 1423 for (Function &F : M) { |
1321 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); | 1424 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); |
1326 continue; | 1429 continue; |
1327 | 1430 |
1328 // If it is, look at each MachineBasicBlock in the function. | 1431 // If it is, look at each MachineBasicBlock in the function. |
1329 for (MachineBasicBlock &MBB : MF) { | 1432 for (MachineBasicBlock &MBB : MF) { |
1330 | 1433 |
1331 // Is there anything in MBB? | 1434 // Is there anything in MBB? And is it the target of an indirect branch? |
1332 if (MBB.empty()) | 1435 if (MBB.empty() || MBB.hasAddressTaken()) |
1333 continue; | 1436 continue; |
1334 | 1437 |
1335 // If yes, map it. | 1438 // If yes, map it. |
1336 Mapper.convertToUnsignedVec(MBB, *TRI, *TII); | 1439 Mapper.convertToUnsignedVec(MBB, *TRI, *TII); |
1337 } | 1440 } |
1348 | 1451 |
1349 // Remove candidates that overlap with other candidates. | 1452 // Remove candidates that overlap with other candidates. |
1350 pruneOverlaps(CandidateList, FunctionList, Mapper, MaxCandidateLen, *TII); | 1453 pruneOverlaps(CandidateList, FunctionList, Mapper, MaxCandidateLen, *TII); |
1351 | 1454 |
1352 // Outline each of the candidates and return true if something was outlined. | 1455 // Outline each of the candidates and return true if something was outlined. |
1353 return outline(M, CandidateList, FunctionList, Mapper); | 1456 bool OutlinedSomething = outline(M, CandidateList, FunctionList, Mapper); |
1457 | |
1458 return OutlinedSomething; | |
1354 } | 1459 } |