summaryrefslogtreecommitdiff
path: root/src/jit/dataflow.h
blob: 615a5d14945286829c709491222d718bd9540ed6 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
//   This class is used to perform data flow optimizations.
//   An example usage would be:
//
//     DataFlow flow(m_pCompiler);
//     flow.ForwardAnalysis(callback);
//
//  The "callback" object needs to implement the necessary callback
//  functions that  the "flow" object will call as the data flow
//  analysis progresses.
//
#pragma once

#include "compiler.h"
#include "jitstd.h"

class DataFlow
{
private:
    DataFlow();

public:
    // The callback interface that needs to be implemented by anyone
    // needing updates by the dataflow object.
    class Callback
    {
    public:
        Callback(Compiler* pCompiler);

        void StartMerge(BasicBlock* block);
        void Merge(BasicBlock* block, BasicBlock* pred, flowList* preds);
        bool EndMerge(BasicBlock* block);

    private:
        Compiler* m_pCompiler;
    };

    DataFlow(Compiler* pCompiler);

    template <typename TCallback>
    void ForwardAnalysis(TCallback& callback);

private:
    Compiler* m_pCompiler;
};

template <typename TCallback>
void DataFlow::ForwardAnalysis(TCallback& callback)
{
    jitstd::list<BasicBlock*> worklist(jitstd::allocator<void>(m_pCompiler->getAllocator()));

    worklist.insert(worklist.begin(), m_pCompiler->fgFirstBB);
    while (!worklist.empty())
    {
        BasicBlock* block = *(worklist.begin());
        worklist.erase(worklist.begin());

        callback.StartMerge(block);
        {
            flowList* preds = m_pCompiler->BlockPredsWithEH(block);
            for (flowList* pred = preds; pred; pred = pred->flNext)
            {
                callback.Merge(block, pred->flBlock, preds);
            }
        }

        if (callback.EndMerge(block))
        {
            for (BasicBlock* succ : block->GetAllSuccs(m_pCompiler))
            {
                worklist.insert(worklist.end(), succ);
            }
        }
    }
}