Debian Codegraph @

gcc/m2/gm2-compiler/M2Optimize.mod

raw
1 (* M2Optimize.mod removes redundant quadruples.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 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, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2Optimize ;
23
24 (*
25 Title : M2Optimize
26 Author : Gaius Mulley
27 System : UNIX (GNU Modula-2)
28 Date : Sat Aug 14 15:07:47 1999
29 Description: removes redundant quadruples, redundant GotoOps, redundant procedures.
30 *)
31
32 FROM M2Debug IMPORT Assert ;
33
34 FROM NameKey IMPORT Name, WriteKey, MakeKey, GetKey ;
35 FROM StrIO IMPORT WriteString, WriteLn ;
36 FROM NumberIO IMPORT WriteCard ;
37
38 FROM M2Error IMPORT InternalError ;
39 FROM M2Batch IMPORT GetModuleNo ;
40 FROM M2Quiet IMPORT qprintf1 ;
41 FROM M2Scope IMPORT ScopeBlock, InitScopeBlock, KillScopeBlock,
42 ForeachScopeBlockDo2, ForeachScopeBlockDo3 ;
43
44 FROM SymbolTable IMPORT GetSymName,
45 GetProcedureQuads, GetModuleQuads,
46 GetModule, GetNthProcedure,
47 GetSubrange, GetModuleScope,
48 PutProcedureReachable, IsProcedureReachable,
49 PutProcedureStartQuad, PutProcedureEndQuad,
50 PutProcedureScopeQuad,
51 PutNeedSavePriority,
52 IsProcedure, GetPriority,
53 GetDeclaredMod, GetFirstUsed,
54 GetType,
55 IsExportQualified, IsExportUnQualified, IsExported,
56 ForeachProcedureDo, ForeachInnerModuleDo,
57 IsModuleWithinProcedure,
58 NulSym ;
59
60 FROM M2Quads IMPORT QuadOperator, GetQuad, GetFirstQuad, GetNextQuad,
61 PutQuad, SubQuad, Opposite, IsReferenced,
62 GetRealQuad, GetQuadOtok, PutQuadOtok ;
63
64 (*
65 FoldBranches - folds unneccessary branches in the list of quadruples.
66 It searches for the following patterns:
67
68 [x] GotoOp _ _ y GotoOp _ _ z
69 ... ...
70 [y] GotoOp _ _ z "deleted"
71
72 WHERE ... may contain 0..n Pseudo Quads
73
74
75 OR
76
77
78 [x] IfREL _ _ z If NOT REL _ _ a
79 ... ...
80 [y] Goto _ _ a "deleted"
81 ... ...
82 [z]
83
84
85 WHERE ... may contain 0..n Pseudo Quads
86 but in this case they must not be a
87 target of any other quad.
88 *)
89
90 PROCEDURE FoldBranches (start, end: CARDINAL) ;
91 VAR
92 Folded : BOOLEAN ;
93 i, j,
94 Right : CARDINAL ;
95 Operator : QuadOperator ;
96 Operand1,
97 Operand2,
98 Operand3 : CARDINAL ;
99 BEGIN
100 REPEAT
101 i := start ;
102 Folded := FALSE ;
103 WHILE (i<=end) AND (i#0) DO
104 j := GetNextQuad(i) ;
105 IF (j>end) OR (j=0)
106 THEN
107 RETURN
108 END ;
109 Right := GetRealQuad(j) ;
110 IF Right=0
111 THEN
112 RETURN
113 END ;
114 GetQuad(i, Operator, Operand1, Operand2, Operand3) ;
115 CASE Operator OF
116
117 GotoOp : Folded := ReduceGoto (i, Operand3,
118 Right, Folded) |
119 IfInOp, IfNotInOp,
120 IfNotEquOp, IfEquOp,
121 IfLessEquOp, IfGreEquOp,
122 IfGreOp, IfLessOp : Folded := ReduceBranch (Operator, i,
123 Operand1, Operand2, Operand3,
124 Right, Folded)
125
126 ELSE
127 END ;
128 i := Right
129 END
130 UNTIL NOT Folded
131 END FoldBranches ;
132
133
134 (*
135 ReduceBranch - searches for the following pattern:
136
137 [x] IfREL _ _ z If NOT REL _ _ a
138 ... ...
139 [y] Goto _ _ a "deleted"
140 ... ...
141 [z]
142
143
144 WHERE ... may contain 0..n Pseudo Quads
145 but in this case they must not be a
146 target of any other quad.
147
148 *)
149
150 PROCEDURE ReduceBranch (Operator: QuadOperator;
151 CurrentQuad,
152 CurrentOperand1, CurrentOperand2,
153 CurrentOperand3: CARDINAL;
154 VAR NextQuad: CARDINAL;
155 Folded: BOOLEAN) : BOOLEAN ;
156 VAR
157 constExpr,
158 overflowChecking: BOOLEAN ;
159 OpNext : QuadOperator ;
160 tok,
161 NextPlusOne,
162 Op1Next,
163 Op2Next,
164 Op3Next,
165 op1tok,
166 op2tok,
167 op3tok,
168 From, To : CARDINAL ;
169 BEGIN
170 (* If op NextQuad+1 *)
171 (* Goto x *)
172
173 IF NextQuad#0
174 THEN
175 IF (GetNextQuad (CurrentQuad) = CurrentOperand3) OR
176 (GetRealQuad (GetNextQuad (CurrentQuad)) = CurrentOperand3)
177 THEN
178 SubQuad (CurrentQuad) ;
179 Folded := TRUE
180 ELSE
181 From := GetNextQuad (CurrentQuad) ; (* start after CurrentQuad *)
182 To := NextQuad ;
183 CurrentOperand3 := GetRealQuad (CurrentOperand3) ;
184
185 NextPlusOne := GetRealQuad (GetNextQuad (NextQuad)) ;
186 GetQuad (NextQuad, OpNext, Op1Next, Op2Next, Op3Next) ;
187 IF (OpNext = GotoOp) AND (NextPlusOne = CurrentOperand3) AND
188 IsBasicBlock (From, To)
189 THEN
190 GetQuadOtok (CurrentQuad, tok, Operator,
191 CurrentOperand1, CurrentOperand2, CurrentOperand3,
192 overflowChecking, constExpr, op1tok, op2tok, op3tok) ;
193 SubQuad (NextQuad) ;
194 PutQuadOtok (CurrentQuad, tok, Opposite (Operator),
195 CurrentOperand1, CurrentOperand2, Op3Next,
196 overflowChecking, constExpr,
197 op1tok, op2tok, op3tok) ;
198 NextQuad := NextPlusOne ;
199 Folded := TRUE
200 END
201 END ;
202 IF FoldMultipleGoto (CurrentQuad)
203 THEN
204 Folded := TRUE
205 END
206 END ;
207 RETURN Folded
208 END ReduceBranch ;
209
210
211 (*
212 IsBasicBlock - returns TRUE if no other quadruple jumps inbetween
213 the range From..To.
214 It assumes that there are no jumps in the quadruples
215 From..To.
216 *)
217
218 PROCEDURE IsBasicBlock (From, To: CARDINAL) : BOOLEAN ;
219 BEGIN
220 WHILE From # To DO
221 IF IsReferenced (From)
222 THEN
223 RETURN FALSE
224 ELSE
225 IF From > To
226 THEN
227 InternalError ('assert failed From should never be larger than To')
228 END ;
229 From := GetNextQuad (From)
230 END
231 END ;
232 RETURN TRUE
233 END IsBasicBlock ;
234
235
236 (*
237 ReduceGoto - searches for the following patterns:
238
239 [x] GotoOp _ _ y GotoOp _ _ z
240 ... ...
241 [y] GotoOp _ _ z "deleted"
242
243
244 *)
245
246 PROCEDURE ReduceGoto (CurrentQuad, CurrentOperand3, NextQuad: CARDINAL;
247 Folded: BOOLEAN) : BOOLEAN ;
248 BEGIN
249 CurrentOperand3 := GetRealQuad (CurrentOperand3) ;
250 (* IF next quad is a GotoOp *)
251 IF CurrentOperand3 = NextQuad
252 THEN
253 SubQuad (CurrentQuad) ;
254 Folded := TRUE
255 ELSE
256 (* Does Goto point to another Goto ? *)
257 IF FoldMultipleGoto (CurrentQuad)
258 THEN
259 Folded := TRUE
260 END
261 END ;
262 RETURN Folded
263 END ReduceGoto ;
264
265
266 (*
267 FoldMultipleGoto - takes a QuadNo and if it jumps to another GotoOp
268 then it takes the later target as a replacement
269 for its own.
270
271 NOTE it does not remove any quadruples.
272 *)
273
274 PROCEDURE FoldMultipleGoto (QuadNo: CARDINAL) : BOOLEAN ;
275 VAR
276 Operator,
277 Op : QuadOperator ;
278 Op1, Op2,
279 Op3,
280 Operand1,
281 Operand2,
282 Operand3: CARDINAL ;
283 BEGIN
284 GetQuad (QuadNo, Operator, Operand1, Operand2, Operand3) ;
285 Operand3 := GetRealQuad (Operand3) ; (* skip pseudo quadruples *)
286 GetQuad (Operand3, Op, Op1, Op2, Op3) ;
287 IF Op = GotoOp
288 THEN
289 PutQuad (QuadNo, Operator, Operand1, Operand2, Op3) ;
290 (* Dont want success to be returned if in fact the Goto *)
291 (* line number has not changed... otherwise we loop *)
292 (* forever. *)
293 RETURN Op3 # Operand3
294 ELSE
295 RETURN FALSE
296 END
297 END FoldMultipleGoto ;
298
299
300 (*
301 CheckNeedSavePriority -
302 *)
303
304 PROCEDURE CheckNeedSavePriority (sym: CARDINAL) ;
305 BEGIN
306 IF IsProcedure(sym) AND (GetPriority(GetModuleScope(sym))#NulSym)
307 THEN
308 PutNeedSavePriority(sym)
309 END
310 END CheckNeedSavePriority ;
311
312
313 (*
314 CheckExportedReachable - checks to see whether procedure, sym, was
315 exported and if so it calls RemoveProcedures.
316 *)
317
318 PROCEDURE CheckExportedReachable (sym: CARDINAL) ;
319 BEGIN
320 IF IsExported(GetModuleScope(sym), sym)
321 THEN
322 RemoveProcedures(sym) ;
323 CheckNeedSavePriority(sym)
324 END
325 END CheckExportedReachable ;
326
327
328 (*
329 RemoveProcedures - removes any procedures that are never referenced
330 by the quadruples.
331 *)
332
333 PROCEDURE RemoveProcedures (scope: CARDINAL) ;
334 VAR
335 sb: ScopeBlock ;
336 BEGIN
337 sb := InitScopeBlock(scope) ;
338 IF IsProcedure(scope)
339 THEN
340 PutProcedureReachable(scope) ;
341 ForeachScopeBlockDo2 (sb, KnownReachable)
342 ELSIF IsModuleWithinProcedure(scope)
343 THEN
344 ForeachScopeBlockDo2 (sb, KnownReachable) ;
345 ForeachProcedureDo(scope, CheckExportedReachable)
346 ELSE
347 ForeachScopeBlockDo2 (sb, KnownReachable) ;
348 ForeachProcedureDo(scope, CheckExportedReachable)
349 END ;
350 ForeachInnerModuleDo(scope, RemoveProcedures) ;
351 KillScopeBlock(sb) ;
352 (* DeleteUnReachableProcedures *)
353 END RemoveProcedures ;
354
355
356 PROCEDURE KnownReachable (Start, End: CARDINAL) ;
357 VAR
358 Op : QuadOperator ;
359 Op1, Op2, Op3: CARDINAL ;
360 BEGIN
361 IF Start#0
362 THEN
363 REPEAT
364 GetQuad (Start, Op, Op1, Op2, Op3) ;
365 CASE Op OF
366
367 CallOp : KnownReach (Op3) |
368 AddrOp,
369 ParamOp,
370 XIndrOp,
371 BecomesOp: KnownReach (Op3) ;
372 CheckNeedSavePriority (Op3)
373
374 ELSE
375 END ;
376 Start := GetNextQuad (Start)
377 UNTIL (Start > End) OR (Start = 0)
378 END
379 END KnownReachable ;
380
381
382 PROCEDURE KnownReach (sym: CARDINAL) ;
383 BEGIN
384 IF IsProcedure (sym) AND (NOT IsProcedureReachable (sym))
385 THEN
386 RemoveProcedures (sym)
387 END
388 END KnownReach ;
389
390
391 (*
392 DeleteUnReachableProcedures - Deletes all procedures that are unreachable.
393 *)
394
395 (*
396 PROCEDURE DeleteUnReachableProcedures ;
397 VAR
398 ProcName: Name ;
399 n, m,
400 Scope,
401 Start,
402 End,
403 Module,
404 Proc : CARDINAL ;
405 BEGIN
406 m := 1 ;
407 REPEAT
408 Module := GetModuleNo(m) ;
409 IF Module#NulSym
410 THEN
411 n := 1 ;
412 Proc := GetNthProcedure(Module, n) ;
413 WHILE Proc#NulSym DO
414 IF IsProcedureReachable(Proc) OR
415 IsExportQualified(Proc) OR IsExportUnQualified(Proc)
416 THEN
417 (* is reachable - do not delete it *)
418 ELSE
419 ProcName := GetSymName(Proc) ;
420 qprintf1('[%a]\n', ProcName) ;
421
422 GetProcedureQuads(Proc, Scope, Start, End) ;
423 IF Start#0
424 THEN
425 Delete(Scope, End) ;
426 (* No Longer any Quads for this Procedure *)
427 PutProcedureScopeQuad(Proc, 0) ;
428 PutProcedureStartQuad(Proc, 0) ;
429 PutProcedureEndQuad(Proc, 0)
430 END
431 END ;
432 INC(n) ;
433 Proc := GetNthProcedure(Module, n)
434 END ;
435 INC(m)
436 END
437 UNTIL Module=NulSym
438 END DeleteUnReachableProcedures ;
439
440
441 (*
442 Delete - deletes all quadruples from Start..End
443 or the end of the procedure.
444 *)
445
446 PROCEDURE Delete (Start, End: CARDINAL) ;
447 VAR
448 Last,
449 i : CARDINAL ;
450 Op : QuadOperator ;
451 Op1,
452 Op2,
453 Op3 : CARDINAL ;
454 BEGIN
455 Last := GetNextQuad(End) ;
456 WHILE (GetFirstQuad()#0) AND (Start#0) AND (Last#Start) DO
457 GetQuad(Start, Op, Op1, Op2, Op3) ;
458 IF Op=DummyOp
459 THEN
460 (* Start has already been deleted - try next quad *)
461 INC(Start)
462 ELSIF Op=ReturnOp
463 THEN
464 (* Found end of procedure therefore just delete and exit *)
465 (* WriteString('Deleting') ; WriteCard(Start, 6) ; WriteLn ; *)
466 SubQuad(Start) ;
467 Start := Last
468 ELSE
469 (* Following the list of quadruples to the End *)
470 i := GetNextQuad(Start) ;
471 (* WriteString('Deleting') ; WriteCard(Start, 6) ; WriteLn ; *)
472 SubQuad(Start) ;
473 Start := i
474 END
475 END
476 END Delete ;
477 *)
478
479
480 (*
481 DisplayReachable - Displays the data structures surrounding Reachablity.
482 *)
483
484 PROCEDURE DisplayReachable ;
485 VAR
486 n, m,
487 Scope,
488 StartInit,
489 EndInit,
490 StartFinish,
491 EndFinish,
492 Module,
493 Proc : CARDINAL ;
494 BEGIN
495 m := 1 ;
496 REPEAT
497 Module := GetModuleNo(m) ;
498 IF Module#NulSym
499 THEN
500 WriteString('Module') ; WriteCard(m, 3) ; WriteKey(GetSymName(Module)) ;
501 GetModuleQuads(Module, StartInit, EndInit, StartFinish, EndFinish) ;
502 WriteString(' Reachable initialization') ;
503 WriteCard(StartInit, 6) ; WriteCard(EndInit, 6) ; WriteLn ;
504 WriteString('Module') ; WriteCard(m, 3) ; WriteKey(GetSymName(Module)) ;
505 GetModuleQuads(Module, StartInit, EndInit, StartFinish, EndFinish) ;
506 WriteString(' Reachable finalization') ;
507 WriteCard(StartFinish, 6) ; WriteCard(EndFinish, 6) ; WriteLn ;
508 n := 1 ;
509 Proc := GetNthProcedure(Module, n) ;
510 WHILE Proc#NulSym DO
511 WriteString('Procedure ') ; WriteKey(GetSymName(Proc)) ;
512 GetProcedureQuads(Proc, Scope, StartInit, EndInit) ;
513 WriteString(' Quads: ') ; WriteCard(StartInit, 6) ; WriteCard(EndInit, 6) ;
514 IF NOT IsProcedureReachable(Proc)
515 THEN
516 WriteString(' UN reachable')
517 ELSE
518 WriteString(' IS reachable')
519 END ;
520 WriteLn ;
521 INC(n) ;
522 Proc := GetNthProcedure(Module, n)
523 END ;
524 INC(m)
525 END
526 UNTIL Module=NulSym
527 END DisplayReachable ;
528
529
530 END M2Optimize.