summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Bytecode/PassManager.h
blob: b4a44da5c5aad7f5907fb5d70247f2bb53e3603a (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
/*
 * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibJS/Bytecode/BasicBlock.h>
#include <LibJS/Bytecode/Generator.h>
#include <sys/time.h>
#include <time.h>

namespace JS::Bytecode {

struct PassPipelineExecutable {
    Executable& executable;
    Optional<HashMap<BasicBlock const*, HashTable<BasicBlock const*>>> cfg {};
    Optional<HashMap<BasicBlock const*, HashTable<BasicBlock const*>>> inverted_cfg {};
    Optional<HashTable<BasicBlock const*>> exported_blocks {};
};

class Pass {
public:
    Pass() = default;
    virtual ~Pass() = default;

    virtual void perform(PassPipelineExecutable&) = 0;
    void started()
    {
        gettimeofday(&m_start_time, nullptr);
    }
    void finished()
    {
        struct timeval end_time {
            0, 0
        };
        gettimeofday(&end_time, nullptr);
        time_t interval_s = end_time.tv_sec - m_start_time.tv_sec;
        suseconds_t interval_us = end_time.tv_usec;
        if (interval_us < m_start_time.tv_usec) {
            interval_s -= 1;
            interval_us += 1000000;
        }
        interval_us -= m_start_time.tv_usec;
        m_time_difference = interval_s * 1000000 + interval_us;
    }

    u64 elapsed() const { return m_time_difference; }

protected:
    struct timeval m_start_time {
        0, 0
    };
    u64 m_time_difference { 0 };
};

class PassManager : public Pass {
public:
    PassManager() = default;
    ~PassManager() override = default;

    void add(NonnullOwnPtr<Pass> pass) { m_passes.append(move(pass)); }

    template<typename PassT, typename... Args>
    void add(Args&&... args) { m_passes.append(make<PassT>(forward<Args>(args)...)); }

    void perform(Executable& executable)
    {
        PassPipelineExecutable pipeline_executable { executable };
        perform(pipeline_executable);
    }

    virtual void perform(PassPipelineExecutable& executable) override
    {
        started();
        for (auto& pass : m_passes)
            pass.perform(executable);
        finished();
    }

private:
    NonnullOwnPtrVector<Pass> m_passes;
};

namespace Passes {

class GenerateCFG : public Pass {
public:
    GenerateCFG() = default;
    ~GenerateCFG() override = default;

private:
    virtual void perform(PassPipelineExecutable&) override;
};

class MergeBlocks : public Pass {
public:
    MergeBlocks() = default;
    ~MergeBlocks() override = default;

private:
    virtual void perform(PassPipelineExecutable&) override;
};

class PlaceBlocks : public Pass {
public:
    PlaceBlocks() = default;
    ~PlaceBlocks() override = default;

private:
    virtual void perform(PassPipelineExecutable&) override;
};

class UnifySameBlocks : public Pass {
public:
    UnifySameBlocks() = default;
    ~UnifySameBlocks() override = default;

private:
    virtual void perform(PassPipelineExecutable&) override;
};

class DumpCFG : public Pass {
public:
    DumpCFG(FILE* file)
        : m_file(file)
    {
    }

    ~DumpCFG() override = default;

private:
    virtual void perform(PassPipelineExecutable&) override;

    FILE* m_file { nullptr };
};

}

}