summaryrefslogtreecommitdiff
path: root/Source/GPUVerify/VariableDefinitionAnalysis.cs
blob: 100dbc0e5e33a7d35f3d05b482c1598ea476f0f8 (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
using System;
using Microsoft.Boogie;
using System.Collections.Generic;
using System.Linq;

namespace GPUVerify {

using VarDef = Tuple<Expr, bool>;

class VariableDefinitionAnalysis {
  GPUVerifier verifier;
  Implementation impl;

  Dictionary<Variable, VarDef> defMap = new Dictionary<Variable, VarDef>();
  Dictionary<string, VarDef> namedDefMap = new Dictionary<string, VarDef>();
  bool changed;

  VariableDefinitionAnalysis(GPUVerifier v, Implementation i) {
    verifier = v;
    impl = i;
  }

  private class IsConstantVisitor : StandardVisitor {
    public bool isConstant = true;

    public IsConstantVisitor() {}

    public override Expr VisitNAryExpr(NAryExpr expr) {
      if (expr.Fun is MapSelect) {
        isConstant = false;
        return expr;
      } else
        return base.VisitNAryExpr(expr);
    }
  };

  private class IsDerivedFromConstantsVisitor : StandardVisitor {
    private VariableDefinitionAnalysis analysis;
    public bool isDerivedFromConstants = true;

    public IsDerivedFromConstantsVisitor(VariableDefinitionAnalysis a) {
      analysis = a;
    }

    public override Expr VisitIdentifierExpr(IdentifierExpr expr) {
      if (expr.Decl is Constant)
        return expr;
      if (!analysis.defMap.ContainsKey(expr.Decl) || !analysis.defMap[expr.Decl].Item2)
        isDerivedFromConstants = false;
      return expr;
    }
  };

  bool IsConstant(Expr e) {
    var v = new IsConstantVisitor();
    v.Visit(e);
    return v.isConstant;
  }

  bool IsDerivedFromConstants(Expr e) {
    var v = new IsDerivedFromConstantsVisitor(this);
    v.Visit(e);
    return v.isDerivedFromConstants;
  }

  void UpdateDefMap(Variable v, Expr def, bool isConstant) {
    if (!defMap.ContainsKey(v)) {
      changed = true;
      defMap[v] = new VarDef(def, isConstant);
      return;
    }

    var d = defMap[v];
    if (d.Item1 != def || d.Item2 != isConstant) {
      changed = true;
      defMap[v] = new VarDef(def, isConstant);
    }
  }

  void AddAssignment(AssignLhs lhs, Expr rhs) {
    if (lhs is SimpleAssignLhs) {
      var sLhs = (SimpleAssignLhs)lhs;
      var theVar = sLhs.DeepAssignedVariable;
      if ((defMap.ContainsKey(theVar) && defMap[theVar].Item1 != rhs) || !IsConstant(rhs)) {
        UpdateDefMap(theVar, null, false);
      } else {
        UpdateDefMap(theVar, rhs, IsDerivedFromConstants(rhs));
      }
    }
  }

  void Analyse() {
    do {
      changed = false;
      foreach (var c in verifier.RootRegion(impl).Cmds()) {
        if (c is AssignCmd) {
          var aCmd = (AssignCmd)c;
          foreach (var a in aCmd.Lhss.Zip(aCmd.Rhss)) {
            AddAssignment(a.Item1, a.Item2);
          }
        }
        if (c is HavocCmd) {
          var hCmd = (HavocCmd)c;
          foreach (IdentifierExpr iExpr in hCmd.Vars)
            UpdateDefMap(iExpr.Decl, null, false);
        }
      }
    } while (changed);
  }

  private class BuildNamedDefVisitor : Duplicator {
    private VariableDefinitionAnalysis analysis;
    public bool isSelfReferential = false;

    public BuildNamedDefVisitor(VariableDefinitionAnalysis a) {
      analysis = a;
    }

    public override Expr VisitIdentifierExpr(IdentifierExpr expr) {
      if (expr.Decl is Constant)
        return expr;
      var def = analysis.BuildNamedDefFor(expr.Decl, expr);
      if (def == null)
        isSelfReferential = true;
      return def;
    }
  }

  HashSet<Variable> vars;

  Expr BuildNamedDefFor(Variable v, Expr e = null) {
    VarDef def;
    if (namedDefMap.TryGetValue(v.Name, out def))
      return def.Item1;

    if (vars.Contains(v))
      return null;

    vars.Add(v);
    if (!defMap.TryGetValue(v, out def))
      return e;
    Expr defExpr;
    bool defIsConstant;
    if (def.Item1 == null) {
      defExpr = e;
      defIsConstant = def.Item2;
    } else {
      var ndv = new BuildNamedDefVisitor(this);
      defExpr = (Expr)ndv.Visit(def.Item1.Clone());
      defIsConstant = def.Item2 && !ndv.isSelfReferential;
      if (ndv.isSelfReferential)
        defExpr = null;
    }
    namedDefMap[v.Name] = new VarDef(defExpr, defIsConstant);
    vars.Remove(v);

    return defExpr;
  }

  void BuildNamedDefMap() {
    foreach (var v in defMap.Keys)
      if (defMap[v].Item1 != null) {
        vars = new HashSet<Variable>();
        BuildNamedDefFor(v);
      }
    vars = null;
  }

  private class SubstDefVisitor : Duplicator {
    private VariableDefinitionAnalysis analysis;
    private string procName;
    public bool isSubstitutable = true;
    public bool isConstant = true;

    public SubstDefVisitor(VariableDefinitionAnalysis a, string p) {
      analysis = a;
      procName = p;
    }

    public override Expr VisitIdentifierExpr(IdentifierExpr expr) {
      if (expr.Decl is Constant)
        return expr;
      int id;
      var varName = GPUVerifier.StripThreadIdentifier(expr.Decl.Name, out id);
      VarDef def;
      if (!analysis.namedDefMap.TryGetValue(varName, out def)) {
        isSubstitutable = false;
        return null;
      }
      if (!def.Item2)
        isConstant = false;
      return analysis.verifier.MaybeDualise(def.Item1, id, procName);
    }
  }

  public Expr SubstDefinitions(Expr e, string procName, out bool isConstant) {
    var v = new SubstDefVisitor(this, procName);
    Expr result = (Expr)v.Visit(e.Clone());
    isConstant = v.isConstant;
    if (!v.isSubstitutable)
      return null;
    return result;
  }

  public Expr SubstDefinitions(Expr e, string procName) {
    bool isConstant;
    var result = SubstDefinitions(e, procName, out isConstant);
    if (!isConstant)
      return null;
    return result;
  }

  public static VariableDefinitionAnalysis Analyse(GPUVerifier verifier, Implementation impl) {
    var a = new VariableDefinitionAnalysis(verifier, impl);
    a.Analyse();
    a.BuildNamedDefMap();
    a.defMap = null;
    return a;
  }

}

}