summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-address-analysis.cpp
blob: a2518b127e6f1e432581f531dbc7b63dab58a2a8 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "slang-ir-address-analysis.h"

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

namespace Slang
{
void moveInstToEarliestPoint(IRDominatorTree* domTree, IRGlobalValueWithCode* func, IRInst* inst)
{
    if (!as<IRBlock>(inst->getParent()))
        return;
    if (domTree->isUnreachable(as<IRBlock>(inst->getParent())))
        return;

    InstWorkList blocks(func->getModule());
    InstHashSet operandInsts(func->getModule());
    for (UInt i = 0; i < inst->getOperandCount(); i++)
    {
        operandInsts.add(inst->getOperand(i));
        auto parentBlock = as<IRBlock>(inst->getOperand(i)->getParent());
        if (parentBlock)
        {
            if (!domTree->isUnreachable(parentBlock))
                blocks.add(parentBlock);
        }
    }
    {
        operandInsts.add(inst->getFullType());
        auto parentBlock = as<IRBlock>(inst->getFullType()->getParent());
        if (parentBlock)
        {
            if (!domTree->isUnreachable(parentBlock))
                blocks.add(parentBlock);
        }
    }
    // Find earliest block that is dominated by all operand blocks.
    IRBlock* earliestBlock = as<IRBlock>(inst->getParent());
    for (auto block : func->getBlocks())
    {
        bool dominated = true;
        for (auto opBlock : blocks)
        {
            if (!domTree->dominates(opBlock, block))
            {
                dominated = false;
                break;
            }
        }
        if (dominated)
        {
            earliestBlock = block;
            break;
        }
    }

    if (!earliestBlock)
        return;

    IRInst* latestOperand = nullptr;
    for (auto childInst : earliestBlock->getChildren())
    {
        if (operandInsts.contains(childInst))
        {
            latestOperand = childInst;
        }
    }

    if (!latestOperand || as<IRParam, IRDynamicCastBehavior::NoUnwrap>(latestOperand))
        inst->insertBefore(earliestBlock->getFirstOrdinaryInst());
    else
        inst->insertAfter(latestOperand);
}

AddressAccessInfo analyzeAddressUse(IRDominatorTree* dom, IRGlobalValueWithCode* func)
{
    DeduplicateContext deduplicateContext;

    AddressAccessInfo info;

    // Deduplicate and move known address insts.
    for (auto block : func->getBlocks())
    {
        for (auto inst : block->getModifiableChildren())
        {
            switch (inst->getOp())
            {
            case kIROp_Var:
                {
                    RefPtr<AddressInfo> addrInfo = new AddressInfo();
                    addrInfo->addrInst = inst;
                    addrInfo->isConstant = true;
                    addrInfo->parentAddress = nullptr;
                    info.addressInfos[inst] = addrInfo;
                }
                break;
            case kIROp_Param:
                if (as<IRPtrTypeBase>(inst->getFullType()))
                {
                    RefPtr<AddressInfo> addrInfo = new AddressInfo();
                    addrInfo->addrInst = inst;
                    addrInfo->isConstant = (block == func->getFirstBlock() ? true : false);
                    addrInfo->parentAddress = nullptr;
                    info.addressInfos[inst] = addrInfo;
                }
                break;
            case kIROp_GetElementPtr:
            case kIROp_FieldAddress:
                {
                    moveInstToEarliestPoint(dom, func, inst->getFullType());
                    moveInstToEarliestPoint(dom, func, inst);
                    auto deduplicated = deduplicateContext.deduplicate(
                        inst,
                        [func](IRInst* inst)
                        {
                            if (!inst->getParent())
                                return false;
                            if (inst->getParent()->getParent() != func)
                                return false;
                            switch (inst->getOp())
                            {
                            case kIROp_GetElementPtr:
                            case kIROp_FieldAddress:
                                return true;
                            default:
                                return false;
                            }
                        });

                    if (deduplicated != inst)
                    {
                        SLANG_RELEASE_ASSERT(dom->dominates(
                            as<IRBlock>(deduplicated->getParent()),
                            as<IRBlock>(inst->getParent())));

                        inst->replaceUsesWith(deduplicated);
                        inst->removeAndDeallocate();
                    }
                    else
                    {
                        RefPtr<AddressInfo> addrInfo = new AddressInfo();
                        addrInfo->addrInst = inst;
                        if (inst->getOp() == kIROp_FieldAddress)
                        {
                            addrInfo->isConstant = true;
                        }
                        else
                        {
                            addrInfo->isConstant =
                                as<IRConstant>(inst->getOperand(1)) ? true : false;
                        }
                        info.addressInfos[inst] = addrInfo;
                    }
                }
                break;
            }
        }
    }

    // Construct address info tree.
    for (auto& addr : info.addressInfos)
    {
        RefPtr<AddressInfo> parentInfo;
        if (addr.value->addrInst->getOperandCount() > 1 &&
            info.addressInfos.tryGetValue(addr.value->addrInst->getOperand(0), parentInfo))
        {
            addr.value->parentAddress = parentInfo;
            parentInfo->children.add(addr.value);
            if (!parentInfo->isConstant)
                addr.value->isConstant = false;
        }
    }
    return info;
}
} // namespace Slang