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