2009-11-13 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / icf.cc
blob03b927adc40de763a486d85395cab9ffddd7fdc9
1 // icf.cc -- Identical Code Folding.
2 //
3 // Copyright 2009 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 // Identical Code Folding Algorithm
24 // ----------------------------------
25 // Detecting identical functions is done here and the basic algorithm
26 // is as follows. A checksum is computed on each .text section using
27 // its contents and relocations. If the symbol name corresponding to
28 // a relocation is known it is used to compute the checksum. If the
29 // symbol name is not known the stringified name of the object and the
30 // section number pointed to by the relocation is used. The checksums
31 // are stored as keys in a hash map and a section is identical to some
32 // other section if its checksum is already present in the hash map.
33 // Checksum collisions are handled by using a multimap and explicitly
34 // checking the contents when two sections have the same checksum.
36 // However, two functions A and B with identical text but with
37 // relocations pointing to different .text sections can be identical if
38 // the corresponding .text sections to which their relocations point to
39 // turn out to be identical. Hence, this checksumming process must be
40 // done repeatedly until convergence is obtained. Here is an example for
41 // the following case :
43 // int funcA () int funcB ()
44 // { {
45 // return foo(); return goo();
46 // } }
48 // The functions funcA and funcB are identical if functions foo() and
49 // goo() are identical.
51 // Hence, as described above, we repeatedly do the checksumming,
52 // assigning identical functions to the same group, until convergence is
53 // obtained. Now, we have two different ways to do this depending on how
54 // we initialize.
56 // Algorithm I :
57 // -----------
58 // We can start with marking all functions as different and repeatedly do
59 // the checksumming. This has the advantage that we do not need to wait
60 // for convergence. We can stop at any point and correctness will be
61 // guaranteed although not all cases would have been found. However, this
62 // has a problem that some cases can never be found even if it is run until
63 // convergence. Here is an example with mutually recursive functions :
65 // int funcA (int a) int funcB (int a)
66 // { {
67 // if (a == 1) if (a == 1)
68 // return 1; return 1;
69 // return 1 + funcB(a - 1); return 1 + funcA(a - 1);
70 // } }
72 // In this example funcA and funcB are identical and one of them could be
73 // folded into the other. However, if we start with assuming that funcA
74 // and funcB are not identical, the algorithm, even after it is run to
75 // convergence, cannot detect that they are identical. It should be noted
76 // that even if the functions were self-recursive, Algorithm I cannot catch
77 // that they are identical, at least as is.
79 // Algorithm II :
80 // ------------
81 // Here we start with marking all functions as identical and then repeat
82 // the checksumming until convergence. This can detect the above case
83 // mentioned above. It can detect all cases that Algorithm I can and more.
84 // However, the caveat is that it has to be run to convergence. It cannot
85 // be stopped arbitrarily like Algorithm I as correctness cannot be
86 // guaranteed. Algorithm II is not implemented.
88 // Algorithm I is used because experiments show that about three
89 // iterations are more than enough to achieve convergence. Algorithm I can
90 // handle recursive calls if it is changed to use a special common symbol
91 // for recursive relocs. This seems to be the most common case that
92 // Algorithm I could not catch as is. Mutually recursive calls are not
93 // frequent and Algorithm I wins because of its ability to be stopped
94 // arbitrarily.
96 // Caveat with using function pointers :
97 // ------------------------------------
99 // Programs using function pointer comparisons/checks should use function
100 // folding with caution as the result of such comparisons could be different
101 // when folding takes place. This could lead to unexpected run-time
102 // behaviour.
105 // How to run : --icf
106 // Optional parameters : --icf-iterations <num> --print-icf-sections
108 // Performance : Less than 20 % link-time overhead on industry strength
109 // applications. Up to 6 % text size reductions.
111 #include "gold.h"
112 #include "object.h"
113 #include "gc.h"
114 #include "icf.h"
115 #include "symtab.h"
116 #include "libiberty.h"
117 #include "demangle.h"
119 namespace gold
122 // This function determines if a section or a group of identical
123 // sections has unique contents. Such unique sections or groups can be
124 // declared final and need not be processed any further.
125 // Parameters :
126 // ID_SECTION : Vector mapping a section index to a Section_id pair.
127 // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical
128 // sections is already known to be unique.
129 // SECTION_CONTENTS : Contains the section's text and relocs to sections
130 // that cannot be folded. SECTION_CONTENTS are NULL
131 // implies that this function is being called for the
132 // first time before the first iteration of icf.
134 static void
135 preprocess_for_unique_sections(const std::vector<Section_id>& id_section,
136 std::vector<bool>* is_secn_or_group_unique,
137 std::vector<std::string>* section_contents)
139 Unordered_map<uint32_t, unsigned int> uniq_map;
140 std::pair<Unordered_map<uint32_t, unsigned int>::iterator, bool>
141 uniq_map_insert;
143 for (unsigned int i = 0; i < id_section.size(); i++)
145 if ((*is_secn_or_group_unique)[i])
146 continue;
148 uint32_t cksum;
149 Section_id secn = id_section[i];
150 section_size_type plen;
151 if (section_contents == NULL)
153 const unsigned char* contents;
154 contents = secn.first->section_contents(secn.second,
155 &plen,
156 false);
157 cksum = xcrc32(contents, plen, 0xffffffff);
159 else
161 const unsigned char* contents_array = reinterpret_cast
162 <const unsigned char*>((*section_contents)[i].c_str());
163 cksum = xcrc32(contents_array, (*section_contents)[i].length(),
164 0xffffffff);
166 uniq_map_insert = uniq_map.insert(std::make_pair(cksum, i));
167 if (uniq_map_insert.second)
169 (*is_secn_or_group_unique)[i] = true;
171 else
173 (*is_secn_or_group_unique)[i] = false;
174 (*is_secn_or_group_unique)[uniq_map_insert.first->second] = false;
179 // This returns the buffer containing the section's contents, both
180 // text and relocs. Relocs are differentiated as those pointing to
181 // sections that could be folded and those that cannot. Only relocs
182 // pointing to sections that could be folded are recomputed on
183 // subsequent invocations of this function.
184 // Parameters :
185 // FIRST_ITERATION : true if it is the first invocation.
186 // SECN : Section for which contents are desired.
187 // SECTION_NUM : Unique section number of this section.
188 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
189 // to ICF sections.
190 // KEPT_SECTION_ID : Vector which maps folded sections to kept sections.
191 // SECTION_CONTENTS : Store the section's text and relocs to non-ICF
192 // sections.
194 static std::string
195 get_section_contents(bool first_iteration,
196 const Section_id& secn,
197 unsigned int section_num,
198 unsigned int* num_tracked_relocs,
199 Symbol_table* symtab,
200 const std::vector<unsigned int>& kept_section_id,
201 std::vector<std::string>* section_contents)
203 section_size_type plen;
204 const unsigned char* contents = NULL;
206 if (first_iteration)
208 contents = secn.first->section_contents(secn.second,
209 &plen,
210 false);
213 // The buffer to hold all the contents including relocs. A checksum
214 // is then computed on this buffer.
215 std::string buffer;
216 std::string icf_reloc_buffer;
218 if (num_tracked_relocs)
219 *num_tracked_relocs = 0;
221 Icf::Section_list& seclist = symtab->icf()->section_reloc_list();
222 Icf::Symbol_list& symlist = symtab->icf()->symbol_reloc_list();
223 Icf::Addend_list& addendlist = symtab->icf()->addend_reloc_list();
225 Icf::Section_list::iterator it_seclist = seclist.find(secn);
226 Icf::Symbol_list::iterator it_symlist = symlist.find(secn);
227 Icf::Addend_list::iterator it_addendlist = addendlist.find(secn);
229 buffer.clear();
230 icf_reloc_buffer.clear();
232 // Process relocs and put them into the buffer.
234 if (it_seclist != seclist.end())
236 gold_assert(it_symlist != symlist.end());
237 gold_assert(it_addendlist != addendlist.end());
238 Icf::Sections_reachable_list v = it_seclist->second;
239 Icf::Symbol_info s = it_symlist->second;
240 Icf::Addend_info a = it_addendlist->second;
241 Icf::Sections_reachable_list::iterator it_v = v.begin();
242 Icf::Symbol_info::iterator it_s = s.begin();
243 Icf::Addend_info::iterator it_a = a.begin();
245 for (; it_v != v.end(); ++it_v, ++it_s, ++it_a)
247 // ADDEND_STR stores the symbol value and addend, each
248 // atmost 16 hex digits long. it_v points to a pair
249 // where first is the symbol value and second is the
250 // addend.
251 char addend_str[34];
252 snprintf(addend_str, sizeof(addend_str), "%llx %llx",
253 (*it_a).first, (*it_a).second);
254 Section_id reloc_secn(it_v->first, it_v->second);
256 // If this reloc turns back and points to the same section,
257 // like a recursive call, use a special symbol to mark this.
258 if (reloc_secn.first == secn.first
259 && reloc_secn.second == secn.second)
261 if (first_iteration)
263 buffer.append("R");
264 buffer.append(addend_str);
265 buffer.append("@");
267 continue;
269 Icf::Uniq_secn_id_map& section_id_map =
270 symtab->icf()->section_to_int_map();
271 Icf::Uniq_secn_id_map::iterator section_id_map_it =
272 section_id_map.find(reloc_secn);
273 if (section_id_map_it != section_id_map.end())
275 // This is a reloc to a section that might be folded.
276 if (num_tracked_relocs)
277 (*num_tracked_relocs)++;
279 char kept_section_str[10];
280 unsigned int secn_id = section_id_map_it->second;
281 snprintf(kept_section_str, sizeof(kept_section_str), "%u",
282 kept_section_id[secn_id]);
283 if (first_iteration)
285 buffer.append("ICF_R");
286 buffer.append(addend_str);
288 icf_reloc_buffer.append(kept_section_str);
289 // Append the addend.
290 icf_reloc_buffer.append(addend_str);
291 icf_reloc_buffer.append("@");
293 else
295 // This is a reloc to a section that cannot be folded.
296 // Process it only in the first iteration.
297 if (!first_iteration)
298 continue;
300 uint64_t secn_flags = (it_v->first)->section_flags(it_v->second);
301 // This reloc points to a merge section. Hash the
302 // contents of this section.
303 if ((secn_flags & elfcpp::SHF_MERGE) != 0)
305 uint64_t entsize =
306 (it_v->first)->section_entsize(it_v->second);
307 long long offset = it_a->first + it_a->second;
308 section_size_type secn_len;
309 const unsigned char* str_contents =
310 (it_v->first)->section_contents(it_v->second,
311 &secn_len,
312 false) + offset;
313 if ((secn_flags & elfcpp::SHF_STRINGS) != 0)
315 // String merge section.
316 const char* str_char =
317 reinterpret_cast<const char*>(str_contents);
318 switch(entsize)
320 case 1:
322 buffer.append(str_char);
323 break;
325 case 2:
327 const uint16_t* ptr_16 =
328 reinterpret_cast<const uint16_t*>(str_char);
329 unsigned int strlen_16 = 0;
330 // Find the NULL character.
331 while(*(ptr_16 + strlen_16) != 0)
332 strlen_16++;
333 buffer.append(str_char, strlen_16 * 2);
335 break;
336 case 4:
338 const uint32_t* ptr_32 =
339 reinterpret_cast<const uint32_t*>(str_char);
340 unsigned int strlen_32 = 0;
341 // Find the NULL character.
342 while(*(ptr_32 + strlen_32) != 0)
343 strlen_32++;
344 buffer.append(str_char, strlen_32 * 4);
346 break;
347 default:
348 gold_unreachable();
351 else
353 // Use the entsize to determine the length.
354 buffer.append(reinterpret_cast<const
355 char*>(str_contents),
356 entsize);
359 else if ((*it_s) != NULL)
361 // If symbol name is available use that.
362 const char *sym_name = (*it_s)->name();
363 buffer.append(sym_name);
364 // Append the addend.
365 buffer.append(addend_str);
366 buffer.append("@");
368 else
370 // Symbol name is not available, like for a local symbol,
371 // use object and section id.
372 buffer.append(it_v->first->name());
373 char secn_id[10];
374 snprintf(secn_id, sizeof(secn_id), "%u",it_v->second);
375 buffer.append(secn_id);
376 // Append the addend.
377 buffer.append(addend_str);
378 buffer.append("@");
384 if (first_iteration)
386 buffer.append("Contents = ");
387 buffer.append(reinterpret_cast<const char*>(contents), plen);
388 // Store the section contents that dont change to avoid recomputing
389 // during the next call to this function.
390 (*section_contents)[section_num] = buffer;
392 else
394 gold_assert(buffer.empty());
395 // Reuse the contents computed in the previous iteration.
396 buffer.append((*section_contents)[section_num]);
399 buffer.append(icf_reloc_buffer);
400 return buffer;
403 // This function computes a checksum on each section to detect and form
404 // groups of identical sections. The first iteration does this for all
405 // sections.
406 // Further iterations do this only for the kept sections from each group to
407 // determine if larger groups of identical sections could be formed. The
408 // first section in each group is the kept section for that group.
410 // CRC32 is the checksumming algorithm and can have collisions. That is,
411 // two sections with different contents can have the same checksum. Hence,
412 // a multimap is used to maintain more than one group of checksum
413 // identical sections. A section is added to a group only after its
414 // contents are explicitly compared with the kept section of the group.
416 // Parameters :
417 // ITERATION_NUM : Invocation instance of this function.
418 // NUM_TRACKED_RELOCS : Vector reference to store the number of relocs
419 // to ICF sections.
420 // KEPT_SECTION_ID : Vector which maps folded sections to kept sections.
421 // ID_SECTION : Vector mapping a section to an unique integer.
422 // IS_SECN_OR_GROUP_UNIQUE : To check if a section or a group of identical
423 // sectionsis already known to be unique.
424 // SECTION_CONTENTS : Store the section's text and relocs to non-ICF
425 // sections.
427 static bool
428 match_sections(unsigned int iteration_num,
429 Symbol_table* symtab,
430 std::vector<unsigned int>* num_tracked_relocs,
431 std::vector<unsigned int>* kept_section_id,
432 const std::vector<Section_id>& id_section,
433 std::vector<bool>* is_secn_or_group_unique,
434 std::vector<std::string>* section_contents)
436 Unordered_multimap<uint32_t, unsigned int> section_cksum;
437 std::pair<Unordered_multimap<uint32_t, unsigned int>::iterator,
438 Unordered_multimap<uint32_t, unsigned int>::iterator> key_range;
439 bool converged = true;
441 if (iteration_num == 1)
442 preprocess_for_unique_sections(id_section,
443 is_secn_or_group_unique,
444 NULL);
445 else
446 preprocess_for_unique_sections(id_section,
447 is_secn_or_group_unique,
448 section_contents);
450 std::vector<std::string> full_section_contents;
452 for (unsigned int i = 0; i < id_section.size(); i++)
454 full_section_contents.push_back("");
455 if ((*is_secn_or_group_unique)[i])
456 continue;
458 Section_id secn = id_section[i];
459 std::string this_secn_contents;
460 uint32_t cksum;
461 if (iteration_num == 1)
463 unsigned int num_relocs = 0;
464 this_secn_contents = get_section_contents(true, secn, i, &num_relocs,
465 symtab, (*kept_section_id),
466 section_contents);
467 (*num_tracked_relocs)[i] = num_relocs;
469 else
471 if ((*kept_section_id)[i] != i)
473 // This section is already folded into something. See
474 // if it should point to a different kept section.
475 unsigned int kept_section = (*kept_section_id)[i];
476 if (kept_section != (*kept_section_id)[kept_section])
478 (*kept_section_id)[i] = (*kept_section_id)[kept_section];
480 continue;
482 this_secn_contents = get_section_contents(false, secn, i, NULL,
483 symtab, (*kept_section_id),
484 section_contents);
487 const unsigned char* this_secn_contents_array =
488 reinterpret_cast<const unsigned char*>(this_secn_contents.c_str());
489 cksum = xcrc32(this_secn_contents_array, this_secn_contents.length(),
490 0xffffffff);
491 size_t count = section_cksum.count(cksum);
493 if (count == 0)
495 // Start a group with this cksum.
496 section_cksum.insert(std::make_pair(cksum, i));
497 full_section_contents[i] = this_secn_contents;
499 else
501 key_range = section_cksum.equal_range(cksum);
502 Unordered_multimap<uint32_t, unsigned int>::iterator it;
503 // Search all the groups with this cksum for a match.
504 for (it = key_range.first; it != key_range.second; ++it)
506 unsigned int kept_section = it->second;
507 if (full_section_contents[kept_section].length()
508 != this_secn_contents.length())
509 continue;
510 if (memcmp(full_section_contents[kept_section].c_str(),
511 this_secn_contents.c_str(),
512 this_secn_contents.length()) != 0)
513 continue;
514 (*kept_section_id)[i] = kept_section;
515 converged = false;
516 break;
518 if (it == key_range.second)
520 // Create a new group for this cksum.
521 section_cksum.insert(std::make_pair(cksum, i));
522 full_section_contents[i] = this_secn_contents;
525 // If there are no relocs to foldable sections do not process
526 // this section any further.
527 if (iteration_num == 1 && (*num_tracked_relocs)[i] == 0)
528 (*is_secn_or_group_unique)[i] = true;
531 return converged;
534 // During safe icf (--icf=safe), only fold functions that are ctors or dtors.
535 // This function returns true if the mangled function name is a ctor or a
536 // dtor.
538 static bool
539 is_function_ctor_or_dtor(const char* mangled_func_name)
541 if ((is_prefix_of("_ZN", mangled_func_name)
542 || is_prefix_of("_ZZ", mangled_func_name))
543 && (is_gnu_v3_mangled_ctor(mangled_func_name)
544 || is_gnu_v3_mangled_dtor(mangled_func_name)))
546 return true;
548 return false;
551 // This is the main ICF function called in gold.cc. This does the
552 // initialization and calls match_sections repeatedly (twice by default)
553 // which computes the crc checksums and detects identical functions.
555 void
556 Icf::find_identical_sections(const Input_objects* input_objects,
557 Symbol_table* symtab)
559 unsigned int section_num = 0;
560 std::vector<unsigned int> num_tracked_relocs;
561 std::vector<bool> is_secn_or_group_unique;
562 std::vector<std::string> section_contents;
564 // Decide which sections are possible candidates first.
566 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
567 p != input_objects->relobj_end();
568 ++p)
570 for (unsigned int i = 0;i < (*p)->shnum(); ++i)
572 const char* section_name = (*p)->section_name(i).c_str();
573 // Only looking to fold functions, so just look at .text sections.
574 if (!is_prefix_of(".text.", section_name))
575 continue;
576 if (!(*p)->is_section_included(i))
577 continue;
578 if (parameters->options().gc_sections()
579 && symtab->gc()->is_section_garbage(*p, i))
580 continue;
581 // With --icf=safe, check if mangled name is a ctor or a dtor.
582 if (parameters->options().icf_safe_folding()
583 && !is_function_ctor_or_dtor(section_name + 6))
584 continue;
585 this->id_section_.push_back(Section_id(*p, i));
586 this->section_id_[Section_id(*p, i)] = section_num;
587 this->kept_section_id_.push_back(section_num);
588 num_tracked_relocs.push_back(0);
589 is_secn_or_group_unique.push_back(false);
590 section_contents.push_back("");
591 section_num++;
595 unsigned int num_iterations = 0;
597 // Default number of iterations to run ICF is 2.
598 unsigned int max_iterations = (parameters->options().icf_iterations() > 0)
599 ? parameters->options().icf_iterations()
600 : 2;
602 bool converged = false;
604 while (!converged && (num_iterations < max_iterations))
606 num_iterations++;
607 converged = match_sections(num_iterations, symtab,
608 &num_tracked_relocs, &this->kept_section_id_,
609 this->id_section_, &is_secn_or_group_unique,
610 &section_contents);
613 if (parameters->options().print_icf_sections())
615 if (converged)
616 gold_info(_("%s: ICF Converged after %u iteration(s)"),
617 program_name, num_iterations);
618 else
619 gold_info(_("%s: ICF stopped after %u iteration(s)"),
620 program_name, num_iterations);
623 // Unfold --keep-unique symbols.
624 for (options::String_set::const_iterator p =
625 parameters->options().keep_unique_begin();
626 p != parameters->options().keep_unique_end();
627 ++p)
629 const char* name = p->c_str();
630 Symbol* sym = symtab->lookup(name);
631 if (sym == NULL)
633 gold_warning(_("Could not find symbol %s to unfold\n"), name);
635 else if (sym->source() == Symbol::FROM_OBJECT
636 && !sym->object()->is_dynamic())
638 Object* obj = sym->object();
639 bool is_ordinary;
640 unsigned int shndx = sym->shndx(&is_ordinary);
641 if (is_ordinary)
643 this->unfold_section(obj, shndx);
649 this->icf_ready();
652 // Unfolds the section denoted by OBJ and SHNDX if folded.
654 void
655 Icf::unfold_section(Object* obj, unsigned int shndx)
657 Section_id secn(obj, shndx);
658 Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
659 if (it == this->section_id_.end())
660 return;
661 unsigned int section_num = it->second;
662 unsigned int kept_section_id = this->kept_section_id_[section_num];
663 if (kept_section_id != section_num)
664 this->kept_section_id_[section_num] = section_num;
667 // This function determines if the section corresponding to the
668 // given object and index is folded based on if the kept section
669 // is different from this section.
671 bool
672 Icf::is_section_folded(Object* obj, unsigned int shndx)
674 Section_id secn(obj, shndx);
675 Uniq_secn_id_map::iterator it = this->section_id_.find(secn);
676 if (it == this->section_id_.end())
677 return false;
678 unsigned int section_num = it->second;
679 unsigned int kept_section_id = this->kept_section_id_[section_num];
680 return kept_section_id != section_num;
683 // This function returns the folded section for the given section.
685 Section_id
686 Icf::get_folded_section(Object* dup_obj, unsigned int dup_shndx)
688 Section_id dup_secn(dup_obj, dup_shndx);
689 Uniq_secn_id_map::iterator it = this->section_id_.find(dup_secn);
690 gold_assert(it != this->section_id_.end());
691 unsigned int section_num = it->second;
692 unsigned int kept_section_id = this->kept_section_id_[section_num];
693 Section_id folded_section = this->id_section_[kept_section_id];
694 return folded_section;
697 } // End of namespace gold.