From 6bdd19d4f9c11c3d511d4abf7cbc1f5c95cbdb7f Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Thu, 17 Aug 2023 03:38:22 +0800 Subject: Simplify IfElse instructions with a single trivial block (#3114) --- source/slang/slang-ir-simplify-cfg.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 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 f2d0c4555..63c40b16f 100644 --- a/source/slang/slang-ir-simplify-cfg.cpp +++ b/source/slang/slang-ir-simplify-cfg.cpp @@ -444,6 +444,30 @@ static bool trySimplifyIfElse(IRBuilder& builder, IRIfElse* ifElseInst) return true; } } + else + { + // Otherwise, we can try to remove at least remove one of the trivial branches + // Remove either the true or false block if it jumps to the after block + // with no parameters. + + const auto afterBlock = ifElseInst->getAfterBlock(); + if(!afterBlock->getFirstParam()) + { + const auto trueBlock = ifElseInst->getTrueBlock(); + const auto falseBlock = ifElseInst->getFalseBlock(); + + if(isTrueBranchTrivial && trueBlock != afterBlock && !trueBlock->hasMoreThanOneUse()) + { + trueBlock->replaceUsesWith(afterBlock); + trueBlock->removeAndDeallocate(); + } + else if(isFalseBranchTrivial && falseBlock != afterBlock && !falseBlock->hasMoreThanOneUse()) + { + falseBlock->replaceUsesWith(afterBlock); + falseBlock->removeAndDeallocate(); + } + } + } return false; } -- cgit v1.2.3