summaryrefslogtreecommitdiff
path: root/Source/GPUVerify/LiveVariableAnalyser.cs
blob: 61ce851f297a7cb135a86d21a4ca52f23d5f175c (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Boogie;

namespace GPUVerify
{
    class LiveVariableAnalyser
    {
        private GPUVerifier verifier;

        // Maps procedure -> loop -> set of variables live at the loop head
        private Dictionary<string, Dictionary<string, HashSet<string>>> liveVariableInfo;

        public LiveVariableAnalyser(GPUVerifier verifier)
        {
            this.verifier = verifier;
            this.liveVariableInfo = new Dictionary<string, Dictionary<string, HashSet<string>>>();
        }

        internal void Analyse()
        {
            foreach (Declaration D in verifier.Program.TopLevelDeclarations)
            {
                if (D is Implementation)
                {
                    Implementation impl = D as Implementation;

                    liveVariableInfo[impl.Name] = new Dictionary<string, HashSet<string>>();

                    LiveVariableAnalysis.ComputeLiveVariables(impl);

                    Analyse(impl.StructuredStmts, impl);

                }
            }
        }

        private void Analyse(StmtList stmtList, Implementation impl)
        {
            foreach (BigBlock bb in stmtList.BigBlocks)
            {
                Analyse(bb, impl);
            }
        }

        private void Analyse(BigBlock bb, Implementation impl)
        {
            if (bb.ec is WhileCmd)
            {
                WhileCmd wc = bb.ec as WhileCmd;

                Debug.Assert(wc.Invariants.Count >= 1);

                string wcLabel = wc.Invariants[0].Attributes.Key;

                Debug.Assert (wcLabel.Contains("loophead_"));

                liveVariableInfo[impl.Name][wcLabel] = new HashSet<string>();

                Block wcStartBlock = GetBlockForWhile(wc, impl);

                Debug.Assert(wcStartBlock != null);

//                Debug.Assert(wcStartBlock.liveVarsBefore != null);

                if (wcStartBlock.liveVarsBefore == null)
                {

                }
                else
                {

                    foreach (Variable v in impl.LocVars)
                    {
                        if (wcStartBlock.IsLive(v))
                        {
                            liveVariableInfo[impl.Name][wcLabel].Add(v.Name);
                        }
                    }
                }

                Analyse(wc.Body, impl);
            }
            else if (bb.ec is IfCmd)
            {
                Analyse((bb.ec as IfCmd).thn, impl);
                if ((bb.ec as IfCmd).elseBlock != null)
                {
                    Analyse((bb.ec as IfCmd).elseBlock, impl);
                }
                Debug.Assert((bb.ec as IfCmd).elseIf == null);
            }

        }

        private Block GetBlockForWhile(WhileCmd wc, Implementation impl)
        {
            string whileLoopIdentifier =
                wc.Invariants[0].Attributes.Key;

            foreach (Block b in impl.Blocks)
            {
                if (b.Cmds.Length > 0 && b.Cmds[0] is AssertCmd)
                {
                    AssertCmd ass = b.Cmds[0] as AssertCmd;
                    if (ass.Attributes != null)
                    {
                        if (ass.Attributes.Key.Equals(whileLoopIdentifier))
                        {
                            return b;
                        }
                    }
                }
            }

            Debug.Assert(false);

            return null;
        }
    }
}