summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-address-analysis.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/slang/slang-ir-address-analysis.cpp
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/slang/slang-ir-address-analysis.cpp')
-rw-r--r--source/slang/slang-ir-address-analysis.cpp251
1 files changed, 126 insertions, 125 deletions
diff --git a/source/slang/slang-ir-address-analysis.cpp b/source/slang/slang-ir-address-analysis.cpp
index 178e15292..1d61eb1d0 100644
--- a/source/slang/slang-ir-address-analysis.cpp
+++ b/source/slang/slang-ir-address-analysis.cpp
@@ -1,171 +1,172 @@
#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;
+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++)
+ 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)
{
- operandInsts.add(inst->getOperand(i));
- auto parentBlock = as<IRBlock>(inst->getOperand(i)->getParent());
- if (parentBlock)
- {
- if (!domTree->isUnreachable(parentBlock))
- blocks.add(parentBlock);
- }
+ if (!domTree->isUnreachable(parentBlock))
+ blocks.add(parentBlock);
}
+ }
+ {
+ operandInsts.add(inst->getFullType());
+ auto parentBlock = as<IRBlock>(inst->getFullType()->getParent());
+ if (parentBlock)
{
- operandInsts.add(inst->getFullType());
- auto parentBlock = as<IRBlock>(inst->getFullType()->getParent());
- if (parentBlock)
- {
- if (!domTree->isUnreachable(parentBlock))
- blocks.add(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())
+ }
+ // 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)
{
- bool dominated = true;
- for (auto opBlock : blocks)
+ if (!domTree->dominates(opBlock, block))
{
- if (!domTree->dominates(opBlock, block))
- {
- dominated = false;
- break;
- }
- }
- if (dominated)
- {
- earliestBlock = block;
+ dominated = false;
break;
}
}
+ if (dominated)
+ {
+ earliestBlock = block;
+ break;
+ }
+ }
- if (!earliestBlock)
- return;
+ if (!earliestBlock)
+ return;
- IRInst* latestOperand = nullptr;
- for (auto childInst : earliestBlock->getChildren())
+ IRInst* latestOperand = nullptr;
+ for (auto childInst : earliestBlock->getChildren())
+ {
+ if (operandInsts.contains(childInst))
{
- if (operandInsts.contains(childInst))
- {
- latestOperand = 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;
+ if (!latestOperand || as<IRParam, IRDynamicCastBehavior::NoUnwrap>(latestOperand))
+ inst->insertBefore(earliestBlock->getFirstOrdinaryInst());
+ else
+ inst->insertAfter(latestOperand);
+}
- AddressAccessInfo info;
+AddressAccessInfo analyzeAddressUse(IRDominatorTree* dom, IRGlobalValueWithCode* func)
+{
+ DeduplicateContext deduplicateContext;
- // Deduplicate and move known address insts.
- for (auto block : func->getBlocks())
+ AddressAccessInfo info;
+
+ // Deduplicate and move known address insts.
+ for (auto block : func->getBlocks())
+ {
+ for (auto inst : block->getModifiableChildren())
{
- for (auto inst : block->getModifiableChildren())
+ switch (inst->getOp())
{
- 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:
{
- case kIROp_Var:
+ 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)
{
- RefPtr<AddressInfo> addrInfo = new AddressInfo();
- addrInfo->addrInst = inst;
- addrInfo->isConstant = true;
- addrInfo->parentAddress = nullptr;
- info.addressInfos[inst] = addrInfo;
+ SLANG_RELEASE_ASSERT(dom->dominates(
+ as<IRBlock>(deduplicated->getParent()),
+ as<IRBlock>(inst->getParent())));
+
+ inst->replaceUsesWith(deduplicated);
+ inst->removeAndDeallocate();
}
- break;
- case kIROp_Param:
- if (as<IRPtrTypeBase>(inst->getFullType()))
+ else
{
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)
+ if (inst->getOp() == kIROp_FieldAddress)
{
- SLANG_RELEASE_ASSERT(dom->dominates(
- as<IRBlock>(deduplicated->getParent()),
- as<IRBlock>(inst->getParent())));
-
- inst->replaceUsesWith(deduplicated);
- inst->removeAndDeallocate();
+ addrInfo->isConstant = true;
}
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;
+ addrInfo->isConstant =
+ as<IRConstant>(inst->getOperand(1)) ? true : false;
}
+ info.addressInfos[inst] = addrInfo;
}
- break;
}
+ break;
}
}
+ }
- // Construct address info tree.
- for (auto& addr : info.addressInfos)
+ // 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))
{
- 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;
- }
+ addr.value->parentAddress = parentInfo;
+ parentInfo->children.add(addr.value);
+ if (!parentInfo->isConstant)
+ addr.value->isConstant = false;
}
- return info;
}
+ return info;
}
+} // namespace Slang