summaryrefslogtreecommitdiff
path: root/ortho/mcode/ortho_code-opts.adb
blob: 75fedd0ed6194e0b0ccbbe1d55e228a330091c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
--  Mcode back-end for ortho - Optimization.
--  Copyright (C) 2006 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY or
--  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
--  for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with GCC; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.
with Ortho_Code.Flags;

package body Ortho_Code.Opts is
   procedure Relabel_Jump (Jmp : O_Enode)
   is
      Label : O_Enode;
      Bb : O_Enode;
   begin
      Label := Get_Jump_Label (Jmp);
      if Get_Expr_Kind (Label) = OE_Label then
         Bb := O_Enode (Get_Label_Info (Label));
         if Bb /= O_Enode_Null then
            Set_Jump_Label (Jmp, Bb);
         end if;
      end if;
   end Relabel_Jump;

   procedure Jmp_To_Bb (Subprg : Subprogram_Data_Acc)
   is
      First : O_Enode;
      Stmt : O_Enode;
      Prev : O_Enode;
      Cur_Bb : O_Enode;
   begin
      --  Get first statement after entry.
      First := Get_Stmt_Link (Subprg.E_Entry);

      --  First loop:
      --  If a label belongs to a BB (ie, is at the beginning of a BB),
      --  then link it to the BB.
      Stmt := First;
      Cur_Bb := O_Enode_Null;
      loop
         case Get_Expr_Kind (Stmt) is
            when OE_Leave =>
               exit;
            when OE_BB =>
               Cur_Bb := Stmt;
            when OE_Label =>
               if Cur_Bb /= O_Enode_Null then
                  Set_Label_Info (Stmt, Int32 (Cur_Bb));
               end if;
            when OE_Jump
              | OE_Jump_T
              | OE_Jump_F =>
               --  This handles backward jump.
               Relabel_Jump (Stmt);
            when others =>
               Cur_Bb := O_Enode_Null;
         end case;
         Stmt := Get_Stmt_Link (Stmt);
      end loop;

      --  Second loop:
      --  Transform jump to label to jump to BB.
      Stmt := First;
      Prev := O_Enode_Null;
      loop
         case Get_Expr_Kind (Stmt) is
            when OE_Leave =>
               exit;
            when OE_Jump
              | OE_Jump_T
              | OE_Jump_F =>
               --  This handles forward jump.
               Relabel_Jump (Stmt);
               --  Update PREV.
               Prev := Stmt;
            when OE_Label =>
               --  Remove the Label.
               --  Do not update PREV.
               if Get_Label_Info (Stmt) /= 0 then
                  Set_Stmt_Link (Prev, Get_Stmt_Link (Stmt));
               end if;
            when others =>
               Prev := Stmt;
         end case;
         Stmt := Get_Stmt_Link (Stmt);
      end loop;
   end Jmp_To_Bb;

   type Oe_Kind_Bool_Array is array (OE_Kind) of Boolean;
   Is_Passive_Stmt : constant Oe_Kind_Bool_Array :=
     (OE_Label | OE_BB | OE_End | OE_Beg => True,
      others => False);

   --  Return the next statement after STMT which really execute instructions.
   function Get_Fall_Stmt (Stmt : O_Enode) return O_Enode
   is
      Res : O_Enode;
   begin
      Res := Stmt;
      loop
         Res := Get_Stmt_Link (Res);
         case Get_Expr_Kind (Res) is
            when OE_Label
              | OE_BB
              | OE_End
              | OE_Beg =>
               null;
            when others =>
               return Res;
         end case;
      end loop;
   end Get_Fall_Stmt;

   procedure Thread_Jump (Subprg : Subprogram_Data_Acc)
   is
      First : O_Enode;
      Stmt : O_Enode;
      Prev, Next : O_Enode;
      Kind : OE_Kind;
   begin
      --  Get first statement after entry.
      First := Get_Stmt_Link (Subprg.E_Entry);

      --  First loop:
      --  If a label belongs to a BB (ie, is at the beginning of a BB),
      --  then link it to the BB.
      Stmt := First;
      Prev := O_Enode_Null;
      loop
         Next := Get_Stmt_Link (Stmt);
         Kind := Get_Expr_Kind (Stmt);
         case Kind is
            when OE_Leave =>
               exit;
            when OE_Jump =>
               --  Remove the jump if followed by the label.
               --    * For _T/_F: should convert to a ignore value.
               --  Discard unreachable statements after the jump.
               declare
                  N_Stmt : O_Enode;
                  P_Stmt : O_Enode;
                  Label : O_Enode;
                  Flag_Discard : Boolean;
                  K_Stmt : OE_Kind;
               begin
                  N_Stmt := Next;
                  P_Stmt := Stmt;
                  Label := Get_Jump_Label (Stmt);
                  Flag_Discard := Kind = OE_Jump;
                  loop
                     if N_Stmt = Label then
                        --  Remove STMT.
                        Set_Stmt_Link (Prev, Next);
                        exit;
                     end if;
                     K_Stmt := Get_Expr_Kind (N_Stmt);
                     if K_Stmt = OE_Label then
                        --  Do not discard anymore statements, since they are
                        --  now reachable.
                        Flag_Discard := False;
                     end if;
                     if not Is_Passive_Stmt (K_Stmt) then
                        if not Flag_Discard then
                           --  We have found the next statement.
                           --  Keep the jump.
                           Prev := Stmt;
                           exit;
                        else
                           --  Delete insn.
                           N_Stmt := Get_Stmt_Link (N_Stmt);
                           Set_Stmt_Link (P_Stmt, N_Stmt);
                        end if;
                     else
                        --  Iterate.
                        P_Stmt := N_Stmt;
                        N_Stmt := Get_Stmt_Link (N_Stmt);
                     end if;
                  end loop;
               end;
            when others =>
               Prev := Stmt;
         end case;
         Stmt := Next;
      end loop;
   end Thread_Jump;

   procedure Optimize_Subprg (Subprg : Subprogram_Data_Acc)
   is
   begin
      --  Jump optimisation:
      --  * discard insns after a OE_JUMP.
      --  * Remove jump if followed by label
      --    (through label, BB, comments, end, line)
      --  * Redirect jump to jump (infinite loop !)
      --  * Revert jump_t/f if expr is not (XXX)
      --  * Jmp_t/f L:; jmp L2; L1:  ->  jmp_f/t L2
      Thread_Jump (Subprg);
      if Flags.Flag_Opt_BB then
         Jmp_To_Bb (Subprg);
      end if;
   end Optimize_Subprg;
end Ortho_Code.Opts;