summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-ssa-simplification.cpp
blob: 43c9f8d2eb6d322bba537f282956b0bae8b63b2d (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
// slang-ir-ssa-simplification.cpp
#include "slang-ir-ssa-simplification.h"
#include "slang-ir.h"
#include "slang-ir-ssa.h"
#include "slang-ir-sccp.h"
#include "slang-ir-dce.h"
#include "slang-ir-simplify-cfg.h"
#include "slang-ir-peephole.h"
#include "slang-ir-hoist-constants.h"
#include "slang-ir-deduplicate-generic-children.h"
#include "slang-ir-remove-unused-generic-param.h"
#include "slang-ir-redundancy-removal.h"
#include "slang-ir-propagate-func-properties.h"
#include "../core/slang-performance-profiler.h"

namespace Slang
{
    // Run a combination of SSA, SCCP, SimplifyCFG, and DeadCodeElimination pass
    // until no more changes are possible.
    void simplifyIR(IRModule* module, DiagnosticSink* sink)
    {
        SLANG_PROFILE;
        bool changed = true;
        const int kMaxIterations = 8;
        const int kMaxFuncIterations = 16;
        int iterationCounter = 0;

        while (changed && iterationCounter < kMaxIterations)
        {
            if (sink && sink->getErrorCount())
                break;

            changed = false;

            changed |= hoistConstants(module);
            changed |= deduplicateGenericChildren(module);
            changed |= propagateFuncProperties(module);
            changed |= removeUnusedGenericParam(module);
            changed |= applySparseConditionalConstantPropagationForGlobalScope(module, sink);
            changed |= peepholeOptimize(module);

            for (auto inst : module->getGlobalInsts())
            {
                auto func = as<IRGlobalValueWithCode>(inst);
                if (!func)
                    continue;
                bool funcChanged = true;
                int funcIterationCount = 0;
                while (funcChanged && funcIterationCount < kMaxFuncIterations)
                {
                    funcChanged = false;
                    funcChanged |= applySparseConditionalConstantPropagation(func, sink);
                    funcChanged |= peepholeOptimize(func);
                    funcChanged |= removeRedundancyInFunc(func);
                    funcChanged |= simplifyCFG(func);
                    eliminateDeadCode(func);
                    funcChanged |= constructSSA(func);
                    changed |= funcChanged;
                    funcIterationCount++;
                }
            }

            // Note: we disregard the `changed` state from dead code elimination pass since
            // SCCP pass could be generating temporarily evaluated constant values and never actually use them.
            // DCE will always remove those nearly generated consts and always returns true here.
            eliminateDeadCode(module);

            iterationCounter++;
        }
    }

    void simplifyNonSSAIR(IRModule* module)
    {
        bool changed = true;
        const int kMaxIterations = 8;
        int iterationCounter = 0;
        while (changed && iterationCounter < kMaxIterations)
        {
            changed = false;
            changed |= peepholeOptimize(module);
            changed |= removeRedundancy(module);
            changed |= simplifyCFG(module);

            // Note: we disregard the `changed` state from dead code elimination pass since
            // SCCP pass could be generating temporarily evaluated constant values and never actually use them.
            // DCE will always remove those nearly generated consts and always returns true here.
            eliminateDeadCode(module);

            iterationCounter++;
        }
    }


    void simplifyFunc(IRGlobalValueWithCode* func, DiagnosticSink* sink)
    {
        bool changed = true;
        const int kMaxIterations = 8;
        int iterationCounter = 0;
        while (changed && iterationCounter < kMaxIterations)
        {
            if (sink && sink->getErrorCount())
                break;

            changed = false;
            changed |= applySparseConditionalConstantPropagation(func, sink);
            changed |= peepholeOptimize(func);
            changed |= removeRedundancyInFunc(func);
            changed |= simplifyCFG(func);

            // Note: we disregard the `changed` state from dead code elimination pass since
            // SCCP pass could be generating temporarily evaluated constant values and never actually use them.
            // DCE will always remove those nearly generated consts and always returns true here.
            eliminateDeadCode(func);

            changed |= constructSSA(func);

            iterationCounter++;

        }
    }
}