* regcache.h (struct thread_info): Forward declare.
[binutils-gdb.git] / gold / icf.h
blobe3365721055d0af1a1daa52f5034744583f46c40
1 // icf.h -- Identical Code Folding
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Sriraman Tallam <tmsriram@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #ifndef GOLD_ICF_H
24 #define GOLD_ICF_H
26 #include <vector>
28 #include "elfcpp.h"
29 #include "symtab.h"
30 #include "object.h"
32 namespace gold
35 class Object;
36 class Input_objects;
37 class Symbol_table;
39 class Icf
41 public:
42 typedef std::vector<Section_id> Sections_reachable_list;
43 typedef std::vector<Symbol*> Symbol_info;
44 typedef std::vector<std::pair<long long, long long> > Addend_info;
45 typedef Unordered_map<Section_id,
46 Sections_reachable_list,
47 Section_id_hash> Section_list;
48 typedef Unordered_map<Section_id, Symbol_info, Section_id_hash> Symbol_list;
49 typedef Unordered_map<Section_id, Addend_info, Section_id_hash> Addend_list;
50 typedef Unordered_map<Section_id,
51 unsigned int,
52 Section_id_hash> Uniq_secn_id_map;
54 Icf()
55 : id_section_(), section_id_(), kept_section_id_(),
56 num_tracked_relocs(NULL), icf_ready_(false),
57 section_reloc_list_(), symbol_reloc_list_(),
58 addend_reloc_list_()
59 { }
61 // Returns the kept folded identical section corresponding to
62 // dup_obj and dup_shndx.
63 Section_id
64 get_folded_section(Object* dup_obj, unsigned int dup_shndx);
66 // Forms groups of identical sections where the first member
67 // of each group is the kept section during folding.
68 void
69 find_identical_sections(const Input_objects* input_objects,
70 Symbol_table* symtab);
72 // This is set when ICF has been run and the groups of
73 // identical sections have been formed.
74 void
75 icf_ready()
76 { this->icf_ready_ = true; }
78 // Returns true if ICF has been run.
79 bool
80 is_icf_ready()
81 { return this->icf_ready_; }
83 // Unfolds the section denoted by OBJ and SHNDX if folded.
84 void
85 unfold_section(Object* obj, unsigned int shndx);
87 // Returns the kept section corresponding to the
88 // given section.
89 bool
90 is_section_folded(Object* obj, unsigned int shndx);
92 // Returns a map of a section to a list of all sections referenced
93 // by its relocations.
94 Section_list&
95 section_reloc_list()
96 { return this->section_reloc_list_; }
98 // Returns a map of a section to a list of all symbols referenced
99 // by its relocations.
100 Symbol_list&
101 symbol_reloc_list()
102 { return this->symbol_reloc_list_; }
104 // Returns a maps of a section to a list of symbol values and addends
105 // of its relocations.
106 Addend_list&
107 addend_reloc_list()
108 { return this->addend_reloc_list_; }
110 // Returns a mapping of each section to a unique integer.
111 Uniq_secn_id_map&
112 section_to_int_map()
113 { return this->section_id_; }
115 private:
117 // Maps integers to sections.
118 std::vector<Section_id> id_section_;
119 // Does the reverse.
120 Uniq_secn_id_map section_id_;
121 // Given a section id, this maps it to the id of the kept
122 // section. If the id's are the same then this section is
123 // not folded.
124 std::vector<unsigned int> kept_section_id_;
125 unsigned int* num_tracked_relocs;
126 // Flag to indicate if ICF has been run.
127 bool icf_ready_;
129 // These lists are populated by gc_process_relocs in gc.h.
130 Section_list section_reloc_list_;
131 Symbol_list symbol_reloc_list_;
132 Addend_list addend_reloc_list_;
135 // This function returns true if this section corresponds to a function that
136 // should be considered by icf as a possible candidate for folding. Some
137 // earlier gcc versions, like 4.0.3, put constructors and destructors in
138 // .gnu.linkonce.t sections and hence should be included too.
139 inline bool
140 is_section_foldable_candidate(const char* section_name)
142 return (is_prefix_of(".text", section_name)
143 || is_prefix_of(".gnu.linkonce.t", section_name));
146 } // End of namespace gold.
148 #endif