From 5128de89a9a8da09587f20e8fb5bc324ea14e0df Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 11 Oct 2022 23:14:41 -0700 Subject: Small IR cleanups. (#2441) --- source/slang/slang-ir-simplify-cfg.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source/slang/slang-ir-simplify-cfg.cpp') 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 workList; @@ -25,12 +28,27 @@ bool processFunc(IRGlobalValueWithCode* func) { if (auto loop = as(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 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) -- cgit v1.2.3