summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-simplify-cfg.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-10-11 23:14:41 -0700
committerGitHub <noreply@github.com>2022-10-11 23:14:41 -0700
commit5128de89a9a8da09587f20e8fb5bc324ea14e0df (patch)
tree292d6488ee966eedcff9736f22511038f9bdc84b /source/slang/slang-ir-simplify-cfg.cpp
parent8c43a19d2fd8f9d1bd9d5854a4b1e64d5231bc41 (diff)
Small IR cleanups. (#2441)
Diffstat (limited to 'source/slang/slang-ir-simplify-cfg.cpp')
-rw-r--r--source/slang/slang-ir-simplify-cfg.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/source/slang/slang-ir-simplify-cfg.cpp b/source/slang/slang-ir-simplify-cfg.cpp
index db0274d32..a1bc38b64 100644
--- a/source/slang/slang-ir-simplify-cfg.cpp
+++ b/source/slang/slang-ir-simplify-cfg.cpp
@@ -12,6 +12,9 @@ bool processFunc(IRGlobalValueWithCode* func)
if (!firstBlock)
return false;
+ SharedIRBuilder sharedBuilder(func->getModule());
+ IRBuilder builder(&sharedBuilder);
+
bool changed = false;
List<IRBlock*> workList;
@@ -25,12 +28,27 @@ bool processFunc(IRGlobalValueWithCode* func)
{
if (auto loop = as<IRLoop>(block->getTerminator()))
{
+ // If continue block is unreachable, remove it.
auto continueBlock = loop->getContinueBlock();
if (continueBlock && !continueBlock->hasMoreThanOneUse())
{
loop->continueBlock.set(loop->getTargetBlock());
continueBlock->removeAndDeallocate();
}
+ // If there isn't any actual back jumps into loop target, remove the header
+ // and turn it into a normal branch.
+ auto targetBlock = loop->getTargetBlock();
+ if (targetBlock->getPredecessors().getCount() == 1 && *targetBlock->getPredecessors().begin() == block)
+ {
+ builder.setInsertBefore(loop);
+ List<IRInst*> args;
+ for (UInt i = 0; i < loop->getArgCount(); i++)
+ {
+ args.add(loop->getArg(i));
+ }
+ builder.emitBranch(targetBlock, args.getCount(), args.getBuffer());
+ loop->removeAndDeallocate();
+ }
}
// If `block` does not end with an unconditional branch, bail.
if (block->getTerminator()->getOp() != kIROp_unconditionalBranch)