summaryrefslogtreecommitdiff
path: root/Source/Core/DeadVarElim.ssc
blob: 0686212eddfa1012c59c5dddce32b020f8b2e2e6 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Collections.Generic;
using Graphing;

namespace Microsoft.Boogie
{
  public class UnusedVarEliminator : VariableCollector {
    public static void Eliminate(Program! program) {
      UnusedVarEliminator elim = new UnusedVarEliminator();
      elim.Visit(program);
    }
  
    private UnusedVarEliminator() {
      base();
    }
      
	public override Implementation! VisitImplementation(Implementation! node) {
	  //Console.WriteLine("Procedure {0}", node.Name);
	  Implementation! impl = base.VisitImplementation(node);
	  //Console.WriteLine("Old number of local variables = {0}", impl.LocVars.Length);
	  Microsoft.Boogie.VariableSeq! vars = new Microsoft.Boogie.VariableSeq();
	  foreach (Variable! var in impl.LocVars) {
	    if (usedVars.Contains(var))
	      vars.Add(var);
	  }
	  impl.LocVars = vars;
	  //Console.WriteLine("New number of local variables = {0}", impl.LocVars.Length);
	  //Console.WriteLine("---------------------------------");
	  usedVars.Clear();
	  return impl;
	}
  }
  
  public class ModSetCollector : StandardVisitor {
    static Procedure proc;
    static Dictionary<Procedure!, Set<Variable!>!>! modSets;
    static bool moreProcessingRequired;
    
    public static void DoModSetAnalysis(Program! program) {
      int procCount = 0;
      foreach (Declaration! decl in program.TopLevelDeclarations) {
        if (decl is Procedure)
          procCount++;
      }
      Console.WriteLine("Number of procedures = {0}", procCount);
      
      modSets = new Dictionary<Procedure!, Set<Variable!>!>();
      
      Set<Procedure!> implementedProcs = new Set<Procedure!> ();
      foreach (Declaration! decl in program.TopLevelDeclarations) {
        if (decl is Implementation) {
          Implementation impl = (Implementation) decl;
          if (impl.Proc != null)
            implementedProcs.Add(impl.Proc);
        }
      }
      foreach (Declaration! decl in program.TopLevelDeclarations) {
        if (decl is Procedure && !implementedProcs.Contains((Procedure!) decl)) {
          proc = (Procedure) decl;
          foreach (IdentifierExpr! expr in proc.Modifies) {
            ProcessVariable(expr.Decl);
          }
          proc = null;
        }
      }
      
      moreProcessingRequired = true;
      while (moreProcessingRequired) {
        moreProcessingRequired = false;
        ModSetCollector modSetCollector = new ModSetCollector();
        modSetCollector.Visit(program);
      }
      
      procCount = 0;
      foreach (Procedure! x in modSets.Keys) {
        procCount++;
        Console.Write("{0} : ", x.Name);
        foreach (Variable! y in modSets[x]) {
          Console.Write("{0}, ", y.Name);
        }
        Console.WriteLine("");
      }
      Console.WriteLine("Number of procedures with nonempty modsets = {0}", procCount);
    }
    
    public override Implementation! VisitImplementation(Implementation! node) {
      proc = node.Proc;
      Implementation! ret = base.VisitImplementation(node);
      proc = null;
      
      return ret;
    }
    public override Cmd! VisitAssignCmd(AssignCmd! assignCmd) {
      Cmd ret = base.VisitAssignCmd(assignCmd);
      foreach (AssignLhs! lhs in assignCmd.Lhss) {
	    ProcessVariable(lhs.DeepAssignedVariable);
	  }
      return ret;
    }
    public override Cmd! VisitHavocCmd(HavocCmd! havocCmd) {
      Cmd ret = base.VisitHavocCmd(havocCmd);
      foreach (IdentifierExpr! expr in havocCmd.Vars) {
        ProcessVariable(expr.Decl);
	  }
	  return ret;
    }
    public override Cmd! VisitCallCmd(CallCmd! callCmd) {
      Cmd ret = base.VisitCallCmd(callCmd);
      Procedure callee = callCmd.Proc;
      if (callee != null && modSets.ContainsKey(callee)) {
        foreach (Variable var in modSets[callee]) {
          ProcessVariable(var);
        }
      }
      return ret;
    }
    private static void ProcessVariable(Variable var) {
      Procedure! localProc = (!)proc;
      if (var == null) return;
	  if (!(var is GlobalVariable)) return;
	  if (var.Name == "alloc") return;
	  if (!modSets.ContainsKey(localProc)) {
	    modSets[localProc] = new Set<Variable!> ();
	  }
	  if (modSets[localProc].Contains(var)) return;
	  moreProcessingRequired = true;
	  modSets[localProc].Add(var);
    }
  }
  
