summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-ssa-simplification.cpp
blob: cd0f67186ee641a91f248e9e673a883409a5413e (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
150
// 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-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"
#include "slang-ir-util.h"

namespace Slang
{
    IRSimplificationOptions IRSimplificationOptions::getDefault(TargetProgram* targetProgram)
    {
        IRSimplificationOptions result;
        result.minimalOptimization = targetProgram ? targetProgram->getOptionSet().shouldPerformMinimumOptimizations() : false;
        if (result.minimalOptimization)
            result.cfgOptions = CFGSimplificationOptions::getFast();
        else
            result.cfgOptions = CFGSimplificationOptions::getDefault();
        result.peepholeOptions = PeepholeOptimizationOptions();
        if (targetProgram)
            result.deadCodeElimOptions.keepGlobalParamsAlive = targetProgram->getOptionSet().getBoolOption(CompilerOptionName::PreserveParameters);
        result.deadCodeElimOptions.useFastAnalysis = result.minimalOptimization;
        return result;
    }

    IRSimplificationOptions IRSimplificationOptions::getFast(TargetProgram* targetProgram)
    {
        IRSimplificationOptions result;
        result.minimalOptimization = targetProgram ? targetProgram->getOptionSet().shouldPerformMinimumOptimizations() : false;
        result.cfgOptions = CFGSimplificationOptions::getFast();
        result.peepholeOptions = PeepholeOptimizationOptions();
        if (targetProgram)
            result.deadCodeElimOptions.keepGlobalParamsAlive = targetProgram->getOptionSet().getBoolOption(CompilerOptionName::PreserveParameters);
        result.deadCodeElimOptions.useFastAnalysis = result.minimalOptimization;
        return result;
    }

    // Run a combination of SSA, SCCP, SimplifyCFG, and DeadCodeElimination pass
    // until no more changes are possible.
    void simplifyIR(TargetProgram* target, IRModule* module, IRSimplificationOptions options, 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 |= deduplicateGenericChildren(module);
            changed |= propagateFuncProperties(module);
            changed |= removeUnusedGenericParam(module);
            changed |= applySparseConditionalConstantPropagationForGlobalScope(module, sink);
            changed |= peepholeOptimizeGlobalScope(target, 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(target, func);
                    if (options.removeRedundancy)
                        funcChanged |= removeRedundancyInFunc(func);
                    funcChanged |= simplifyCFG(func, options.cfgOptions);
                    // 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, options.deadCodeElimOptions);
                    if (funcIterationCount == 0)
                        funcChanged |= constructSSA(func);
                    changed |= funcChanged;
                    funcIterationCount++;
                }
            }
            iterationCounter++;
        }
        eliminateDeadCode(module, options.deadCodeElimOptions);
    }

    void simplifyNonSSAIR(TargetProgram* target, IRModule* module, IRSimplificationOptions options)
    {
        bool changed = true;
        const int kMaxIterations = 8;
        int iterationCounter = 0;

        while (changed && iterationCounter < kMaxIterations)
        {
            changed = false;
            changed |= peepholeOptimize(target, module, options.peepholeOptions);

            if (!options.minimalOptimization)
                changed |= removeRedundancy(module);
            changed |= simplifyCFG(module, options.cfgOptions);

            // 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, options.deadCodeElimOptions);
            iterationCounter++;
        }
    }


    void simplifyFunc(TargetProgram* target, IRGlobalValueWithCode* func, IRSimplificationOptions options, 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(target, func);
            if (!options.minimalOptimization)
                changed |= removeRedundancyInFunc(func);
            changed |= simplifyCFG(func, options.cfgOptions);

            // 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, options.deadCodeElimOptions);

            changed |= constructSSA(func);

            iterationCounter++;

        }
    }
}