summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-augment-make-existential.cpp
blob: c2f816e4942c9a667edc4e5961ed584fe62e90c5 (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
#include "slang-ir-augment-make-existential.h"

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

namespace Slang
{
struct AugmentMakeExistentialContext
{
    IRModule* module;

    InstWorkList workList;
    InstHashSet workListSet;

    AugmentMakeExistentialContext(IRModule* inModule)
        : module(inModule), workList(inModule), workListSet(inModule)
    {
    }

    void addToWorkList(IRInst* inst)
    {
        if (workListSet.contains(inst))
            return;

        workList.add(inst);
        workListSet.add(inst);
    }

    void processMakeExistential(IRMakeExistential* inst)
    {
        IRBuilder builderStorage(module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);

        auto augInst = builder->emitMakeExistentialWithRTTI(
            inst->getFullType(),
            inst->getWrappedValue(),
            inst->getWitnessTable(),
            inst->getWrappedValue()->getDataType());
        inst->replaceUsesWith(augInst);
        inst->removeAndDeallocate();
    }

    void processInst(IRInst* inst)
    {
        switch (inst->getOp())
        {
        case kIROp_MakeExistential:
            processMakeExistential((IRMakeExistential*)inst);
            break;
        default:
            break;
        }
    }

    void processModule()
    {
        addToWorkList(module->getModuleInst());

        while (workList.getCount() != 0)
        {
            IRInst* inst = workList.getLast();

            workList.removeLast();
            workListSet.remove(inst);

            processInst(inst);

            for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
            {
                addToWorkList(child);
            }
        }
    }
};

void augmentMakeExistentialInsts(IRModule* module)
{
    AugmentMakeExistentialContext context(module);
    context.processModule();
}
} // namespace Slang