aboutsummaryrefslogtreecommitdiff
path: root/src/mid/dce.cc
blob: 6ee631a4a77a3e9f51e63d8682fd4da27006a50f (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
/* Source code for RCC-Legacy.
   Copyright (C) 2026 Richard Qin

This file is part of RCC.

RCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

RCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with RCC; see the file LICENSE.  If not see
<http://www.gnu.org/licenses/>.  */

/// @file dce.cc
/// @brief Dead Code Elimination: mark-and-sweep starting from side-effect / control flow roots.
///
/// See docs/dce.md for architectural notes.

#include "rcc/all.h"

#include <vector>

namespace rcc {

namespace {

bool is_root(uint op) {
    if (isstore(op))
        return true;
    if (isalloc(op))
        return true;
    if (ispar(op))
        return true;
    if (isarg(op))
        return true;
    if (op == Ocall)
        return true;
    if (op == Oblit0 || op == Oblit1)
        return true;
    if (op == Ovastart || op == Ovaarg)
        return true;
    if (op == Odbgloc)
        return true;

    if (op == Osel0 || op == Osel1)
        return true;
    return false;
}

}  // namespace

void dce(Ctx*, Fn* fn) {
    int ntmp = fn->ntmp;
    std::vector<Phi*> phi_def(static_cast<size_t>(ntmp), nullptr);
    std::vector<Ins*> ins_def(static_cast<size_t>(ntmp), nullptr);

    for (Blk* b = fn->start; b; b = b->link) {
        for (Phi* p = b->phi; p; p = p->link)
            if (rtype(p->to) == RTmp && p->to.val < static_cast<uint>(ntmp))
                phi_def[p->to.val] = p;
        for (uint k = 0; k < b->nins; k++) {
            Ins* i = &b->ins[k];
            if (rtype(i->to) == RTmp && i->to.val < static_cast<uint>(ntmp))
                ins_def[i->to.val] = i;
        }
    }

    std::vector<char> alive(static_cast<size_t>(ntmp), 0);
    std::vector<uint> wl;
    auto mark = [&](Ref r) {
        if (rtype(r) == RTmp && r.val < static_cast<uint>(ntmp) && !alive[r.val]) {
            alive[r.val] = 1;
            wl.push_back(r.val);
        }
    };

    for (Blk* b = fn->start; b; b = b->link) {
        for (uint k = 0; k < b->nins; k++) {
            Ins* i = &b->ins[k];
            if (is_root(i->op)) {
                mark(i->arg[0]);
                mark(i->arg[1]);
            }
        }
        mark(b->jmp.arg);
    }

    while (!wl.empty()) {
        uint t = wl.back();
        wl.pop_back();
        if (Ins* d = ins_def[t]) {
            mark(d->arg[0]);
            mark(d->arg[1]);
            continue;
        }
        if (Phi* p = phi_def[t]) {
            for (uint a = 0; a < p->narg; a++)
                mark(p->arg[a]);
        }
    }

    int killed_ins = 0, killed_phi = 0;
    for (Blk* b = fn->start; b; b = b->link) {
        Phi** pp = &b->phi;
        while (*pp) {
            Phi* p = *pp;
            bool dead =
                (rtype(p->to) == RTmp && p->to.val < static_cast<uint>(ntmp) && !alive[p->to.val]);
            if (dead) {
                *pp = p->link;
                killed_phi++;
            } else {
                pp = &p->link;
            }
        }

        uint w = 0;
        for (uint k = 0; k < b->nins; k++) {
            Ins* i = &b->ins[k];
            if (i->op == Onop) {
                killed_ins++;
                continue;
            }
            bool root = is_root(i->op);
            bool has_to = (rtype(i->to) == RTmp && i->to.val < static_cast<uint>(ntmp));
            if (!root && has_to && !alive[i->to.val]) {
                killed_ins++;
                continue;
            }
            if (w != k)
                b->ins[w] = *i;
            w++;
        }
        b->nins = w;
    }

    if (debug['D']) {
        fprintf(stderr, "\n> DCE for %s: killed %d ins, %d phi\n", fn->name, killed_ins,
                killed_phi);
    }
}

}  // namespace rcc