1 //===- IdenticalCodeFolding.h ---------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_LD_IDENTICALCODEFOLDING_H_ 10 #define MCLD_LD_IDENTICALCODEFOLDING_H_ 11 12 #include <llvm/ADT/MapVector.h> 13 14 #include <string> 15 #include <vector> 16 17 namespace mcld { 18 19 class Input; 20 class LDSection; 21 class LinkerConfig; 22 class Module; 23 class Relocation; 24 class TargetLDBackend; 25 26 /** \class IdenticalCodeFolding 27 * \brief Implementation of identical code folding for --icf=[none|all|safe] 28 * @ref Safe ICF: Pointer Safe and Unwinding Aware Identical Code Folding in 29 * Gold, http://research.google.com/pubs/pub36912.html 30 */ 31 class IdenticalCodeFolding { 32 public: 33 typedef std::pair<Input*, size_t> ObjectAndId; 34 typedef llvm::MapVector<LDSection*, ObjectAndId> KeptSections; 35 36 private: 37 class FoldingCandidate { 38 public: FoldingCandidate()39 FoldingCandidate() : sect(NULL), reloc_sect(NULL), obj(NULL) {} FoldingCandidate(LDSection * pCode,LDSection * pReloc,Input * pInput)40 FoldingCandidate(LDSection* pCode, LDSection* pReloc, Input* pInput) 41 : sect(pCode), reloc_sect(pReloc), obj(pInput) {} 42 43 void initConstantContent( 44 const TargetLDBackend& pBackend, 45 const IdenticalCodeFolding::KeptSections& pKeptSections); 46 std::string getContentWithVariables( 47 const TargetLDBackend& pBackend, 48 const IdenticalCodeFolding::KeptSections& pKeptSections); 49 50 LDSection* sect; 51 LDSection* reloc_sect; 52 Input* obj; 53 std::string content; 54 std::vector<Relocation*> variable_relocs; 55 }; 56 57 typedef std::vector<FoldingCandidate> FoldingCandidates; 58 59 public: 60 IdenticalCodeFolding(const LinkerConfig& pConfig, 61 const TargetLDBackend& pBackend, 62 Module& pModule); 63 64 void foldIdenticalCode(); 65 66 private: 67 void findCandidates(FoldingCandidates& pCandidateList); 68 69 bool matchCandidates(FoldingCandidates& pCandidateList); 70 71 private: 72 const LinkerConfig& m_Config; 73 const TargetLDBackend& m_Backend; 74 Module& m_Module; 75 KeptSections m_KeptSections; 76 }; 77 78 } // namespace mcld 79 80 #endif // MCLD_LD_IDENTICALCODEFOLDING_H_ 81