forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataflow.h
98 lines (80 loc) · 2.55 KB
/
dataflow.h
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license 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:
// Used to ask the dataflow object to restart analysis.
enum UpdateResult
{
RestartAnalysis,
ContinueAnalysis
};
// 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);
void EndMerge(BasicBlock* block);
bool Changed(BasicBlock* block);
DataFlow::UpdateResult Update(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);
}
}
callback.EndMerge(block);
if (callback.Changed(block))
{
UpdateResult result = callback.Update(block);
assert(result == DataFlow::ContinueAnalysis);
AllSuccessorIter succsBegin = block->GetAllSuccs(m_pCompiler).begin();
AllSuccessorIter succsEnd = block->GetAllSuccs(m_pCompiler).end();
for (AllSuccessorIter succ = succsBegin; succ != succsEnd; ++succ)
{
worklist.insert(worklist.end(), *succ);
}
}
}
}