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

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

namespace Slang {

struct ExistentialTypeSimplificationContext
{
    List<IRInst*> instsToRemove;

};

void simplifyExistentialTypesRec(
    ExistentialTypeSimplificationContext*   context,
    IRInst*                                 inst)
{
    switch( inst->op )
    {
    default:
        break;

    case kIROp_ExtractExistentialValue:
        {
            auto arg = inst->getOperand(0);
            if( auto makeExistential = as<IRMakeExistential>(arg) )
            {
                auto value = makeExistential->getWrappedValue();
                inst->replaceUsesWith(value);
                context->instsToRemove.Add(inst);
            }
        }
        break;

    case kIROp_ExtractExistentialType:
        {
            auto arg = inst->getOperand(0);
            if( auto makeExistential = as<IRMakeExistential>(arg) )
            {
                auto value = makeExistential->getWrappedValue();
                inst->replaceUsesWith(value->getFullType());
                context->instsToRemove.Add(inst);
            }
        }
        break;

    case kIROp_ExtractExistentialWitnessTable:
        {
            auto arg = inst->getOperand(0);
            if( auto makeExistential = as<IRMakeExistential>(arg) )
            {
                auto witnessTable = makeExistential->getWitnessTable();
                inst->replaceUsesWith(witnessTable);
                context->instsToRemove.Add(inst);
            }
        }
        break;
    }

    for( auto childInst : inst->getChildren() )
    {
        simplifyExistentialTypesRec(context, childInst);
    }
}

void removeUnusedExistentialsRec(
    ExistentialTypeSimplificationContext*   context,
    IRInst*                                 inst)
{
    switch( inst->op )
    {
    default:
        break;

    case kIROp_MakeExistential:
        {
            if( !inst->hasUses() )
            {
                context->instsToRemove.Add(inst);
            }
        }
        break;
    }

    for( auto childInst : inst->getChildren() )
    {
        removeUnusedExistentialsRec(context, childInst);
    }
}

void simplifyExistentialTypes(
    IRModule*   module)
{
    {
        ExistentialTypeSimplificationContext context;
        simplifyExistentialTypesRec(&context, module->getModuleInst());
        for( auto inst : context.instsToRemove )
        {
            inst->removeAndDeallocate();
        }
    }

    {
        ExistentialTypeSimplificationContext context;
        removeUnusedExistentialsRec(&context, module->getModuleInst());
        for( auto inst : context.instsToRemove )
        {
            inst->removeAndDeallocate();
        }
    }
}

} // namespace Slang