summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-strip.cpp
blob: 6c64bfca277e97d13b488c0cc95b99c18a573904 (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
// slang-ir-strip.cpp
#include "slang-ir-strip.h"

#include "slang-ir-insts.h"
#include "slang-ir.h"

namespace Slang
{

/// Should `inst` be stripped, given the current `options`?
static bool _shouldStripInst(IRInst* inst, IRStripOptions const& options)
{
    switch (inst->getOp())
    {
    default:
        return false;

    case kIROp_HighLevelDeclDecoration:
        return true;

    case kIROp_NameHintDecoration:
        return options.shouldStripNameHints;
    }
}

/// Recursively strip `inst` and its children according to `options`.
static void _stripFrontEndOnlyInstructionsRec(IRInst* inst, IRStripOptions const& options)
{
    if (_shouldStripInst(inst, options))
    {
        inst->removeAndDeallocate();
        return;
    }

    if (options.stripSourceLocs)
    {
        inst->sourceLoc = SourceLoc();
    }

    IRInst* nextChild = nullptr;
    for (IRInst* child = inst->getFirstDecorationOrChild(); child; child = nextChild)
    {
        nextChild = child->getNextInst();

        _stripFrontEndOnlyInstructionsRec(child, options);
    }
}

void stripFrontEndOnlyInstructions(IRModule* module, IRStripOptions const& options)
{
    _stripFrontEndOnlyInstructionsRec(module->getModuleInst(), options);
}

void stripImportedWitnessTable(IRModule* module)
{
    for (auto globalInst : module->getGlobalInsts())
    {
        auto inst = globalInst;
        switch (globalInst->getOp())
        {
        case kIROp_Generic:
            inst = findInnerMostGenericReturnVal(as<IRGeneric>(globalInst));
            break;
        case kIROp_WitnessTable:
            break;
        default:
            continue;
        }
        if (inst->getOp() != kIROp_WitnessTable)
            continue;
        if (!globalInst->findDecoration<IRImportDecoration>())
            continue;
        IRInst* nextChild = nullptr;
        for (auto child = inst->getFirstChild(); child;)
        {
            nextChild = child->getNextInst();
            if (child->getOp() == kIROp_WitnessTable)
                child->removeAndDeallocate();
            child = nextChild;
        }
    }
}

} // namespace Slang