summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-init-local-var.cpp
blob: 645344f2eef55f9d25246f2b7a15897e6ff870c0 (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
// slang-ir-init-local-var.cpp
#include "slang-ir-init-local-var.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"

namespace Slang
{

void initializeLocalVariables(IRModule* module, IRGlobalValueWithCode* func)
{
    IRBuilder builder(module);
    for (auto block : func->getBlocks())
    {
        for (auto inst : block->getChildren())
        {
            if (inst->getOp() == kIROp_Var)
            {
                auto firstUse = inst->firstUse;
                bool initialized =
                    (firstUse && firstUse->getUser()->getOp() == kIROp_Store &&
                        firstUse->getUser()->getParent() == inst->getParent());
                if (initialized)
                    continue;
                builder.setInsertAfter(inst);
                builder.emitStore(
                    inst,
                    builder.emitDefaultConstruct(
                        as<IRPtrTypeBase>(inst->getFullType())->getValueType()));
            }
        }
    }
}

} // namespace Slang