Debian Codegraph @

gcc/m2/gm2-compiler/M2SSA.mod

raw
1 (* M2SSA.mod discover very obvious single assignment temporaries.
2
3 Copyright (C) 2021-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 M2SSA ;
23
24
25 FROM M2Debug IMPORT Assert ;
26
27 FROM NameKey IMPORT Name, WriteKey, MakeKey, GetKey ;
28 FROM StrIO IMPORT WriteString, WriteLn ;
29 FROM NumberIO IMPORT WriteCard ;
30
31 FROM M2Error IMPORT InternalError ;
32 FROM M2Batch IMPORT GetModuleNo ;
33 FROM M2Quiet IMPORT qprintf1 ;
34 FROM M2Scope IMPORT ScopeBlock, InitScopeBlock, KillScopeBlock ;
35 FROM M2StackWord IMPORT StackOfWord, InitStackWord, KillStackWord, PushWord, PopWord, PeepWord ;
36 FROM M2Options IMPORT CompilerDebugging ;
37 FROM Lists IMPORT InitList, KillList, List, IncludeItemIntoList, IsItemInList ;
38 FROM M2Printf IMPORT printf0, printf1, printf2 ;
39
40 FROM SymbolTable IMPORT GetSymName,
41 GetProcedureQuads, GetModuleQuads,
42 GetModule, GetNthProcedure,
43 GetSubrange, GetModuleScope,
44 PutProcedureReachable, IsProcedureReachable,
45 PutProcedureStartQuad, PutProcedureEndQuad,
46 PutProcedureScopeQuad,
47 PutNeedSavePriority,
48 IsProcedure, GetPriority,
49 GetDeclaredMod, GetFirstUsed,
50 GetType, GetNth, GetWriteQuads, GetReadQuads, GetMode,
51 IsExportQualified, IsExportUnQualified, IsExported,
52 ForeachProcedureDo, ForeachInnerModuleDo,
53 IsModuleWithinProcedure, IsTemporary,
54 PutVariableSSA,
55 NulSym ;
56
57 FROM M2Quads IMPORT QuadOperator, GetQuad, GetFirstQuad, GetNextQuad,
58 PutQuad, SubQuad, Opposite, IsReferenced,
59 GetRealQuad ;
60
61 VAR
62 stack : StackOfWord ;
63 visited: List ;
64
65
66 (*
67 writtenOnce - return TRUE if variable is written to once.
68 *)
69
70 PROCEDURE writtenOnce (variable: CARDINAL) : BOOLEAN ;
71 VAR
72 writeStart, writeEnd: CARDINAL ;
73 readStart, readEnd : CARDINAL ;
74 BEGIN
75 GetWriteQuads (variable, GetMode (variable), writeStart, writeEnd) ;
76 GetReadQuads (variable, GetMode (variable), readStart, readEnd) ;
77 RETURN (writeStart = writeEnd) AND ((readStart > writeStart) OR (readStart = 0))
78 END writtenOnce ;
79
80
81 (*
82 DetermineSSA - performs a trivial check to see if the temporary is written to
83 once.
84 *)
85
86 PROCEDURE DetermineSSA (variable: CARDINAL) ;
87 VAR
88 name: Name ;
89 BEGIN
90 IF EnableSSA AND IsTemporary (variable)
91 THEN
92 name := GetSymName (variable) ;
93 IF writtenOnce (variable)
94 THEN
95 PutVariableSSA (variable, TRUE) ;
96 IF CompilerDebugging
97 THEN
98 printf1 (" temporary: %a SSA found\n", name)
99 END
100 ELSE
101 IF CompilerDebugging
102 THEN
103 printf1 (" temporary: %a not SSA\n", name)
104 END
105 END
106 END
107 END DetermineSSA ;
108
109
110 (*
111 DiscoverSSATemporaries -
112 *)
113
114 PROCEDURE DiscoverSSATemporaries (scope: CARDINAL) ;
115 VAR
116 n,
117 variable: CARDINAL ;
118 BEGIN
119 IF CompilerDebugging
120 THEN
121 printf1 ("examining scope %d\n", scope)
122 END ;
123 n := 1 ;
124 variable := GetNth (scope, n) ;
125 WHILE variable # NulSym DO
126 DetermineSSA (variable) ;
127 INC (n) ;
128 variable := GetNth (scope, n)
129 END
130 END DiscoverSSATemporaries ;
131
132
133 (*
134 DiscoverSSA - perform a very simple check to determine whether a
135 temporary is a single use write.
136 *)
137
138 PROCEDURE DiscoverSSA (scope: CARDINAL) ;
139 VAR
140 sb: ScopeBlock ;
141 BEGIN
142 IF NOT IsItemInList (visited, scope)
143 THEN
144 IncludeItemIntoList (visited, scope) ;
145 sb := InitScopeBlock (scope) ;
146 PushWord (stack, scope) ;
147 IF CompilerDebugging
148 THEN
149 printf1 ("DiscoverSSA %d\n", scope)
150 END ;
151
152 IF CompilerDebugging
153 THEN
154 printf0 ("ForeachInnerModuleDo\n")
155 END ;
156 ForeachInnerModuleDo(scope, DiscoverSSA) ;
157 IF CompilerDebugging
158 THEN
159 printf0 ("ForeachProcedureDo\n")
160 END ;
161 ForeachProcedureDo(scope, DiscoverSSA) ;
162 DiscoverSSATemporaries (scope) ;
163
164 Assert (PopWord (stack) = scope) ;
165 KillScopeBlock (sb)
166 END
167 END DiscoverSSA ;
168
169
170 BEGIN
171 stack := InitStackWord () ;
172 InitList (visited)
173 END M2SSA.