  public class VariableCollector : StandardVisitor {
	public System.Collections.Generic.Set<Variable!>! usedVars;
	public VariableCollector() {
	  usedVars = new System.Collections.Generic.Set<Variable!>();
	}
	
    public override Expr! VisitIdentifierExpr(IdentifierExpr! node) {
      if (node.Decl != null) {
        usedVars.Add(node.Decl);
      }
      return node;
    }
  } 
  
  public class BlockCoalescer : StandardVisitor { 
    public static void CoalesceBlocks(Program! program) {
      BlockCoalescer blockCoalescer = new BlockCoalescer();
      blockCoalescer.Visit(program);
    }
    
    private static Set<Block!>! ComputeMultiPredecessorBlocks(Implementation !impl) {
      Set<Block!> visitedBlocks = new Set<Block!>();
      Set<Block!> multiPredBlocks = new Set<Block!>();
      Stack<Block!> dfsStack = new Stack<Block!>();
      dfsStack.Push(impl.Blocks[0]);
      while (dfsStack.Count > 0) {
        Block! b = dfsStack.Pop();
        if (visitedBlocks.Contains(b)) {
          multiPredBlocks.Add(b);
          continue;
        }
        visitedBlocks.Add(b);
        if (b.TransferCmd == null) continue;
        if (b.TransferCmd is ReturnCmd) continue;
        assert b.TransferCmd is GotoCmd;
        GotoCmd gotoCmd = (GotoCmd) b.TransferCmd;
        if (gotoCmd.labelTargets == null) continue;
        foreach (Block! succ in gotoCmd.labelTargets) {
          dfsStack.Push(succ);
        }
      }
      return multiPredBlocks;
    }
    
    public override Implementation! VisitImplementation(Implementation! impl) {
      //Console.WriteLine("Procedure {0}", impl.Name);
      //Console.WriteLine("Initial number of blocks = {0}", impl.Blocks.Count);
      
      Set<Block!> multiPredBlocks = ComputeMultiPredecessorBlocks(impl);
      Set<Block!> visitedBlocks = new Set<Block!>();
      Set<Block!> removedBlocks = new Set<Block!>();
      Stack<Block!> dfsStack = new Stack<Block!>();
      dfsStack.Push(impl.Blocks[0]);
      while (dfsStack.Count > 0) {
        Block! b = dfsStack.Pop();
        if (visitedBlocks.Contains(b)) continue;
        visitedBlocks.Add(b);
        if (b.TransferCmd == null) continue;
        if (b.TransferCmd is ReturnCmd) continue;
        assert b.TransferCmd is GotoCmd;
        GotoCmd gotoCmd = (GotoCmd) b.TransferCmd;
        if (gotoCmd.labelTargets == null) continue;
        if (gotoCmd.labelTargets.Length == 1) {
          Block! succ = (!)gotoCmd.labelTargets[0];
          if (!multiPredBlocks.Contains(succ)) {
            foreach (Cmd! cmd in succ.Cmds) {
              b.Cmds.Add(cmd);
            }
            b.TransferCmd = succ.TransferCmd;
            if (!b.tok.IsValid && succ.tok.IsValid) {
              b.tok = succ.tok;
              b.Label = succ.Label;
            }
            removedBlocks.Add(succ);
            dfsStack.Push(b);
            visitedBlocks.Remove(b);
            continue;
          }
        } 
        foreach (Block! succ in gotoCmd.labelTargets) {
          dfsStack.Push(succ);
        }
      }
      
      List<Block!> newBlocks = new List<Block!>();
      foreach (Block! b in impl.Blocks) {
        if (!removedBlocks.Contains(b)) {
          newBlocks.Add(b);
        }
      }
      impl.Blocks = newBlocks;
      
      // Console.WriteLine("Final number of blocks = {0}", impl.Blocks.Count);
      return impl;
    }
  }
  
  public class LiveVariableAnalysis {
    public static void ClearLiveVariables(Implementation! impl) {
      foreach (Block! block in impl.Blocks) {
        block.liveVarsBefore = null;
      }
    }
    
