blob: f2c199af66d093baa45a2b9a9ec533e066fc343a (
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
|
#include "slang-ir-insts.h"
namespace Slang
{
struct DeduplicateContext
{
SharedIRBuilder* builder;
IRInst* addValue(IRInst* value)
{
if (!value) return nullptr;
if (as<IRType>(value))
return addTypeValue(value);
if (auto constValue = as<IRConstant>(value))
return addConstantValue(constValue);
return value;
}
IRInst* addConstantValue(IRConstant* value)
{
IRConstantKey key = { value };
value->setFullType((IRType*)addValue(value->getFullType()));
if (auto newValue = builder->getConstantMap().TryGetValue(key))
return *newValue;
builder->getConstantMap()[key] = value;
return value;
}
IRInst* addTypeValue(IRInst* value)
{
// Do not deduplicate struct or interface types.
switch (value->getOp())
{
case kIROp_StructType:
case kIROp_InterfaceType:
return value;
default:
break;
}
for (UInt i = 0; i < value->getOperandCount(); i++)
{
value->setOperand(i, addValue(value->getOperand(i)));
}
value->setFullType((IRType*)addValue(value->getFullType()));
IRInstKey key = { value };
if (auto newValue = builder->getGlobalValueNumberingMap().TryGetValue(key))
return *newValue;
builder->getGlobalValueNumberingMap()[key] = value;
return value;
}
};
void SharedIRBuilder::deduplicateAndRebuildGlobalNumberingMap()
{
DeduplicateContext context;
context.builder = this;
m_constantMap.Clear();
m_globalValueNumberingMap.Clear();
for (auto inst : m_module->getGlobalInsts())
{
if (auto constVal = as<IRConstant>(inst))
{
context.addConstantValue(constVal);
}
}
List<IRInst*> instToRemove;
for (auto inst : m_module->getGlobalInsts())
{
if (as<IRType>(inst))
{
auto newInst = context.addTypeValue(inst);
if (newInst != inst)
{
inst->replaceUsesWith(newInst);
instToRemove.add(inst);
}
}
}
for (auto inst : instToRemove)
inst->removeAndDeallocate();
}
}
|