+dtneeded*
[massrebuild.git] / dwarfredundant
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 $|=1;
5 my $quiet=shift @ARGV if ($ARGV[0]||"") eq "-q";
6 for my $argv (@ARGV) {
7   local *F;
8   open F,"llvm-dwarfdump --color=0 --verbose --debug-info '$argv'|" or die;
9   my $redundant;
10   my $redundant_text;
11   my $redundant_addr;
12   my $redundant_children;
13   my $die_addr;
14   my $die_text;
15   my $saved_total=0;
16   my $emptyline;
17   sub null($) {
18     my($addr)=@_;
19     die if !$redundant;
20     my $saved=eval($addr)+1-eval($redundant_addr);
21     $saved_total+=$saved;
22     print "saved=$saved:\n${redundant_text}saved_total=$saved_total\n\n" if !$quiet;
23     $redundant=0;
24   }
25   while (<F>) {
26     if (/^\s*$/s) {
27       $emptyline=1;
28       next;
29     }
30     my($addr,$spaces,$rest)=(/^(0x[0-9a-f]{8}:| {11})( +)(.*)$/) or do {
31       next;
32     };
33     if ($addr ne " "x11) {
34       $addr=~s/:$// or die;
35     } else {
36       $addr=0;
37     }
38     if ($addr) {
39       if ($emptyline&&$redundant&&!$redundant_children) {
40         null($addr);
41       }
42       $die_addr=$addr;
43       $die_text=$_;
44     } else {
45       $die_text.=$_;
46     }
47     $emptyline=0;
48     if ($redundant) {
49       $redundant_text.=$_ if !$quiet;
50     }
51     my $spacesl=length $spaces or die;
52     if ($rest=~/\bDW_FORM_addr\b.*0x0000000000000000|\bDW_AT_signature\b/) {
53       next if $redundant;
54       $redundant=$spacesl;
55       $redundant_text=$die_text if !$quiet;
56       $redundant_children=($die_text=~/^.* [*]\n/);
57       $redundant_addr=$die_addr or die;
58       next;
59     }
60     if ($rest=~/DW_AT_stmt_list/) {
61       $redundant=0;
62       next;
63     }
64     if ($rest eq "NULL") {
65       next if !$redundant;
66       next if $spacesl>$redundant;
67       null($addr);
68     }
69   }
70   close F or die;
71   print "$saved_total\n";
72 }