blob: 2910fa05ab31efc840289d5f332f4c40619cf176 (
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
|
// slang-ir-init-local-var.cpp
#include "slang-ir-init-local-var.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"
namespace Slang
{
void initializeLocalVariables(IRModule* module, IRGlobalValueWithCode* func)
{
IRBuilder builder(module);
InstHashSet userSet(module);
for (auto block : func->getBlocks())
{
for (auto inst : block->getChildren())
{
if (inst->getOp() == kIROp_Var)
{
bool initialized = false;
userSet.clear();
for (auto use = inst->firstUse; use; use = use->nextUse)
userSet.add(use->getUser());
// Check if the variable is initialized in the same block.
for (auto nextInst = inst->next; nextInst; nextInst = nextInst->next)
{
switch (nextInst->getOp())
{
case kIROp_Store:
if (nextInst->getOperand(0) == inst)
initialized = true;
break;
case kIROp_GetElementPtr:
case kIROp_FieldAddress:
continue;
default:
if (userSet.contains(nextInst))
{
// We encountered a user of the variable before it was initialized.
// Break out of the loop and insert the initialization code.
goto breakLabel;
}
}
if (initialized)
break;
}
breakLabel:;
if (initialized)
continue;
IRBuilderSourceLocRAII sourceLocationScope(&builder, inst->sourceLoc);
builder.setInsertAfter(inst);
builder.emitStore(
inst,
builder.emitDefaultConstruct(
as<IRPtrTypeBase>(inst->getFullType())->getValueType()));
}
}
}
}
} // namespace Slang
|