From 9476d4543f4336a66308e55f722b0b0b2bd69dd2 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 16 Mar 2023 23:46:14 -0700 Subject: Fix Phi simplification bug. (#2710) * Fix Phi simplification bug. * Fix up. * Fix. * Fix. * Fix. * Fix. * Fix. * Fix test. * Fix test. --------- Co-authored-by: Yong He --- source/slang/slang-ir-peephole.cpp | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'source/slang/slang-ir-peephole.cpp') diff --git a/source/slang/slang-ir-peephole.cpp b/source/slang/slang-ir-peephole.cpp index 5d5a41726..65b5d2f45 100644 --- a/source/slang/slang-ir-peephole.cpp +++ b/source/slang/slang-ir-peephole.cpp @@ -1,6 +1,8 @@ #include "slang-ir-peephole.h" #include "slang-ir-inst-pass-base.h" #include "slang-ir-sccp.h" +#include "slang-ir-dominators.h" +#include "slang-ir-util.h" namespace Slang { @@ -290,6 +292,9 @@ struct PeepholeContext : InstPassBase return false; } + RefPtr domTree; + IRGlobalValueWithCode* domTreeFunc = nullptr; + void processInst(IRInst* inst) { if (as(inst)) @@ -679,9 +684,35 @@ struct PeepholeContext : InstPassBase { if (inst->hasUses()) { - inst->replaceUsesWith(argValue); - // Never remove param inst. - changed = true; + // Is argValue a global constant? + if (isChildInstOf(inst, argValue->getParent())) + { + inst->replaceUsesWith(argValue); + // Never remove param inst. + changed = true; + } + else + { + // If argValue is defined locally, + // we can replace only if argVal dominates inst. + auto parentFunc = getParentFunc(inst); + if (!parentFunc) + break; + if (domTreeFunc != parentFunc) + { + domTree = computeDominatorTree(parentFunc); + domTreeFunc = parentFunc; + } + if (!domTree) + break; + + if (domTree->dominates(argValue, inst)) + { + inst->replaceUsesWith(argValue); + // Never remove param inst. + changed = true; + } + } } } } @@ -694,7 +725,6 @@ struct PeepholeContext : InstPassBase bool processFunc(IRInst* func) { bool result = false; - for (;;) { changed = false; -- cgit v1.2.3