	public static void ComputeLiveVariables(Implementation! impl) {
	  Microsoft.Boogie.Helpers.ExtraTraceInformation("Starting live variable analysis");
	  Graphing.Graph<Block> dag = new Graph<Block>();
      dag.AddSource((!)impl.Blocks[0]); // there is always at least one node in the graph
      foreach (Block b in impl.Blocks)
      {
        GotoCmd gtc = b.TransferCmd as GotoCmd;
        if (gtc != null)
        {
          assume gtc.labelTargets != null;
          foreach (Block! dest in gtc.labelTargets)
          {
            dag.AddEdge(dest, b);
          }
        }
      }
      
      IEnumerable<Block> sortedNodes = dag.TopologicalSort();
	  foreach (Block! block in sortedNodes) {
	    Set<Variable!>! liveVarsAfter = new Set<Variable!>();
	    if (block.TransferCmd is GotoCmd) {
	      GotoCmd gotoCmd = (GotoCmd) block.TransferCmd;
	      if (gotoCmd.labelTargets != null) {
	        foreach (Block! succ in gotoCmd.labelTargets) {
	          assert succ.liveVarsBefore != null;
	          liveVarsAfter.AddRange(succ.liveVarsBefore);
	        }
	      }
	    }
	    
        CmdSeq cmds = block.Cmds;
	    int len = cmds.Length;
	    for (int i = len - 1; i >= 0; i--) {
	      Propagate(cmds[i], liveVarsAfter);
	    }
	    
	    block.liveVarsBefore = liveVarsAfter;
	  }
	}
	
	// perform in place update of liveSet
	private static void Propagate(Cmd! cmd, Set<Variable!>! liveSet) {
	  if (cmd is AssignCmd) {
	    AssignCmd! assignCmd = (AssignCmd) cmd;
	    // I must first iterate over all the targets and remove the live ones.
	    // After the removals are done, I must add the variables referred on 
	    // the right side of the removed targets
	    Set<int> indexSet = new Set<int>();
	    int index = 0;
	    foreach (AssignLhs! lhs in assignCmd.Lhss) {
	      Variable var = lhs.DeepAssignedVariable;
	      if (var != null && liveSet.Contains(var)) {
	        indexSet.Add(index);
	        if (lhs is SimpleAssignLhs) {
	          // we should only remove non-map target variables because there is an implicit
	          // read of a map variable in an assignment to it
			  liveSet.Remove(var);
			}
	      }
	      index++;
	    }
	    index = 0;
	    foreach (Expr! expr in assignCmd.Rhss) {
	      if (indexSet.Contains(index)) {
	        VariableCollector! collector = new VariableCollector();
	        collector.Visit(expr);
	        liveSet.AddRange(collector.usedVars);
	        AssignLhs lhs = assignCmd.Lhss[index];
	        if (lhs is MapAssignLhs) {
	          // If the target is a map, then all indices are also read
	          MapAssignLhs malhs = (MapAssignLhs) lhs;
	          foreach (Expr e in malhs.Indexes) {
	            VariableCollector! c = new VariableCollector();
	            c.Visit(e);
	            liveSet.AddRange(c.usedVars);
	          }
	        }
	      }
	      index++;
	    }
	  } else if (cmd is HavocCmd) {
	    HavocCmd! havocCmd = (HavocCmd) cmd;
	    foreach (IdentifierExpr! expr in havocCmd.Vars) {
	      if (expr.Decl != null) {
	        liveSet.Remove(expr.Decl);
	      }
	    }
	  } else if (cmd is PredicateCmd) {
	    assert (cmd is AssertCmd || cmd is AssumeCmd);
	    PredicateCmd! predicateCmd = (PredicateCmd) cmd;
	    if (predicateCmd.Expr is LiteralExpr) {
	      LiteralExpr le = (LiteralExpr) predicateCmd.Expr;
	      if (le.IsFalse) {
	        liveSet.Clear();
	      }
	    } else {
	      VariableCollector! collector = new VariableCollector();
	      collector.Visit(predicateCmd.Expr);
	      liveSet.AddRange(collector.usedVars);
	    }
	  } else if (cmd is CommentCmd) {
        // comments are just for debugging and don't affect verification
      } else if (cmd is SugaredCmd) {
        SugaredCmd! sugCmd = (SugaredCmd) cmd;
        Propagate(sugCmd.Desugaring, liveSet);
      } else if (cmd is StateCmd) {
        StateCmd! stCmd = (StateCmd) cmd;
        CmdSeq! cmds = stCmd.Cmds;
        int len = cmds.Length;
        for (int i = len - 1; i >= 0; i--) {
          Propagate(cmds[i], liveSet);
        }
        foreach (Variable! v in stCmd.Locals) {
          liveSet.Remove(v);
        }
      } else {
        assert false;
      }
	}
  }
}